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});
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 // Don't generate buttons for browsers which don't fully
49 var textbox = document.createElement('textarea'); // abstract, don't assume wpTextbox1 is always there
50 if (!(document.selection && document.selection.createRange)
51 && textbox.selectionStart === null) {
55 for (var i = 0; i < mwEditButtons.length; i++) {
56 mwInsertEditButton(toolbar, mwEditButtons[i]);
58 for (var i = 0; i < mwCustomEditButtons.length; i++) {
59 mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
64 // apply tagOpen/tagClose to selection in textarea,
65 // use sampleText instead of selection if there is none
66 function insertTags(tagOpen, tagClose, sampleText) {
68 if (document.editform) {
69 txtarea = currentFocused;
71 // some alternate form? take the first one we can find
72 var areas = document.getElementsByTagName('textarea');
75 var selText, isSample = false;
77 if (document.selection && document.selection.createRange) { // IE/Opera
79 //save window scroll position
80 if (document.documentElement && document.documentElement.scrollTop)
81 var winScroll = document.documentElement.scrollTop
82 else if (document.body)
83 var winScroll = document.body.scrollTop;
84 //get current selection
86 var range = document.selection.createRange();
90 range.text = tagOpen + selText + tagClose;
91 //mark sample text as selected
92 if (isSample && range.moveStart) {
94 tagClose = tagClose.replace(/\n/g,'');
95 range.moveStart('character', - tagClose.length - selText.length);
96 range.moveEnd('character', - tagClose.length);
99 //restore window scroll position
100 if (document.documentElement && document.documentElement.scrollTop)
101 document.documentElement.scrollTop = winScroll
102 else if (document.body)
103 document.body.scrollTop = winScroll;
105 } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
107 //save textarea scroll position
108 var textScroll = txtarea.scrollTop;
109 //get current selection
111 var startPos = txtarea.selectionStart;
112 var endPos = txtarea.selectionEnd;
113 selText = txtarea.value.substring(startPos, endPos);
116 txtarea.value = txtarea.value.substring(0, startPos)
117 + tagOpen + selText + tagClose
118 + txtarea.value.substring(endPos, txtarea.value.length);
121 txtarea.selectionStart = startPos + tagOpen.length;
122 txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
124 txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
125 txtarea.selectionEnd = txtarea.selectionStart;
127 //restore textarea scroll position
128 txtarea.scrollTop = textScroll;
131 function checkSelectedText(){
133 selText = sampleText;
135 } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
136 selText = selText.substring(0, selText.length - 1);
144 * Restore the edit box scroll state following a preview operation,
145 * and set up a form submission handler to remember this state
147 function scrollEditBox() {
148 var editBox = document.getElementById( 'wpTextbox1' );
149 var scrollTop = document.getElementById( 'wpScrolltop' );
150 var editForm = document.getElementById( 'editform' );
151 if( editForm && editBox && scrollTop ) {
152 if( scrollTop.value )
153 editBox.scrollTop = scrollTop.value;
154 addHandler( editForm, 'submit', function() {
155 scrollTop.value = editBox.scrollTop;
159 hookEvent( 'load', scrollEditBox );
160 hookEvent( 'load', mwSetupToolbar );
161 hookEvent( 'load', function() {
162 currentFocused = document.getElementById( 'wpTextbox1' );