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);
35 parent.appendChild(image);
39 function mwSetupToolbar() {
40 var toolbar = document.getElementById('toolbar');
41 if (!toolbar) { return false; }
43 var textbox = document.getElementById('wpTextbox1');
44 if (!textbox) { return false; }
46 // Don't generate buttons for browsers which don't fully
48 if (!(document.selection && document.selection.createRange)
49 && textbox.selectionStart === null) {
53 for (var i = 0; i < mwEditButtons.length; i++) {
54 mwInsertEditButton(toolbar, mwEditButtons[i]);
56 for (var i = 0; i < mwCustomEditButtons.length; i++) {
57 mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
62 // apply tagOpen/tagClose to selection in textarea,
63 // use sampleText instead of selection if there is none
64 function insertTags(tagOpen, tagClose, sampleText) {
66 if (document.editform) {
67 txtarea = currentFocused;
69 // some alternate form? take the first one we can find
70 var areas = document.getElementsByTagName('textarea');
73 var selText, isSample = false;
75 if (document.selection && document.selection.createRange) { // IE/Opera
77 //save window scroll position
78 if (document.documentElement && document.documentElement.scrollTop)
79 var winScroll = document.documentElement.scrollTop
80 else if (document.body)
81 var winScroll = document.body.scrollTop;
82 //get current selection
84 var range = document.selection.createRange();
88 range.text = tagOpen + selText + tagClose;
89 //mark sample text as selected
90 if (isSample && range.moveStart) {
92 tagClose = tagClose.replace(/\n/g,'');
93 range.moveStart('character', - tagClose.length - selText.length);
94 range.moveEnd('character', - tagClose.length);
97 //restore window scroll position
98 if (document.documentElement && document.documentElement.scrollTop)
99 document.documentElement.scrollTop = winScroll
100 else if (document.body)
101 document.body.scrollTop = winScroll;
103 } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
105 //save textarea scroll position
106 var textScroll = txtarea.scrollTop;
107 //get current selection
109 var startPos = txtarea.selectionStart;
110 var endPos = txtarea.selectionEnd;
111 selText = txtarea.value.substring(startPos, endPos);
114 txtarea.value = txtarea.value.substring(0, startPos)
115 + tagOpen + selText + tagClose
116 + txtarea.value.substring(endPos, txtarea.value.length);
119 txtarea.selectionStart = startPos + tagOpen.length;
120 txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
122 txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
123 txtarea.selectionEnd = txtarea.selectionStart;
125 //restore textarea scroll position
126 txtarea.scrollTop = textScroll;
129 function checkSelectedText(){
131 selText = sampleText;
133 } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
134 selText = selText.substring(0, selText.length - 1);
142 * Restore the edit box scroll state following a preview operation,
143 * and set up a form submission handler to remember this state
145 function scrollEditBox() {
146 var editBox = document.getElementById( 'wpTextbox1' );
147 var scrollTop = document.getElementById( 'wpScrolltop' );
148 var editForm = document.getElementById( 'editform' );
149 if( editBox && scrollTop ) {
150 if( scrollTop.value )
151 editBox.scrollTop = scrollTop.value;
152 addHandler( editForm, 'submit', function() {
153 document.getElementById( 'wpScrolltop' ).value = document.getElementById( 'wpTextbox1' ).scrollTop;
157 hookEvent( 'load', scrollEditBox );
158 hookEvent( 'load', mwSetupToolbar );
159 hookEvent( 'load', function() {
160 if ( document.editform ) {
161 currentFocused = document.editform.wpTextbox1;
162 document.editform.wpTextbox1.onfocus = function() { currentFocused = document.editform.wpTextbox1; };
163 document.editform.wpSummary.onfocus = function() { currentFocused = document.editform.wpSummary; };