1 // MediaWiki JavaScript support functions
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+)/);
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 // Tests for chrome here, to avoid breaking old scripts safari left alone
13 // This is here for accesskeys
14 var is_chrome
= clientPC
.indexOf('chrome') !== -1 &&
15 clientPC
.indexOf('spoofer') === -1;
16 var is_chrome_mac
= is_chrome
&& clientPC
.indexOf('mac') !== -1
18 // For accesskeys; note that FF3+ is included here!
19 var is_ff2
= /firefox\/[2-9]|minefield\/3/.test( clientPC
);
20 var ff2_bugs
= /firefox\/2/.test( clientPC
);
21 // These aren't used here, but some custom scripts rely on them
22 var is_ff2_win
= is_ff2
&& clientPC
.indexOf('windows') !== -1;
23 var is_ff2_x11
= is_ff2
&& clientPC
.indexOf('x11') !== -1;
24 if (clientPC
.indexOf('opera') !== -1) {
26 var is_opera_preseven
= window
.opera
&& !document
.childNodes
;
27 var is_opera_seven
= window
.opera
&& document
.childNodes
;
28 var is_opera_95
= /opera\/(9\.[5-9]|[1-9][0-9])/.test( clientPC
);
29 var opera6_bugs
= is_opera_preseven
;
30 var opera7_bugs
= is_opera_seven
&& !is_opera_95
;
31 var opera95_bugs
= /opera\/(9\.5)/.test( clientPC
);
33 // As recommended by <http://msdn.microsoft.com/en-us/library/ms537509.aspx>,
34 // avoiding false positives from moronic extensions that append to the IE UA
36 var ie6_bugs
= /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec( clientPC
) && parseFloat( RegExp
.$1 ) <= 6.0 );
38 // Global external objects used by this script.
39 /*extern ta, stylepath, skin */
41 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
44 if (!window
.onloadFuncts
) {
45 var onloadFuncts
= [];
48 function addOnloadHook( hookFunct
) {
49 // Allows add-on scripts to add onload functions
50 if( !doneOnloadHook
) {
51 onloadFuncts
[onloadFuncts
.length
] = hookFunct
;
53 hookFunct(); // bug in MSIE script loading
57 function hookEvent( hookName
, hookFunct
) {
58 addHandler( window
, hookName
, hookFunct
);
61 function importScript( page
) {
62 // TODO: might want to introduce a utility function to match wfUrlencode() in PHP
63 var uri
= wgScript
+ '?title=' +
64 encodeURIComponent(page
.replace(/ /g,'_')).replace(/%2F
/ig,'/').replace(/%3A/ig,':') +
65 '&action
=raw
&ctype
=text
/javascript
';
66 return importScriptURI( uri );
69 var loadedScripts = {}; // included-scripts tracker
70 function importScriptURI( url ) {
71 if ( loadedScripts[url] ) {
74 loadedScripts[url] = true;
75 var s = document.createElement( 'script
' );
76 s.setAttribute( 'src
', url );
77 s.setAttribute( 'type
', 'text
/javascript
' );
78 document.getElementsByTagName('head
')[0].appendChild( s );
82 function importStylesheet( page ) {
83 return importStylesheetURI( wgScript + '?action
=raw
&ctype
=text
/css&title=' + encodeURIComponent( page.replace(/ /g
,'_') ) );
86 function importStylesheetURI( url
, media
) {
87 var l
= document
.createElement( 'link' );
94 document
.getElementsByTagName('head')[0].appendChild( l
);
98 function appendCSS( text
) {
99 var s
= document
.createElement( 'style' );
101 s
.rel
= 'stylesheet';
102 if ( s
.styleSheet
) {
103 s
.styleSheet
.cssText
= text
; // IE
105 s
.appendChild( document
.createTextNode( text
+ '' ) ); // Safari sometimes borks on null
107 document
.getElementsByTagName('head')[0].appendChild( s
);
111 // Special stylesheet links for Monobook only (see bug 14717)
112 if ( typeof stylepath
!== 'undefined' && skin
=== 'monobook' ) {
114 importStylesheetURI( stylepath
+ '/' + skin
+ '/Opera6Fixes.css' );
115 } else if ( opera7_bugs
) {
116 importStylesheetURI( stylepath
+ '/' + skin
+ '/Opera7Fixes.css' );
117 } else if ( opera95_bugs
) {
118 importStylesheetURI( stylepath
+ '/' + skin
+ '/Opera9Fixes.css' );
119 } else if ( ff2_bugs
) {
120 importStylesheetURI( stylepath
+ '/' + skin
+ '/FF2Fixes.css' );
125 if ( wgBreakFrames
) {
126 // Un-trap us from framesets
127 if ( window
.top
!= window
) {
128 window
.top
.location
= window
.location
;
132 function showTocToggle() {
133 if ( document
.createTextNode
) {
134 // Uses DOM calls to avoid document.write + XHTML issues
136 var linkHolder
= document
.getElementById( 'toctitle' );
137 var existingLink
= document
.getElementById( 'togglelink' );
138 if ( !linkHolder
|| existingLink
) {
139 // Don't add the toggle link twice
143 var outerSpan
= document
.createElement( 'span' );
144 outerSpan
.className
= 'toctoggle';
146 var toggleLink
= document
.createElement( 'a' );
147 toggleLink
.id
= 'togglelink';
148 toggleLink
.className
= 'internal';
149 toggleLink
.href
= '#';
150 addClickHandler( toggleLink
, function( evt
) { toggleToc(); return killEvt( evt
); } );
152 toggleLink
.appendChild( document
.createTextNode( tocHideText
) );
154 outerSpan
.appendChild( document
.createTextNode( '[' ) );
155 outerSpan
.appendChild( toggleLink
);
156 outerSpan
.appendChild( document
.createTextNode( ']' ) );
158 linkHolder
.appendChild( document
.createTextNode( ' ' ) );
159 linkHolder
.appendChild( outerSpan
);
161 var cookiePos
= document
.cookie
.indexOf( "hidetoc=" );
162 if ( cookiePos
> -1 && document
.cookie
.charAt( cookiePos
+ 8 ) === 1 ) {
168 function changeText( el
, newText
) {
169 // Safari work around
170 if ( el
.innerText
) {
171 el
.innerText
= newText
;
172 } else if ( el
.firstChild
&& el
.firstChild
.nodeValue
) {
173 el
.firstChild
.nodeValue
= newText
;
177 function killEvt( evt
) {
178 evt
= evt
|| window
.event
|| window
.Event
; // W3C, IE, Netscape
179 if ( typeof ( evt
.preventDefault
) !== 'undefined' ) {
180 evt
.preventDefault(); // Don't follow the link
181 evt
.stopPropagation();
183 evt
.cancelBubble
= true; // IE
185 return false; // Don't follow the link (IE)
188 function toggleToc() {
189 var tocmain
= document
.getElementById( 'toc' );
190 var toc
= document
.getElementById('toc').getElementsByTagName('ul')[0];
191 var toggleLink
= document
.getElementById( 'togglelink' );
193 if ( toc
&& toggleLink
&& toc
.style
.display
=== 'none' ) {
194 changeText( toggleLink
, tocHideText
);
195 toc
.style
.display
= 'block';
196 document
.cookie
= "hidetoc=0";
197 tocmain
.className
= 'toc';
199 changeText( toggleLink
, tocShowText
);
200 toc
.style
.display
= 'none';
201 document
.cookie
= "hidetoc=1";
202 tocmain
.className
= 'toc tochidden';
207 var mwEditButtons
= [];
208 var mwCustomEditButtons
= []; // eg to add in MediaWiki:Common.js
210 function escapeQuotes( text
) {
211 var re
= new RegExp( "'", "g" );
212 text
= text
.replace( re
, "\\'" );
213 re
= new RegExp( "\\n", "g" );
214 text
= text
.replace( re
, "\\n" );
215 return escapeQuotesHTML( text
);
218 function escapeQuotesHTML( text
) {
219 var re
= new RegExp( '&', "g" );
220 text
= text
.replace( re
, "&" );
221 re
= new RegExp( '"', "g" );
222 text
= text
.replace( re
, """ );
223 re
= new RegExp( '<', "g" );
224 text
= text
.replace( re
, "<" );
225 re
= new RegExp( '>', "g" );
226 text
= text
.replace( re
, ">" );
231 * Set the accesskey prefix based on browser detection.
233 var tooltipAccessKeyPrefix
= 'alt-';
235 tooltipAccessKeyPrefix
= 'shift-esc-';
236 } else if ( is_chrome
) {
237 tooltipAccessKeyPrefix
= is_chrome_mac
? 'ctrl-option-' : 'alt-';
238 } else if ( !is_safari_win
&& is_safari
&& webkit_version
> 526 ) {
239 tooltipAccessKeyPrefix
= 'ctrl-alt-';
240 } else if ( !is_safari_win
&& ( is_safari
241 || clientPC
.indexOf('mac') !== -1
242 || clientPC
.indexOf('konqueror') !== -1 ) ) {
243 tooltipAccessKeyPrefix
= 'ctrl-';
244 } else if ( is_ff2
) {
245 tooltipAccessKeyPrefix
= 'alt-shift-';
247 var tooltipAccessKeyRegexp
= /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/;
250 * Add the appropriate prefix to the accesskey shown in the tooltip.
251 * If the nodeList parameter is given, only those nodes are updated;
252 * otherwise, all the nodes that will probably have accesskeys by
253 * default are updated.
255 * @param Array nodeList -- list of elements to update
257 function updateTooltipAccessKeys( nodeList
) {
259 // Rather than scan all links on the whole page, we can just scan these
260 // containers which contain the relevant links. This is really just an
261 // optimization technique.
262 var linkContainers
= [
263 'column-one', // Monobook and Modern
264 'mw-head', 'mw-panel', 'p-logo' // Vector
266 for ( var i
in linkContainers
) {
267 var linkContainer
= document
.getElementById( linkContainers
[i
] );
268 if ( linkContainer
) {
269 updateTooltipAccessKeys( linkContainer
.getElementsByTagName( 'a' ) );
272 // these are rare enough that no such optimization is needed
273 updateTooltipAccessKeys( document
.getElementsByTagName( 'input' ) );
274 updateTooltipAccessKeys( document
.getElementsByTagName( 'label' ) );
278 for ( var i
= 0; i
< nodeList
.length
; i
++ ) {
279 var element
= nodeList
[i
];
280 var tip
= element
.getAttribute( 'title' );
281 if ( tip
&& tooltipAccessKeyRegexp
.exec( tip
) ) {
282 tip
= tip
.replace(tooltipAccessKeyRegexp
,
283 '[' + tooltipAccessKeyPrefix
+ "$5]");
284 element
.setAttribute( 'title', tip
);
290 * Add a link to one of the portlet menus on the page, including:
292 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
293 * p-personal: Personal tools (shown at the top right of the page in Monobook)
294 * p-navigation: Navigation
297 * This function exists for the convenience of custom JS authors. All
298 * but the first three parameters are optional, though providing at
299 * least an id and a tooltip is recommended.
301 * By default the new link will be added to the end of the list. To
302 * add the link before a given existing item, pass the DOM node of
303 * that item (easily obtained with document.getElementById()) as the
304 * nextnode parameter; to add the link _after_ an existing item, pass
305 * the node's nextSibling instead.
307 * @param String portlet -- id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
308 * @param String href -- link URL
309 * @param String text -- link text (will be automatically lowercased by CSS for p-cactions in Monobook)
310 * @param String id -- id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
311 * @param String tooltip -- text to show when hovering over the link, without accesskey suffix
312 * @param String accesskey -- accesskey to activate this link (one character, try to avoid conflicts)
313 * @param Node nextnode -- the DOM node before which the new item should be added, should be another item in the same list
315 * @return Node -- the DOM node of the new item (an LI element) or null
317 function addPortletLink( portlet
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
318 var root
= document
.getElementById( portlet
);
322 var node
= root
.getElementsByTagName( 'ul' )[0];
327 // unhide portlet if it was hidden before
328 root
.className
= root
.className
.replace( /(^| )emptyPortlet( |$)/, "$2" );
330 var span
= document
.createElement( 'span' );
331 span
.appendChild( document
.createTextNode( text
) );
333 var link
= document
.createElement( 'a' );
334 link
.appendChild( span
);
337 var item
= document
.createElement( 'li' );
338 item
.appendChild( link
);
344 link
.setAttribute( 'accesskey', accesskey
);
345 tooltip
+= ' [' + accesskey
+ ']';
348 link
.setAttribute( 'title', tooltip
);
350 if ( accesskey
&& tooltip
) {
351 updateTooltipAccessKeys( new Array( link
) );
354 if ( nextnode
&& nextnode
.parentNode
=== node
) {
355 node
.insertBefore( item
, nextnode
);
357 node
.appendChild( item
); // IE compatibility (?)
363 function getInnerText( el
) {
364 if ( typeof el
=== 'string' ) {
367 if ( typeof el
=== 'undefined' ) {
370 if ( el
.textContent
) {
371 return el
.textContent
; // not needed but it is faster
373 if ( el
.innerText
) {
374 return el
.innerText
; // IE doesn't have textContent
378 var cs
= el
.childNodes
;
380 for ( var i
= 0; i
< l
; i
++ ) {
381 switch ( cs
[i
].nodeType
) {
382 case 1: // ELEMENT_NODE
383 str
+= ts_getInnerText( cs
[i
] );
386 str
+= cs
[i
].nodeValue
;
393 /* Dummy for deprecated function */
395 function akeytt( doId
) {
401 function setupCheckboxShiftClick() {
403 lastCheckbox
= undefined;
404 var inputs
= document
.getElementsByTagName( 'input' );
405 addCheckboxClickHandlers( inputs
);
408 function addCheckboxClickHandlers( inputs
, start
) {
413 var finish
= start
+ 250;
414 if ( finish
> inputs
.length
) {
415 finish
= inputs
.length
;
418 for ( var i
= start
; i
< finish
; i
++ ) {
420 if ( !cb
.type
|| cb
.type
.toLowerCase() !== 'checkbox' ) {
423 var end
= checkboxes
.length
;
424 checkboxes
[end
] = cb
;
426 addClickHandler( cb
, checkboxClickHandler
);
429 if ( finish
< inputs
.length
) {
430 setTimeout( function() {
431 addCheckboxClickHandlers( inputs
, finish
);
436 function checkboxClickHandler( e
) {
437 if ( typeof e
=== 'undefined' ) {
440 if ( !e
.shiftKey
|| lastCheckbox
=== undefined ) {
441 lastCheckbox
= this.index
;
444 var endState
= this.checked
;
446 if ( this.index
< lastCheckbox
) {
447 start
= this.index
+ 1;
448 finish
= lastCheckbox
;
450 start
= lastCheckbox
;
451 finish
= this.index
- 1;
453 for ( var i
= start
; i
<= finish
; ++i
) {
454 checkboxes
[i
].checked
= endState
;
455 if( i
> start
&& typeof checkboxes
[i
].onchange
=== 'function' ) {
456 checkboxes
[i
].onchange(); // fire triggers
459 lastCheckbox
= this.index
;
465 Written by Jonathan Snook, http://www.snook.ca/jonathan
466 Add-ons by Robert Nyman, http://www.robertnyman.com
467 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
468 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
470 function getElementsByClassName( oElm
, strTagName
, oClassNames
) {
471 var arrReturnElements
= new Array();
472 if ( typeof( oElm
.getElementsByClassName
) === 'function' ) {
473 /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
474 var arrNativeReturn
= oElm
.getElementsByClassName( oClassNames
);
475 if ( strTagName
=== '*' ) {
476 return arrNativeReturn
;
478 for ( var h
= 0; h
< arrNativeReturn
.length
; h
++ ) {
479 if( arrNativeReturn
[h
].tagName
.toLowerCase() === strTagName
.toLowerCase() ) {
480 arrReturnElements
[arrReturnElements
.length
] = arrNativeReturn
[h
];
483 return arrReturnElements
;
485 var arrElements
= ( strTagName
=== '*' && oElm
.all
) ? oElm
.all
: oElm
.getElementsByTagName( strTagName
);
486 var arrRegExpClassNames
= new Array();
487 if( typeof oClassNames
=== 'object' ) {
488 for( var i
= 0; i
< oClassNames
.length
; i
++ ) {
489 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
490 new RegExp("(^|\\s)" + oClassNames
[i
].replace(/\-/g, "\\-") + "(\\s|$)");
493 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
494 new RegExp("(^|\\s)" + oClassNames
.replace(/\-/g, "\\-") + "(\\s|$)");
498 for( var j
= 0; j
< arrElements
.length
; j
++ ) {
499 oElement
= arrElements
[j
];
501 for( var k
= 0; k
< arrRegExpClassNames
.length
; k
++ ) {
502 if( !arrRegExpClassNames
[k
].test( oElement
.className
) ) {
508 arrReturnElements
[arrReturnElements
.length
] = oElement
;
511 return ( arrReturnElements
);
514 function redirectToFragment( fragment
) {
515 var match
= navigator
.userAgent
.match(/AppleWebKit\/(\d+)/);
517 var webKitVersion
= parseInt( match
[1] );
518 if ( webKitVersion
< 420 ) {
519 // Released Safari w/ WebKit 418.9.1 messes up horribly
520 // Nightlies of 420+ are ok
524 if ( window
.location
.hash
=== '' ) {
525 window
.location
.hash
= fragment
;
527 // Mozilla needs to wait until after load, otherwise the window doesn't
528 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
529 // There's no obvious way to detect this programmatically, so we use
530 // version-testing. If Firefox fixes the bug, they'll jump twice, but
531 // better twice than not at all, so make the fix hit future versions as
534 addOnloadHook(function() {
535 if ( window
.location
.hash
=== fragment
) {
536 window
.location
.hash
= fragment
;
544 * Table sorting script based on one (c) 1997-2006 Stuart Langridge and Joost
546 * http://www.joostdevalk.nl/code/sortable-table/
547 * http://www.kryogenix.org/code/browser/sorttable/
549 * @todo don't break on colspans/rowspans (bug 8028)
550 * @todo language-specific digit grouping/decimals (bug 8063)
551 * @todo support all accepted date formats (bug 8226)
554 var ts_image_path
= stylepath
+ '/common/images/';
555 var ts_image_up
= 'sort_up.gif';
556 var ts_image_down
= 'sort_down.gif';
557 var ts_image_none
= 'sort_none.gif';
558 var ts_europeandate
= wgContentLanguage
!== 'en'; // The non-American-inclined can change to "true"
559 var ts_alternate_row_colors
= false;
560 var ts_number_transform_table
;
563 function sortables_init() {
565 // Find all tables with class sortable and make them sortable
566 var tables
= getElementsByClassName( document
, 'table', 'sortable' );
567 for ( var ti
= 0; ti
< tables
.length
; ti
++ ) {
568 if ( !tables
[ti
].id
) {
569 tables
[ti
].setAttribute( 'id', 'sortable_table_id_' + idnum
);
572 ts_makeSortable( tables
[ti
] );
576 function ts_makeSortable( table
) {
578 if ( table
.rows
&& table
.rows
.length
> 0 ) {
579 if ( table
.tHead
&& table
.tHead
.rows
.length
> 0 ) {
580 firstRow
= table
.tHead
.rows
[table
.tHead
.rows
.length
-1];
582 firstRow
= table
.rows
[0];
589 // We have a first row: assume it's the header, and make its contents clickable links
590 for ( var i
= 0; i
< firstRow
.cells
.length
; i
++ ) {
591 var cell
= firstRow
.cells
[i
];
592 if ( (' ' + cell
.className
+ ' ').indexOf(' unsortable ') === -1 ) {
593 cell
.innerHTML
+= '<a href="#" class="sortheader" '
594 + 'onclick="ts_resortTable(this);return false;">'
595 + '<span class="sortarrow">'
599 + '" alt="↓"/></span></a>';
602 if ( ts_alternate_row_colors
) {
603 ts_alternate( table
);
607 function ts_getInnerText( el
) {
608 return getInnerText( el
);
611 function ts_resortTable( lnk
) {
613 var span
= lnk
.getElementsByTagName('span')[0];
615 var td
= lnk
.parentNode
;
616 var tr
= td
.parentNode
;
617 var column
= td
.cellIndex
;
619 var table
= tr
.parentNode
;
620 while ( table
&& !( table
.tagName
&& table
.tagName
.toLowerCase() === 'table' ) ) {
621 table
= table
.parentNode
;
627 if ( table
.rows
.length
<= 1 ) {
631 // Generate the number transform table if it's not done already
632 if ( ts_number_transform_table
=== undefined ) {
633 ts_initTransformTable();
636 // Work out a type for the column
637 // Skip the first row if that's where the headings are
638 var rowStart
= ( table
.tHead
&& table
.tHead
.rows
.length
> 0 ? 0 : 1 );
640 if (rowStart
=== 0 && table
.tBodies
) {
641 for (var i
=0; i
< table
.tBodies
.length
; i
++ ) {
642 bodyRows
+= table
.tBodies
[i
].rows
.length
;
644 if (bodyRows
< table
.rows
.length
)
649 for ( var i
= rowStart
; i
< table
.rows
.length
; i
++ ) {
650 if ( table
.rows
[i
].cells
.length
> column
) {
651 itm
= ts_getInnerText(table
.rows
[i
].cells
[column
]);
652 itm
= itm
.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '');
659 // TODO: bug 8226, localised date formats
660 var sortfn
= ts_sort_generic
;
661 var preprocessor
= ts_toLowerCase
;
662 if ( /^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/.test( itm
) ) {
663 preprocessor
= ts_dateToSortKey
;
664 } else if ( /^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/.test( itm
) ) {
665 preprocessor
= ts_dateToSortKey
;
666 } else if ( /^\d\d[\/.-]\d\d[\/.-]\d\d$/.test( itm
) ) {
667 preprocessor
= ts_dateToSortKey
;
668 // (minus sign)([pound dollar euro yen currency]|cents)
669 } else if ( /(^([-\u2212] *)?[\u00a3$\u20ac\u00a4\u00a5]|\u00a2$)/.test( itm
) ) {
670 preprocessor
= ts_currencyToSortKey
;
671 } else if ( ts_number_regex
.test( itm
) ) {
672 preprocessor
= ts_parseFloat
;
675 var reverse
= ( span
.getAttribute( 'sortdir' ) === 'down' );
677 var newRows
= new Array();
678 var staticRows
= new Array();
679 for ( var j
= rowStart
; j
< table
.rows
.length
; j
++ ) {
680 var row
= table
.rows
[j
];
681 if( (' ' + row
.className
+ ' ').indexOf(' unsortable ') < 0 ) {
682 var keyText
= ts_getInnerText( row
.cells
[column
] );
683 if( keyText
=== undefined ) {
686 var oldIndex
= ( reverse
? -j
: j
);
687 var preprocessed
= preprocessor( keyText
.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '') );
689 newRows
[newRows
.length
] = new Array( row
, preprocessed
, oldIndex
);
691 staticRows
[staticRows
.length
] = new Array( row
, false, j
-rowStart
);
695 newRows
.sort( sortfn
);
699 arrowHTML
= '<img src="' + ts_image_path
+ ts_image_down
+ '" alt="↓"/>';
701 span
.setAttribute( 'sortdir', 'up' );
703 arrowHTML
= '<img src="' + ts_image_path
+ ts_image_up
+ '" alt="↑"/>';
704 span
.setAttribute( 'sortdir', 'down' );
707 for ( var i
= 0; i
< staticRows
.length
; i
++ ) {
708 var row
= staticRows
[i
];
709 newRows
.splice( row
[2], 0, row
);
712 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
713 // don't do sortbottom rows
714 for ( var i
= 0; i
< newRows
.length
; i
++ ) {
715 if ( ( ' ' + newRows
[i
][0].className
+ ' ').indexOf(' sortbottom ') === -1 ) {
716 table
.tBodies
[0].appendChild( newRows
[i
][0] );
719 // do sortbottom rows only
720 for ( var i
= 0; i
< newRows
.length
; i
++ ) {
721 if ( ( ' ' + newRows
[i
][0].className
+ ' ').indexOf(' sortbottom ') !== -1 ) {
722 table
.tBodies
[0].appendChild( newRows
[i
][0] );
726 // Delete any other arrows there may be showing
727 var spans
= getElementsByClassName( tr
, 'span', 'sortarrow' );
728 for ( var i
= 0; i
< spans
.length
; i
++ ) {
729 spans
[i
].innerHTML
= '<img src="' + ts_image_path
+ ts_image_none
+ '" alt="↓"/>';
731 span
.innerHTML
= arrowHTML
;
733 if ( ts_alternate_row_colors
) {
734 ts_alternate( table
);
738 function ts_initTransformTable() {
739 if ( typeof wgSeparatorTransformTable
=== 'undefined'
740 || ( wgSeparatorTransformTable
[0] === '' && wgDigitTransformTable
[2] === '' ) )
742 var digitClass
= "[0-9,.]";
743 ts_number_transform_table
= false;
745 ts_number_transform_table
= {};
746 // Unpack the transform table
748 var ascii
= wgSeparatorTransformTable
[0].split("\t");
749 var localised
= wgSeparatorTransformTable
[1].split("\t");
750 for ( var i
= 0; i
< ascii
.length
; i
++ ) {
751 ts_number_transform_table
[localised
[i
]] = ascii
[i
];
754 ascii
= wgDigitTransformTable
[0].split("\t");
755 localised
= wgDigitTransformTable
[1].split("\t");
756 for ( var i
= 0; i
< ascii
.length
; i
++ ) {
757 ts_number_transform_table
[localised
[i
]] = ascii
[i
];
760 // Construct regex for number identification
761 var digits
= ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\\.'];
762 var maxDigitLength
= 1;
763 for ( var digit
in ts_number_transform_table
) {
764 // Escape regex metacharacters
766 digit
.replace( /[\\\\$\*\+\?\.\(\)\|\{\}\[\]\-]/,
767 function( s
) { return '\\' + s
; } )
769 if ( digit
.length
> maxDigitLength
) {
770 maxDigitLength
= digit
.length
;
773 if ( maxDigitLength
> 1 ) {
774 var digitClass
= '[' + digits
.join( '', digits
) + ']';
776 var digitClass
= '(' + digits
.join( '|', digits
) + ')';
780 // We allow a trailing percent sign, which we just strip. This works fine
781 // if percents and regular numbers aren't being mixed.
782 ts_number_regex
= new RegExp(
784 "[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?" + // Fortran-style scientific
786 "[-+\u2212]?" + digitClass
+ "+%?" + // Generic localised
791 function ts_toLowerCase( s
) {
792 return s
.toLowerCase();
795 function ts_dateToSortKey( date
) {
796 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
797 if ( date
.length
=== 11 ) {
798 switch ( date
.substr( 3, 3 ).toLowerCase() ) {
835 // default: var month = '00';
837 return date
.substr( 7, 4 ) + month
+ date
.substr( 0, 2 );
838 } else if ( date
.length
=== 10 ) {
839 if ( ts_europeandate
=== false ) {
840 return date
.substr( 6, 4 ) + date
.substr( 0, 2 ) + date
.substr( 3, 2 );
842 return date
.substr( 6, 4 ) + date
.substr( 3, 2 ) + date
.substr( 0, 2 );
844 } else if ( date
.length
=== 8 ) {
845 var yr
= date
.substr( 6, 2 );
846 if ( parseInt( yr
) < 50 ) {
851 if ( ts_europeandate
=== true ) {
852 return yr
+ date
.substr( 3, 2 ) + date
.substr( 0, 2 );
854 return yr
+ date
.substr( 0, 2 ) + date
.substr( 3, 2 );
860 function ts_parseFloat( s
) {
864 if ( ts_number_transform_table
!== false ) {
867 for ( var p
= 0; p
< s
.length
; p
++ ) {
869 if ( c
in ts_number_transform_table
) {
870 newNum
+= ts_number_transform_table
[c
];
877 var num
= parseFloat( s
.replace(/[, ]/g, '').replace("\u2212", '-') );
878 return ( isNaN( num
) ? -Infinity
: num
);
881 function ts_currencyToSortKey( s
) {
882 return ts_parseFloat(s
.replace(/[^-\u22120-9.,]/g,''));
885 function ts_sort_generic( a
, b
) {
886 return a
[1] < b
[1] ? -1 : a
[1] > b
[1] ? 1 : a
[2] - b
[2];
889 function ts_alternate( table
) {
890 // Take object table and get all it's tbodies.
891 var tableBodies
= table
.getElementsByTagName( 'tbody' );
892 // Loop through these tbodies
893 for ( var i
= 0; i
< tableBodies
.length
; i
++ ) {
894 // Take the tbody, and get all it's rows
895 var tableRows
= tableBodies
[i
].getElementsByTagName( 'tr' );
896 // Loop through these rows
897 // Start at 1 because we want to leave the heading row untouched
898 for ( var j
= 0; j
< tableRows
.length
; j
++ ) {
899 // Check if j is even, and apply classes for both possible results
900 var oldClasses
= tableRows
[j
].className
.split(' ');
901 var newClassName
= '';
902 for ( var k
= 0; k
< oldClasses
.length
; k
++ ) {
903 if ( oldClasses
[k
] !== '' && oldClasses
[k
] !== 'even' && oldClasses
[k
] !== 'odd' ) {
904 newClassName
+= oldClasses
[k
] + ' ';
907 tableRows
[j
].className
= newClassName
+ ( j
% 2 === 0 ? 'even' : 'odd' );
913 * End of table sorting code
918 * Add a cute little box at the top of the screen to inform the user of
919 * something, replacing any preexisting message.
921 * @param String -or- Dom Object message HTML to be put inside the right div
922 * @param String className Used in adding a class; should be different for each
923 * call to allow CSS/JS to hide different boxes. null = no class used.
924 * @return Boolean True on success, false on failure
926 function jsMsg( message
, className
) {
927 if ( !document
.getElementById
) {
930 // We special-case skin structures provided by the software. Skins that
931 // choose to abandon or significantly modify our formatting can just define
932 // an mw-js-message div to start with.
933 var messageDiv
= document
.getElementById( 'mw-js-message' );
935 messageDiv
= document
.createElement( 'div' );
936 if ( document
.getElementById( 'column-content' )
937 && document
.getElementById( 'content' ) ) {
938 // MonoBook, presumably
939 document
.getElementById( 'content' ).insertBefore(
941 document
.getElementById( 'content' ).firstChild
943 } else if ( document
.getElementById( 'content' )
944 && document
.getElementById( 'article' ) ) {
945 // Non-Monobook but still recognizable (old-style)
946 document
.getElementById( 'article').insertBefore(
948 document
.getElementById( 'article' ).firstChild
955 messageDiv
.setAttribute( 'id', 'mw-js-message' );
956 messageDiv
.style
.display
= 'block';
958 messageDiv
.setAttribute( 'class', 'mw-js-message-' + className
);
961 if ( typeof message
=== 'object' ) {
962 while ( messageDiv
.hasChildNodes() ) { // Remove old content
963 messageDiv
.removeChild( messageDiv
.firstChild
);
965 messageDiv
.appendChild( message
); // Append new content
967 messageDiv
.innerHTML
= message
;
973 * Inject a cute little progress spinner after the specified element
975 * @param element Element to inject after
976 * @param id Identifier string (for use with removeSpinner(), below)
978 function injectSpinner( element
, id
) {
979 var spinner
= document
.createElement( 'img' );
980 spinner
.id
= 'mw-spinner-' + id
;
981 spinner
.src
= stylepath
+ '/common/images/spinner.gif';
982 spinner
.alt
= spinner
.title
= '...';
983 if( element
.nextSibling
) {
984 element
.parentNode
.insertBefore( spinner
, element
.nextSibling
);
986 element
.parentNode
.appendChild( spinner
);
991 * Remove a progress spinner added with injectSpinner()
993 * @param id Identifier string
995 function removeSpinner( id
) {
996 var spinner
= document
.getElementById( 'mw-spinner-' + id
);
998 spinner
.parentNode
.removeChild( spinner
);
1002 function runOnloadHook() {
1003 // don't run anything below this for non-dom browsers
1004 if ( doneOnloadHook
|| !( document
.getElementById
&& document
.getElementsByTagName
) ) {
1008 // set this before running any hooks, since any errors below
1009 // might cause the function to terminate prematurely
1010 doneOnloadHook
= true;
1012 updateTooltipAccessKeys( null );
1013 setupCheckboxShiftClick();
1016 // Run any added-on functions
1017 for ( var i
= 0; i
< onloadFuncts
.length
; i
++ ) {
1023 * Add an event handler to an element
1025 * @param Element element Element to add handler to
1026 * @param String attach Event to attach to
1027 * @param callable handler Event handler callback
1029 function addHandler( element
, attach
, handler
) {
1030 if( window
.addEventListener
) {
1031 element
.addEventListener( attach
, handler
, false );
1032 } else if( window
.attachEvent
) {
1033 element
.attachEvent( 'on' + attach
, handler
);
1038 * Add a click event handler to an element
1040 * @param Element element Element to add handler to
1041 * @param callable handler Event handler callback
1043 function addClickHandler( element
, handler
) {
1044 addHandler( element
, 'click', handler
);
1048 * Removes an event handler from an element
1050 * @param Element element Element to remove handler from
1051 * @param String remove Event to remove
1052 * @param callable handler Event handler callback to remove
1054 function removeHandler( element
, remove
, handler
) {
1055 if( window
.removeEventListener
) {
1056 element
.removeEventListener( remove
, handler
, false );
1057 } else if( window
.detachEvent
) {
1058 element
.detachEvent( 'on' + remove
, handler
);
1061 // note: all skins should call runOnloadHook() at the end of html output,
1062 // so the below should be redundant. It's there just in case.
1063 hookEvent( 'load', runOnloadHook
);
1066 importScriptURI( stylepath
+ '/common/IEFixes.js' );