* Experimental autofocus on searchbox support, see bug 1864
[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 searchfocus();
25 }
26 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
27 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
28
29
30 // document.write special stylesheet links
31 if(typeof stylepath != 'undefined' && typeof skin != 'undefined') {
32 if (is_opera_preseven) {
33 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera6Fixes.css">');
34 } else if (is_opera_seven) {
35 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera7Fixes.css">');
36 } else if (is_khtml) {
37 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/KHTMLFixes.css">');
38 }
39 }
40 // Un-trap us from framesets
41 if( window.top != window ) window.top.location = window.location;
42
43 // for enhanced RecentChanges
44 function toggleVisibility( _levelId, _otherId, _linkId) {
45 var thisLevel = document.getElementById( _levelId );
46 var otherLevel = document.getElementById( _otherId );
47 var linkLevel = document.getElementById( _linkId );
48 if ( thisLevel.style.display == 'none' ) {
49 thisLevel.style.display = 'block';
50 otherLevel.style.display = 'none';
51 linkLevel.style.display = 'inline';
52 } else {
53 thisLevel.style.display = 'none';
54 otherLevel.style.display = 'inline';
55 linkLevel.style.display = 'none';
56 }
57 }
58
59 // page history stuff
60 // attach event handlers to the input elements on history page
61 function histrowinit () {
62 hf = document.getElementById('pagehistory');
63 if(!hf) return;
64 lis = hf.getElementsByTagName('li');
65 for (i=0;i<lis.length;i++) {
66 inputs=lis[i].getElementsByTagName('input');
67 if(inputs[0] && inputs[1]) {
68 inputs[0].onclick = diffcheck;
69 inputs[1].onclick = diffcheck;
70 }
71 }
72 diffcheck();
73 }
74 // check selection and tweak visibility/class onclick
75 function diffcheck() {
76 var dli = false; // the li where the diff radio is checked
77 var oli = false; // the li where the oldid radio is checked
78 hf = document.getElementById('pagehistory');
79 if(!hf) return;
80 lis = hf.getElementsByTagName('li');
81 for (i=0;i<lis.length;i++) {
82 inputs=lis[i].getElementsByTagName('input');
83 if(inputs[1] && inputs[0]) {
84 if(inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
85 if(inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) return false;
86 if(oli) { // it's the second checked radio
87 if(inputs[1].checked) {
88 oli.className = "selected";
89 return false
90 }
91 } else if (inputs[0].checked) {
92 return false;
93 }
94 if(inputs[0].checked) dli = lis[i];
95 if(!oli) inputs[0].style.visibility = 'hidden';
96 if(dli) inputs[1].style.visibility = 'hidden';
97 lis[i].className = "selected";
98 oli = lis[i];
99 } else { // no radio is checked in this row
100 if(!oli) inputs[0].style.visibility = 'hidden';
101 else inputs[0].style.visibility = 'visible';
102 if(dli) inputs[1].style.visibility = 'hidden';
103 else inputs[1].style.visibility = 'visible';
104 lis[i].className = "";
105 }
106 }
107 }
108 }
109
110 // generate toc from prefs form, fold sections
111 // XXX: needs testing on IE/Mac and safari
112 // more comments to follow
113 function tabbedprefs() {
114 prefform = document.getElementById('preferences');
115 if(!prefform || !document.createElement) return;
116 if(prefform.nodeName.toLowerCase() == 'a') return; // Occasional IE problem
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.toLowerCase().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.appendChild(document.createTextNode(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() {
210 if (document.createTextNode) {
211 // Uses DOM calls to avoid document.write + XHTML issues
212
213 var linkHolder = document.getElementById('toctitle')
214 if (!linkHolder) return;
215
216 var outerSpan = document.createElement('span');
217 outerSpan.className = 'toctoggle';
218
219 var toggleLink = document.createElement('a');
220 toggleLink.id = 'togglelink';
221 toggleLink.className = 'internal';
222 toggleLink.href = 'javascript:toggleToc()';
223 toggleLink.appendChild(document.createTextNode(tocHideText));
224
225 outerSpan.appendChild(document.createTextNode('['));
226 outerSpan.appendChild(toggleLink);
227 outerSpan.appendChild(document.createTextNode(']'));
228
229 linkHolder.appendChild(document.createTextNode(' '));
230 linkHolder.appendChild(outerSpan);
231
232 var cookiePos = document.cookie.indexOf("hidetoc=");
233 if (cookiePos > -1 && document.cookie.charAt(cookiePos + 8) == 1)
234 toggleToc();
235 }
236 }
237
238 function changeText(el, newText) {
239 // Safari work around
240 if (el.innerText)
241 el.innerText = newText;
242 else if (el.firstChild && el.firstChild.nodeValue)
243 el.firstChild.nodeValue = newText;
244 }
245
246 function toggleToc() {
247 var toc = document.getElementById('toc').getElementsByTagName('ul')[0];
248 var toggleLink = document.getElementById('togglelink')
249
250 if(toc && toggleLink && toc.style.display == 'none') {
251 changeText(toggleLink, tocHideText);
252 toc.style.display = 'block';
253 document.cookie = "hidetoc=0";
254 } else {
255 changeText(toggleLink, tocShowText);
256 toc.style.display = 'none';
257 document.cookie = "hidetoc=1";
258 }
259 }
260
261 // this function generates the actual toolbar buttons with localized text
262 // we use it to avoid creating the toolbar where javascript is not enabled
263 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
264
265 speedTip=escapeQuotes(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('"',"g");
312 text=text.replace(re,'&quot;');
313 re=new RegExp("\\n","g");
314 text=text.replace(re,"\\n");
315 return text;
316 }
317
318 function escapeQuotesHTML(text) {
319 var re=new RegExp('"',"g");
320 text=text.replace(re,"&quot;");
321 return text;
322 }
323
324 // apply tagOpen/tagClose to selection in textarea,
325 // use sampleText instead of selection if there is none
326 // copied and adapted from phpBB
327 function insertTags(tagOpen, tagClose, sampleText) {
328
329 var txtarea = document.editform.wpTextbox1;
330 // IE
331 if(document.selection && !is_gecko) {
332 var theSelection = document.selection.createRange().text;
333 if(!theSelection) { theSelection=sampleText;}
334 txtarea.focus();
335 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
336 theSelection = theSelection.substring(0, theSelection.length - 1);
337 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
338 } else {
339 document.selection.createRange().text = tagOpen + theSelection + tagClose;
340 }
341
342 // Mozilla
343 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
344 var startPos = txtarea.selectionStart;
345 var endPos = txtarea.selectionEnd;
346 var scrollTop=txtarea.scrollTop;
347 var myText = (txtarea.value).substring(startPos, endPos);
348 if(!myText) { myText=sampleText;}
349 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
350 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
351 } else {
352 subst = tagOpen + myText + tagClose;
353 }
354 txtarea.value = txtarea.value.substring(0, startPos) + subst +
355 txtarea.value.substring(endPos, txtarea.value.length);
356 txtarea.focus();
357
358 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
359 txtarea.selectionStart=cPos;
360 txtarea.selectionEnd=cPos;
361 txtarea.scrollTop=scrollTop;
362
363 // All others
364 } else {
365 var copy_alertText=alertText;
366 var re1=new RegExp("\\$1","g");
367 var re2=new RegExp("\\$2","g");
368 copy_alertText=copy_alertText.replace(re1,sampleText);
369 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
370 var text;
371 if (sampleText) {
372 text=prompt(copy_alertText);
373 } else {
374 text="";
375 }
376 if(!text) { text=sampleText;}
377 text=tagOpen+text+tagClose;
378 document.infoform.infobox.value=text;
379 // in Safari this causes scrolling
380 if(!is_safari) {
381 txtarea.focus();
382 }
383 noOverwrite=true;
384 }
385 // reposition cursor if possible
386 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
387 }
388
389 function akeytt() {
390 if(typeof ta == "undefined" || !ta) return;
391 pref = 'alt-';
392 if(is_safari || navigator.userAgent.toLowerCase().indexOf( 'mac' ) + 1 ) pref = 'control-';
393 if(is_opera) pref = 'shift-esc-';
394 for(id in ta) {
395 n = document.getElementById(id);
396 if(n){
397 a = n.childNodes[0];
398 if(a){
399 if(ta[id][0].length > 0) {
400 a.accessKey = ta[id][0];
401 ak = ' ['+pref+ta[id][0]+']';
402 } else {
403 ak = '';
404 }
405 a.title = ta[id][1]+ak;
406 } else {
407 if(ta[id][0].length > 0) {
408 n.accessKey = ta[id][0];
409 ak = ' ['+pref+ta[id][0]+']';
410 } else {
411 ak = '';
412 }
413 n.title = ta[id][1]+ak;
414 }
415 }
416 }
417 }
418
419 function setupRightClickEdit() {
420 if( document.getElementsByTagName ) {
421 var divs = document.getElementsByTagName( 'div' );
422 for( var i = 0; i < divs.length; i++ ) {
423 var el = divs[i];
424 if( el.className == 'editsection' ) {
425 addRightClickEditHandler( el );
426 }
427 }
428 }
429 }
430
431 function addRightClickEditHandler( el ) {
432 for( var i = 0; i < el.childNodes.length; i++ ) {
433 var link = el.childNodes[i];
434 if( link.nodeType == 1 && link.nodeName.toLowerCase() == 'a' ) {
435 var editHref = link.getAttribute( 'href' );
436
437 // find the following a
438 var next = el.nextSibling;
439 while( next.nodeType != 1 )
440 next = next.nextSibling;
441
442 // find the following header
443 next = next.nextSibling;
444 while( next.nodeType != 1 )
445 next = next.nextSibling;
446
447 if( next && next.nodeType == 1 &&
448 next.nodeName.match( /^[Hh][1-6]$/ ) ) {
449 next.oncontextmenu = function() {
450 document.location = editHref;
451 return false;
452 }
453 }
454 }
455 }
456 }
457
458 function searchfocus() {
459 // @bug 1864, autofocus on the searchform for MonoBook
460 // The former search is <form name="searchform" and the latter is <input name="search"
461 document.searchform.search.focus();
462 }