Don't load nonexistent fix files for non-Monobook
[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 webkit_match = clientPC.match(/applewebkit\/(\d+)/);
7 if (webkit_match) {
8 var is_safari = clientPC.indexOf('applewebkit') != -1 &&
9 clientPC.indexOf('spoofer') == -1;
10 var is_safari_win = is_safari && clientPC.indexOf('windows') != -1;
11 var webkit_version = parseInt(webkit_match[1]);
12 }
13 // For accesskeys; note that FF3+ is included here!
14 var is_ff2 = /firefox\/[2-9]|minefield\/3/.test( clientPC );
15 var ff2_bugs = /firefox\/2/.test( clientPC );
16 // These aren't used here, but some custom scripts rely on them
17 var is_ff2_win = is_ff2 && clientPC.indexOf('windows') != -1;
18 var is_ff2_x11 = is_ff2 && clientPC.indexOf('x11') != -1;
19 if (clientPC.indexOf('opera') != -1) {
20 var is_opera = true;
21 var is_opera_preseven = window.opera && !document.childNodes;
22 var is_opera_seven = window.opera && document.childNodes;
23 var is_opera_95 = /opera\/(9\.[5-9]|[1-9][0-9])/.test( clientPC );
24 var opera6_bugs = is_opera_preseven;
25 var opera7_bugs = is_opera_seven && !is_opera_95;
26 var opera95_bugs = /opera\/(9\.5)/.test( clientPC );
27 }
28 // Start at 4 to minimize the chance of breaking on IE10 :)
29 var ie6_bugs = /msie [4-6]/.test( clientPC );
30
31 // Global external objects used by this script.
32 /*extern ta, stylepath, skin */
33
34 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
35 var doneOnloadHook;
36
37 if (!window.onloadFuncts) {
38 var onloadFuncts = [];
39 }
40
41 function addOnloadHook( hookFunct ) {
42 // Allows add-on scripts to add onload functions
43 if( !doneOnloadHook ) {
44 onloadFuncts[onloadFuncts.length] = hookFunct;
45 } else {
46 hookFunct(); // bug in MSIE script loading
47 }
48 }
49
50 function hookEvent( hookName, hookFunct ) {
51 addHandler( window, hookName, hookFunct );
52 }
53
54 function importScript( page ) {
55 // TODO: might want to introduce a utility function to match wfUrlencode() in PHP
56 var uri = wgScript + '?title=' +
57 encodeURIComponent(page.replace(/ /g,'_')).replace(/%2F/ig,'/').replace(/%3A/ig,':') +
58 '&action=raw&ctype=text/javascript';
59 return importScriptURI( uri );
60 }
61
62 var loadedScripts = {}; // included-scripts tracker
63 function importScriptURI( url ) {
64 if ( loadedScripts[url] ) {
65 return null;
66 }
67 loadedScripts[url] = true;
68 var s = document.createElement( 'script' );
69 s.setAttribute( 'src', url );
70 s.setAttribute( 'type', 'text/javascript' );
71 document.getElementsByTagName('head')[0].appendChild( s );
72 return s;
73 }
74
75 function importStylesheet( page ) {
76 return importStylesheetURI( wgScript + '?action=raw&ctype=text/css&title=' + encodeURIComponent( page.replace(/ /g,'_') ) );
77 }
78
79 function importStylesheetURI( url, media ) {
80 var l = document.createElement( 'link' );
81 l.type = 'text/css';
82 l.rel = 'stylesheet';
83 l.href = url;
84 if( media ) {
85 l.media = media;
86 }
87 document.getElementsByTagName('head')[0].appendChild( l );
88 return l;
89 }
90
91 function appendCSS( text ) {
92 var s = document.createElement( 'style' );
93 s.type = 'text/css';
94 s.rel = 'stylesheet';
95 if ( s.styleSheet ) {
96 s.styleSheet.cssText = text; // IE
97 } else {
98 s.appendChild( document.createTextNode( text + '' ) ); // Safari sometimes borks on null
99 }
100 document.getElementsByTagName('head')[0].appendChild( s );
101 return s;
102 }
103
104 // Special stylesheet links for Monobook only (see bug 14717)
105 if ( typeof stylepath != 'undefined' && skin == 'monobook' ) {
106 if ( opera6_bugs ) {
107 importStylesheetURI( stylepath + '/' + skin + '/Opera6Fixes.css' );
108 } else if ( opera7_bugs ) {
109 importStylesheetURI( stylepath + '/' + skin + '/Opera7Fixes.css' );
110 } else if ( opera95_bugs ) {
111 importStylesheetURI( stylepath + '/' + skin + '/Opera9Fixes.css' );
112 } else if ( ff2_bugs ) {
113 importStylesheetURI( stylepath + '/' + skin + '/FF2Fixes.css' );
114 }
115 }
116
117
118 if ( wgBreakFrames ) {
119 // Un-trap us from framesets
120 if ( window.top != window ) {
121 window.top.location = window.location;
122 }
123 }
124
125 function showTocToggle() {
126 if ( document.createTextNode ) {
127 // Uses DOM calls to avoid document.write + XHTML issues
128
129 var linkHolder = document.getElementById( 'toctitle' );
130 var existingLink = document.getElementById( 'togglelink' );
131 if ( !linkHolder || existingLink ) {
132 // Don't add the toggle link twice
133 return;
134 }
135
136 var outerSpan = document.createElement( 'span' );
137 outerSpan.className = 'toctoggle';
138
139 var toggleLink = document.createElement( 'a' );
140 toggleLink.id = 'togglelink';
141 toggleLink.className = 'internal';
142 toggleLink.href = 'javascript:toggleToc()';
143 toggleLink.appendChild( document.createTextNode( tocHideText ) );
144
145 outerSpan.appendChild( document.createTextNode( '[' ) );
146 outerSpan.appendChild( toggleLink );
147 outerSpan.appendChild( document.createTextNode( ']' ) );
148
149 linkHolder.appendChild( document.createTextNode( ' ' ) );
150 linkHolder.appendChild( outerSpan );
151
152 var cookiePos = document.cookie.indexOf( "hidetoc=" );
153 if ( cookiePos > -1 && document.cookie.charAt( cookiePos + 8 ) == 1 ) {
154 toggleToc();
155 }
156 }
157 }
158
159 function changeText( el, newText ) {
160 // Safari work around
161 if ( el.innerText ) {
162 el.innerText = newText;
163 } else if ( el.firstChild && el.firstChild.nodeValue ) {
164 el.firstChild.nodeValue = newText;
165 }
166 }
167
168 function toggleToc() {
169 var tocmain = document.getElementById( 'toc' );
170 var toc = document.getElementById('toc').getElementsByTagName('ul')[0];
171 var toggleLink = document.getElementById( 'togglelink' );
172
173 if ( toc && toggleLink && toc.style.display == 'none' ) {
174 changeText( toggleLink, tocHideText );
175 toc.style.display = 'block';
176 document.cookie = "hidetoc=0";
177 tocmain.className = 'toc';
178 } else {
179 changeText( toggleLink, tocShowText );
180 toc.style.display = 'none';
181 document.cookie = "hidetoc=1";
182 tocmain.className = 'toc tochidden';
183 }
184 }
185
186 var mwEditButtons = [];
187 var mwCustomEditButtons = []; // eg to add in MediaWiki:Common.js
188
189 function escapeQuotes( text ) {
190 var re = new RegExp( "'", "g" );
191 text = text.replace( re, "\\'" );
192 re = new RegExp( "\\n", "g" );
193 text = text.replace( re, "\\n" );
194 return escapeQuotesHTML( text );
195 }
196
197 function escapeQuotesHTML( text ) {
198 var re = new RegExp( '&', "g" );
199 text = text.replace( re, "&" );
200 re = new RegExp( '"', "g" );
201 text = text.replace( re, """ );
202 re = new RegExp( '<', "g" );
203 text = text.replace( re, "&lt;" );
204 re = new RegExp( '>', "g" );
205 text = text.replace( re, "&gt;" );
206 return text;
207 }
208
209 /**
210 * Set the accesskey prefix based on browser detection.
211 */
212 var tooltipAccessKeyPrefix = 'alt-';
213 if ( is_opera ) {
214 tooltipAccessKeyPrefix = 'shift-esc-';
215 } else if ( !is_safari_win && is_safari && webkit_version > 526 ) {
216 tooltipAccessKeyPrefix = 'ctrl-alt-';
217 } else if ( !is_safari_win && ( is_safari
218 || clientPC.indexOf('mac') != -1
219 || clientPC.indexOf('konqueror') != -1 ) ) {
220 tooltipAccessKeyPrefix = 'ctrl-';
221 } else if ( is_ff2 ) {
222 tooltipAccessKeyPrefix = 'alt-shift-';
223 }
224 var tooltipAccessKeyRegexp = /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/;
225
226 /**
227 * Add the appropriate prefix to the accesskey shown in the tooltip.
228 * If the nodeList parameter is given, only those nodes are updated;
229 * otherwise, all the nodes that will probably have accesskeys by
230 * default are updated.
231 *
232 * @param Array nodeList -- list of elements to update
233 */
234 function updateTooltipAccessKeys( nodeList ) {
235 if ( !nodeList ) {
236 // Rather than scan all links on the whole page, we can just scan these
237 // containers which contain the relevant links. This is really just an
238 // optimization technique.
239 var linkContainers = [
240 'column-one', // Monobook and Modern
241 'head', 'panel', 'p-logo' // Vector
242 ];
243 for ( var i in linkContainers ) {
244 var linkContainer = document.getElementById( linkContainers[i] );
245 if ( linkContainer ) {
246 updateTooltipAccessKeys( linkContainer.getElementsByTagName( 'a' ) );
247 }
248 }
249 // these are rare enough that no such optimization is needed
250 updateTooltipAccessKeys( document.getElementsByTagName( 'input' ) );
251 updateTooltipAccessKeys( document.getElementsByTagName( 'label' ) );
252 return;
253 }
254
255 for ( var i = 0; i < nodeList.length; i++ ) {
256 var element = nodeList[i];
257 var tip = element.getAttribute( 'title' );
258 if ( tip && tooltipAccessKeyRegexp.exec( tip ) ) {
259 tip = tip.replace(tooltipAccessKeyRegexp,
260 '[' + tooltipAccessKeyPrefix + "$5]");
261 element.setAttribute( 'title', tip );
262 }
263 }
264 }
265
266 /**
267 * Add a link to one of the portlet menus on the page, including:
268 *
269 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
270 * p-personal: Personal tools (shown at the top right of the page in Monobook)
271 * p-navigation: Navigation
272 * p-tb: Toolbox
273 *
274 * This function exists for the convenience of custom JS authors. All
275 * but the first three parameters are optional, though providing at
276 * least an id and a tooltip is recommended.
277 *
278 * By default the new link will be added to the end of the list. To
279 * add the link before a given existing item, pass the DOM node of
280 * that item (easily obtained with document.getElementById()) as the
281 * nextnode parameter; to add the link _after_ an existing item, pass
282 * the node's nextSibling instead.
283 *
284 * @param String portlet -- id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
285 * @param String href -- link URL
286 * @param String text -- link text (will be automatically lowercased by CSS for p-cactions in Monobook)
287 * @param String id -- id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
288 * @param String tooltip -- text to show when hovering over the link, without accesskey suffix
289 * @param String accesskey -- accesskey to activate this link (one character, try to avoid conflicts)
290 * @param Node nextnode -- the DOM node before which the new item should be added, should be another item in the same list
291 *
292 * @return Node -- the DOM node of the new item (an LI element) or null
293 */
294 function addPortletLink( portlet, href, text, id, tooltip, accesskey, nextnode ) {
295 var root = document.getElementById( portlet );
296 if ( !root ) {
297 return null;
298 }
299 var node = root.getElementsByTagName( 'ul' )[0];
300 if ( !node ) {
301 return null;
302 }
303
304 // unhide portlet if it was hidden before
305 root.className = root.className.replace( /(^| )emptyPortlet( |$)/, "$2" );
306
307 var span = document.createElement( 'span' );
308 span.appendChild( document.createTextNode( text ) );
309
310 var link = document.createElement( 'a' );
311 link.appendChild( span );
312 link.href = href;
313
314 var item = document.createElement( 'li' );
315 item.appendChild( link );
316 if ( id ) {
317 item.id = id;
318 }
319
320 if ( accesskey ) {
321 link.setAttribute( 'accesskey', accesskey );
322 tooltip += ' [' + accesskey + ']';
323 }
324 if ( tooltip ) {
325 link.setAttribute( 'title', tooltip );
326 }
327 if ( accesskey && tooltip ) {
328 updateTooltipAccessKeys( new Array( link ) );
329 }
330
331 if ( nextnode && nextnode.parentNode == node ) {
332 node.insertBefore( item, nextnode );
333 } else {
334 node.appendChild( item ); // IE compatibility (?)
335 }
336
337 return item;
338 }
339
340 function getInnerText( el ) {
341 if ( typeof el == 'string' ) {
342 return el;
343 }
344 if ( typeof el == 'undefined' ) {
345 return el;
346 }
347 if ( el.textContent ) {
348 return el.textContent; // not needed but it is faster
349 }
350 if ( el.innerText ) {
351 return el.innerText; // IE doesn't have textContent
352 }
353 var str = '';
354
355 var cs = el.childNodes;
356 var l = cs.length;
357 for ( var i = 0; i < l; i++ ) {
358 switch ( cs[i].nodeType ) {
359 case 1: // ELEMENT_NODE
360 str += ts_getInnerText( cs[i] );
361 break;
362 case 3: // TEXT_NODE
363 str += cs[i].nodeValue;
364 break;
365 }
366 }
367 return str;
368 }
369
370 /* Dummy for deprecated function */
371 function akeytt( doId ) {
372 }
373
374 var checkboxes;
375 var lastCheckbox;
376
377 function setupCheckboxShiftClick() {
378 checkboxes = [];
379 lastCheckbox = null;
380 var inputs = document.getElementsByTagName( 'input' );
381 addCheckboxClickHandlers( inputs );
382 }
383
384 function addCheckboxClickHandlers( inputs, start ) {
385 if ( !start ) {
386 start = 0;
387 }
388
389 var finish = start + 250;
390 if ( finish > inputs.length ) {
391 finish = inputs.length;
392 }
393
394 for ( var i = start; i < finish; i++ ) {
395 var cb = inputs[i];
396 if ( !cb.type || cb.type.toLowerCase() != 'checkbox' ) {
397 continue;
398 }
399 var end = checkboxes.length;
400 checkboxes[end] = cb;
401 cb.index = end;
402 cb.onclick = checkboxClickHandler;
403 }
404
405 if ( finish < inputs.length ) {
406 setTimeout( function() {
407 addCheckboxClickHandlers( inputs, finish );
408 }, 200 );
409 }
410 }
411
412 function checkboxClickHandler( e ) {
413 if ( typeof e == 'undefined' ) {
414 e = window.event;
415 }
416 if ( !e.shiftKey || lastCheckbox === null ) {
417 lastCheckbox = this.index;
418 return true;
419 }
420 var endState = this.checked;
421 var start, finish;
422 if ( this.index < lastCheckbox ) {
423 start = this.index + 1;
424 finish = lastCheckbox;
425 } else {
426 start = lastCheckbox;
427 finish = this.index - 1;
428 }
429 for ( var i = start; i <= finish; ++i ) {
430 checkboxes[i].checked = endState;
431 if( i > start && typeof checkboxes[i].onchange == 'function' ) {
432 checkboxes[i].onchange(); // fire triggers
433 }
434 }
435 lastCheckbox = this.index;
436 return true;
437 }
438
439
440 /*
441 Written by Jonathan Snook, http://www.snook.ca/jonathan
442 Add-ons by Robert Nyman, http://www.robertnyman.com
443 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
444 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
445 */
446 function getElementsByClassName( oElm, strTagName, oClassNames ) {
447 var arrReturnElements = new Array();
448 if ( typeof( oElm.getElementsByClassName ) == 'function' ) {
449 /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
450 var arrNativeReturn = oElm.getElementsByClassName( oClassNames );
451 if ( strTagName == '*' ) {
452 return arrNativeReturn;
453 }
454 for ( var h = 0; h < arrNativeReturn.length; h++ ) {
455 if( arrNativeReturn[h].tagName.toLowerCase() == strTagName.toLowerCase() ) {
456 arrReturnElements[arrReturnElements.length] = arrNativeReturn[h];
457 }
458 }
459 return arrReturnElements;
460 }
461 var arrElements = ( strTagName == '*' && oElm.all ) ? oElm.all : oElm.getElementsByTagName( strTagName );
462 var arrRegExpClassNames = new Array();
463 if( typeof oClassNames == 'object' ) {
464 for( var i = 0; i < oClassNames.length; i++ ) {
465 arrRegExpClassNames[arrRegExpClassNames.length] =
466 new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)");
467 }
468 } else {
469 arrRegExpClassNames[arrRegExpClassNames.length] =
470 new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)");
471 }
472 var oElement;
473 var bMatchesAll;
474 for( var j = 0; j < arrElements.length; j++ ) {
475 oElement = arrElements[j];
476 bMatchesAll = true;
477 for( var k = 0; k < arrRegExpClassNames.length; k++ ) {
478 if( !arrRegExpClassNames[k].test( oElement.className ) ) {
479 bMatchesAll = false;
480 break;
481 }
482 }
483 if( bMatchesAll ) {
484 arrReturnElements[arrReturnElements.length] = oElement;
485 }
486 }
487 return ( arrReturnElements );
488 }
489
490 function redirectToFragment( fragment ) {
491 var match = navigator.userAgent.match(/AppleWebKit\/(\d+)/);
492 if ( match ) {
493 var webKitVersion = parseInt( match[1] );
494 if ( webKitVersion < 420 ) {
495 // Released Safari w/ WebKit 418.9.1 messes up horribly
496 // Nightlies of 420+ are ok
497 return;
498 }
499 }
500 if ( is_gecko ) {
501 // Mozilla needs to wait until after load, otherwise the window doesn't scroll
502 addOnloadHook(function() {
503 if ( window.location.hash == '' ) {
504 window.location.hash = fragment;
505 }
506 });
507 } else {
508 if ( window.location.hash == '' ) {
509 window.location.hash = fragment;
510 }
511 }
512 }
513
514 /*
515 * Table sorting script based on one (c) 1997-2006 Stuart Langridge and Joost
516 * de Valk:
517 * http://www.joostdevalk.nl/code/sortable-table/
518 * http://www.kryogenix.org/code/browser/sorttable/
519 *
520 * @todo don't break on colspans/rowspans (bug 8028)
521 * @todo language-specific digit grouping/decimals (bug 8063)
522 * @todo support all accepted date formats (bug 8226)
523 */
524
525 var ts_image_path = stylepath + '/common/images/';
526 var ts_image_up = 'sort_up.gif';
527 var ts_image_down = 'sort_down.gif';
528 var ts_image_none = 'sort_none.gif';
529 var ts_europeandate = wgContentLanguage != 'en'; // The non-American-inclined can change to "true"
530 var ts_alternate_row_colors = false;
531 var ts_number_transform_table = null;
532 var ts_number_regex = null;
533
534 function sortables_init() {
535 var idnum = 0;
536 // Find all tables with class sortable and make them sortable
537 var tables = getElementsByClassName( document, 'table', 'sortable' );
538 for ( var ti = 0; ti < tables.length ; ti++ ) {
539 if ( !tables[ti].id ) {
540 tables[ti].setAttribute( 'id', 'sortable_table_id_' + idnum );
541 ++idnum;
542 }
543 ts_makeSortable( tables[ti] );
544 }
545 }
546
547 function ts_makeSortable( table ) {
548 var firstRow;
549 if ( table.rows && table.rows.length > 0 ) {
550 if ( table.tHead && table.tHead.rows.length > 0 ) {
551 firstRow = table.tHead.rows[table.tHead.rows.length-1];
552 } else {
553 firstRow = table.rows[0];
554 }
555 }
556 if ( !firstRow ) {
557 return;
558 }
559
560 // We have a first row: assume it's the header, and make its contents clickable links
561 for ( var i = 0; i < firstRow.cells.length; i++ ) {
562 var cell = firstRow.cells[i];
563 if ( (' ' + cell.className + ' ').indexOf(' unsortable ') == -1 ) {
564 cell.innerHTML += '<a href="#" class="sortheader" '
565 + 'onclick="ts_resortTable(this);return false;">'
566 + '<span class="sortarrow">'
567 + '<img src="'
568 + ts_image_path
569 + ts_image_none
570 + '" alt="&darr;"/></span></a>';
571 }
572 }
573 if ( ts_alternate_row_colors ) {
574 ts_alternate( table );
575 }
576 }
577
578 function ts_getInnerText( el ) {
579 return getInnerText( el );
580 }
581
582 function ts_resortTable( lnk ) {
583 // get the span
584 var span = lnk.getElementsByTagName('span')[0];
585
586 var td = lnk.parentNode;
587 var tr = td.parentNode;
588 var column = td.cellIndex;
589
590 var table = tr.parentNode;
591 while ( table && !( table.tagName && table.tagName.toLowerCase() == 'table' ) ) {
592 table = table.parentNode;
593 }
594 if ( !table ) {
595 return;
596 }
597
598 if ( table.rows.length <= 1 ) {
599 return;
600 }
601
602 // Generate the number transform table if it's not done already
603 if ( ts_number_transform_table === null ) {
604 ts_initTransformTable();
605 }
606
607 // Work out a type for the column
608 // Skip the first row if that's where the headings are
609 var rowStart = ( table.tHead && table.tHead.rows.length > 0 ? 0 : 1 );
610
611 var itm = '';
612 for ( var i = rowStart; i < table.rows.length; i++ ) {
613 if ( table.rows[i].cells.length > column ) {
614 itm = ts_getInnerText(table.rows[i].cells[column]);
615 itm = itm.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '');
616 if ( itm != '' ) {
617 break;
618 }
619 }
620 }
621
622 // TODO: bug 8226, localised date formats
623 var sortfn = ts_sort_generic;
624 var preprocessor = ts_toLowerCase;
625 if ( /^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/.test( itm ) ) {
626 preprocessor = ts_dateToSortKey;
627 } else if ( /^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/.test( itm ) ) {
628 preprocessor = ts_dateToSortKey;
629 } else if ( /^\d\d[\/.-]\d\d[\/.-]\d\d$/.test( itm ) ) {
630 preprocessor = ts_dateToSortKey;
631 // (minus sign)([pound dollar euro yen currency]|cents)
632 } else if ( /(^([-\u2212] *)?[\u00a3$\u20ac\u00a4\u00a5]|\u00a2$)/.test( itm ) ) {
633 preprocessor = ts_currencyToSortKey;
634 } else if ( ts_number_regex.test( itm ) ) {
635 preprocessor = ts_parseFloat;
636 }
637
638 var reverse = ( span.getAttribute( 'sortdir' ) == 'down' );
639
640 var newRows = new Array();
641 var staticRows = new Array();
642 for ( var j = rowStart; j < table.rows.length; j++ ) {
643 var row = table.rows[j];
644 if( (' ' + row.className + ' ').indexOf(' unsortable ') < 0 ) {
645 var keyText = ts_getInnerText( row.cells[column] );
646 if( keyText === undefined ) {
647 keyText = '';
648 }
649 var oldIndex = ( reverse ? -j : j );
650 var preprocessed = preprocessor( keyText.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '') );
651
652 newRows[newRows.length] = new Array( row, preprocessed, oldIndex );
653 } else {
654 staticRows[staticRows.length] = new Array( row, false, j-rowStart );
655 }
656 }
657
658 newRows.sort( sortfn );
659
660 var arrowHTML;
661 if ( reverse ) {
662 arrowHTML = '<img src="' + ts_image_path + ts_image_down + '" alt="&darr;"/>';
663 newRows.reverse();
664 span.setAttribute( 'sortdir', 'up' );
665 } else {
666 arrowHTML = '<img src="' + ts_image_path + ts_image_up + '" alt="&uarr;"/>';
667 span.setAttribute( 'sortdir', 'down' );
668 }
669
670 for ( var i = 0; i < staticRows.length; i++ ) {
671 var row = staticRows[i];
672 newRows.splice( row[2], 0, row );
673 }
674
675 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
676 // don't do sortbottom rows
677 for ( var i = 0; i < newRows.length; i++ ) {
678 if ( ( ' ' + newRows[i][0].className + ' ').indexOf(' sortbottom ') == -1 ) {
679 table.tBodies[0].appendChild( newRows[i][0] );
680 }
681 }
682 // do sortbottom rows only
683 for ( var i = 0; i < newRows.length; i++ ) {
684 if ( ( ' ' + newRows[i][0].className + ' ').indexOf(' sortbottom ') != -1 ) {
685 table.tBodies[0].appendChild( newRows[i][0] );
686 }
687 }
688
689 // Delete any other arrows there may be showing
690 var spans = getElementsByClassName( tr, 'span', 'sortarrow' );
691 for ( var i = 0; i < spans.length; i++ ) {
692 spans[i].innerHTML = '<img src="' + ts_image_path + ts_image_none + '" alt="&darr;"/>';
693 }
694 span.innerHTML = arrowHTML;
695
696 if ( ts_alternate_row_colors ) {
697 ts_alternate( table );
698 }
699 }
700
701 function ts_initTransformTable() {
702 if ( typeof wgSeparatorTransformTable == 'undefined'
703 || ( wgSeparatorTransformTable[0] == '' && wgDigitTransformTable[2] == '' ) )
704 {
705 digitClass = "[0-9,.]";
706 ts_number_transform_table = false;
707 } else {
708 ts_number_transform_table = {};
709 // Unpack the transform table
710 // Separators
711 ascii = wgSeparatorTransformTable[0].split("\t");
712 localised = wgSeparatorTransformTable[1].split("\t");
713 for ( var i = 0; i < ascii.length; i++ ) {
714 ts_number_transform_table[localised[i]] = ascii[i];
715 }
716 // Digits
717 ascii = wgDigitTransformTable[0].split("\t");
718 localised = wgDigitTransformTable[1].split("\t");
719 for ( var i = 0; i < ascii.length; i++ ) {
720 ts_number_transform_table[localised[i]] = ascii[i];
721 }
722
723 // Construct regex for number identification
724 digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\\.'];
725 maxDigitLength = 1;
726 for ( var digit in ts_number_transform_table ) {
727 // Escape regex metacharacters
728 digits.push(
729 digit.replace( /[\\\\$\*\+\?\.\(\)\|\{\}\[\]\-]/,
730 function( s ) { return '\\' + s; } )
731 );
732 if ( digit.length > maxDigitLength ) {
733 maxDigitLength = digit.length;
734 }
735 }
736 if ( maxDigitLength > 1 ) {
737 digitClass = '[' + digits.join( '', digits ) + ']';
738 } else {
739 digitClass = '(' + digits.join( '|', digits ) + ')';
740 }
741 }
742
743 // We allow a trailing percent sign, which we just strip. This works fine
744 // if percents and regular numbers aren't being mixed.
745 ts_number_regex = new RegExp(
746 "^(" +
747 "[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?" + // Fortran-style scientific
748 "|" +
749 "[-+\u2212]?" + digitClass + "+%?" + // Generic localised
750 ")$", "i"
751 );
752 }
753
754 function ts_toLowerCase( s ) {
755 return s.toLowerCase();
756 }
757
758 function ts_dateToSortKey( date ) {
759 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
760 if ( date.length == 11 ) {
761 switch ( date.substr( 3, 3 ).toLowerCase() ) {
762 case 'jan':
763 var month = '01';
764 break;
765 case 'feb':
766 var month = '02';
767 break;
768 case 'mar':
769 var month = '03';
770 break;
771 case 'apr':
772 var month = '04';
773 break;
774 case 'may':
775 var month = '05';
776 break;
777 case 'jun':
778 var month = '06';
779 break;
780 case 'jul':
781 var month = '07';
782 break;
783 case 'aug':
784 var month = '08';
785 break;
786 case 'sep':
787 var month = '09';
788 break;
789 case 'oct':
790 var month = '10';
791 break;
792 case 'nov':
793 var month = '11';
794 break;
795 case 'dec':
796 var month = '12';
797 break;
798 // default: var month = '00';
799 }
800 return date.substr( 7, 4 ) + month + date.substr( 0, 2 );
801 } else if ( date.length == 10 ) {
802 if ( ts_europeandate == false ) {
803 return date.substr( 6, 4 ) + date.substr( 0, 2 ) + date.substr( 3, 2 );
804 } else {
805 return date.substr( 6, 4 ) + date.substr( 3, 2 ) + date.substr( 0, 2 );
806 }
807 } else if ( date.length == 8 ) {
808 yr = date.substr( 6, 2 );
809 if ( parseInt( yr ) < 50 ) {
810 yr = '20' + yr;
811 } else {
812 yr = '19' + yr;
813 }
814 if ( ts_europeandate == true ) {
815 return yr + date.substr( 3, 2 ) + date.substr( 0, 2 );
816 } else {
817 return yr + date.substr( 0, 2 ) + date.substr( 3, 2 );
818 }
819 }
820 return '00000000';
821 }
822
823 function ts_parseFloat( s ) {
824 if ( !s ) {
825 return 0;
826 }
827 if ( ts_number_transform_table != false ) {
828 var newNum = '', c;
829
830 for ( var p = 0; p < s.length; p++ ) {
831 c = s.charAt( p );
832 if ( c in ts_number_transform_table ) {
833 newNum += ts_number_transform_table[c];
834 } else {
835 newNum += c;
836 }
837 }
838 s = newNum;
839 }
840 num = parseFloat( s.replace(/[, ]/g, '').replace("\u2212", '-') );
841 return ( isNaN( num ) ? -Infinity : num );
842 }
843
844 function ts_currencyToSortKey( s ) {
845 return ts_parseFloat(s.replace(/[^-\u22120-9.,]/g,''));
846 }
847
848 function ts_sort_generic( a, b ) {
849 return a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2];
850 }
851
852 function ts_alternate( table ) {
853 // Take object table and get all it's tbodies.
854 var tableBodies = table.getElementsByTagName( 'tbody' );
855 // Loop through these tbodies
856 for ( var i = 0; i < tableBodies.length; i++ ) {
857 // Take the tbody, and get all it's rows
858 var tableRows = tableBodies[i].getElementsByTagName( 'tr' );
859 // Loop through these rows
860 // Start at 1 because we want to leave the heading row untouched
861 for ( var j = 0; j < tableRows.length; j++ ) {
862 // Check if j is even, and apply classes for both possible results
863 var oldClasses = tableRows[j].className.split(' ');
864 var newClassName = '';
865 for ( var k = 0; k < oldClasses.length; k++ ) {
866 if ( oldClasses[k] != '' && oldClasses[k] != 'even' && oldClasses[k] != 'odd' ) {
867 newClassName += oldClasses[k] + ' ';
868 }
869 }
870 tableRows[j].className = newClassName + ( j % 2 == 0 ? 'even' : 'odd' );
871 }
872 }
873 }
874
875 /*
876 * End of table sorting code
877 */
878
879
880 /**
881 * Add a cute little box at the top of the screen to inform the user of
882 * something, replacing any preexisting message.
883 *
884 * @param String -or- Dom Object message HTML to be put inside the right div
885 * @param String className Used in adding a class; should be different for each
886 * call to allow CSS/JS to hide different boxes. null = no class used.
887 * @return Boolean True on success, false on failure
888 */
889 function jsMsg( message, className ) {
890 if ( !document.getElementById ) {
891 return false;
892 }
893 // We special-case skin structures provided by the software. Skins that
894 // choose to abandon or significantly modify our formatting can just define
895 // an mw-js-message div to start with.
896 var messageDiv = document.getElementById( 'mw-js-message' );
897 if ( !messageDiv ) {
898 messageDiv = document.createElement( 'div' );
899 if ( document.getElementById( 'column-content' )
900 && document.getElementById( 'content' ) ) {
901 // MonoBook, presumably
902 document.getElementById( 'content' ).insertBefore(
903 messageDiv,
904 document.getElementById( 'content' ).firstChild
905 );
906 } else if ( document.getElementById( 'content' )
907 && document.getElementById( 'article' ) ) {
908 // Non-Monobook but still recognizable (old-style)
909 document.getElementById( 'article').insertBefore(
910 messageDiv,
911 document.getElementById( 'article' ).firstChild
912 );
913 } else {
914 return false;
915 }
916 }
917
918 messageDiv.setAttribute( 'id', 'mw-js-message' );
919 messageDiv.style.display = 'block';
920 if( className ) {
921 messageDiv.setAttribute( 'class', 'mw-js-message-' + className );
922 }
923
924 if ( typeof message === 'object' ) {
925 while ( messageDiv.hasChildNodes() ) { // Remove old content
926 messageDiv.removeChild( messageDiv.firstChild );
927 }
928 messageDiv.appendChild( message ); // Append new content
929 } else {
930 messageDiv.innerHTML = message;
931 }
932 return true;
933 }
934
935 /**
936 * Inject a cute little progress spinner after the specified element
937 *
938 * @param element Element to inject after
939 * @param id Identifier string (for use with removeSpinner(), below)
940 */
941 function injectSpinner( element, id ) {
942 var spinner = document.createElement( 'img' );
943 spinner.id = 'mw-spinner-' + id;
944 spinner.src = stylepath + '/common/images/spinner.gif';
945 spinner.alt = spinner.title = '...';
946 if( element.nextSibling ) {
947 element.parentNode.insertBefore( spinner, element.nextSibling );
948 } else {
949 element.parentNode.appendChild( spinner );
950 }
951 }
952
953 /**
954 * Remove a progress spinner added with injectSpinner()
955 *
956 * @param id Identifier string
957 */
958 function removeSpinner( id ) {
959 var spinner = document.getElementById( 'mw-spinner-' + id );
960 if( spinner ) {
961 spinner.parentNode.removeChild( spinner );
962 }
963 }
964
965 function runOnloadHook() {
966 // don't run anything below this for non-dom browsers
967 if ( doneOnloadHook || !( document.getElementById && document.getElementsByTagName ) ) {
968 return;
969 }
970
971 // set this before running any hooks, since any errors below
972 // might cause the function to terminate prematurely
973 doneOnloadHook = true;
974
975 updateTooltipAccessKeys( null );
976 setupCheckboxShiftClick();
977 sortables_init();
978
979 // Run any added-on functions
980 for ( var i = 0; i < onloadFuncts.length; i++ ) {
981 onloadFuncts[i]();
982 }
983 }
984
985 /**
986 * Add an event handler to an element
987 *
988 * @param Element element Element to add handler to
989 * @param String attach Event to attach to
990 * @param callable handler Event handler callback
991 */
992 function addHandler( element, attach, handler ) {
993 if( window.addEventListener ) {
994 element.addEventListener( attach, handler, false );
995 } else if( window.attachEvent ) {
996 element.attachEvent( 'on' + attach, handler );
997 }
998 }
999
1000 /**
1001 * Add a click event handler to an element
1002 *
1003 * @param Element element Element to add handler to
1004 * @param callable handler Event handler callback
1005 */
1006 function addClickHandler( element, handler ) {
1007 addHandler( element, 'click', handler );
1008 }
1009
1010 /**
1011 * Removes an event handler from an element
1012 *
1013 * @param Element element Element to remove handler from
1014 * @param String remove Event to remove
1015 * @param callable handler Event handler callback to remove
1016 */
1017 function removeHandler( element, remove, handler ) {
1018 if( window.removeEventListener ) {
1019 element.removeEventListener( remove, handler, false );
1020 } else if( window.detachEvent ) {
1021 element.detachEvent( 'on' + remove, handler );
1022 }
1023 }
1024 // note: all skins should call runOnloadHook() at the end of html output,
1025 // so the below should be redundant. It's there just in case.
1026 hookEvent( 'load', runOnloadHook );
1027
1028 if ( ie6_bugs ) {
1029 importScriptURI( stylepath + '/common/IEFixes.js' );
1030 }