3a0c496b6aac58464f3cf37d1a0ae1393b35b20a
[lhc/web/wiklou.git] / skins / common / wikibits.js
1 // MediaWiki 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 // For accesskeys
9 var is_ff2_win = (clientPC.indexOf('firefox/2')!=-1 || clientPC.indexOf('minefield/3')!=-1) && clientPC.indexOf('windows')!=-1;
10 var is_ff2_x11 = (clientPC.indexOf('firefox/2')!=-1 || clientPC.indexOf('minefield/3')!=-1) && clientPC.indexOf('x11')!=-1;
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 // Global external objects used by this script.
18 /*extern ta, stylepath, skin */
19
20 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
21 var doneOnloadHook;
22
23 if (!window.onloadFuncts) {
24 var onloadFuncts = [];
25 }
26
27 function addOnloadHook(hookFunct) {
28 // Allows add-on scripts to add onload functions
29 onloadFuncts[onloadFuncts.length] = hookFunct;
30 }
31
32 function hookEvent(hookName, hookFunct) {
33 if (window.addEventListener) {
34 window.addEventListener(hookName, hookFunct, false);
35 } else if (window.attachEvent) {
36 window.attachEvent("on" + hookName, hookFunct);
37 }
38 }
39
40 // document.write special stylesheet links
41 if (typeof stylepath != 'undefined' && typeof skin != 'undefined') {
42 if (is_opera_preseven) {
43 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera6Fixes.css">');
44 } else if (is_opera_seven) {
45 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera7Fixes.css">');
46 } else if (is_khtml) {
47 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/KHTMLFixes.css">');
48 }
49 }
50
51 if (wgBreakFrames) {
52 // Un-trap us from framesets
53 if (window.top != window) {
54 window.top.location = window.location;
55 }
56 }
57
58 // for enhanced RecentChanges
59 function toggleVisibility(_levelId, _otherId, _linkId) {
60 var thisLevel = document.getElementById(_levelId);
61 var otherLevel = document.getElementById(_otherId);
62 var linkLevel = document.getElementById(_linkId);
63 if (thisLevel.style.display == 'none') {
64 thisLevel.style.display = 'block';
65 otherLevel.style.display = 'none';
66 linkLevel.style.display = 'inline';
67 } else {
68 thisLevel.style.display = 'none';
69 otherLevel.style.display = 'inline';
70 linkLevel.style.display = 'none';
71 }
72 }
73
74 function historyRadios(parent) {
75 var inputs = parent.getElementsByTagName('input');
76 var radios = [];
77 for (var i = 0; i < inputs.length; i++) {
78 if (inputs[i].name == "diff" || inputs[i].name == "oldid") {
79 radios[radios.length] = inputs[i];
80 }
81 }
82 return radios;
83 }
84
85 // check selection and tweak visibility/class onclick
86 function diffcheck() {
87 var dli = false; // the li where the diff radio is checked
88 var oli = false; // the li where the oldid radio is checked
89 var hf = document.getElementById('pagehistory');
90 if (!hf) {
91 return true;
92 }
93 var lis = hf.getElementsByTagName('li');
94 for (var i=0;i<lis.length;i++) {
95 var inputs = historyRadios(lis[i]);
96 if (inputs[1] && inputs[0]) {
97 if (inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
98 if (inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) {
99 return false;
100 }
101 if (oli) { // it's the second checked radio
102 if (inputs[1].checked) {
103 oli.className = "selected";
104 return false;
105 }
106 } else if (inputs[0].checked) {
107 return false;
108 }
109 if (inputs[0].checked) {
110 dli = lis[i];
111 }
112 if (!oli) {
113 inputs[0].style.visibility = 'hidden';
114 }
115 if (dli) {
116 inputs[1].style.visibility = 'hidden';
117 }
118 lis[i].className = "selected";
119 oli = lis[i];
120 } else { // no radio is checked in this row
121 if (!oli) {
122 inputs[0].style.visibility = 'hidden';
123 } else {
124 inputs[0].style.visibility = 'visible';
125 }
126 if (dli) {
127 inputs[1].style.visibility = 'hidden';
128 } else {
129 inputs[1].style.visibility = 'visible';
130 }
131 lis[i].className = "";
132 }
133 }
134 }
135 return true;
136 }
137
138 // page history stuff
139 // attach event handlers to the input elements on history page
140 function histrowinit() {
141 var hf = document.getElementById('pagehistory');
142 if (!hf) {
143 return;
144 }
145 var lis = hf.getElementsByTagName('li');
146 for (var i = 0; i < lis.length; i++) {
147 var inputs = historyRadios(lis[i]);
148 if (inputs[0] && inputs[1]) {
149 inputs[0].onclick = diffcheck;
150 inputs[1].onclick = diffcheck;
151 }
152 }
153 diffcheck();
154 }
155
156 // generate toc from prefs form, fold sections
157 // XXX: needs testing on IE/Mac and safari
158 // more comments to follow
159 function tabbedprefs() {
160 var prefform = document.getElementById('preferences');
161 if (!prefform || !document.createElement) {
162 return;
163 }
164 if (prefform.nodeName.toLowerCase() == 'a') {
165 return; // Occasional IE problem
166 }
167 prefform.className = prefform.className + 'jsprefs';
168 var sections = [];
169 var children = prefform.childNodes;
170 var seci = 0;
171 for (var i = 0; i < children.length; i++) {
172 if (children[i].nodeName.toLowerCase() == 'fieldset') {
173 children[i].id = 'prefsection-' + seci;
174 children[i].className = 'prefsection';
175 if (is_opera || is_khtml) {
176 children[i].className = 'prefsection operaprefsection';
177 }
178 var legends = children[i].getElementsByTagName('legend');
179 sections[seci] = {};
180 legends[0].className = 'mainLegend';
181 if (legends[0] && legends[0].firstChild.nodeValue) {
182 sections[seci].text = legends[0].firstChild.nodeValue;
183 } else {
184 sections[seci].text = '# ' + seci;
185 }
186 sections[seci].secid = children[i].id;
187 seci++;
188 if (sections.length != 1) {
189 children[i].style.display = 'none';
190 } else {
191 var selectedid = children[i].id;
192 }
193 }
194 }
195 var toc = document.createElement('ul');
196 toc.id = 'preftoc';
197 toc.selectedid = selectedid;
198 for (i = 0; i < sections.length; i++) {
199 var li = document.createElement('li');
200 if (i === 0) {
201 li.className = 'selected';
202 }
203 var a = document.createElement('a');
204 a.href = '#' + sections[i].secid;
205 a.onmousedown = a.onclick = uncoversection;
206 a.appendChild(document.createTextNode(sections[i].text));
207 a.secid = sections[i].secid;
208 li.appendChild(a);
209 toc.appendChild(li);
210 }
211 prefform.parentNode.insertBefore(toc, prefform.parentNode.childNodes[0]);
212 document.getElementById('prefsubmit').id = 'prefcontrol';
213 }
214
215 function uncoversection() {
216 var oldsecid = this.parentNode.parentNode.selectedid;
217 var newsec = document.getElementById(this.secid);
218 if (oldsecid != this.secid) {
219 var ul = document.getElementById('preftoc');
220 document.getElementById(oldsecid).style.display = 'none';
221 newsec.style.display = 'block';
222 ul.selectedid = this.secid;
223 var lis = ul.getElementsByTagName('li');
224 for (var i = 0; i< lis.length; i++) {
225 lis[i].className = '';
226 }
227 this.parentNode.className = 'selected';
228 }
229 return false;
230 }
231
232 // Timezone stuff
233 // tz in format [+-]HHMM
234 function checkTimezone(tz, msg) {
235 var localclock = new Date();
236 // returns negative offset from GMT in minutes
237 var tzRaw = localclock.getTimezoneOffset();
238 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
239 var tzMin = Math.abs(tzRaw) % 60;
240 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
241 if (tz != tzString) {
242 var junk = msg.split('$1');
243 document.write(junk[0] + "UTC" + tzString + junk[1]);
244 }
245 }
246
247 function unhidetzbutton() {
248 var tzb = document.getElementById('guesstimezonebutton');
249 if (tzb) {
250 tzb.style.display = 'inline';
251 }
252 }
253
254 // in [-]HH:MM format...
255 // won't yet work with non-even tzs
256 function fetchTimezone() {
257 // FIXME: work around Safari bug
258 var localclock = new Date();
259 // returns negative offset from GMT in minutes
260 var tzRaw = localclock.getTimezoneOffset();
261 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
262 var tzMin = Math.abs(tzRaw) % 60;
263 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
264 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
265 return tzString;
266 }
267
268 function guessTimezone(box) {
269 document.getElementsByName("wpHourDiff")[0].value = fetchTimezone();
270 }
271
272 function showTocToggle() {
273 if (document.createTextNode) {
274 // Uses DOM calls to avoid document.write + XHTML issues
275
276 var linkHolder = document.getElementById('toctitle');
277 if (!linkHolder) {
278 return;
279 }
280
281 var outerSpan = document.createElement('span');
282 outerSpan.className = 'toctoggle';
283
284 var toggleLink = document.createElement('a');
285 toggleLink.id = 'togglelink';
286 toggleLink.className = 'internal';
287 toggleLink.href = 'javascript:toggleToc()';
288 toggleLink.appendChild(document.createTextNode(tocHideText));
289
290 outerSpan.appendChild(document.createTextNode('['));
291 outerSpan.appendChild(toggleLink);
292 outerSpan.appendChild(document.createTextNode(']'));
293
294 linkHolder.appendChild(document.createTextNode(' '));
295 linkHolder.appendChild(outerSpan);
296
297 var cookiePos = document.cookie.indexOf("hidetoc=");
298 if (cookiePos > -1 && document.cookie.charAt(cookiePos + 8) == 1) {
299 toggleToc();
300 }
301 }
302 }
303
304 function changeText(el, newText) {
305 // Safari work around
306 if (el.innerText) {
307 el.innerText = newText;
308 } else if (el.firstChild && el.firstChild.nodeValue) {
309 el.firstChild.nodeValue = newText;
310 }
311 }
312
313 function toggleToc() {
314 var toc = document.getElementById('toc').getElementsByTagName('ul')[0];
315 var toggleLink = document.getElementById('togglelink');
316
317 if (toc && toggleLink && toc.style.display == 'none') {
318 changeText(toggleLink, tocHideText);
319 toc.style.display = 'block';
320 document.cookie = "hidetoc=0";
321 } else {
322 changeText(toggleLink, tocShowText);
323 toc.style.display = 'none';
324 document.cookie = "hidetoc=1";
325 }
326 }
327
328 var mwEditButtons = [];
329 var mwCustomEditButtons = []; // eg to add in MediaWiki:Common.js
330
331 // this function generates the actual toolbar buttons with localized text
332 // we use it to avoid creating the toolbar where javascript is not enabled
333 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
334 // Don't generate buttons for browsers which don't fully
335 // support it.
336 mwEditButtons[mwEditButtons.length] =
337 {"imageFile": imageFile,
338 "speedTip": speedTip,
339 "tagOpen": tagOpen,
340 "tagClose": tagClose,
341 "sampleText": sampleText};
342 }
343
344 // this function generates the actual toolbar buttons with localized text
345 // we use it to avoid creating the toolbar where javascript is not enabled
346 function mwInsertEditButton(parent, item) {
347 var image = document.createElement("img");
348 image.width = 23;
349 image.height = 22;
350 image.src = item.imageFile;
351 image.border = 0;
352 image.alt = item.speedTip;
353 image.title = item.speedTip;
354 image.style.cursor = "pointer";
355 image.onclick = function() {
356 insertTags(item.tagOpen, item.tagClose, item.sampleText);
357 return false;
358 };
359
360 parent.appendChild(image);
361 return true;
362 }
363
364 function mwSetupToolbar() {
365 var toolbar = document.getElementById('toolbar');
366 if (!toolbar) { return false; }
367
368 var textbox = document.getElementById('wpTextbox1');
369 if (!textbox) { return false; }
370
371 // Don't generate buttons for browsers which don't fully
372 // support it.
373 if (!document.selection && textbox.selectionStart === null) {
374 return false;
375 }
376
377 for (var i in mwEditButtons) {
378 mwInsertEditButton(toolbar, mwEditButtons[i]);
379 }
380 for (i in mwCustomEditButtons) {
381 mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
382 }
383 return true;
384 }
385
386 function escapeQuotes(text) {
387 var re = new RegExp("'","g");
388 text = text.replace(re,"\\'");
389 re = new RegExp("\\n","g");
390 text = text.replace(re,"\\n");
391 return escapeQuotesHTML(text);
392 }
393
394 function escapeQuotesHTML(text) {
395 var re = new RegExp('&',"g");
396 text = text.replace(re,"&amp;");
397 re = new RegExp('"',"g");
398 text = text.replace(re,"&quot;");
399 re = new RegExp('<',"g");
400 text = text.replace(re,"&lt;");
401 re = new RegExp('>',"g");
402 text = text.replace(re,"&gt;");
403 return text;
404 }
405
406 // apply tagOpen/tagClose to selection in textarea,
407 // use sampleText instead of selection if there is none
408 // copied and adapted from phpBB
409 function insertTags(tagOpen, tagClose, sampleText) {
410 var txtarea;
411 if (document.editform) {
412 txtarea = document.editform.wpTextbox1;
413 } else {
414 // some alternate form? take the first one we can find
415 var areas = document.getElementsByTagName('textarea');
416 txtarea = areas[0];
417 }
418
419 // IE
420 if (document.selection && !is_gecko) {
421 var theSelection = document.selection.createRange().text;
422 if (!theSelection) {
423 theSelection=sampleText;
424 }
425 txtarea.focus();
426 if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
427 theSelection = theSelection.substring(0, theSelection.length - 1);
428 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
429 } else {
430 document.selection.createRange().text = tagOpen + theSelection + tagClose;
431 }
432
433 // Mozilla
434 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
435 var replaced = false;
436 var startPos = txtarea.selectionStart;
437 var endPos = txtarea.selectionEnd;
438 if (endPos-startPos) {
439 replaced = true;
440 }
441 var scrollTop = txtarea.scrollTop;
442 var myText = (txtarea.value).substring(startPos, endPos);
443 if (!myText) {
444 myText=sampleText;
445 }
446 var subst;
447 if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
448 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
449 } else {
450 subst = tagOpen + myText + tagClose;
451 }
452 txtarea.value = txtarea.value.substring(0, startPos) + subst +
453 txtarea.value.substring(endPos, txtarea.value.length);
454 txtarea.focus();
455 //set new selection
456 if (replaced) {
457 var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
458 txtarea.selectionStart = cPos;
459 txtarea.selectionEnd = cPos;
460 } else {
461 txtarea.selectionStart = startPos+tagOpen.length;
462 txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
463 }
464 txtarea.scrollTop = scrollTop;
465
466 // All other browsers get no toolbar.
467 // There was previously support for a crippled "help"
468 // bar, but that caused more problems than it solved.
469 }
470 // reposition cursor if possible
471 if (txtarea.createTextRange) {
472 txtarea.caretPos = document.selection.createRange().duplicate();
473 }
474 }
475
476
477 /**
478 * Set the accesskey prefix based on browser detection.
479 */
480 var tooltipAccessKeyPrefix = 'alt-';
481 if (is_opera) {
482 tooltipAccessKeyPrefix = 'shift-esc-';
483 } else if (is_safari
484 || navigator.userAgent.toLowerCase().indexOf('mac') != -1
485 || navigator.userAgent.toLowerCase().indexOf('konqueror') != -1 ) {
486 tooltipAccessKeyPrefix = 'ctrl-';
487 } else if (is_ff2_x11) {
488 tooltipAccessKeyPrefix = 'ctrl-shift-';
489 } else if (is_ff2_win) {
490 tooltipAccessKeyPrefix = 'alt-shift-';
491 }
492 var tooltipAccessKeyRegexp = /\[(ctrl-)?(alt-)?(shift-)?(esc-)?.\]$/;
493
494 /**
495 * Add the appropriate prefix to the accesskey shown in the tooltip.
496 * If the nodeList parameter is given, only those nodes are updated.
497 *
498 * @param Array nodeList -- list of elements to update
499 */
500 function updateTooltipAccessKeys( nodeList ) {
501 if ( !nodeList )
502 nodeList = document.getElementsByTagName("*");
503
504 for ( var i = 0; i < nodeList.length; i++ ) {
505 var element = nodeList[i];
506 var tip = element.getAttribute("title");
507 var key = element.getAttribute("accesskey");
508 if ( key && tooltipAccessKeyRegexp.exec(tip) ) {
509 tip = tip.replace(tooltipAccessKeyRegexp,
510 "["+tooltipAccessKeyPrefix+key+"]");
511 element.setAttribute("title", tip );
512 }
513 }
514 }
515
516 /**
517 * Add a link to one of the portlet menus on the page, including:
518 *
519 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
520 * p-personal: Personal tools (shown at the top right of the page in Monobook)
521 * p-navigation: Navigation
522 * p-tb: Toolbox
523 *
524 * This function exists for the convenience of custom JS authors. All
525 * but the first three parameters are optional, though providing at
526 * least an id and a tooltip is recommended.
527 *
528 * By default the new link will be added to the end of the list. To
529 * add the link before a given existing item, pass the DOM node of
530 * that item (easily obtained with document.getElementById()) as the
531 * nextnode parameter; to add the link _after_ an existing item, pass
532 * the node's nextSibling instead.
533 *
534 * @param String portlet -- id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
535 * @param String href -- link URL
536 * @param String text -- link text (will be automatically lowercased by CSS for p-cactions in Monobook)
537 * @param String id -- id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
538 * @param String tooltip -- text to show when hovering over the link, without accesskey suffix
539 * @param String accesskey -- accesskey to activate this link (one character, try to avoid conflicts)
540 * @param Node nextnode -- the DOM node before which the new item should be added, should be another item in the same list
541 *
542 * @return Node -- the DOM node of the new item (an LI element) or null
543 */
544 function addPortletLink(portlet, href, text, id, tooltip, accesskey, nextnode) {
545 var node = document.getElementById(portlet);
546 if ( !node ) return null;
547 node = node.getElementsByTagName( "ul" )[0];
548 if ( !node ) return null;
549
550 var link = document.createElement( "a" );
551 link.appendChild( document.createTextNode( text ) );
552 link.href = href;
553
554 var item = document.createElement( "li" );
555 item.appendChild( link );
556 if ( id ) item.id = id;
557
558 if ( accesskey ) {
559 link.setAttribute( "accesskey", accesskey );
560 tooltip += " ["+accesskey+"]";
561 }
562 if ( tooltip ) {
563 link.setAttribute( "title", tooltip );
564 }
565 updateTooltipAccessKeys( new Array( link ) );
566
567 if ( nextnode && nextnode.parentNode != node ) nextnode = null;
568 node.insertBefore( item, nextnode );
569
570 return item;
571 }
572
573
574 /**
575 * Set up accesskeys/tooltips from the deprecated ta array. If doId
576 * is specified, only set up for that id. Note that this function is
577 * deprecated and will not be supported indefinitely -- use
578 * updateTooltipAccessKey() instead.
579 *
580 * @param mixed doId string or null
581 */
582 function akeytt( doId ) {
583 // A lot of user scripts (and some of the code below) break if
584 // ta isn't defined, so we make sure it is. Explictly using
585 // window.ta avoids a "ta is not defined" error.
586 if (!window.ta) window.ta = new Array;
587
588 // Make a local, possibly restricted, copy to avoid clobbering
589 // the original.
590 var ta;
591 if ( doId ) {
592 ta = new Array;
593 ta[doId] = window.ta[doId];
594 } else {
595 ta = window.ta;
596 }
597
598 // Now deal with evil deprecated ta
599 watchCheckboxExists = document.getElementById( 'wpWatchthis' ) ? true : false;
600 for (var id in ta) {
601 var n = document.getElementById(id);
602 if (n) {
603 var a = null;
604 var ak = '';
605 // Are we putting accesskey in it
606 if (ta[id][0].length > 0) {
607 // Is this object a object? If not assume it's the next child.
608
609 if (n.nodeName.toLowerCase() == "a") {
610 a = n;
611 } else {
612 a = n.childNodes[0];
613 }
614 // Don't add an accesskey for the watch tab if the watch
615 // checkbox is also available.
616 if (a && ((id != 'ca-watch' && id != 'ca-unwatch') || !watchCheckboxExists)) {
617 a.accessKey = ta[id][0];
618 ak = ' ['+tooltipAccessKeyPrefix+ta[id][0]+']';
619 }
620 } else {
621 // We don't care what type the object is when assigning tooltip
622 a = n;
623 ak = '';
624 }
625
626 if (a) {
627 a.title = ta[id][1]+ak;
628 }
629 }
630 }
631 }
632
633 function setupRightClickEdit() {
634 if (document.getElementsByTagName) {
635 var spans = document.getElementsByTagName('span');
636 for (var i = 0; i < spans.length; i++) {
637 var el = spans[i];
638 if(el.className == 'editsection') {
639 addRightClickEditHandler(el);
640 }
641 }
642 }
643 }
644
645 function addRightClickEditHandler(el) {
646 for (var i = 0; i < el.childNodes.length; i++) {
647 var link = el.childNodes[i];
648 if (link.nodeType == 1 && link.nodeName.toLowerCase() == 'a') {
649 var editHref = link.getAttribute('href');
650 // find the enclosing (parent) header
651 var prev = el.parentNode;
652 if (prev && prev.nodeType == 1 &&
653 prev.nodeName.match(/^[Hh][1-6]$/)) {
654 prev.oncontextmenu = function(e) {
655 if (!e) { e = window.event; }
656 // e is now the event in all browsers
657 var targ;
658 if (e.target) { targ = e.target; }
659 else if (e.srcElement) { targ = e.srcElement; }
660 if (targ.nodeType == 3) { // defeat Safari bug
661 targ = targ.parentNode;
662 }
663 // targ is now the target element
664
665 // We don't want to deprive the noble reader of a context menu
666 // for the section edit link, do we? (Might want to extend this
667 // to all <a>'s?)
668 if (targ.nodeName.toLowerCase() != 'a'
669 || targ.parentNode.className != 'editsection') {
670 document.location = editHref;
671 return false;
672 }
673 return true;
674 };
675 }
676 }
677 }
678 }
679
680 function setupCheckboxShiftClick() {
681 if (document.getElementsByTagName) {
682 var uls = document.getElementsByTagName('ul');
683 var len = uls.length;
684 for (var i = 0; i < len; ++i) {
685 addCheckboxClickHandlers(uls[i]);
686 }
687 }
688 }
689
690 function addCheckboxClickHandlers(ul, start, finish) {
691 if (ul.checkboxHandlersTimer) {
692 clearInterval(ul.checkboxHandlersTimer);
693 }
694 if ( !ul.childNodes ) {
695 return;
696 }
697 var len = ul.childNodes.length;
698 if (len < 2) {
699 return;
700 }
701 start = start || 0;
702 finish = finish || start + 250;
703 if ( finish > len ) { finish = len; }
704 ul.checkboxes = ul.checkboxes || [];
705 ul.lastCheckbox = ul.lastCheckbox || null;
706 for (var i = start; i<finish; ++i) {
707 var child = ul.childNodes[i];
708 if ( child && child.childNodes && child.childNodes[0] ) {
709 var cb = child.childNodes[0];
710 if ( !cb.nodeName || cb.nodeName.toLowerCase() != 'input' ||
711 !cb.type || cb.type.toLowerCase() != 'checkbox' ) {
712 return;
713 }
714 cb.index = ul.checkboxes.push(cb) - 1;
715 cb.container = ul;
716 cb.onmouseup = checkboxMouseupHandler;
717 }
718 }
719 if (finish < len) {
720 var f=function(){ addCheckboxClickHandlers(ul, finish, finish+250); };
721 ul.checkboxHandlersTimer=setInterval(f, 200);
722 }
723 }
724
725 function checkboxMouseupHandler(e) {
726 if (typeof e == 'undefined') {
727 e = window.event;
728 }
729 if ( !e.shiftKey || this.container.lastCheckbox === null ) {
730 this.container.lastCheckbox = this.index;
731 return true;
732 }
733 var endState = !this.checked;
734 if ( is_opera ) { // opera has already toggled the checkbox by this point
735 endState = !endState;
736 }
737 var start, finish;
738 if ( this.index < this.container.lastCheckbox ) {
739 start = this.index + 1;
740 finish = this.container.lastCheckbox;
741 } else {
742 start = this.container.lastCheckbox;
743 finish = this.index - 1;
744 }
745 for (var i = start; i <= finish; ++i ) {
746 this.container.checkboxes[i].checked = endState;
747 }
748 this.container.lastCheckbox = this.index;
749 return true;
750 }
751
752 function toggle_element_activation(ida,idb) {
753 if (!document.getElementById) {
754 return;
755 }
756 document.getElementById(ida).disabled=true;
757 document.getElementById(idb).disabled=false;
758 }
759
760 function toggle_element_check(ida,idb) {
761 if (!document.getElementById) {
762 return;
763 }
764 document.getElementById(ida).checked=true;
765 document.getElementById(idb).checked=false;
766 }
767
768 function fillDestFilename(id) {
769 if (!document.getElementById) {
770 return;
771 }
772 var path = document.getElementById(id).value;
773 // Find trailing part
774 var slash = path.lastIndexOf('/');
775 var backslash = path.lastIndexOf('\\');
776 var fname;
777 if (slash == -1 && backslash == -1) {
778 fname = path;
779 } else if (slash > backslash) {
780 fname = path.substring(slash+1, 10000);
781 } else {
782 fname = path.substring(backslash+1, 10000);
783 }
784
785 // Capitalise first letter and replace spaces by underscores
786 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
787
788 // Output result
789 var destFile = document.getElementById('wpDestFile');
790 if (destFile) {
791 destFile.value = fname;
792 }
793 }
794
795
796 function considerChangingExpiryFocus() {
797 if (!document.getElementById) {
798 return;
799 }
800 var drop = document.getElementById('wpBlockExpiry');
801 if (!drop) {
802 return;
803 }
804 var field = document.getElementById('wpBlockOther');
805 if (!field) {
806 return;
807 }
808 var opt = drop.value;
809 if (opt == 'other') {
810 field.style.display = '';
811 } else {
812 field.style.display = 'none';
813 }
814 }
815
816 function scrollEditBox() {
817 var editBoxEl = document.getElementById("wpTextbox1");
818 var scrollTopEl = document.getElementById("wpScrolltop");
819 var editFormEl = document.getElementById("editform");
820
821 if (editBoxEl && scrollTopEl) {
822 if (scrollTopEl.value) { editBoxEl.scrollTop = scrollTopEl.value; }
823 editFormEl.onsubmit = function() {
824 document.getElementById("wpScrolltop").value = document.getElementById("wpTextbox1").scrollTop;
825 };
826 }
827 }
828
829 hookEvent("load", scrollEditBox);
830
831 var allmessages_nodelist = false;
832 var allmessages_modified = false;
833 var allmessages_timeout = false;
834 var allmessages_running = false;
835
836 function allmessagesmodified() {
837 allmessages_modified = !allmessages_modified;
838 allmessagesfilter();
839 }
840
841 function allmessagesfilter() {
842 if ( allmessages_timeout )
843 window.clearTimeout( allmessages_timeout );
844
845 if ( !allmessages_running )
846 allmessages_timeout = window.setTimeout( 'allmessagesfilter_do();', 500 );
847 }
848
849 function allmessagesfilter_do() {
850 if ( !allmessages_nodelist )
851 return;
852
853 var text = document.getElementById('allmessagesinput').value;
854 var nodef = allmessages_modified;
855
856 allmessages_running = true;
857
858 for ( var name in allmessages_nodelist ) {
859 var nodes = allmessages_nodelist[name];
860 var display = ( name.indexOf( text ) == -1 ? 'none' : '' );
861
862 for ( var i = 0; i < nodes.length; i++)
863 nodes[i].style.display =
864 ( nodes[i].className == "def" && nodef
865 ? 'none' : display );
866 }
867
868 if ( text != document.getElementById('allmessagesinput').value ||
869 nodef != allmessages_modified )
870 allmessagesfilter_do(); // repeat
871
872 allmessages_running = false;
873 }
874
875 function allmessagesfilter_init() {
876 if ( allmessages_nodelist )
877 return;
878
879 var nodelist = new Array();
880 var templist = new Array();
881
882 var table = document.getElementById('allmessagestable');
883 if ( !table ) return;
884
885 var rows = document.getElementsByTagName('tr');
886 for ( var i = 0; i < rows.length; i++ ) {
887 var id = rows[i].getAttribute('id')
888 if ( id && id.substring(0,16) != 'sp-allmessages-r' ) continue;
889 templist[ id ] = rows[i];
890 }
891
892 var spans = table.getElementsByTagName('span');
893 for ( var i = 0; i < spans.length; i++ ) {
894 var id = spans[i].getAttribute('id')
895 if ( id && id.substring(0,17) != 'sp-allmessages-i-' ) continue;
896 if ( !spans[i].firstChild || spans[i].firstChild.nodeType != 3 ) continue;
897
898 var nodes = new Array();
899 var row1 = templist[ id.replace('i', 'r1') ];
900 var row2 = templist[ id.replace('i', 'r2') ];
901
902 if ( row1 ) nodes[nodes.length] = row1;
903 if ( row2 ) nodes[nodes.length] = row2;
904 nodelist[ spans[i].firstChild.nodeValue ] = nodes;
905 }
906
907 var k = document.getElementById('allmessagesfilter');
908 if (k) { k.style.display = ''; }
909
910 allmessages_nodelist = nodelist;
911 }
912
913 hookEvent( "load", allmessagesfilter_init );
914
915 /*
916 Written by Jonathan Snook, http://www.snook.ca/jonathan
917 Add-ons by Robert Nyman, http://www.robertnyman.com
918 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
919 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
920 */
921 function getElementsByClassName(oElm, strTagName, oClassNames){
922 var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
923 var arrReturnElements = new Array();
924 var arrRegExpClassNames = new Array();
925 if(typeof oClassNames == "object"){
926 for(var i=0; i<oClassNames.length; i++){
927 arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
928 }
929 }
930 else{
931 arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
932 }
933 var oElement;
934 var bMatchesAll;
935 for(var j=0; j<arrElements.length; j++){
936 oElement = arrElements[j];
937 bMatchesAll = true;
938 for(var k=0; k<arrRegExpClassNames.length; k++){
939 if(!arrRegExpClassNames[k].test(oElement.className)){
940 bMatchesAll = false;
941 break;
942 }
943 }
944 if(bMatchesAll){
945 arrReturnElements.push(oElement);
946 }
947 }
948 return (arrReturnElements)
949 }
950
951 function sortableTables() {
952 if (getElementsByClassName(document, "table", "sortable").length != 0) {
953 document.write('<script type="text/javascript" src="'+stylepath+'/common/sorttable.js"></script>');
954 }
955 }
956
957 function redirectToFragment(fragment) {
958 var match = navigator.userAgent.match(/AppleWebKit\/(\d+)/);
959 if (match) {
960 var webKitVersion = parseInt(match[1]);
961 if (webKitVersion < 420) {
962 // Released Safari w/ WebKit 418.9.1 messes up horribly
963 // Nightlies of 420+ are ok
964 return;
965 }
966 }
967 if (is_gecko) {
968 // Mozilla needs to wait until after load, otherwise the window doesn't scroll
969 addOnloadHook(function () {
970 if (window.location.hash == "")
971 window.location.hash = fragment;
972 });
973 } else {
974 if (window.location.hash == "")
975 window.location.hash = fragment;
976 }
977 }
978
979 function runOnloadHook() {
980 // don't run anything below this for non-dom browsers
981 if (doneOnloadHook || !(document.getElementById && document.getElementsByTagName)) {
982 return;
983 }
984
985 // set this before running any hooks, since any errors below
986 // might cause the function to terminate prematurely
987 doneOnloadHook = true;
988
989 histrowinit();
990 unhidetzbutton();
991 tabbedprefs();
992 updateTooltipAccessKeys( null );
993 akeytt( null );
994 scrollEditBox();
995 setupCheckboxShiftClick();
996 sortableTables();
997
998 // Run any added-on functions
999 for (var i = 0; i < onloadFuncts.length; i++) {
1000 onloadFuncts[i]();
1001 }
1002 }
1003
1004 //note: all skins should call runOnloadHook() at the end of html output,
1005 // so the below should be redundant. It's there just in case.
1006 hookEvent("load", runOnloadHook);
1007
1008 hookEvent("load", mwSetupToolbar);