74483e45951d1b9a904fe87747f860c43d2f1afb
[lhc/web/wiklou.git] / skins / common / wikibits.js
1 // Wikipedia JavaScript support functions
2 // if this is true, the toolbar will no longer overwrite the infobox when you move the mouse over individual items
3 var noOverwrite=false;
4 var alertText;
5 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
6 var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
7 && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
8 var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1));
9 var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
10 if (clientPC.indexOf('opera')!=-1) {
11 var is_opera = true;
12 var is_opera_preseven = (window.opera && !document.childNodes);
13 var is_opera_seven = (window.opera && document.childNodes);
14 }
15
16 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
17 function onloadhook () {
18 // don't run anything below this for non-dom browsers
19 if(!(document.getElementById && document.getElementsByTagName)) return;
20 histrowinit();
21 unhidetzbutton();
22 tabbedprefs();
23 akeytt();
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 if(typeof stylepath != 'undefined' && typeof skin != 'undefined') {
31 if (is_opera_preseven) {
32 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera6Fixes.css">');
33 } else if (is_opera_seven) {
34 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera7Fixes.css">');
35 } else if (is_khtml) {
36 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/KHTMLFixes.css">');
37 }
38 }
39 // Un-trap us from framesets
40 if( window.top != window ) window.top.location = window.location;
41
42 // for enhanced RecentChanges
43 function toggleVisibility( _levelId, _otherId, _linkId) {
44 var thisLevel = document.getElementById( _levelId );
45 var otherLevel = document.getElementById( _otherId );
46 var linkLevel = document.getElementById( _linkId );
47 if ( thisLevel.style.display == 'none' ) {
48 thisLevel.style.display = 'block';
49 otherLevel.style.display = 'none';
50 linkLevel.style.display = 'inline';
51 } else {
52 thisLevel.style.display = 'none';
53 otherLevel.style.display = 'inline';
54 linkLevel.style.display = 'none';
55 }
56 }
57
58 // page history stuff
59 // attach event handlers to the input elements on history page
60 function histrowinit () {
61 hf = document.getElementById('pagehistory');
62 if(!hf) return;
63 lis = hf.getElementsByTagName('li');
64 for (i=0;i<lis.length;i++) {
65 inputs=lis[i].getElementsByTagName('input');
66 if(inputs[0] && inputs[1]) {
67 inputs[0].onclick = diffcheck;
68 inputs[1].onclick = diffcheck;
69 }
70 }
71 diffcheck();
72 }
73 // check selection and tweak visibility/class onclick
74 function diffcheck() {
75 var dli = false; // the li where the diff radio is checked
76 var oli = false; // the li where the oldid radio is checked
77 hf = document.getElementById('pagehistory');
78 if(!hf) return;
79 lis = hf.getElementsByTagName('li');
80 for (i=0;i<lis.length;i++) {
81 inputs=lis[i].getElementsByTagName('input');
82 if(inputs[1] && inputs[0]) {
83 if(inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
84 if(inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) return false;
85 if(oli) { // it's the second checked radio
86 if(inputs[1].checked) {
87 oli.className = "selected";
88 return false
89 }
90 } else if (inputs[0].checked) {
91 return false;
92 }
93 if(inputs[0].checked) dli = lis[i];
94 if(!oli) inputs[0].style.visibility = 'hidden';
95 if(dli) inputs[1].style.visibility = 'hidden';
96 lis[i].className = "selected";
97 oli = lis[i];
98 } else { // no radio is checked in this row
99 if(!oli) inputs[0].style.visibility = 'hidden';
100 else inputs[0].style.visibility = 'visible';
101 if(dli) inputs[1].style.visibility = 'hidden';
102 else inputs[1].style.visibility = 'visible';
103 lis[i].className = "";
104 }
105 }
106 }
107 }
108
109 // generate toc from prefs form, fold sections
110 // XXX: needs testing on IE/Mac and safari
111 // more comments to follow
112 function tabbedprefs() {
113 prefform = document.getElementById('preferences');
114 if(!prefform || !document.createElement) return;
115 if(prefform.nodeName.toLowerCase() == 'a') return; // Occasional IE problem
116 prefform.className = prefform.className + 'jsprefs';
117 var sections = new Array();
118 children = prefform.childNodes;
119 var seci = 0;
120 for(i=0;i<children.length;i++) {
121 if(children[i].nodeName.toLowerCase().indexOf('fieldset') != -1) {
122 children[i].id = 'prefsection-' + seci;
123 children[i].className = 'prefsection';
124 if(is_opera || is_khtml) children[i].className = 'prefsection operaprefsection';
125 legends = children[i].getElementsByTagName('legend');
126 sections[seci] = new Object();
127 if(legends[0] && legends[0].firstChild.nodeValue)
128 sections[seci].text = legends[0].firstChild.nodeValue;
129 else
130 sections[seci].text = '# ' + seci;
131 sections[seci].secid = children[i].id;
132 seci++;
133 if(sections.length != 1) children[i].style.display = 'none';
134 else var selectedid = children[i].id;
135 }
136 }
137 var toc = document.createElement('ul');
138 toc.id = 'preftoc';
139 toc.selectedid = selectedid;
140 for(i=0;i<sections.length;i++) {
141 var li = document.createElement('li');
142 if(i == 0) li.className = 'selected';
143 var a = document.createElement('a');
144 a.href = '#' + sections[i].secid;
145 a.onclick = uncoversection;
146 a.appendChild(document.createTextNode(sections[i].text));
147 a.secid = sections[i].secid;
148 li.appendChild(a);
149 toc.appendChild(li);
150 }
151 prefform.insertBefore(toc, children[0]);
152 document.getElementById('prefsubmit').id = 'prefcontrol';
153 }
154 function uncoversection() {
155 oldsecid = this.parentNode.parentNode.selectedid;
156 newsec = document.getElementById(this.secid);
157 if(oldsecid != this.secid) {
158 ul = document.getElementById('preftoc');
159 document.getElementById(oldsecid).style.display = 'none';
160 newsec.style.display = 'block';
161 ul.selectedid = this.secid;
162 lis = ul.getElementsByTagName('li');
163 for(i=0;i< lis.length;i++) {
164 lis[i].className = '';
165 }
166 this.parentNode.className = 'selected';
167 }
168 return false;
169 }
170
171 // Timezone stuff
172 // tz in format [+-]HHMM
173 function checkTimezone( tz, msg ) {
174 var localclock = new Date();
175 // returns negative offset from GMT in minutes
176 var tzRaw = localclock.getTimezoneOffset();
177 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
178 var tzMin = Math.abs(tzRaw) % 60;
179 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
180 if( tz != tzString ) {
181 var junk = msg.split( '$1' );
182 document.write( junk[0] + "UTC" + tzString + junk[1] );
183 }
184 }
185 function unhidetzbutton() {
186 tzb = document.getElementById('guesstimezonebutton')
187 if(tzb) tzb.style.display = 'inline';
188 }
189
190 // in [-]HH:MM format...
191 // won't yet work with non-even tzs
192 function fetchTimezone() {
193 // FIXME: work around Safari bug
194 var localclock = new Date();
195 // returns negative offset from GMT in minutes
196 var tzRaw = localclock.getTimezoneOffset();
197 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
198 var tzMin = Math.abs(tzRaw) % 60;
199 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
200 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
201 return tzString;
202 }
203
204 function guessTimezone(box) {
205 document.preferences.wpHourDiff.value = fetchTimezone();
206 }
207
208 function showTocToggle() {
209 if (document.createTextNode) {
210 // Uses DOM calls to avoid document.write + XHTML issues
211
212 var linkHolder = document.getElementById('toctitle')
213 if (!linkHolder) return;
214
215 var outerSpan = document.createElement('span');
216 outerSpan.className = 'toctoggle';
217
218 var toggleLink = document.createElement('a');
219 toggleLink.id = 'togglelink';
220 toggleLink.className = 'internal';
221 toggleLink.href = 'javascript:toggleToc()';
222 toggleLink.appendChild(document.createTextNode(tocHideText));
223
224 outerSpan.appendChild(document.createTextNode('['));
225 outerSpan.appendChild(toggleLink);
226 outerSpan.appendChild(document.createTextNode(']'));
227
228 linkHolder.appendChild(document.createTextNode(' '));
229 linkHolder.appendChild(outerSpan);
230
231 var cookiePos = document.cookie.indexOf("hidetoc=");
232 if (cookiePos > -1 && document.cookie.charAt(cookiePos + 8) == 1)
233 toggleToc();
234 }
235 }
236
237 function changeText(el, newText) {
238 // Safari work around
239 if (el.innerText)
240 el.innerText = newText;
241 else if (el.firstChild && el.firstChild.nodeValue)
242 el.firstChild.nodeValue = newText;
243 }
244
245 function toggleToc() {
246 var toc = document.getElementById('toc').getElementsByTagName('ul')[0];
247 var toggleLink = document.getElementById('togglelink')
248
249 if(toc && toggleLink && toc.style.display == 'none') {
250 changeText(toggleLink, tocHideText);
251 toc.style.display = 'block';
252 document.cookie = "hidetoc=0";
253 } else {
254 changeText(toggleLink, tocShowText);
255 toc.style.display = 'none';
256 document.cookie = "hidetoc=1";
257 }
258 }
259
260 // this function generates the actual toolbar buttons with localized text
261 // we use it to avoid creating the toolbar where javascript is not enabled
262 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
263
264 speedTip=escapeQuotes(speedTip);
265 tagOpen=escapeQuotes(tagOpen);
266 tagClose=escapeQuotes(tagClose);
267 sampleText=escapeQuotes(sampleText);
268 var mouseOver="";
269
270 // we can't change the selection, so we show example texts
271 // when moving the mouse instead, until the first button is clicked
272 if(!document.selection && !is_gecko) {
273 // filter backslashes so it can be shown in the infobox
274 var re=new RegExp("\\\\n","g");
275 tagOpen=tagOpen.replace(re,"");
276 tagClose=tagClose.replace(re,"");
277 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
278 }
279
280 document.write("<a href=\"javascript:insertTags");
281 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
282
283 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" alt=\""+speedTip+"\" title=\""+speedTip+"\""+mouseOver+">");
284 document.write("</a>");
285 return;
286 }
287
288 function addInfobox(infoText,text_alert) {
289 alertText=text_alert;
290 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
291
292 var re=new RegExp("\\\\n","g");
293 alertText=alertText.replace(re,"\n");
294
295 // if no support for changing selection, add a small copy & paste field
296 // document.selection is an IE-only property. The full toolbar works in IE and
297 // Gecko-based browsers.
298 if(!document.selection && !is_gecko) {
299 infoText=escapeQuotesHTML(infoText);
300 document.write("<form name='infoform' id='infoform'>"+
301 "<input size=80 id='infobox' name='infobox' value=\""+
302 infoText+"\" readonly='readonly'></form>");
303 }
304
305 }
306
307 function escapeQuotes(text) {
308 var re=new RegExp("'","g");
309 text=text.replace(re,"\\'");
310 re=new RegExp('"',"g");
311 text=text.replace(re,'&quot;');
312 re=new RegExp("\\n","g");
313 text=text.replace(re,"\\n");
314 return text;
315 }
316
317 function escapeQuotesHTML(text) {
318 var re=new RegExp('"',"g");
319 text=text.replace(re,"&quot;");
320 return text;
321 }
322
323 // apply tagOpen/tagClose to selection in textarea,
324 // use sampleText instead of selection if there is none
325 // copied and adapted from phpBB
326 function insertTags(tagOpen, tagClose, sampleText) {
327
328 var txtarea = document.editform.wpTextbox1;
329 // IE
330 if(document.selection && !is_gecko) {
331 var theSelection = document.selection.createRange().text;
332 if(!theSelection) { theSelection=sampleText;}
333 txtarea.focus();
334 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
335 theSelection = theSelection.substring(0, theSelection.length - 1);
336 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
337 } else {
338 document.selection.createRange().text = tagOpen + theSelection + tagClose;
339 }
340
341 // Mozilla
342 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
343 var startPos = txtarea.selectionStart;
344 var endPos = txtarea.selectionEnd;
345 var scrollTop=txtarea.scrollTop;
346 var myText = (txtarea.value).substring(startPos, endPos);
347 if(!myText) { myText=sampleText;}
348 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
349 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
350 } else {
351 subst = tagOpen + myText + tagClose;
352 }
353 txtarea.value = txtarea.value.substring(0, startPos) + subst +
354 txtarea.value.substring(endPos, txtarea.value.length);
355 txtarea.focus();
356
357 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
358 txtarea.selectionStart=cPos;
359 txtarea.selectionEnd=cPos;
360 txtarea.scrollTop=scrollTop;
361
362 // All others
363 } else {
364 var copy_alertText=alertText;
365 var re1=new RegExp("\\$1","g");
366 var re2=new RegExp("\\$2","g");
367 copy_alertText=copy_alertText.replace(re1,sampleText);
368 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
369 var text;
370 if (sampleText) {
371 text=prompt(copy_alertText);
372 } else {
373 text="";
374 }
375 if(!text) { text=sampleText;}
376 text=tagOpen+text+tagClose;
377 document.infoform.infobox.value=text;
378 // in Safari this causes scrolling
379 if(!is_safari) {
380 txtarea.focus();
381 }
382 noOverwrite=true;
383 }
384 // reposition cursor if possible
385 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
386 }
387
388 function akeytt() {
389 if(typeof ta == "undefined" || !ta) return;
390 pref = 'alt-';
391 if(is_safari || navigator.userAgent.toLowerCase().indexOf( 'mac' ) + 1 ) pref = 'control-';
392 if(is_opera) pref = 'shift-esc-';
393 for(id in ta) {
394 n = document.getElementById(id);
395 if(n){
396 a = n.childNodes[0];
397 if(a){
398 if(ta[id][0].length > 0) {
399 a.accessKey = ta[id][0];
400 ak = ' ['+pref+ta[id][0]+']';
401 } else {
402 ak = '';
403 }
404 a.title = ta[id][1]+ak;
405 } else {
406 if(ta[id][0].length > 0) {
407 n.accessKey = ta[id][0];
408 ak = ' ['+pref+ta[id][0]+']';
409 } else {
410 ak = '';
411 }
412 n.title = ta[id][1]+ak;
413 }
414 }
415 }
416 }
417
418 function setupRightClickEdit() {
419 if( document.getElementsByTagName ) {
420 var divs = document.getElementsByTagName( 'div' );
421 for( var i = 0; i < divs.length; i++ ) {
422 var el = divs[i];
423 if( el.className == 'editsection' ) {
424 addRightClickEditHandler( el );
425 }
426 }
427 }
428 }
429
430 function addRightClickEditHandler( el ) {
431 for( var i = 0; i < el.childNodes.length; i++ ) {
432 var link = el.childNodes[i];
433 if( link.nodeType == 1 && link.nodeName.toLowerCase() == 'a' ) {
434 var editHref = link.getAttribute( 'href' );
435
436 // find the following a
437 var next = el.nextSibling;
438 while( next.nodeType != 1 )
439 next = next.nextSibling;
440
441 // find the following header
442 next = next.nextSibling;
443 while( next.nodeType != 1 )
444 next = next.nextSibling;
445
446 if( next && next.nodeType == 1 &&
447 next.nodeName.match( /^[Hh][1-6]$/ ) ) {
448 next.oncontextmenu = function() {
449 document.location = editHref;
450 return false;
451 }
452 }
453 }
454 }
455 }
456
457 function fillDestFilename() {
458 if (!document.getElementById) return;
459 var path = document.getElementById('wpUploadFile').value;
460 // Find trailing part
461 var slash = path.lastIndexOf( '/' );
462 var backslash = path.lastIndexOf( '\\' );
463 var fname;
464 if ( slash == -1 && backslash == -1 ) {
465 fname = path;
466 } else if ( slash > backslash ) {
467 fname = path.substring( slash+1, 10000 );
468 } else {
469 fname = path.substring( backslash+1, 10000 );
470 }
471
472 // Capitalise first letter and replace spaces by underscores
473 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace( / /g, '_' );
474
475 // Output result
476 var destFile = document.getElementById('wpDestFile');
477 if (destFile.value == '' || destFile.value == document.fname ) {
478 document.getElementById('wpDestFile').value = fname;
479 }
480 document.fname = fname;
481 }
482