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 if ( !( document.selection && document.selection.createRange )
61 && textboxes[0].selectionStart === null ) {
65 for ( var i = 0; i < mwEditButtons.length; i++ ) {
66 mwInsertEditButton( toolbar, mwEditButtons[i] );
68 for ( var i = 0; i < mwCustomEditButtons.length; i++ ) {
69 mwInsertEditButton( toolbar, mwCustomEditButtons[i] );
74 // apply tagOpen/tagClose to selection in textarea,
75 // use sampleText instead of selection if there is none
76 function insertTags( tagOpen, tagClose, sampleText ) {
78 if ( document.editform ) {
79 txtarea = currentFocused;
81 // some alternate form? take the first one we can find
82 var areas = document.getElementsByTagName( 'textarea' );
85 var selText, isSample = false;
87 if ( document.selection && document.selection.createRange ) { // IE/Opera
88 // save window scroll position
89 if ( document.documentElement && document.documentElement.scrollTop ) {
90 var winScroll = document.documentElement.scrollTop
91 } else if ( document.body ) {
92 var winScroll = document.body.scrollTop;
94 // get current selection
96 var range = document.selection.createRange();
100 range.text = tagOpen + selText + tagClose;
101 // mark sample text as selected
102 if ( isSample && range.moveStart ) {
103 if ( window.opera ) {
104 tagClose = tagClose.replace(/\n/g,'');
106 range.moveStart('character', - tagClose.length - selText.length);
107 range.moveEnd('character', - tagClose.length);
110 // restore window scroll position
111 if ( document.documentElement && document.documentElement.scrollTop ) {
112 document.documentElement.scrollTop = winScroll;
113 } else if ( document.body ) {
114 document.body.scrollTop = winScroll;
117 } else if ( txtarea.selectionStart || txtarea.selectionStart == '0' ) { // Mozilla
118 // save textarea scroll position
119 var textScroll = txtarea.scrollTop;
120 // get current selection
122 var startPos = txtarea.selectionStart;
123 var endPos = txtarea.selectionEnd;
124 selText = txtarea.value.substring( startPos, endPos );
127 txtarea.value = txtarea.value.substring(0, startPos)
128 + tagOpen + selText + tagClose
129 + txtarea.value.substring(endPos, txtarea.value.length);
132 txtarea.selectionStart = startPos + tagOpen.length;
133 txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
135 txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
136 txtarea.selectionEnd = txtarea.selectionStart;
138 // restore textarea scroll position
139 txtarea.scrollTop = textScroll;
142 function checkSelectedText() {
144 selText = sampleText;
146 } else if ( selText.charAt(selText.length - 1) == ' ' ) { // exclude ending space char
147 selText = selText.substring(0, selText.length - 1);
155 * Restore the edit box scroll state following a preview operation,
156 * and set up a form submission handler to remember this state
158 function scrollEditBox() {
159 var editBox = document.getElementById( 'wpTextbox1' );
160 var scrollTop = document.getElementById( 'wpScrolltop' );
161 var editForm = document.getElementById( 'editform' );
162 if( editForm && editBox && scrollTop ) {
163 if( scrollTop.value ) {
164 editBox.scrollTop = scrollTop.value;
166 addHandler( editForm, 'submit', function() {
167 scrollTop.value = editBox.scrollTop;
171 hookEvent( 'load', scrollEditBox );
172 hookEvent( 'load', mwSetupToolbar );
173 hookEvent( 'load', function() {
174 currentFocused = document.getElementById( 'wpTextbox1' );
175 // http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
176 // focus does not bubble normally, but using a trick we can do event delegation
177 // on the focus event on all text inputs to make the toolbox usable on all of them
178 var editForm = document.getElementById( 'editform' );
182 function onfocus( e ) {
187 var tagName = elm.tagName.toLowerCase();
188 var type = elm.type.toLowerCase();
189 if ( tagName !== 'textarea' && tagName !== 'input' ) {
192 if ( tagName === 'input' && type && type !== 'text' ) {
196 currentFocused = elm;
199 if ( editForm.addEventListener ) {
200 // Gecko, WebKit, Opera, etc... (all standards compliant browsers)
201 editForm.addEventListener( 'focus', onfocus, true ); // This MUST be true to work
202 } else if ( editForm.attachEvent ) {
203 // IE needs a specific trick here since it doesn't support the standard
204 editForm.attachEvent( 'onfocusin', function() { onfocus( event ); } );