3368c7335cd93f96121501a5cb8ee6c56b5acb6f
[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 = /gecko/.test( clientPC ) &&
5 !/khtml|spoofer|netscape\/7\.0/.test(clientPC);
6 var is_safari = clientPC.indexOf('applewebkit') != -1 &&
7 clientPC.indexOf('spoofer') == -1;
8 var is_khtml = navigator.vendor == 'KDE' ||
9 ( document.childNodes && !document.all && !navigator.taintEnabled );
10 // For accesskeys; note that FF3+ is included here!
11 var is_ff2 = /firefox\/[2-9]|minefield\/3/.test( clientPC );
12 // These aren't used here, but some custom scripts rely on them
13 var is_ff2_win = is_ff2 && clientPC.indexOf('windows') != -1;
14 var is_ff2_x11 = is_ff2 && clientPC.indexOf('x11') != -1;
15 if (clientPC.indexOf('opera') != -1) {
16 var is_opera = true;
17 var is_opera_preseven = window.opera && !document.childNodes;
18 var is_opera_seven = window.opera && document.childNodes;
19 var is_opera_95 = /opera\/(9.[5-9]|[1-9][0-9])/.test( clientPC );
20 }
21
22 // Global external objects used by this script.
23 /*extern ta, stylepath, skin */
24
25 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
26 var doneOnloadHook;
27
28 if (!window.onloadFuncts) {
29 var onloadFuncts = [];
30 }
31
32 function addOnloadHook(hookFunct) {
33 // Allows add-on scripts to add onload functions
34 onloadFuncts[onloadFuncts.length] = hookFunct;
35 }
36
37 function hookEvent(hookName, hookFunct) {
38 if (window.addEventListener) {
39 window.addEventListener(hookName, hookFunct, false);
40 } else if (window.attachEvent) {
41 window.attachEvent("on" + hookName, hookFunct);
42 }
43 }
44
45 // document.write special stylesheet links
46 if (typeof stylepath != 'undefined' && typeof skin != 'undefined') {
47 if (is_opera_preseven) {
48 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera6Fixes.css">');
49 } else if (is_opera_seven && !is_opera_95) {
50 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/Opera7Fixes.css">');
51 } else if (is_khtml) {
52 document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'/'+skin+'/KHTMLFixes.css">');
53 }
54 }
55
56 if (wgBreakFrames) {
57 // Un-trap us from framesets
58 if (window.top != window) {
59 window.top.location = window.location;
60 }
61 }
62
63 // for enhanced RecentChanges
64 function toggleVisibility(_levelId, _otherId, _linkId) {
65 var thisLevel = document.getElementById(_levelId);
66 var otherLevel = document.getElementById(_otherId);
67 var linkLevel = document.getElementById(_linkId);
68 if (thisLevel.style.display == 'none') {
69 thisLevel.style.display = 'block';
70 otherLevel.style.display = 'none';
71 linkLevel.style.display = 'inline';
72 } else {
73 thisLevel.style.display = 'none';
74 otherLevel.style.display = 'inline';
75 linkLevel.style.display = 'none';
76 }
77 }
78
79 function showTocToggle() {
80 if (document.createTextNode) {
81 // Uses DOM calls to avoid document.write + XHTML issues
82
83 var linkHolder = document.getElementById('toctitle');
84 if (!linkHolder) {
85 return;
86 }
87
88 var outerSpan = document.createElement('span');
89 outerSpan.className = 'toctoggle';
90
91 var toggleLink = document.createElement('a');
92 toggleLink.id = 'togglelink';
93 toggleLink.className = 'internal';
94 toggleLink.href = 'javascript:toggleToc()';
95 toggleLink.appendChild(document.createTextNode(tocHideText));
96
97 outerSpan.appendChild(document.createTextNode('['));
98 outerSpan.appendChild(toggleLink);
99 outerSpan.appendChild(document.createTextNode(']'));
100
101 linkHolder.appendChild(document.createTextNode(' '));
102 linkHolder.appendChild(outerSpan);
103
104 var cookiePos = document.cookie.indexOf("hidetoc=");
105 if (cookiePos > -1 && document.cookie.charAt(cookiePos + 8) == 1) {
106 toggleToc();
107 }
108 }
109 }
110
111 function changeText(el, newText) {
112 // Safari work around
113 if (el.innerText) {
114 el.innerText = newText;
115 } else if (el.firstChild && el.firstChild.nodeValue) {
116 el.firstChild.nodeValue = newText;
117 }
118 }
119
120 function toggleToc() {
121 var toc = document.getElementById('toc').getElementsByTagName('ul')[0];
122 var toggleLink = document.getElementById('togglelink');
123
124 if (toc && toggleLink && toc.style.display == 'none') {
125 changeText(toggleLink, tocHideText);
126 toc.style.display = 'block';
127 document.cookie = "hidetoc=0";
128 } else {
129 changeText(toggleLink, tocShowText);
130 toc.style.display = 'none';
131 document.cookie = "hidetoc=1";
132 }
133 }
134
135 var mwEditButtons = [];
136 var mwCustomEditButtons = []; // eg to add in MediaWiki:Common.js
137
138 function escapeQuotes(text) {
139 var re = new RegExp("'","g");
140 text = text.replace(re,"\\'");
141 re = new RegExp("\\n","g");
142 text = text.replace(re,"\\n");
143 return escapeQuotesHTML(text);
144 }
145
146 function escapeQuotesHTML(text) {
147 var re = new RegExp('&',"g");
148 text = text.replace(re,"&amp;");
149 re = new RegExp('"',"g");
150 text = text.replace(re,"&quot;");
151 re = new RegExp('<',"g");
152 text = text.replace(re,"&lt;");
153 re = new RegExp('>',"g");
154 text = text.replace(re,"&gt;");
155 return text;
156 }
157
158
159 /**
160 * Set the accesskey prefix based on browser detection.
161 */
162 var tooltipAccessKeyPrefix = 'alt-';
163 if (is_opera) {
164 tooltipAccessKeyPrefix = 'shift-esc-';
165 } else if (is_safari
166 || navigator.userAgent.toLowerCase().indexOf('mac') != -1
167 || navigator.userAgent.toLowerCase().indexOf('konqueror') != -1 ) {
168 tooltipAccessKeyPrefix = 'ctrl-';
169 } else if (is_ff2) {
170 tooltipAccessKeyPrefix = 'alt-shift-';
171 }
172 var tooltipAccessKeyRegexp = /\[(ctrl-)?(alt-)?(shift-)?(esc-)?.\]$/;
173
174 /**
175 * Add the appropriate prefix to the accesskey shown in the tooltip.
176 * If the nodeList parameter is given, only those nodes are updated;
177 * otherwise, all the nodes that will probably have accesskeys by
178 * default are updated.
179 *
180 * @param Array nodeList -- list of elements to update
181 */
182 function updateTooltipAccessKeys( nodeList ) {
183 if ( !nodeList ) {
184 // skins without a "column-one" element don't seem to have links with accesskeys either
185 var columnOne = document.getElementById("column-one");
186 if ( columnOne )
187 updateTooltipAccessKeys( columnOne.getElementsByTagName("a") );
188 // these are rare enough that no such optimization is needed
189 updateTooltipAccessKeys( document.getElementsByTagName("input") );
190 updateTooltipAccessKeys( document.getElementsByTagName("label") );
191 return;
192 }
193
194 for ( var i = 0; i < nodeList.length; i++ ) {
195 var element = nodeList[i];
196 var tip = element.getAttribute("title");
197 var key = element.getAttribute("accesskey");
198 if ( key && tooltipAccessKeyRegexp.exec(tip) ) {
199 tip = tip.replace(tooltipAccessKeyRegexp,
200 "["+tooltipAccessKeyPrefix+key+"]");
201 element.setAttribute("title", tip );
202 }
203 }
204 }
205
206 /**
207 * Add a link to one of the portlet menus on the page, including:
208 *
209 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
210 * p-personal: Personal tools (shown at the top right of the page in Monobook)
211 * p-navigation: Navigation
212 * p-tb: Toolbox
213 *
214 * This function exists for the convenience of custom JS authors. All
215 * but the first three parameters are optional, though providing at
216 * least an id and a tooltip is recommended.
217 *
218 * By default the new link will be added to the end of the list. To
219 * add the link before a given existing item, pass the DOM node of
220 * that item (easily obtained with document.getElementById()) as the
221 * nextnode parameter; to add the link _after_ an existing item, pass
222 * the node's nextSibling instead.
223 *
224 * @param String portlet -- id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
225 * @param String href -- link URL
226 * @param String text -- link text (will be automatically lowercased by CSS for p-cactions in Monobook)
227 * @param String id -- id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
228 * @param String tooltip -- text to show when hovering over the link, without accesskey suffix
229 * @param String accesskey -- accesskey to activate this link (one character, try to avoid conflicts)
230 * @param Node nextnode -- the DOM node before which the new item should be added, should be another item in the same list
231 *
232 * @return Node -- the DOM node of the new item (an LI element) or null
233 */
234 function addPortletLink(portlet, href, text, id, tooltip, accesskey, nextnode) {
235 var node = document.getElementById(portlet);
236 if ( !node ) return null;
237 node = node.getElementsByTagName( "ul" )[0];
238 if ( !node ) return null;
239
240 var link = document.createElement( "a" );
241 link.appendChild( document.createTextNode( text ) );
242 link.href = href;
243
244 var item = document.createElement( "li" );
245 item.appendChild( link );
246 if ( id ) item.id = id;
247
248 if ( accesskey ) {
249 link.setAttribute( "accesskey", accesskey );
250 tooltip += " ["+accesskey+"]";
251 }
252 if ( tooltip ) {
253 link.setAttribute( "title", tooltip );
254 }
255 if ( accesskey && tooltip ) {
256 updateTooltipAccessKeys( new Array( link ) );
257 }
258
259 if ( nextnode && nextnode.parentNode == node )
260 node.insertBefore( item, nextnode );
261 else
262 node.appendChild( item ); // IE compatibility (?)
263
264 return item;
265 }
266
267
268 /**
269 * Set up accesskeys/tooltips from the deprecated ta array. If doId
270 * is specified, only set up for that id. Note that this function is
271 * deprecated and will not be supported indefinitely -- use
272 * updateTooltipAccessKey() instead.
273 *
274 * @param mixed doId string or null
275 */
276 function akeytt( doId ) {
277 // A lot of user scripts (and some of the code below) break if
278 // ta isn't defined, so we make sure it is. Explictly using
279 // window.ta avoids a "ta is not defined" error.
280 if (!window.ta) window.ta = new Array;
281
282 // Make a local, possibly restricted, copy to avoid clobbering
283 // the original.
284 var ta;
285 if ( doId ) {
286 ta = [doId];
287 } else {
288 ta = window.ta;
289 }
290
291 // Now deal with evil deprecated ta
292 var watchCheckboxExists = document.getElementById( 'wpWatchthis' ) ? true : false;
293 for (var id in ta) {
294 var n = document.getElementById(id);
295 if (n) {
296 var a = null;
297 var ak = '';
298 // Are we putting accesskey in it
299 if (ta[id][0].length > 0) {
300 // Is this object a object? If not assume it's the next child.
301
302 if (n.nodeName.toLowerCase() == "a") {
303 a = n;
304 } else {
305 a = n.childNodes[0];
306 }
307 // Don't add an accesskey for the watch tab if the watch
308 // checkbox is also available.
309 if (a && ((id != 'ca-watch' && id != 'ca-unwatch') || !watchCheckboxExists)) {
310 a.accessKey = ta[id][0];
311 ak = ' ['+tooltipAccessKeyPrefix+ta[id][0]+']';
312 }
313 } else {
314 // We don't care what type the object is when assigning tooltip
315 a = n;
316 ak = '';
317 }
318
319 if (a) {
320 a.title = ta[id][1]+ak;
321 }
322 }
323 }
324 }
325
326 var checkboxes;
327 var lastCheckbox;
328
329 function setupCheckboxShiftClick() {
330 checkboxes = [];
331 lastCheckbox = null;
332 var inputs = document.getElementsByTagName('input');
333 addCheckboxClickHandlers(inputs);
334 }
335
336 function addCheckboxClickHandlers(inputs, start) {
337 if ( !start) start = 0;
338
339 var finish = start + 250;
340 if ( finish > inputs.length )
341 finish = inputs.length;
342
343 for ( var i = start; i < finish; i++ ) {
344 var cb = inputs[i];
345 if ( !cb.type || cb.type.toLowerCase() != 'checkbox' )
346 continue;
347 var end = checkboxes.length;
348 checkboxes[end] = cb;
349 cb.index = end;
350 cb.onclick = checkboxClickHandler;
351 }
352
353 if ( finish < inputs.length ) {
354 setTimeout( function () {
355 addCheckboxClickHandlers(inputs, finish);
356 }, 200 );
357 }
358 }
359
360 function checkboxClickHandler(e) {
361 if (typeof e == 'undefined') {
362 e = window.event;
363 }
364 if ( !e.shiftKey || lastCheckbox === null ) {
365 lastCheckbox = this.index;
366 return true;
367 }
368 var endState = this.checked;
369 var start, finish;
370 if ( this.index < lastCheckbox ) {
371 start = this.index + 1;
372 finish = lastCheckbox;
373 } else {
374 start = lastCheckbox;
375 finish = this.index - 1;
376 }
377 for (var i = start; i <= finish; ++i ) {
378 checkboxes[i].checked = endState;
379 }
380 lastCheckbox = this.index;
381 return true;
382 }
383
384 function toggle_element_activation(ida,idb) {
385 if (!document.getElementById) {
386 return;
387 }
388 document.getElementById(ida).disabled=true;
389 document.getElementById(idb).disabled=false;
390 }
391
392 function toggle_element_check(ida,idb) {
393 if (!document.getElementById) {
394 return;
395 }
396 document.getElementById(ida).checked=true;
397 document.getElementById(idb).checked=false;
398 }
399
400 /*
401 Written by Jonathan Snook, http://www.snook.ca/jonathan
402 Add-ons by Robert Nyman, http://www.robertnyman.com
403 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
404 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
405 */
406 function getElementsByClassName(oElm, strTagName, oClassNames){
407 var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
408 var arrReturnElements = new Array();
409 var arrRegExpClassNames = new Array();
410 if(typeof oClassNames == "object"){
411 for(var i=0; i<oClassNames.length; i++){
412 arrRegExpClassNames[arrRegExpClassNames.length] =
413 new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)");
414 }
415 }
416 else{
417 arrRegExpClassNames[arrRegExpClassNames.length] =
418 new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)");
419 }
420 var oElement;
421 var bMatchesAll;
422 for(var j=0; j<arrElements.length; j++){
423 oElement = arrElements[j];
424 bMatchesAll = true;
425 for(var k=0; k<arrRegExpClassNames.length; k++){
426 if(!arrRegExpClassNames[k].test(oElement.className)){
427 bMatchesAll = false;
428 break;
429 }
430 }
431 if(bMatchesAll){
432 arrReturnElements[arrReturnElements.length] = oElement;
433 }
434 }
435 return (arrReturnElements)
436 }
437
438 function redirectToFragment(fragment) {
439 var match = navigator.userAgent.match(/AppleWebKit\/(\d+)/);
440 if (match) {
441 var webKitVersion = parseInt(match[1]);
442 if (webKitVersion < 420) {
443 // Released Safari w/ WebKit 418.9.1 messes up horribly
444 // Nightlies of 420+ are ok
445 return;
446 }
447 }
448 if (is_gecko) {
449 // Mozilla needs to wait until after load, otherwise the window doesn't scroll
450 addOnloadHook(function () {
451 if (window.location.hash == "")
452 window.location.hash = fragment;
453 });
454 } else {
455 if (window.location.hash == "")
456 window.location.hash = fragment;
457 }
458 }
459
460 /*
461 * Table sorting script by Joost de Valk, check it out at http://www.joostdevalk.nl/code/sortable-table/.
462 * Based on a script from http://www.kryogenix.org/code/browser/sorttable/.
463 * Distributed under the MIT license: http://www.kryogenix.org/code/browser/licence.html .
464 *
465 * Copyright (c) 1997-2006 Stuart Langridge, Joost de Valk.
466 *
467 * @todo don't break on colspans/rowspans (bug 8028)
468 * @todo language-specific digit grouping/decimals (bug 8063)
469 * @todo support all accepted date formats (bug 8226)
470 */
471
472 var ts_image_path = stylepath+"/common/images/";
473 var ts_image_up = "sort_up.gif";
474 var ts_image_down = "sort_down.gif";
475 var ts_image_none = "sort_none.gif";
476 var ts_europeandate = wgContentLanguage != "en"; // The non-American-inclined can change to "true"
477 var ts_alternate_row_colors = true;
478 var SORT_COLUMN_INDEX;
479
480 function sortables_init() {
481 var idnum = 0;
482 // Find all tables with class sortable and make them sortable
483 var tables = getElementsByClassName(document, "table", "sortable");
484 for (var ti = 0; ti < tables.length ; ti++) {
485 if (!tables[ti].id) {
486 tables[ti].setAttribute('id','sortable_table_id_'+idnum);
487 ++idnum;
488 }
489 ts_makeSortable(tables[ti]);
490 }
491 }
492
493 function ts_makeSortable(table) {
494 var firstRow;
495 if (table.rows && table.rows.length > 0) {
496 if (table.tHead && table.tHead.rows.length > 0) {
497 firstRow = table.tHead.rows[table.tHead.rows.length-1];
498 } else {
499 firstRow = table.rows[0];
500 }
501 }
502 if (!firstRow) return;
503
504 // We have a first row: assume it's the header, and make its contents clickable links
505 for (var i = 0; i < firstRow.cells.length; i++) {
506 var cell = firstRow.cells[i];
507 if ((" "+cell.className+" ").indexOf(" unsortable ") == -1) {
508 cell.innerHTML += '&nbsp;&nbsp;<a href="#" class="sortheader" onclick="ts_resortTable(this);return false;"><span class="sortarrow"><img src="'+ ts_image_path + ts_image_none + '" alt="&darr;"/></span></a>';
509 }
510 }
511 if (ts_alternate_row_colors) {
512 ts_alternate(table);
513 }
514 }
515
516 function ts_getInnerText(el) {
517 if (typeof el == "string") return el;
518 if (typeof el == "undefined") { return el };
519 if (el.textContent) return el.textContent; // not needed but it is faster
520 if (el.innerText) return el.innerText; // IE doesn't have textContent
521 var str = "";
522
523 var cs = el.childNodes;
524 var l = cs.length;
525 for (var i = 0; i < l; i++) {
526 switch (cs[i].nodeType) {
527 case 1: //ELEMENT_NODE
528 str += ts_getInnerText(cs[i]);
529 break;
530 case 3: //TEXT_NODE
531 str += cs[i].nodeValue;
532 break;
533 }
534 }
535 return str;
536 }
537
538 function ts_resortTable(lnk) {
539 // get the span
540 var span = lnk.getElementsByTagName('span')[0];
541
542 var td = lnk.parentNode;
543 var tr = td.parentNode;
544 var column = td.cellIndex;
545
546 var table = tr.parentNode;
547 while (table && !(table.tagName && table.tagName.toLowerCase() == 'table'))
548 table = table.parentNode;
549 if (!table) return;
550
551 // Work out a type for the column
552 if (table.rows.length <= 1) return;
553
554 // Skip the first row if that's where the headings are
555 var rowStart = (table.tHead && table.tHead.rows.length > 0 ? 0 : 1);
556
557 var itm = "";
558 for (var i = rowStart; i < table.rows.length; i++) {
559 if (table.rows[i].cells.length > column) {
560 itm = ts_getInnerText(table.rows[i].cells[column]);
561 itm = itm.replace(/^[\s\xa0]+/, "").replace(/[\s\xa0]+$/, "");
562 if (itm != "") break;
563 }
564 }
565
566 sortfn = ts_sort_caseinsensitive;
567 if (itm.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/))
568 sortfn = ts_sort_date;
569 if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/))
570 sortfn = ts_sort_date;
571 if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d$/))
572 sortfn = ts_sort_date;
573 if (itm.match(/^[\u00a3$\u20ac]/)) // pound dollar euro
574 sortfn = ts_sort_currency;
575 if (itm.match(/^[\d.,]+\%?$/))
576 sortfn = ts_sort_numeric;
577
578 var reverse = (span.getAttribute("sortdir") == 'down');
579
580 var newRows = new Array();
581 for (var j = rowStart; j < table.rows.length; j++) {
582 var row = table.rows[j];
583 var keyText = ts_getInnerText(row.cells[column]);
584 var oldIndex = (reverse ? -j : j);
585
586 newRows[newRows.length] = new Array(row, keyText, oldIndex);
587 }
588
589 newRows.sort(sortfn);
590
591 var arrowHTML;
592 if (reverse) {
593 arrowHTML = '<img src="'+ ts_image_path + ts_image_down + '" alt="&darr;"/>';
594 newRows.reverse();
595 span.setAttribute('sortdir','up');
596 } else {
597 arrowHTML = '<img src="'+ ts_image_path + ts_image_up + '" alt="&uarr;"/>';
598 span.setAttribute('sortdir','down');
599 }
600
601 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
602 // don't do sortbottom rows
603 for (var i = 0; i < newRows.length; i++) {
604 if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") == -1)
605 table.tBodies[0].appendChild(newRows[i][0]);
606 }
607 // do sortbottom rows only
608 for (var i = 0; i < newRows.length; i++) {
609 if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") != -1)
610 table.tBodies[0].appendChild(newRows[i][0]);
611 }
612
613 // Delete any other arrows there may be showing
614 var spans = getElementsByClassName(tr, "span", "sortarrow");
615 for (var i = 0; i < spans.length; i++) {
616 spans[i].innerHTML = '<img src="'+ ts_image_path + ts_image_none + '" alt="&darr;"/>';
617 }
618 span.innerHTML = arrowHTML;
619
620 ts_alternate(table);
621 }
622
623 function ts_dateToSortKey(date) {
624 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
625 if (date.length == 11) {
626 switch (date.substr(3,3).toLowerCase()) {
627 case "jan": var month = "01"; break;
628 case "feb": var month = "02"; break;
629 case "mar": var month = "03"; break;
630 case "apr": var month = "04"; break;
631 case "may": var month = "05"; break;
632 case "jun": var month = "06"; break;
633 case "jul": var month = "07"; break;
634 case "aug": var month = "08"; break;
635 case "sep": var month = "09"; break;
636 case "oct": var month = "10"; break;
637 case "nov": var month = "11"; break;
638 case "dec": var month = "12"; break;
639 // default: var month = "00";
640 }
641 return date.substr(7,4)+month+date.substr(0,2);
642 } else if (date.length == 10) {
643 if (ts_europeandate == false) {
644 return date.substr(6,4)+date.substr(0,2)+date.substr(3,2);
645 } else {
646 return date.substr(6,4)+date.substr(3,2)+date.substr(0,2);
647 }
648 } else if (date.length == 8) {
649 yr = date.substr(6,2);
650 if (parseInt(yr) < 50) {
651 yr = '20'+yr;
652 } else {
653 yr = '19'+yr;
654 }
655 if (ts_europeandate == true) {
656 return yr+date.substr(3,2)+date.substr(0,2);
657 } else {
658 return yr+date.substr(0,2)+date.substr(3,2);
659 }
660 }
661 return "00000000";
662 }
663
664 function ts_parseFloat(num) {
665 if (!num) return 0;
666 num = parseFloat(num.replace(/,/g, ""));
667 return (isNaN(num) ? 0 : num);
668 }
669
670 function ts_sort_date(a,b) {
671 var aa = ts_dateToSortKey(a[1]);
672 var bb = ts_dateToSortKey(b[1]);
673 return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]);
674 }
675
676 function ts_sort_currency(a,b) {
677 var aa = ts_parseFloat(a[1].replace(/[^0-9.]/g,''));
678 var bb = ts_parseFloat(b[1].replace(/[^0-9.]/g,''));
679 return (aa != bb ? aa - bb : a[2] - b[2]);
680 }
681
682 function ts_sort_numeric(a,b) {
683 var aa = ts_parseFloat(a[1]);
684 var bb = ts_parseFloat(b[1]);
685 return (aa != bb ? aa - bb : a[2] - b[2]);
686 }
687
688 function ts_sort_caseinsensitive(a,b) {
689 var aa = a[1].toLowerCase();
690 var bb = b[1].toLowerCase();
691 return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]);
692 }
693
694 function ts_sort_default(a,b) {
695 return (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2]);
696 }
697
698 function ts_alternate(table) {
699 // Take object table and get all it's tbodies.
700 var tableBodies = table.getElementsByTagName("tbody");
701 // Loop through these tbodies
702 for (var i = 0; i < tableBodies.length; i++) {
703 // Take the tbody, and get all it's rows
704 var tableRows = tableBodies[i].getElementsByTagName("tr");
705 // Loop through these rows
706 // Start at 1 because we want to leave the heading row untouched
707 for (var j = 0; j < tableRows.length; j++) {
708 // Check if j is even, and apply classes for both possible results
709 var oldClasses = tableRows[j].className.split(" ");
710 var newClassName = "";
711 for (var k = 0; k < oldClasses.length; k++) {
712 if (oldClasses[k] != "" && oldClasses[k] != "even" && oldClasses[k] != "odd")
713 newClassName += oldClasses[k] + " ";
714 }
715 tableRows[j].className = newClassName + (j % 2 == 0 ? "even" : "odd");
716 }
717 }
718 }
719
720 /*
721 * End of table sorting code
722 */
723
724
725 /**
726 * Add a cute little box at the top of the screen to inform the user of
727 * something, replacing any preexisting message.
728 *
729 * @param String message HTML to be put inside the right div
730 * @param String className Used in adding a class; should be different for each
731 * call to allow CSS/JS to hide different boxes. null = no class used.
732 * @return Boolean True on success, false on failure
733 */
734 function jsMsg( message, className ) {
735 if ( !document.getElementById ) {
736 return false;
737 }
738 // We special-case skin structures provided by the software. Skins that
739 // choose to abandon or significantly modify our formatting can just define
740 // an mw-js-message div to start with.
741 var messageDiv = document.getElementById( 'mw-js-message' );
742 if ( !messageDiv ) {
743 messageDiv = document.createElement( 'div' );
744 if ( document.getElementById( 'column-content' )
745 && document.getElementById( 'content' ) ) {
746 // MonoBook, presumably
747 document.getElementById( 'content' ).insertBefore(
748 messageDiv,
749 document.getElementById( 'content' ).firstChild
750 );
751 } else if ( document.getElementById('content')
752 && document.getElementById( 'article' ) ) {
753 // Non-Monobook but still recognizable (old-style)
754 document.getElementById( 'article').insertBefore(
755 messageDiv,
756 document.getElementById( 'article' ).firstChild
757 );
758 } else {
759 return false;
760 }
761 }
762
763 messageDiv.setAttribute( 'id', 'mw-js-message' );
764 if( className ) {
765 messageDiv.setAttribute( 'class', 'mw-js-message-'+className );
766 }
767 messageDiv.innerHTML = message;
768 return true;
769 }
770
771 /**
772 * Inject a cute little progress spinner after the specified element
773 *
774 * @param element Element to inject after
775 * @param id Identifier string (for use with removeSpinner(), below)
776 */
777 function injectSpinner( element, id ) {
778 var spinner = document.createElement( "img" );
779 spinner.id = "mw-spinner-" + id;
780 spinner.src = stylepath + "/common/images/spinner.gif";
781 spinner.alt = spinner.title = "...";
782 if( element.nextSibling ) {
783 element.parentNode.insertBefore( spinner, element.nextSibling );
784 } else {
785 element.parentNode.appendChild( spinner );
786 }
787 }
788
789 /**
790 * Remove a progress spinner added with injectSpinner()
791 *
792 * @param id Identifier string
793 */
794 function removeSpinner( id ) {
795 var spinner = document.getElementById( "mw-spinner-" + id );
796 if( spinner ) {
797 spinner.parentNode.removeChild( spinner );
798 }
799 }
800
801 function runOnloadHook() {
802 // don't run anything below this for non-dom browsers
803 if (doneOnloadHook || !(document.getElementById && document.getElementsByTagName)) {
804 return;
805 }
806
807 // set this before running any hooks, since any errors below
808 // might cause the function to terminate prematurely
809 doneOnloadHook = true;
810
811 updateTooltipAccessKeys( null );
812 akeytt( null );
813 setupCheckboxShiftClick();
814 sortables_init();
815
816 // Run any added-on functions
817 for (var i = 0; i < onloadFuncts.length; i++) {
818 onloadFuncts[i]();
819 }
820 }
821
822 /**
823 * Add an event handler to an element
824 *
825 * @param Element element Element to add handler to
826 * @param String attach Event to attach to
827 * @param callable handler Event handler callback
828 */
829 function addHandler( element, attach, handler ) {
830 if( window.addEventListener ) {
831 element.addEventListener( attach, handler, false );
832 } else if( window.attachEvent ) {
833 element.attachEvent( 'on' + attach, handler );
834 }
835 }
836
837 /**
838 * Add a click event handler to an element
839 *
840 * @param Element element Element to add handler to
841 * @param callable handler Event handler callback
842 */
843 function addClickHandler( element, handler ) {
844 addHandler( element, 'click', handler );
845 }
846 //note: all skins should call runOnloadHook() at the end of html output,
847 // so the below should be redundant. It's there just in case.
848 hookEvent("load", runOnloadHook);