1 // This file is still referenced from
2 // tests/selenium/data/SimpleSeleniumTestDB.sql
3 // includes/specials/SpecialUpload.php
4 // /extensions/SemanticForms/specials/SF_UploadWindow2.php
5 window
.currentFocused
= undefined;
7 // this function adds a toolbar button to the mwEditButtons list
8 window
.addButton = function( imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
, imageId
, selectText
) {
9 // Don't generate buttons for browsers which don't fully
13 'imageFile': imageFile
,
17 'sampleText': sampleText
,
18 'selectText': selectText
22 // this function adds one toolbar button from a mwEditButtons/mwCustomEditButtons item
23 window
.mwInsertEditButton = function( parent
, item
) {
24 var image
= document
.createElement( 'img' );
27 image
.className
= 'mw-toolbar-editbutton';
29 image
.id
= item
.imageId
;
31 image
.src
= item
.imageFile
;
33 image
.alt
= item
.speedTip
;
34 image
.title
= item
.speedTip
;
35 image
.style
.cursor
= 'pointer';
36 image
.onclick = function() {
37 insertTags( item
.tagOpen
, item
.tagClose
, item
.sampleText
, item
.selectText
);
39 if ( ( typeof $ != 'undefined' ) && ( typeof $.trackAction
!= 'undefined' ) ) {
40 $.trackAction( 'oldedit.' + item
.speedTip
.replace(/ /g
, '-') );
45 parent
.appendChild( image
);
49 // this function generates the actual toolbar buttons with localized text
50 // we use it to avoid creating the toolbar where javascript is not enabled
51 window
.mwSetupToolbar = function() {
52 var toolbar
= document
.getElementById( 'toolbar' );
58 // Don't generate buttons for browsers which don't fully
60 // but don't assume wpTextbox1 is always here
61 var textboxes
= document
.getElementsByTagName( 'textarea' );
62 if ( !textboxes
.length
) {
63 // No toolbar if we can't find any textarea
66 // Only check for selection capability if the textarea is visible - errors will occur otherwise - just because
67 // the textarea is not visible, doesn't mean we shouldn't build out the toolbar though - it might have been replaced
68 // with some other kind of control
69 if ( textboxes
[0].style
.display
!= 'none' ) {
70 if ( !( document
.selection
&& document
.selection
.createRange
)
71 && textboxes
[0].selectionStart
=== null ) {
75 for ( i
= 0; i
< mwEditButtons
.length
; i
++ ) {
76 mwInsertEditButton( toolbar
, mwEditButtons
[i
] );
78 for ( i
= 0; i
< mwCustomEditButtons
.length
; i
++ ) {
79 mwInsertEditButton( toolbar
, mwCustomEditButtons
[i
] );
84 // apply tagOpen/tagClose to selection in textarea,
85 // use sampleText instead of selection if there is none
86 window
.insertTags = function( tagOpen
, tagClose
, sampleText
, selectText
) {
87 if ( typeof $ != 'undefined' && typeof $.fn
.textSelection
!= 'undefined' && currentFocused
&&
88 ( currentFocused
.nodeName
.toLowerCase() == 'iframe' || currentFocused
.id
== 'wpTextbox1' ) ) {
89 $( '#wpTextbox1' ).textSelection(
90 'encapsulateSelection', { 'pre': tagOpen
, 'peri': sampleText
, 'post': tagClose
}
95 if ( document
.editform
) {
96 txtarea
= currentFocused
;
98 // some alternate form? take the first one we can find
99 var areas
= document
.getElementsByTagName( 'textarea' );
102 var selText
, isSample
= false;
104 function checkSelectedText() {
106 selText
= sampleText
;
108 } else if ( selText
.charAt(selText
.length
- 1) == ' ' ) { // exclude ending space char
109 selText
= selText
.substring(0, selText
.length
- 1);
114 if ( document
.selection
&& document
.selection
.createRange
) { // IE/Opera
115 // save window scroll position
116 var winScroll
= null;
117 if ( document
.documentElement
&& document
.documentElement
.scrollTop
) {
118 winScroll
= document
.documentElement
.scrollTop
;
119 } else if ( document
.body
) {
120 winScroll
= document
.body
.scrollTop
;
122 // get current selection
124 var range
= document
.selection
.createRange();
125 selText
= range
.text
;
128 range
.text
= tagOpen
+ selText
+ tagClose
;
129 // mark sample text as selected if not switched off by option
130 if ( selectText
!== false ) {
131 if ( isSample
&& range
.moveStart
) {
132 if ( window
.opera
) {
133 tagClose
= tagClose
.replace(/\n/g,'');
135 range
.moveStart('character', - tagClose
.length
- selText
.length
);
136 range
.moveEnd('character', - tagClose
.length
);
140 // restore window scroll position
141 if ( document
.documentElement
&& document
.documentElement
.scrollTop
) {
142 document
.documentElement
.scrollTop
= winScroll
;
143 } else if ( document
.body
) {
144 document
.body
.scrollTop
= winScroll
;
147 } else if ( txtarea
.selectionStart
|| txtarea
.selectionStart
== '0' ) { // Mozilla
148 // save textarea scroll position
149 var textScroll
= txtarea
.scrollTop
;
150 // get current selection
152 var startPos
= txtarea
.selectionStart
;
153 var endPos
= txtarea
.selectionEnd
;
154 selText
= txtarea
.value
.substring( startPos
, endPos
);
157 txtarea
.value
= txtarea
.value
.substring(0, startPos
)
158 + tagOpen
+ selText
+ tagClose
159 + txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
161 if ( isSample
&& ( selectText
!== false )) {
162 txtarea
.selectionStart
= startPos
+ tagOpen
.length
;
163 txtarea
.selectionEnd
= startPos
+ tagOpen
.length
+ selText
.length
;
165 txtarea
.selectionStart
= startPos
+ tagOpen
.length
+ selText
.length
+ tagClose
.length
;
166 txtarea
.selectionEnd
= txtarea
.selectionStart
;
168 // restore textarea scroll position
169 txtarea
.scrollTop
= textScroll
;
175 * Restore the edit box scroll state following a preview operation,
176 * and set up a form submission handler to remember this state
178 window
.scrollEditBox = function() {
179 var editBox
= document
.getElementById( 'wpTextbox1' );
180 var scrollTop
= document
.getElementById( 'wpScrolltop' );
181 var editForm
= document
.getElementById( 'editform' );
182 if( editForm
&& editBox
&& scrollTop
) {
183 if( scrollTop
.value
) {
184 editBox
.scrollTop
= scrollTop
.value
;
186 addHandler( editForm
, 'submit', function() {
187 scrollTop
.value
= editBox
.scrollTop
;
191 hookEvent( 'load', scrollEditBox
);
192 hookEvent( 'load', mwSetupToolbar
);
193 hookEvent( 'load', function() {
194 currentFocused
= document
.getElementById( 'wpTextbox1' );
195 // http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
196 // focus does not bubble normally, but using a trick we can do event delegation
197 // on the focus event on all text inputs to make the toolbox usable on all of them
198 var editForm
= document
.getElementById( 'editform' );
202 function onfocus( e
) {
203 var elm
= e
.target
|| e
.srcElement
;
207 var tagName
= elm
.tagName
.toLowerCase();
208 var type
= elm
.type
|| '';
209 if ( tagName
!== 'textarea' && tagName
!== 'input' ) {
212 if ( tagName
=== 'input' && type
.toLowerCase() !== 'text' ) {
216 currentFocused
= elm
;
219 if ( editForm
.addEventListener
) {
220 // Gecko, WebKit, Opera, etc... (all standards compliant browsers)
221 editForm
.addEventListener( 'focus', onfocus
, true ); // This MUST be true to work
222 } else if ( editForm
.attachEvent
) {
223 // IE needs a specific trick here since it doesn't support the standard
224 editForm
.attachEvent( 'onfocusin', function() { onfocus( event
); } );
227 // HACK: make currentFocused work with the usability iframe
228 // With proper focus detection support (HTML 5!) this'll be much cleaner
229 if ( typeof $ != 'undefined' ) {
230 var iframe
= $( '.wikiEditor-ui-text iframe' );
231 if ( iframe
.length
> 0 ) {
232 $( iframe
.get( 0 ).contentWindow
.document
)
233 .add( iframe
.get( 0 ).contentWindow
.document
.body
) // for IE
234 .focus( function() { currentFocused
= iframe
.get( 0 ); } );