c251c787320522eeeb2d73f6fc0d568e1dc1e136
[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 imageFile=escapeQuotesHTML(imageFile);
265 speedTip=escapeQuotesHTML(speedTip);
266 tagOpen=escapeQuotes(tagOpen);
267 tagClose=escapeQuotes(tagClose);
268 sampleText=escapeQuotes(sampleText);
269 var mouseOver="";
270
271 // we can't change the selection, so we show example texts
272 // when moving the mouse instead, until the first button is clicked
273 if(!document.selection && !is_gecko) {
274 // filter backslashes so it can be shown in the infobox
275 var re=new RegExp("\\\\n","g");
276 tagOpen=tagOpen.replace(re,"");
277 tagClose=tagClose.replace(re,"");
278 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
279 }
280
281 document.write("<a href=\"javascript:insertTags");
282 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
283
284 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" alt=\""+speedTip+"\" title=\""+speedTip+"\""+mouseOver+">");
285 document.write("</a>");
286 return;
287 }
288
289 function addInfobox(infoText,text_alert) {
290 alertText=text_alert;
291 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
292
293 var re=new RegExp("\\\\n","g");
294 alertText=alertText.replace(re,"\n");
295
296 // if no support for changing selection, add a small copy & paste field
297 // document.selection is an IE-only property. The full toolbar works in IE and
298 // Gecko-based browsers.
299 if(!document.selection && !is_gecko) {
300 infoText=escapeQuotesHTML(infoText);
301 document.write("<form name='infoform' id='infoform'>"+
302 "<input size=80 id='infobox' name='infobox' value=\""+
303 infoText+"\" readonly='readonly'></form>");
304 }
305
306 }
307
308 function escapeQuotes(text) {
309 var re=new RegExp("'","g");
310 text=text.replace(re,"\\'");
311 re=new RegExp("\\n","g");
312 text=text.replace(re,"\\n");
313 return escapeQuotesHTML(text);
314 }
315
316 function escapeQuotesHTML(text) {
317 var re=new RegExp('&',"g");
318 text=text.replace(re,"&amp;");
319 var re=new RegExp('"',"g");
320 text=text.replace(re,"&quot;");
321 var re=new RegExp('<',"g");
322 text=text.replace(re,"&lt;");
323 var re=new RegExp('>',"g");
324 text=text.replace(re,"&gt;");
325 return text;
326 }
327
328 // apply tagOpen/tagClose to selection in textarea,
329 // use sampleText instead of selection if there is none
330 // copied and adapted from phpBB
331 function insertTags(tagOpen, tagClose, sampleText) {
332
333 var txtarea = document.editform.wpTextbox1;
334 // IE
335 if(document.selection && !is_gecko) {
336 var theSelection = document.selection.createRange().text;
337 if(!theSelection) { theSelection=sampleText;}
338 txtarea.focus();
339 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
340 theSelection = theSelection.substring(0, theSelection.length - 1);
341 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
342 } else {
343 document.selection.createRange().text = tagOpen + theSelection + tagClose;
344 }
345
346 // Mozilla
347 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
348 var startPos = txtarea.selectionStart;
349 var endPos = txtarea.selectionEnd;
350 var scrollTop=txtarea.scrollTop;
351 var myText = (txtarea.value).substring(startPos, endPos);
352 if(!myText) { myText=sampleText;}
353 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
354 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
355 } else {
356 subst = tagOpen + myText + tagClose;
357 }
358 txtarea.value = txtarea.value.substring(0, startPos) + subst +
359 txtarea.value.substring(endPos, txtarea.value.length);
360 txtarea.focus();
361
362 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
363 txtarea.selectionStart=cPos;
364 txtarea.selectionEnd=cPos;
365 txtarea.scrollTop=scrollTop;
366
367 // All others
368 } else {
369 var copy_alertText=alertText;
370 var re1=new RegExp("\\$1","g");
371 var re2=new RegExp("\\$2","g");
372 copy_alertText=copy_alertText.replace(re1,sampleText);
373 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
374 var text;
375 if (sampleText) {
376 text=prompt(copy_alertText);
377 } else {
378 text="";
379 }
380 if(!text) { text=sampleText;}
381 text=tagOpen+text+tagClose;
382 document.infoform.infobox.value=text;
383 // in Safari this causes scrolling
384 if(!is_safari) {
385 txtarea.focus();
386 }
387 noOverwrite=true;
388 }
389 // reposition cursor if possible
390 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
391 }
392
393 function akeytt() {
394 if(typeof ta == "undefined" || !ta) return;
395 pref = 'alt-';
396 if(is_safari || navigator.userAgent.toLowerCase().indexOf( 'mac' ) + 1 ) pref = 'control-';
397 if(is_opera) pref = 'shift-esc-';
398 for(id in ta) {
399 n = document.getElementById(id);
400 if(n){
401 a = n.childNodes[0];
402 if(a){
403 if(ta[id][0].length > 0) {
404 a.accessKey = ta[id][0];
405 ak = ' ['+pref+ta[id][0]+']';
406 } else {
407 ak = '';
408 }
409 a.title = ta[id][1]+ak;
410 } else {
411 if(ta[id][0].length > 0) {
412 n.accessKey = ta[id][0];
413 ak = ' ['+pref+ta[id][0]+']';
414 } else {
415 ak = '';
416 }
417 n.title = ta[id][1]+ak;
418 }
419 }
420 }
421 }
422
423 function setupRightClickEdit() {
424 if( document.getElementsByTagName ) {
425 var divs = document.getElementsByTagName( 'div' );
426 for( var i = 0; i < divs.length; i++ ) {
427 var el = divs[i];
428 if( el.className == 'editsection' ) {
429 addRightClickEditHandler( el );
430 }
431 }
432 }
433 }
434
435 function addRightClickEditHandler( el ) {
436 for( var i = 0; i < el.childNodes.length; i++ ) {
437 var link = el.childNodes[i];
438 if( link.nodeType == 1 && link.nodeName.toLowerCase() == 'a' ) {
439 var editHref = link.getAttribute( 'href' );
440
441 // find the following a
442 var next = el.nextSibling;
443 while( next.nodeType != 1 )
444 next = next.nextSibling;
445
446 // find the following header
447 next = next.nextSibling;
448 while( next.nodeType != 1 )
449 next = next.nextSibling;
450
451 if( next && next.nodeType == 1 &&
452 next.nodeName.match( /^[Hh][1-6]$/ ) ) {
453 next.oncontextmenu = function() {
454 document.location = editHref;
455 return false;
456 }
457 }
458 }
459 }
460 }
461
462 function fillDestFilename() {
463 if (!document.getElementById) return;
464 var path = document.getElementById('wpUploadFile').value;
465 // Find trailing part
466 var slash = path.lastIndexOf( '/' );
467 var backslash = path.lastIndexOf( '\\' );
468 var fname;
469 if ( slash == -1 && backslash == -1 ) {
470 fname = path;
471 } else if ( slash > backslash ) {
472 fname = path.substring( slash+1, 10000 );
473 } else {
474 fname = path.substring( backslash+1, 10000 );
475 }
476
477 // Capitalise first letter and replace spaces by underscores
478 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace( / /g, '_' );
479
480 // Output result
481 var destFile = document.getElementById('wpDestFile');
482 if (destFile.value == '' || destFile.value == document.fname ) {
483 document.getElementById('wpDestFile').value = fname;
484 }
485 document.fname = fname;
486 }
487