Using WikiError seems to be useless here
[mediawiki.git] / skins / common / edit.js
blob945059e07b5ba31fb83e95273ebece0cf8e01012
1 // this function generates the actual toolbar buttons with localized text
2 // we use it to avoid creating the toolbar where javascript is not enabled
3 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
4         // Don't generate buttons for browsers which don't fully
5         // support it.
6         mwEditButtons[mwEditButtons.length] =
7                 {"imageId": imageId,
8                  "imageFile": imageFile,
9                  "speedTip": speedTip,
10                  "tagOpen": tagOpen,
11                  "tagClose": tagClose,
12                  "sampleText": sampleText};
15 // this function generates the actual toolbar buttons with localized text
16 // we use it to avoid creating the toolbar where javascript is not enabled
17 function mwInsertEditButton(parent, item) {
18         var image = document.createElement("img");
19         image.width = 23;
20         image.height = 22;
21         image.className = "mw-toolbar-editbutton";
22         if (item.imageId) image.id = item.imageId;
23         image.src = item.imageFile;
24         image.border = 0;
25         image.alt = item.speedTip;
26         image.title = item.speedTip;
27         image.style.cursor = "pointer";
28         image.onclick = function() {
29                 insertTags(item.tagOpen, item.tagClose, item.sampleText);
30                 return false;
31         };
33         parent.appendChild(image);
34         return true;
37 function mwSetupToolbar() {
38         var toolbar = document.getElementById('toolbar');
39         if (!toolbar) { return false; }
41         var textbox = document.getElementById('wpTextbox1');
42         if (!textbox) { return false; }
44         // Don't generate buttons for browsers which don't fully
45         // support it.
46         if (!(document.selection && document.selection.createRange)
47                 && textbox.selectionStart === null) {
48                 return false;
49         }
51         for (var i = 0; i < mwEditButtons.length; i++) {
52                 mwInsertEditButton(toolbar, mwEditButtons[i]);
53         }
54         for (var i = 0; i < mwCustomEditButtons.length; i++) {
55                 mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
56         }
57         return true;
60 // apply tagOpen/tagClose to selection in textarea,
61 // use sampleText instead of selection if there is none
62 function insertTags(tagOpen, tagClose, sampleText) {
63         var txtarea;
64         if (document.editform) {
65                 txtarea = document.editform.wpTextbox1;
66         } else {
67                 // some alternate form? take the first one we can find
68                 var areas = document.getElementsByTagName('textarea');
69                 txtarea = areas[0];
70         }
71         var selText, isSample = false;
73         if (document.selection  && document.selection.createRange) { // IE/Opera
75                 //save window scroll position
76                 if (document.documentElement && document.documentElement.scrollTop)
77                         var winScroll = document.documentElement.scrollTop
78                 else if (document.body)
79                         var winScroll = document.body.scrollTop;
80                 //get current selection  
81                 txtarea.focus();
82                 var range = document.selection.createRange();
83                 selText = range.text;
84                 //insert tags
85                 checkSelectedText();
86                 range.text = tagOpen + selText + tagClose;
87                 //mark sample text as selected
88                 if (isSample && range.moveStart) {
89                         if (window.opera)
90                                 tagClose = tagClose.replace(/\n/g,'');
91                         range.moveStart('character', - tagClose.length - selText.length); 
92                         range.moveEnd('character', - tagClose.length); 
93                 }
94                 range.select();   
95                 //restore window scroll position
96                 if (document.documentElement && document.documentElement.scrollTop)
97                         document.documentElement.scrollTop = winScroll
98                 else if (document.body)
99                         document.body.scrollTop = winScroll;
101         } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
103                 //save textarea scroll position
104                 var textScroll = txtarea.scrollTop;
105                 //get current selection
106                 txtarea.focus();
107                 var startPos = txtarea.selectionStart;
108                 var endPos = txtarea.selectionEnd;
109                 selText = txtarea.value.substring(startPos, endPos);
110                 //insert tags
111                 checkSelectedText();
112                 txtarea.value = txtarea.value.substring(0, startPos)
113                         + tagOpen + selText + tagClose
114                         + txtarea.value.substring(endPos, txtarea.value.length);
115                 //set new selection
116                 if (isSample) {
117                         txtarea.selectionStart = startPos + tagOpen.length;
118                         txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
119                 } else {
120                         txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
121                         txtarea.selectionEnd = txtarea.selectionStart;
122                 }
123                 //restore textarea scroll position
124                 txtarea.scrollTop = textScroll;
125         } 
127         function checkSelectedText(){
128                 if (!selText) {
129                         selText = sampleText;
130                         isSample = true;
131                 } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
132                         selText = selText.substring(0, selText.length - 1);
133                         tagClose += ' '
134                 } 
135         }
140  * Restore the edit box scroll state following a preview operation,
141  * and set up a form submission handler to remember this state
142  */
143 function scrollEditBox() {
144         var editBox = document.getElementById( 'wpTextbox1' );
145         var scrollTop = document.getElementById( 'wpScrolltop' );
146         var editForm = document.getElementById( 'editform' );
147         if( editBox && scrollTop ) {
148                 if( scrollTop.value )
149                         editBox.scrollTop = scrollTop.value;
150                 addHandler( editForm, 'submit', function() {
151                         document.getElementById( 'wpScrolltop' ).value = document.getElementById( 'wpTextbox1' ).scrollTop; 
152                 } );
153         }
155 hookEvent( 'load', scrollEditBox );
156 hookEvent( 'load', mwSetupToolbar );