1 // this function generates the actual toolbar buttons with localized text
2 // we use it to avoid creating the toolbar where javascript is not enabled
3 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
, imageId
) {
4 // Don't generate buttons for browsers which don't fully
6 mwEditButtons
[mwEditButtons
.length
] =
8 "imageFile": imageFile
,
12 "sampleText": sampleText
};
15 // this function generates the actual toolbar buttons with localized text
16 // we use it to avoid creating the toolbar where javascript is not enabled
17 function mwInsertEditButton(parent
, item
) {
18 var image
= document
.createElement("img");
21 image
.className
= "mw-toolbar-editbutton";
22 if (item
.imageId
) image
.id
= item
.imageId
;
23 image
.src
= item
.imageFile
;
25 image
.alt
= item
.speedTip
;
26 image
.title
= item
.speedTip
;
27 image
.style
.cursor
= "pointer";
28 image
.onclick = function() {
29 insertTags(item
.tagOpen
, item
.tagClose
, item
.sampleText
);
33 parent
.appendChild(image
);
37 function mwSetupToolbar() {
38 var toolbar
= document
.getElementById('toolbar');
39 if (!toolbar
) { return false; }
41 var textbox
= document
.getElementById('wpTextbox1');
42 if (!textbox
) { return false; }
44 // Don't generate buttons for browsers which don't fully
46 if (!(document
.selection
&& document
.selection
.createRange
)
47 && textbox
.selectionStart
=== null) {
51 for (var i
= 0; i
< mwEditButtons
.length
; i
++) {
52 mwInsertEditButton(toolbar
, mwEditButtons
[i
]);
54 for (var i
= 0; i
< mwCustomEditButtons
.length
; i
++) {
55 mwInsertEditButton(toolbar
, mwCustomEditButtons
[i
]);
60 // apply tagOpen/tagClose to selection in textarea,
61 // use sampleText instead of selection if there is none
62 function insertTags(tagOpen
, tagClose
, sampleText
) {
64 if (document
.editform
) {
65 txtarea
= document
.editform
.wpTextbox1
;
67 // some alternate form? take the first one we can find
68 var areas
= document
.getElementsByTagName('textarea');
71 var selText
, isSample
= false;
73 if (document
.selection
&& document
.selection
.createRange
) { // IE/Opera
75 //save window scroll position
76 if (document
.documentElement
&& document
.documentElement
.scrollTop
)
77 var winScroll
= document
.documentElement
.scrollTop
78 else if (document
.body
)
79 var winScroll
= document
.body
.scrollTop
;
80 //get current selection
82 var range
= document
.selection
.createRange();
86 range
.text
= tagOpen
+ selText
+ tagClose
;
87 //mark sample text as selected
88 if (isSample
&& range
.moveStart
) {
90 tagClose
= tagClose
.replace(/\n/g,'');
91 range
.moveStart('character', - tagClose
.length
- selText
.length
);
92 range
.moveEnd('character', - tagClose
.length
);
95 //restore window scroll position
96 if (document
.documentElement
&& document
.documentElement
.scrollTop
)
97 document
.documentElement
.scrollTop
= winScroll
98 else if (document
.body
)
99 document
.body
.scrollTop
= winScroll
;
101 } else if (txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') { // Mozilla
103 //save textarea scroll position
104 var textScroll
= txtarea
.scrollTop
;
105 //get current selection
107 var startPos
= txtarea
.selectionStart
;
108 var endPos
= txtarea
.selectionEnd
;
109 selText
= txtarea
.value
.substring(startPos
, endPos
);
112 txtarea
.value
= txtarea
.value
.substring(0, startPos
)
113 + tagOpen
+ selText
+ tagClose
114 + txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
117 txtarea
.selectionStart
= startPos
+ tagOpen
.length
;
118 txtarea
.selectionEnd
= startPos
+ tagOpen
.length
+ selText
.length
;
120 txtarea
.selectionStart
= startPos
+ tagOpen
.length
+ selText
.length
+ tagClose
.length
;
121 txtarea
.selectionEnd
= txtarea
.selectionStart
;
123 //restore textarea scroll position
124 txtarea
.scrollTop
= textScroll
;
127 function checkSelectedText(){
129 selText
= sampleText
;
131 } else if (selText
.charAt(selText
.length
- 1) == ' ') { //exclude ending space char
132 selText
= selText
.substring(0, selText
.length
- 1);
140 * Restore the edit box scroll state following a preview operation,
141 * and set up a form submission handler to remember this state
143 function scrollEditBox() {
144 var editBox
= document
.getElementById( 'wpTextbox1' );
145 var scrollTop
= document
.getElementById( 'wpScrolltop' );
146 var editForm
= document
.getElementById( 'editform' );
147 if( editBox
&& scrollTop
) {
148 if( scrollTop
.value
)
149 editBox
.scrollTop
= scrollTop
.value
;
150 addHandler( editForm
, 'submit', function() {
151 document
.getElementById( 'wpScrolltop' ).value
= document
.getElementById( 'wpTextbox1' ).scrollTop
;
155 hookEvent( 'load', scrollEditBox
);
156 hookEvent( 'load', mwSetupToolbar
);