Opera 8 can't get a ref to a form element, so it was just spitting out errors. This...
[lhc/web/wiklou.git] / skins / common / edit.js
1 var currentFocused;
2
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
7 // support it.
8 mwEditButtons[mwEditButtons.length] =
9 {"imageId": imageId,
10 "imageFile": imageFile,
11 "speedTip": speedTip,
12 "tagOpen": tagOpen,
13 "tagClose": tagClose,
14 "sampleText": sampleText};
15 }
16
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");
21 image.width = 23;
22 image.height = 22;
23 image.className = "mw-toolbar-editbutton";
24 if (item.imageId) image.id = item.imageId;
25 image.src = item.imageFile;
26 image.border = 0;
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);
32 return false;
33 };
34
35 parent.appendChild(image);
36 return true;
37 }
38
39 function mwSetupToolbar() {
40 var toolbar = document.getElementById('toolbar');
41 if (!toolbar) { return false; }
42
43 var textbox = document.getElementById('wpTextbox1');
44 if (!textbox) { return false; }
45
46 // Don't generate buttons for browsers which don't fully
47 // support it.
48 if (!(document.selection && document.selection.createRange)
49 && textbox.selectionStart === null) {
50 return false;
51 }
52
53 for (var i = 0; i < mwEditButtons.length; i++) {
54 mwInsertEditButton(toolbar, mwEditButtons[i]);
55 }
56 for (var i = 0; i < mwCustomEditButtons.length; i++) {
57 mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
58 }
59 return true;
60 }
61
62 // apply tagOpen/tagClose to selection in textarea,
63 // use sampleText instead of selection if there is none
64 function insertTags(tagOpen, tagClose, sampleText) {
65 var txtarea;
66 if (document.editform) {
67 txtarea = currentFocused;
68 } else {
69 // some alternate form? take the first one we can find
70 var areas = document.getElementsByTagName('textarea');
71 txtarea = areas[0];
72 }
73 var selText, isSample = false;
74
75 if (document.selection && document.selection.createRange) { // IE/Opera
76
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
83 txtarea.focus();
84 var range = document.selection.createRange();
85 selText = range.text;
86 //insert tags
87 checkSelectedText();
88 range.text = tagOpen + selText + tagClose;
89 //mark sample text as selected
90 if (isSample && range.moveStart) {
91 if (window.opera)
92 tagClose = tagClose.replace(/\n/g,'');
93 range.moveStart('character', - tagClose.length - selText.length);
94 range.moveEnd('character', - tagClose.length);
95 }
96 range.select();
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;
102
103 } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
104
105 //save textarea scroll position
106 var textScroll = txtarea.scrollTop;
107 //get current selection
108 txtarea.focus();
109 var startPos = txtarea.selectionStart;
110 var endPos = txtarea.selectionEnd;
111 selText = txtarea.value.substring(startPos, endPos);
112 //insert tags
113 checkSelectedText();
114 txtarea.value = txtarea.value.substring(0, startPos)
115 + tagOpen + selText + tagClose
116 + txtarea.value.substring(endPos, txtarea.value.length);
117 //set new selection
118 if (isSample) {
119 txtarea.selectionStart = startPos + tagOpen.length;
120 txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
121 } else {
122 txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
123 txtarea.selectionEnd = txtarea.selectionStart;
124 }
125 //restore textarea scroll position
126 txtarea.scrollTop = textScroll;
127 }
128
129 function checkSelectedText(){
130 if (!selText) {
131 selText = sampleText;
132 isSample = true;
133 } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
134 selText = selText.substring(0, selText.length - 1);
135 tagClose += ' '
136 }
137 }
138
139 }
140
141 /**
142 * Restore the edit box scroll state following a preview operation,
143 * and set up a form submission handler to remember this state
144 */
145 function scrollEditBox() {
146 var editBox = document.getElementById( 'wpTextbox1' );
147 var scrollTop = document.getElementById( 'wpScrolltop' );
148 var editForm = document.getElementById( 'editform' );
149 if( editForm && editBox && scrollTop ) {
150 if( scrollTop.value )
151 editBox.scrollTop = scrollTop.value;
152 addHandler( editForm, 'submit', function() {
153 alert(1);
154 document.getElementById( 'wpScrolltop' ).value = document.getElementById( 'wpTextbox1' ).scrollTop;
155 } );
156 }
157 }
158 hookEvent( 'load', scrollEditBox );
159 hookEvent( 'load', mwSetupToolbar );
160 hookEvent( 'load', function() {
161 if ( document.editform ) {
162 currentFocused = document.editform.wpTextbox1;
163 document.editform.wpTextbox1.onfocus = function() { currentFocused = document.editform.wpTextbox1; };
164 document.editform.wpSummary.onfocus = function() { currentFocused = document.editform.wpSummary; };
165 }
166 } );
167