experimental quickEdit() function
[lhc/web/wiklou.git] / stylesheets / wikibits.js
1 // Wikipedia JavaScript support functions
2
3 // if this is true, the toolbar will no longer overwrite the infobox when you move the mouse over individual items
4 var noOverwrite=false;
5 var alertText;
6 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
7 var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
8 && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
9 var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1));
10 var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
11 if (clientPC.indexOf('opera')!=-1) {
12 var is_opera = true;
13 var is_opera_preseven = (window.opera && !document.childNodes);
14 var is_opera_seven = (window.opera && document.childNodes);
15 }
16
17 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
18 function onloadhook () {
19 // don't run anything below this for non-dom browsers
20 if(!(document.getElementById && document.getElementsByTagName)) return;
21 histrowinit();
22 unhidetzbutton();
23 tabbedprefs();
24 }
25 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
26 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
27
28
29 // document.write special stylesheet links
30 function addcss ( stylepath ) {
31 if (is_opera_preseven) {
32 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'Opera6Fixes.css">');
33 } else if (is_opera_seven) {
34 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'Opera7Fixes.css">');
35 } else if (is_khtml) {
36 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'KHTMLFixes.css">');
37 }
38 return;
39 }
40
41 // Un-trap us from framesets
42 if( window.top != window ) window.top.location = window.location;
43
44 // for enhanced RecentChanges
45 function toggleVisibility( _levelId, _otherId, _linkId) {
46 var thisLevel = document.getElementById( _levelId );
47 var otherLevel = document.getElementById( _otherId );
48 var linkLevel = document.getElementById( _linkId );
49 if ( thisLevel.style.display == 'none' ) {
50 thisLevel.style.display = 'block';
51 otherLevel.style.display = 'none';
52 linkLevel.style.display = 'inline';
53 } else {
54 thisLevel.style.display = 'none';
55 otherLevel.style.display = 'inline';
56 linkLevel.style.display = 'none';
57 }
58 }
59
60 // page history stuff
61 // attach event handlers to the input elements on history page
62 function histrowinit () {
63 hf = document.getElementById('pagehistory');
64 if(!hf) return;
65 lis = hf.getElementsByTagName('LI');
66 for (i=0;i<lis.length;i++) {
67 inputs=lis[i].getElementsByTagName('INPUT');
68 if(inputs[0] && inputs[1]) {
69 inputs[0].onclick = diffcheck;
70 inputs[1].onclick = diffcheck;
71 }
72 }
73 diffcheck();
74 }
75 // check selection and tweak visibility/class onclick
76 function diffcheck() {
77 var dli = false; // the li where the diff radio is checked
78 var oli = false; // the li where the oldid radio is checked
79 hf = document.getElementById('pagehistory');
80 if(!hf) return;
81 lis = hf.getElementsByTagName('LI');
82 for (i=0;i<lis.length;i++) {
83 inputs=lis[i].getElementsByTagName('INPUT');
84 if(inputs[1] && inputs[0]) {
85 if(inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
86 if(inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) return false;
87 if(oli) { // it's the second checked radio
88 if(inputs[1].checked) {
89 oli.className = "selected";
90 return false
91 }
92 } else if (inputs[0].checked) {
93 return false;
94 }
95 if(inputs[0].checked) dli = lis[i];
96 if(!oli) inputs[0].style.visibility = 'hidden';
97 if(dli) inputs[1].style.visibility = 'hidden';
98 lis[i].className = "selected";
99 oli = lis[i];
100 } else { // no radio is checked in this row
101 if(!oli) inputs[0].style.visibility = 'hidden';
102 else inputs[0].style.visibility = 'visible';
103 if(dli) inputs[1].style.visibility = 'hidden';
104 else inputs[1].style.visibility = 'visible';
105 lis[i].className = "";
106 }
107 }
108 }
109 }
110
111 // generate toc from prefs form, fold sections
112 // XXX: needs testing on IE/Mac and safari
113 // more comments to follow
114 function tabbedprefs() {
115 prefform = document.getElementById('preferences');
116 if(!prefform || !document.createElement) return;
117 prefform.className = prefform.className + 'jsprefs';
118 var sections = new Array();
119 children = prefform.childNodes;
120 var seci = 0;
121 for(i=0;i<children.length;i++) {
122 if(children[i].nodeName.indexOf('FIELDSET') != -1) {
123 children[i].id = 'prefsection-' + seci;
124 children[i].className = 'prefsection';
125 if(is_opera || is_khtml) children[i].className = 'prefsection operaprefsection';
126 legends = children[i].getElementsByTagName('LEGEND');
127 sections[seci] = new Object();
128 if(legends[0] && legends[0].firstChild.nodeValue)
129 sections[seci].text = legends[0].firstChild.nodeValue;
130 else
131 sections[seci].text = '# ' + seci;
132 sections[seci].secid = children[i].id;
133 seci++;
134 if(sections.length != 1) children[i].style.display = 'none';
135 else var selectedid = children[i].id;
136 }
137 }
138 var toc = document.createElement('UL');
139 toc.id = 'preftoc';
140 toc.selectedid = selectedid;
141 for(i=0;i<sections.length;i++) {
142 var li = document.createElement('LI');
143 if(i == 0) li.className = 'selected';
144 var a = document.createElement('A');
145 a.href = '#' + sections[i].secid;
146 a.onclick = uncoversection;
147 a.innerHTML = sections[i].text;
148 a.secid = sections[i].secid;
149 li.appendChild(a);
150 toc.appendChild(li);
151 }
152 prefform.insertBefore(toc, children[0]);
153 document.getElementById('prefsubmit').id = 'prefcontrol';
154 }
155 function uncoversection() {
156 oldsecid = this.parentNode.parentNode.selectedid;
157 newsec = document.getElementById(this.secid);
158 if(oldsecid != this.secid) {
159 ul = document.getElementById('preftoc');
160 document.getElementById(oldsecid).style.display = 'none';
161 newsec.style.display = 'block';
162 ul.selectedid = this.secid;
163 lis = ul.getElementsByTagName('LI');
164 for(i=0;i< lis.length;i++) {
165 lis[i].className = '';
166 }
167 this.parentNode.className = 'selected';
168 }
169 return false;
170 }
171
172 // Timezone stuff
173 // tz in format [+-]HHMM
174 function checkTimezone( tz, msg ) {
175 var localclock = new Date();
176 // returns negative offset from GMT in minutes
177 var tzRaw = localclock.getTimezoneOffset();
178 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
179 var tzMin = Math.abs(tzRaw) % 60;
180 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
181 if( tz != tzString ) {
182 var junk = msg.split( '$1' );
183 document.write( junk[0] + "UTC" + tzString + junk[1] );
184 }
185 }
186 function unhidetzbutton() {
187 tzb = document.getElementById('guesstimezonebutton')
188 if(tzb) tzb.style.display = 'inline';
189 }
190
191 // in [-]HH:MM format...
192 // won't yet work with non-even tzs
193 function fetchTimezone() {
194 // FIXME: work around Safari bug
195 var localclock = new Date();
196 // returns negative offset from GMT in minutes
197 var tzRaw = localclock.getTimezoneOffset();
198 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
199 var tzMin = Math.abs(tzRaw) % 60;
200 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
201 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
202 return tzString;
203 }
204
205 function guessTimezone(box) {
206 document.preferences.wpHourDiff.value = fetchTimezone();
207 }
208
209 function showTocToggle(show,hide) {
210 if(document.getElementById) {
211 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
212 '<span id="showlink" style="display:none;">' + show + '</span>' +
213 '<span id="hidelink">' + hide + '</span>'
214 + '</a>]</span>');
215 }
216 }
217
218
219 function toggleToc() {
220 var toc = document.getElementById('tocinside');
221 var showlink=document.getElementById('showlink');
222 var hidelink=document.getElementById('hidelink');
223 if(toc.style.display == 'none') {
224 toc.style.display = tocWas;
225 hidelink.style.display='';
226 showlink.style.display='none';
227
228 } else {
229 tocWas = toc.style.display;
230 toc.style.display = 'none';
231 hidelink.style.display='none';
232 showlink.style.display='';
233
234 }
235 }
236
237 // this function generates the actual toolbar buttons with localized text
238 // we use it to avoid creating the toolbar where javascript is not enabled
239 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
240
241 speedTip=escapeQuotes(speedTip);
242 tagOpen=escapeQuotes(tagOpen);
243 tagClose=escapeQuotes(tagClose);
244 sampleText=escapeQuotes(sampleText);
245 var mouseOver="";
246
247 // we can't change the selection, so we show example texts
248 // when moving the mouse instead, until the first button is clicked
249 if(!document.selection && !is_gecko) {
250 // filter backslashes so it can be shown in the infobox
251 var re=new RegExp("\\\\n","g");
252 tagOpen=tagOpen.replace(re,"");
253 tagClose=tagClose.replace(re,"");
254 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
255 }
256
257 document.write("<a href=\"javascript:insertTags");
258 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
259
260 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
261 document.write("</a>");
262 return;
263 }
264
265 function addInfobox(infoText,text_alert) {
266 alertText=text_alert;
267 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
268
269 var re=new RegExp("\\\\n","g");
270 alertText=alertText.replace(re,"\n");
271
272 // if no support for changing selection, add a small copy & paste field
273 // document.selection is an IE-only property. The full toolbar works in IE and
274 // Gecko-based browsers.
275 if(!document.selection && !is_gecko) {
276 infoText=escapeQuotesHTML(infoText);
277 document.write("<form name='infoform' id='infoform'>"+
278 "<input size=80 id='infobox' name='infobox' value=\""+
279 infoText+"\" READONLY></form>");
280 }
281
282 }
283
284 function escapeQuotes(text) {
285 var re=new RegExp("'","g");
286 text=text.replace(re,"\\'");
287 re=new RegExp('"',"g");
288 text=text.replace(re,'&quot;');
289 re=new RegExp("\\n","g");
290 text=text.replace(re,"\\n");
291 return text;
292 }
293
294 function escapeQuotesHTML(text) {
295 var re=new RegExp('"',"g");
296 text=text.replace(re,"&quot;");
297 return text;
298 }
299
300 // apply tagOpen/tagClose to selection in textarea,
301 // use sampleText instead of selection if there is none
302 // copied and adapted from phpBB
303 function insertTags(tagOpen, tagClose, sampleText) {
304
305 var txtarea = document.editform.wpTextbox1;
306 // IE
307 if(document.selection && !is_gecko) {
308 var theSelection = document.selection.createRange().text;
309 if(!theSelection) { theSelection=sampleText;}
310 txtarea.focus();
311 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
312 theSelection = theSelection.substring(0, theSelection.length - 1);
313 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
314 } else {
315 document.selection.createRange().text = tagOpen + theSelection + tagClose;
316 }
317
318 // Mozilla
319 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
320 var startPos = txtarea.selectionStart;
321 var endPos = txtarea.selectionEnd;
322 var scrollTop=txtarea.scrollTop;
323 var myText = (txtarea.value).substring(startPos, endPos);
324 if(!myText) { myText=sampleText;}
325 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
326 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
327 } else {
328 subst = tagOpen + myText + tagClose;
329 }
330 txtarea.value = txtarea.value.substring(0, startPos) + subst +
331 txtarea.value.substring(endPos, txtarea.value.length);
332 txtarea.focus();
333
334 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
335 txtarea.selectionStart=cPos;
336 txtarea.selectionEnd=cPos;
337 txtarea.scrollTop=scrollTop;
338
339 // All others
340 } else {
341 var copy_alertText=alertText;
342 var re1=new RegExp("\\$1","g");
343 var re2=new RegExp("\\$2","g");
344 copy_alertText=copy_alertText.replace(re1,sampleText);
345 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
346 var text;
347 if (sampleText) {
348 text=prompt(copy_alertText);
349 } else {
350 text="";
351 }
352 if(!text) { text=sampleText;}
353 text=tagOpen+text+tagClose;
354 document.infoform.infobox.value=text;
355 // in Safari this causes scrolling
356 if(!is_safari) {
357 txtarea.focus();
358 }
359 noOverwrite=true;
360 }
361 // reposition cursor if possible
362 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
363 }