a36e5db440d9c815f1fef1c8ba6e67d520dd8c3c
3 // this function generates the actual toolbar buttons with localized text
4 // we use it to avoid creating the toolbar where javascript is not enabled
5 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
, imageId
) {
6 // Don't generate buttons for browsers which don't fully
8 mwEditButtons
[mwEditButtons
.length
] =
10 "imageFile": imageFile
,
14 "sampleText": sampleText
};
17 // this function generates the actual toolbar buttons with localized text
18 // we use it to avoid creating the toolbar where javascript is not enabled
19 function mwInsertEditButton(parent
, item
) {
20 var image
= document
.createElement("img");
23 image
.className
= "mw-toolbar-editbutton";
24 if (item
.imageId
) image
.id
= item
.imageId
;
25 image
.src
= item
.imageFile
;
27 image
.alt
= item
.speedTip
;
28 image
.title
= item
.speedTip
;
29 image
.style
.cursor
= "pointer";
30 image
.onclick = function() {
31 insertTags(item
.tagOpen
, item
.tagClose
, item
.sampleText
);
33 if ( ( $j
!= undefined ) && ( $j
.trackAction
!= undefined ) ) {
34 $j
.trackAction("oldedit." + item
.speedTip
.replace(/ /g
, "-"));
39 parent
.appendChild(image
);
43 function mwSetupToolbar() {
44 var toolbar
= document
.getElementById('toolbar');
45 if (!toolbar
) { return false; }
47 var textbox
= document
.getElementById('wpTextbox1');
48 if (!textbox
) { return false; }
50 // Don't generate buttons for browsers which don't fully
52 if (!(document
.selection
&& document
.selection
.createRange
)
53 && textbox
.selectionStart
=== null) {
57 for (var i
= 0; i
< mwEditButtons
.length
; i
++) {
58 mwInsertEditButton(toolbar
, mwEditButtons
[i
]);
60 for (var i
= 0; i
< mwCustomEditButtons
.length
; i
++) {
61 mwInsertEditButton(toolbar
, mwCustomEditButtons
[i
]);
66 // apply tagOpen/tagClose to selection in textarea,
67 // use sampleText instead of selection if there is none
68 function insertTags(tagOpen
, tagClose
, sampleText
) {
70 if (document
.editform
) {
71 txtarea
= currentFocused
;
73 // some alternate form? take the first one we can find
74 var areas
= document
.getElementsByTagName('textarea');
77 var selText
, isSample
= false;
79 if (document
.selection
&& document
.selection
.createRange
) { // IE/Opera
81 //save window scroll position
82 if (document
.documentElement
&& document
.documentElement
.scrollTop
)
83 var winScroll
= document
.documentElement
.scrollTop
84 else if (document
.body
)
85 var winScroll
= document
.body
.scrollTop
;
86 //get current selection
88 var range
= document
.selection
.createRange();
92 range
.text
= tagOpen
+ selText
+ tagClose
;
93 //mark sample text as selected
94 if (isSample
&& range
.moveStart
) {
96 tagClose
= tagClose
.replace(/\n/g,'');
97 range
.moveStart('character', - tagClose
.length
- selText
.length
);
98 range
.moveEnd('character', - tagClose
.length
);
101 //restore window scroll position
102 if (document
.documentElement
&& document
.documentElement
.scrollTop
)
103 document
.documentElement
.scrollTop
= winScroll
104 else if (document
.body
)
105 document
.body
.scrollTop
= winScroll
;
107 } else if (txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') { // Mozilla
109 //save textarea scroll position
110 var textScroll
= txtarea
.scrollTop
;
111 //get current selection
113 var startPos
= txtarea
.selectionStart
;
114 var endPos
= txtarea
.selectionEnd
;
115 selText
= txtarea
.value
.substring(startPos
, endPos
);
118 txtarea
.value
= txtarea
.value
.substring(0, startPos
)
119 + tagOpen
+ selText
+ tagClose
120 + txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
123 txtarea
.selectionStart
= startPos
+ tagOpen
.length
;
124 txtarea
.selectionEnd
= startPos
+ tagOpen
.length
+ selText
.length
;
126 txtarea
.selectionStart
= startPos
+ tagOpen
.length
+ selText
.length
+ tagClose
.length
;
127 txtarea
.selectionEnd
= txtarea
.selectionStart
;
129 //restore textarea scroll position
130 txtarea
.scrollTop
= textScroll
;
133 function checkSelectedText(){
135 selText
= sampleText
;
137 } else if (selText
.charAt(selText
.length
- 1) == ' ') { //exclude ending space char
138 selText
= selText
.substring(0, selText
.length
- 1);
146 * Restore the edit box scroll state following a preview operation,
147 * and set up a form submission handler to remember this state
149 function scrollEditBox() {
150 var editBox
= document
.getElementById( 'wpTextbox1' );
151 var scrollTop
= document
.getElementById( 'wpScrolltop' );
152 var editForm
= document
.getElementById( 'editform' );
153 if( editForm
&& editBox
&& scrollTop
) {
154 if( scrollTop
.value
)
155 editBox
.scrollTop
= scrollTop
.value
;
156 addHandler( editForm
, 'submit', function() {
157 document
.getElementById( 'wpScrolltop' ).value
= document
.getElementById( 'wpTextbox1' ).scrollTop
;
161 hookEvent( 'load', scrollEditBox
);
162 hookEvent( 'load', mwSetupToolbar
);
163 hookEvent( 'load', function() {
164 if ( document
.editform
) {
165 currentFocused
= document
.editform
.wpTextbox1
;
166 document
.editform
.wpTextbox1
.onfocus = function() { currentFocused
= document
.editform
.wpTextbox1
; };
167 document
.editform
.wpSummary
.onfocus = function() { currentFocused
= document
.editform
.wpSummary
; };