8001164a887285b39bf71318c1cad16013f4c1ac
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
10 'imageFile': imageFile
,
14 'sampleText': sampleText
18 // this function generates the actual toolbar buttons with localized text
19 // we use it to avoid creating the toolbar where JavaScript is not enabled
20 function mwInsertEditButton( parent
, item
) {
21 var image
= document
.createElement( 'img' );
24 image
.className
= 'mw-toolbar-editbutton';
26 image
.id
= item
.imageId
;
28 image
.src
= item
.imageFile
;
30 image
.alt
= item
.speedTip
;
31 image
.title
= item
.speedTip
;
32 image
.style
.cursor
= 'pointer';
33 image
.onclick = function() {
34 insertTags( item
.tagOpen
, item
.tagClose
, item
.sampleText
);
36 if ( ( typeof $j
!= 'undefined' ) && ( typeof $j
.trackAction
!= 'undefined' ) ) {
37 $j
.trackAction( 'oldedit.' + item
.speedTip
.replace(/ /g
, "-") );
42 parent
.appendChild( image
);
46 function mwSetupToolbar() {
47 var toolbar
= document
.getElementById( 'toolbar' );
52 // Don't generate buttons for browsers which don't fully
54 // but don't assume wpTextbox1 is always here
55 var textboxes
= document
.getElementsByTagName( 'textarea' );
56 if ( !textboxes
.length
) {
57 // No toolbar if we can't find any textarea
60 // Only check for selection capability if the textarea is visible - errors will occur otherwise - just because
61 // the textarea is not visible, doesn't mean we shouldn't build out the toolbar though - it might have been replaced
62 // with some other kind of control
63 if ( textboxes
[0].style
.display
!= 'none' ) {
64 if ( !( document
.selection
&& document
.selection
.createRange
)
65 && textboxes
[0].selectionStart
=== null ) {
69 for ( var i
= 0; i
< mwEditButtons
.length
; i
++ ) {
70 mwInsertEditButton( toolbar
, mwEditButtons
[i
] );
72 for ( var i
= 0; i
< mwCustomEditButtons
.length
; i
++ ) {
73 mwInsertEditButton( toolbar
, mwCustomEditButtons
[i
] );
78 // apply tagOpen/tagClose to selection in textarea,
79 // use sampleText instead of selection if there is none
80 function insertTags( tagOpen
, tagClose
, sampleText
) {
81 if ( typeof $j
!= 'undefined' && typeof $j
.fn
.textSelection
!= 'undefined' ) {
82 $j( '#wpTextbox1' ).textSelection(
83 'encapsulateSelection', { 'pre': tagOpen
, 'peri': sampleText
, 'post': tagClose
}
88 if ( document
.editform
) {
89 txtarea
= currentFocused
;
91 // some alternate form? take the first one we can find
92 var areas
= document
.getElementsByTagName( 'textarea' );
95 var selText
, isSample
= false;
97 if ( document
.selection
&& document
.selection
.createRange
) { // IE/Opera
98 // save window scroll position
99 if ( document
.documentElement
&& document
.documentElement
.scrollTop
) {
100 var winScroll
= document
.documentElement
.scrollTop
101 } else if ( document
.body
) {
102 var winScroll
= document
.body
.scrollTop
;
104 // get current selection
106 var range
= document
.selection
.createRange();
107 selText
= range
.text
;
110 range
.text
= tagOpen
+ selText
+ tagClose
;
111 // mark sample text as selected
112 if ( isSample
&& range
.moveStart
) {
113 if ( window
.opera
) {
114 tagClose
= tagClose
.replace(/\n/g,'');
116 range
.moveStart('character', - tagClose
.length
- selText
.length
);
117 range
.moveEnd('character', - tagClose
.length
);
120 // restore window scroll position
121 if ( document
.documentElement
&& document
.documentElement
.scrollTop
) {
122 document
.documentElement
.scrollTop
= winScroll
;
123 } else if ( document
.body
) {
124 document
.body
.scrollTop
= winScroll
;
127 } else if ( txtarea
.selectionStart
|| txtarea
.selectionStart
== '0' ) { // Mozilla
128 // save textarea scroll position
129 var textScroll
= txtarea
.scrollTop
;
130 // get current selection
132 var startPos
= txtarea
.selectionStart
;
133 var endPos
= txtarea
.selectionEnd
;
134 selText
= txtarea
.value
.substring( startPos
, endPos
);
137 txtarea
.value
= txtarea
.value
.substring(0, startPos
)
138 + tagOpen
+ selText
+ tagClose
139 + txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
142 txtarea
.selectionStart
= startPos
+ tagOpen
.length
;
143 txtarea
.selectionEnd
= startPos
+ tagOpen
.length
+ selText
.length
;
145 txtarea
.selectionStart
= startPos
+ tagOpen
.length
+ selText
.length
+ tagClose
.length
;
146 txtarea
.selectionEnd
= txtarea
.selectionStart
;
148 // restore textarea scroll position
149 txtarea
.scrollTop
= textScroll
;
152 function checkSelectedText() {
154 selText
= sampleText
;
156 } else if ( selText
.charAt(selText
.length
- 1) == ' ' ) { // exclude ending space char
157 selText
= selText
.substring(0, selText
.length
- 1);
165 * Restore the edit box scroll state following a preview operation,
166 * and set up a form submission handler to remember this state
168 function scrollEditBox() {
169 var editBox
= document
.getElementById( 'wpTextbox1' );
170 var scrollTop
= document
.getElementById( 'wpScrolltop' );
171 var editForm
= document
.getElementById( 'editform' );
172 if( editForm
&& editBox
&& scrollTop
) {
173 if( scrollTop
.value
) {
174 editBox
.scrollTop
= scrollTop
.value
;
176 addHandler( editForm
, 'submit', function() {
177 scrollTop
.value
= editBox
.scrollTop
;
181 hookEvent( 'load', scrollEditBox
);
182 hookEvent( 'load', mwSetupToolbar
);
183 hookEvent( 'load', function() {
184 currentFocused
= document
.getElementById( 'wpTextbox1' );
185 // http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
186 // focus does not bubble normally, but using a trick we can do event delegation
187 // on the focus event on all text inputs to make the toolbox usable on all of them
188 var editForm
= document
.getElementById( 'editform' );
192 function onfocus( e
) {
197 var tagName
= elm
.tagName
.toLowerCase();
198 var type
= elm
.type
.toLowerCase();
199 if ( tagName
!== 'textarea' && tagName
!== 'input' ) {
202 if ( tagName
=== 'input' && type
&& type
!== 'text' ) {
206 currentFocused
= elm
;
209 if ( editForm
.addEventListener
) {
210 // Gecko, WebKit, Opera, etc... (all standards compliant browsers)
211 editForm
.addEventListener( 'focus', onfocus
, true ); // This MUST be true to work
212 } else if ( editForm
.attachEvent
) {
213 // IE needs a specific trick here since it doesn't support the standard
214 editForm
.attachEvent( 'onfocusin', function() { onfocus( event
); } );