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