1 // MediaWiki JavaScript support functions
3 window
.clientPC
= navigator
.userAgent
.toLowerCase(); // Get client info
4 window
.is_gecko
= /gecko/.test( clientPC
) &&
5 !/khtml|spoofer|netscape\/7\.0/.test(clientPC
);
7 window
.is_safari
= window
.is_safari_win
= window
.webkit_version
=
8 window
.is_chrome
= window
.is_chrome_mac
= false;
9 window
.webkit_match
= clientPC
.match(/applewebkit\/(\d+)/);
11 window
.is_safari
= clientPC
.indexOf('applewebkit') != -1 &&
12 clientPC
.indexOf('spoofer') == -1;
13 window
.is_safari_win
= is_safari
&& clientPC
.indexOf('windows') != -1;
14 window
.webkit_version
= parseInt(webkit_match
[1]);
15 // Tests for chrome here, to avoid breaking old scripts safari left alone
16 // This is here for accesskeys
17 window
.is_chrome
= clientPC
.indexOf('chrome') !== -1 &&
18 clientPC
.indexOf('spoofer') === -1;
19 window
.is_chrome_mac
= is_chrome
&& clientPC
.indexOf('mac') !== -1
22 // For accesskeys; note that FF3+ is included here!
23 window
.is_ff2
= /firefox\/[2-9]|minefield\/3/.test( clientPC
);
24 window
.ff2_bugs
= /firefox\/2/.test( clientPC
);
25 // These aren't used here, but some custom scripts rely on them
26 window
.is_ff2_win
= is_ff2
&& clientPC
.indexOf('windows') != -1;
27 window
.is_ff2_x11
= is_ff2
&& clientPC
.indexOf('x11') != -1;
29 window
.is_opera
= window
.is_opera_preseven
= window
.is_opera_95
=
30 window
.opera6_bugs
= window
.opera7_bugs
= window
.opera95_bugs
= false;
31 if (clientPC
.indexOf('opera') != -1) {
32 window
.is_opera
= true;
33 window
.is_opera_preseven
= window
.opera
&& !document
.childNodes
;
34 window
.is_opera_seven
= window
.opera
&& document
.childNodes
;
35 window
.is_opera_95
= /opera\/(9\.[5-9]|[1-9][0-9])/.test( clientPC
);
36 window
.opera6_bugs
= is_opera_preseven
;
37 window
.opera7_bugs
= is_opera_seven
&& !is_opera_95
;
38 window
.opera95_bugs
= /opera\/(9\.5)/.test( clientPC
);
40 // As recommended by <http://msdn.microsoft.com/en-us/library/ms537509.aspx>,
41 // avoiding false positives from moronic extensions that append to the IE UA
43 window
.ie6_bugs
= false;
44 if ( /msie ([0-9]{1,}[\.0-9]{0,})/.exec( clientPC
) != null
45 && parseFloat( RegExp
.$1 ) <= 6.0 ) {
49 // Global external objects used by this script.
50 /*extern ta, stylepath, skin */
52 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
53 window
.doneOnloadHook
= undefined;
55 if (!window
.onloadFuncts
) {
56 window
.onloadFuncts
= [];
59 window
.addOnloadHook = function( hookFunct
) {
60 // Allows add-on scripts to add onload functions
61 if( !doneOnloadHook
) {
62 onloadFuncts
[onloadFuncts
.length
] = hookFunct
;
64 hookFunct(); // bug in MSIE script loading
68 window
.importScript = function( page
) {
69 // TODO: might want to introduce a utility function to match wfUrlencode() in PHP
70 var uri
= wgScript
+ '?title=' +
71 encodeURIComponent(page
.replace(/ /g,'_')).replace(/%2F
/ig,'/').replace(/%3A/ig,':') +
72 '&action
=raw
&ctype
=text
/javascript
';
73 return importScriptURI( uri );
76 window.loadedScripts = {}; // included-scripts tracker
77 window.importScriptURI = function( url ) {
78 if ( loadedScripts[url] ) {
81 loadedScripts[url] = true;
82 var s = document.createElement( 'script
' );
83 s.setAttribute( 'src
', url );
84 s.setAttribute( 'type
', 'text
/javascript
' );
85 document.getElementsByTagName('head
')[0].appendChild( s );
89 window.importStylesheet = function( page ) {
90 return importStylesheetURI( wgScript + '?action
=raw
&ctype
=text
/css&title=' + encodeURIComponent( page.replace(/ /g
,'_') ) );
93 window
.importStylesheetURI = function( url
, media
) {
94 var l
= document
.createElement( 'link' );
101 document
.getElementsByTagName('head')[0].appendChild( l
);
105 window
.appendCSS = function( text
) {
106 var s
= document
.createElement( 'style' );
108 s
.rel
= 'stylesheet';
109 if ( s
.styleSheet
) {
110 s
.styleSheet
.cssText
= text
; // IE
112 s
.appendChild( document
.createTextNode( text
+ '' ) ); // Safari sometimes borks on null
114 document
.getElementsByTagName('head')[0].appendChild( s
);
118 // Special stylesheet links for Monobook only (see bug 14717)
119 if ( typeof stylepath
!= 'undefined' && skin
== 'monobook' ) {
121 importStylesheetURI( stylepath
+ '/' + skin
+ '/Opera6Fixes.css' );
122 } else if ( opera7_bugs
) {
123 importStylesheetURI( stylepath
+ '/' + skin
+ '/Opera7Fixes.css' );
124 } else if ( opera95_bugs
) {
125 importStylesheetURI( stylepath
+ '/' + skin
+ '/Opera9Fixes.css' );
126 } else if ( ff2_bugs
) {
127 importStylesheetURI( stylepath
+ '/' + skin
+ '/FF2Fixes.css' );
132 if ( 'wgBreakFrames' in window
&& window
.wgBreakFrames
) {
133 // Un-trap us from framesets
134 if ( window
.top
!= window
) {
135 window
.top
.location
= window
.location
;
139 window
.changeText = function( el
, newText
) {
140 // Safari work around
141 if ( el
.innerText
) {
142 el
.innerText
= newText
;
143 } else if ( el
.firstChild
&& el
.firstChild
.nodeValue
) {
144 el
.firstChild
.nodeValue
= newText
;
148 window
.killEvt = function( evt
) {
149 evt
= evt
|| window
.event
|| window
.Event
; // W3C, IE, Netscape
150 if ( typeof ( evt
.preventDefault
) != 'undefined' ) {
151 evt
.preventDefault(); // Don't follow the link
152 evt
.stopPropagation();
154 evt
.cancelBubble
= true; // IE
156 return false; // Don't follow the link (IE)
159 window
.mwEditButtons
= [];
160 window
.mwCustomEditButtons
= []; // eg to add in MediaWiki:Common.js
162 window
.escapeQuotes = function( text
) {
163 var re
= new RegExp( "'", "g" );
164 text
= text
.replace( re
, "\\'" );
165 re
= new RegExp( "\\n", "g" );
166 text
= text
.replace( re
, "\\n" );
167 return escapeQuotesHTML( text
);
170 window
.escapeQuotesHTML = function( text
) {
171 var re
= new RegExp( '&', "g" );
172 text
= text
.replace( re
, "&" );
173 re
= new RegExp( '"', "g" );
174 text
= text
.replace( re
, """ );
175 re
= new RegExp( '<', "g" );
176 text
= text
.replace( re
, "<" );
177 re
= new RegExp( '>', "g" );
178 text
= text
.replace( re
, ">" );
183 * Set the accesskey prefix based on browser detection.
185 window
.tooltipAccessKeyPrefix
= 'alt-';
187 tooltipAccessKeyPrefix
= 'shift-esc-';
188 } else if ( is_chrome
) {
189 tooltipAccessKeyPrefix
= is_chrome_mac
? 'ctrl-option-' : 'alt-';
190 } else if ( !is_safari_win
&& is_safari
&& webkit_version
> 526 ) {
191 tooltipAccessKeyPrefix
= 'ctrl-alt-';
192 } else if ( !is_safari_win
&& ( is_safari
193 || clientPC
.indexOf('mac') != -1
194 || clientPC
.indexOf('konqueror') != -1 ) ) {
195 tooltipAccessKeyPrefix
= 'ctrl-';
196 } else if ( is_ff2
) {
197 tooltipAccessKeyPrefix
= 'alt-shift-';
199 window
.tooltipAccessKeyRegexp
= /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/;
202 * Add the appropriate prefix to the accesskey shown in the tooltip.
203 * If the nodeList parameter is given, only those nodes are updated;
204 * otherwise, all the nodes that will probably have accesskeys by
205 * default are updated.
207 * @param nodeList Array list of elements to update
209 window
.updateTooltipAccessKeys = function( nodeList
) {
211 // Rather than scan all links on the whole page, we can just scan these
212 // containers which contain the relevant links. This is really just an
213 // optimization technique.
214 var linkContainers
= [
215 'column-one', // Monobook and Modern
216 'mw-head', 'mw-panel', 'p-logo' // Vector
218 for ( var i
in linkContainers
) {
219 var linkContainer
= document
.getElementById( linkContainers
[i
] );
220 if ( linkContainer
) {
221 updateTooltipAccessKeys( linkContainer
.getElementsByTagName( 'a' ) );
224 // these are rare enough that no such optimization is needed
225 updateTooltipAccessKeys( document
.getElementsByTagName( 'input' ) );
226 updateTooltipAccessKeys( document
.getElementsByTagName( 'label' ) );
230 for ( var i
= 0; i
< nodeList
.length
; i
++ ) {
231 var element
= nodeList
[i
];
232 var tip
= element
.getAttribute( 'title' );
233 if ( tip
&& tooltipAccessKeyRegexp
.exec( tip
) ) {
234 tip
= tip
.replace(tooltipAccessKeyRegexp
,
235 '[' + tooltipAccessKeyPrefix
+ "$5]");
236 element
.setAttribute( 'title', tip
);
242 * Add a link to one of the portlet menus on the page, including:
244 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
245 * p-personal: Personal tools (shown at the top right of the page in Monobook)
246 * p-navigation: Navigation
249 * This function exists for the convenience of custom JS authors. All
250 * but the first three parameters are optional, though providing at
251 * least an id and a tooltip is recommended.
253 * By default the new link will be added to the end of the list. To
254 * add the link before a given existing item, pass the DOM node of
255 * that item (easily obtained with document.getElementById()) as the
256 * nextnode parameter; to add the link _after_ an existing item, pass
257 * the node's nextSibling instead.
259 * @param portlet String id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
260 * @param href String link URL
261 * @param text String link text (will be automatically lowercased by CSS for p-cactions in Monobook)
262 * @param id String id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
263 * @param tooltip String text to show when hovering over the link, without accesskey suffix
264 * @param accesskey String accesskey to activate this link (one character, try to avoid conflicts)
265 * @param nextnode Node the DOM node before which the new item should be added, should be another item in the same list
267 * @return Node -- the DOM node of the new item (an LI element) or null
269 window
.addPortletLink = function( portlet
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
270 var root
= document
.getElementById( portlet
);
274 var uls
= root
.getElementsByTagName( 'ul' );
276 if ( uls
.length
> 0 ) {
279 node
= document
.createElement( 'ul' );
280 var lastElementChild
= null;
281 for ( var i
= 0; i
< root
.childNodes
.length
; ++i
) { /* get root.lastElementChild */
282 if ( root
.childNodes
[i
].nodeType
== 1 ) {
283 lastElementChild
= root
.childNodes
[i
];
286 if ( lastElementChild
&& lastElementChild
.nodeName
.match( /div/i ) ) {
287 /* Insert into the menu divs */
288 lastElementChild
.appendChild( node
);
290 root
.appendChild( node
);
297 // unhide portlet if it was hidden before
298 root
.className
= root
.className
.replace( /(^| )emptyPortlet( |$)/, "$2" );
300 var link
= document
.createElement( 'a' );
301 link
.appendChild( document
.createTextNode( text
) );
304 // Wrap in a span - make it work with vector tabs and has no effect on any other portlets
305 var span
= document
.createElement( 'span' );
306 span
.appendChild( link
);
308 var item
= document
.createElement( 'li' );
309 item
.appendChild( span
);
315 link
.setAttribute( 'accesskey', accesskey
);
316 tooltip
+= ' [' + accesskey
+ ']';
319 link
.setAttribute( 'title', tooltip
);
321 if ( accesskey
&& tooltip
) {
322 updateTooltipAccessKeys( new Array( link
) );
325 if ( nextnode
&& nextnode
.parentNode
== node
) {
326 node
.insertBefore( item
, nextnode
);
328 node
.appendChild( item
); // IE compatibility (?)
334 window
.getInnerText = function( el
) {
335 if ( typeof el
== 'string' ) {
338 if ( typeof el
== 'undefined' ) {
341 // Custom sort value through 'data-sort-value' attribute
342 // (no need to prepend hidden text to change sort value)
343 if ( el
.nodeType
&& el
.getAttribute( 'data-sort-value' ) !== null ) {
344 // Make sure it's a valid DOM element (.nodeType) and that the attribute is set (!null)
345 return el
.getAttribute( 'data-sort-value' );
347 if ( el
.textContent
) {
348 return el
.textContent
; // not needed but it is faster
350 if ( el
.innerText
) {
351 return el
.innerText
; // IE doesn't have textContent
355 var cs
= el
.childNodes
;
357 for ( var i
= 0; i
< l
; i
++ ) {
358 switch ( cs
[i
].nodeType
) {
359 case 1: // ELEMENT_NODE
360 str
+= ts_getInnerText( cs
[i
] );
363 str
+= cs
[i
].nodeValue
;
370 /* Dummy for deprecated function */
372 window
.akeytt = function( doId
) {
375 window
.checkboxes
= undefined;
376 window
.lastCheckbox
= undefined;
378 window
.setupCheckboxShiftClick = function() {
381 var inputs
= document
.getElementsByTagName( 'input' );
382 addCheckboxClickHandlers( inputs
);
385 window
.addCheckboxClickHandlers = function( inputs
, start
) {
390 var finish
= start
+ 250;
391 if ( finish
> inputs
.length
) {
392 finish
= inputs
.length
;
395 for ( var i
= start
; i
< finish
; i
++ ) {
397 if ( !cb
.type
|| cb
.type
.toLowerCase() != 'checkbox' || ( ' ' + cb
.className
+ ' ' ).indexOf( ' noshiftselect ' ) != -1 ) {
400 var end
= checkboxes
.length
;
401 checkboxes
[end
] = cb
;
403 addClickHandler( cb
, checkboxClickHandler
);
406 if ( finish
< inputs
.length
) {
407 setTimeout( function() {
408 addCheckboxClickHandlers( inputs
, finish
);
413 window
.checkboxClickHandler = function( e
) {
414 if ( typeof e
== 'undefined' ) {
417 if ( !e
.shiftKey
|| lastCheckbox
=== null ) {
418 lastCheckbox
= this.index
;
421 var endState
= this.checked
;
423 if ( this.index
< lastCheckbox
) {
424 start
= this.index
+ 1;
425 finish
= lastCheckbox
;
427 start
= lastCheckbox
;
428 finish
= this.index
- 1;
430 for ( var i
= start
; i
<= finish
; ++i
) {
431 checkboxes
[i
].checked
= endState
;
432 if( i
> start
&& typeof checkboxes
[i
].onchange
== 'function' ) {
433 checkboxes
[i
].onchange(); // fire triggers
436 lastCheckbox
= this.index
;
442 Written by Jonathan Snook, http://www.snook.ca/jonathan
443 Add-ons by Robert Nyman, http://www.robertnyman.com
444 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
445 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
447 window
.getElementsByClassName = function( oElm
, strTagName
, oClassNames
) {
448 var arrReturnElements
= new Array();
449 if ( typeof( oElm
.getElementsByClassName
) == 'function' ) {
450 /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
451 var arrNativeReturn
= oElm
.getElementsByClassName( oClassNames
);
452 if ( strTagName
== '*' ) {
453 return arrNativeReturn
;
455 for ( var h
= 0; h
< arrNativeReturn
.length
; h
++ ) {
456 if( arrNativeReturn
[h
].tagName
.toLowerCase() == strTagName
.toLowerCase() ) {
457 arrReturnElements
[arrReturnElements
.length
] = arrNativeReturn
[h
];
460 return arrReturnElements
;
462 var arrElements
= ( strTagName
== '*' && oElm
.all
) ? oElm
.all
: oElm
.getElementsByTagName( strTagName
);
463 var arrRegExpClassNames
= new Array();
464 if( typeof oClassNames
== 'object' ) {
465 for( var i
= 0; i
< oClassNames
.length
; i
++ ) {
466 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
467 new RegExp("(^|\\s)" + oClassNames
[i
].replace(/\-/g, "\\-") + "(\\s|$)");
470 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
471 new RegExp("(^|\\s)" + oClassNames
.replace(/\-/g, "\\-") + "(\\s|$)");
475 for( var j
= 0; j
< arrElements
.length
; j
++ ) {
476 oElement
= arrElements
[j
];
478 for( var k
= 0; k
< arrRegExpClassNames
.length
; k
++ ) {
479 if( !arrRegExpClassNames
[k
].test( oElement
.className
) ) {
485 arrReturnElements
[arrReturnElements
.length
] = oElement
;
488 return ( arrReturnElements
);
491 window
.redirectToFragment = function( fragment
) {
492 var match
= navigator
.userAgent
.match(/AppleWebKit\/(\d+)/);
494 var webKitVersion
= parseInt( match
[1] );
495 if ( webKitVersion
< 420 ) {
496 // Released Safari w/ WebKit 418.9.1 messes up horribly
497 // Nightlies of 420+ are ok
501 if ( window
.location
.hash
== '' ) {
502 window
.location
.hash
= fragment
;
504 // Mozilla needs to wait until after load, otherwise the window doesn't
505 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
506 // There's no obvious way to detect this programmatically, so we use
507 // version-testing. If Firefox fixes the bug, they'll jump twice, but
508 // better twice than not at all, so make the fix hit future versions as
511 addOnloadHook(function() {
512 if ( window
.location
.hash
== fragment
) {
513 window
.location
.hash
= fragment
;
521 * Table sorting script based on one (c) 1997-2006 Stuart Langridge and Joost
523 * http://www.joostdevalk.nl/code/sortable-table/
524 * http://www.kryogenix.org/code/browser/sorttable/
526 * @todo don't break on colspans/rowspans (bug 8028)
527 * @todo language-specific digit grouping/decimals (bug 8063)
528 * @todo support all accepted date formats (bug 8226)
531 window
.ts_image_path
= stylepath
+ '/common/images/';
532 window
.ts_image_up
= 'sort_up.gif';
533 window
.ts_image_down
= 'sort_down.gif';
534 window
.ts_image_none
= 'sort_none.gif';
535 window
.ts_europeandate
= wgContentLanguage
!= 'en'; // The non-American-inclined can change to "true"
536 window
.ts_alternate_row_colors
= false;
537 window
.ts_number_transform_table
= null;
538 window
.ts_number_regex
= null;
540 window
.sortables_init = function() {
542 // Find all tables with class sortable and make them sortable
543 var tables
= getElementsByClassName( document
, 'table', 'sortable' );
544 for ( var ti
= 0; ti
< tables
.length
; ti
++ ) {
545 if ( !tables
[ti
].id
) {
546 tables
[ti
].setAttribute( 'id', 'sortable_table_id_' + idnum
);
549 ts_makeSortable( tables
[ti
] );
553 window
.ts_makeSortable = function( table
) {
555 if ( table
.rows
&& table
.rows
.length
> 0 ) {
556 if ( table
.tHead
&& table
.tHead
.rows
.length
> 0 ) {
557 firstRow
= table
.tHead
.rows
[table
.tHead
.rows
.length
-1];
559 firstRow
= table
.rows
[0];
566 // We have a first row: assume it's the header, and make its contents clickable links
567 for ( var i
= 0; i
< firstRow
.cells
.length
; i
++ ) {
568 var cell
= firstRow
.cells
[i
];
569 if ( (' ' + cell
.className
+ ' ').indexOf(' unsortable ') == -1 ) {
570 $(cell
).append ( '<a href="#" class="sortheader" '
571 + 'onclick="ts_resortTable(this);return false;">'
572 + '<span class="sortarrow">'
576 + '" alt="↓"/></span></a>');
579 if ( ts_alternate_row_colors
) {
580 ts_alternate( table
);
584 window
.ts_getInnerText = function( el
) {
585 return getInnerText( el
);
588 window
.ts_resortTable = function( lnk
) {
590 var span
= lnk
.getElementsByTagName('span')[0];
592 var td
= lnk
.parentNode
;
593 var tr
= td
.parentNode
;
594 var column
= td
.cellIndex
;
596 var table
= tr
.parentNode
;
597 while ( table
&& !( table
.tagName
&& table
.tagName
.toLowerCase() == 'table' ) ) {
598 table
= table
.parentNode
;
604 if ( table
.rows
.length
<= 1 ) {
608 // Generate the number transform table if it's not done already
609 if ( ts_number_transform_table
=== null ) {
610 ts_initTransformTable();
613 // Work out a type for the column
614 // Skip the first row if that's where the headings are
615 var rowStart
= ( table
.tHead
&& table
.tHead
.rows
.length
> 0 ? 0 : 1 );
617 if (rowStart
== 0 && table
.tBodies
) {
618 for (var i
=0; i
< table
.tBodies
.length
; i
++ ) {
619 bodyRows
+= table
.tBodies
[i
].rows
.length
;
621 if (bodyRows
< table
.rows
.length
)
626 for ( var i
= rowStart
; i
< table
.rows
.length
; i
++ ) {
627 if ( table
.rows
[i
].cells
.length
> column
) {
628 itm
= ts_getInnerText(table
.rows
[i
].cells
[column
]);
629 itm
= itm
.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '');
636 // TODO: bug 8226, localised date formats
637 var sortfn
= ts_sort_generic
;
638 var preprocessor
= ts_toLowerCase
;
639 if ( /^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/.test( itm
) ) {
640 preprocessor
= ts_dateToSortKey
;
641 } else if ( /^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/.test( itm
) ) {
642 preprocessor
= ts_dateToSortKey
;
643 } else if ( /^\d\d[\/.-]\d\d[\/.-]\d\d$/.test( itm
) ) {
644 preprocessor
= ts_dateToSortKey
;
645 // (minus sign)([pound dollar euro yen currency]|cents)
646 } else if ( /(^([-\u2212] *)?[\u00a3$\u20ac\u00a4\u00a5]|\u00a2$)/.test( itm
) ) {
647 preprocessor
= ts_currencyToSortKey
;
648 } else if ( ts_number_regex
.test( itm
) ) {
649 preprocessor
= ts_parseFloat
;
652 var reverse
= ( span
.getAttribute( 'sortdir' ) == 'down' );
654 var newRows
= new Array();
655 var staticRows
= new Array();
656 for ( var j
= rowStart
; j
< table
.rows
.length
; j
++ ) {
657 var row
= table
.rows
[j
];
658 if( (' ' + row
.className
+ ' ').indexOf(' unsortable ') < 0 ) {
659 var keyText
= ts_getInnerText( row
.cells
[column
] );
660 if( keyText
=== undefined ) {
663 var oldIndex
= ( reverse
? -j
: j
);
664 var preprocessed
= preprocessor( keyText
.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '') );
666 newRows
[newRows
.length
] = new Array( row
, preprocessed
, oldIndex
);
668 staticRows
[staticRows
.length
] = new Array( row
, false, j
-rowStart
);
672 newRows
.sort( sortfn
);
676 arrowHTML
= '<img src="' + ts_image_path
+ ts_image_down
+ '" alt="↓"/>';
678 span
.setAttribute( 'sortdir', 'up' );
680 arrowHTML
= '<img src="' + ts_image_path
+ ts_image_up
+ '" alt="↑"/>';
681 span
.setAttribute( 'sortdir', 'down' );
684 for ( var i
= 0; i
< staticRows
.length
; i
++ ) {
685 var row
= staticRows
[i
];
686 newRows
.splice( row
[2], 0, row
);
689 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
690 // don't do sortbottom rows
691 for ( var i
= 0; i
< newRows
.length
; i
++ ) {
692 if ( ( ' ' + newRows
[i
][0].className
+ ' ').indexOf(' sortbottom ') == -1 ) {
693 table
.tBodies
[0].appendChild( newRows
[i
][0] );
696 // do sortbottom rows only
697 for ( var i
= 0; i
< newRows
.length
; i
++ ) {
698 if ( ( ' ' + newRows
[i
][0].className
+ ' ').indexOf(' sortbottom ') != -1 ) {
699 table
.tBodies
[0].appendChild( newRows
[i
][0] );
703 // Delete any other arrows there may be showing
704 var spans
= getElementsByClassName( tr
, 'span', 'sortarrow' );
705 for ( var i
= 0; i
< spans
.length
; i
++ ) {
706 spans
[i
].innerHTML
= '<img src="' + ts_image_path
+ ts_image_none
+ '" alt="↓"/>';
708 span
.innerHTML
= arrowHTML
;
710 if ( ts_alternate_row_colors
) {
711 ts_alternate( table
);
715 window
.ts_initTransformTable = function() {
716 if ( typeof wgSeparatorTransformTable
== 'undefined'
717 || ( wgSeparatorTransformTable
[0] == '' && wgDigitTransformTable
[2] == '' ) )
719 var digitClass
= "[0-9,.]";
720 ts_number_transform_table
= false;
722 ts_number_transform_table
= {};
723 // Unpack the transform table
725 var ascii
= wgSeparatorTransformTable
[0].split("\t");
726 var localised
= wgSeparatorTransformTable
[1].split("\t");
727 for ( var i
= 0; i
< ascii
.length
; i
++ ) {
728 ts_number_transform_table
[localised
[i
]] = ascii
[i
];
731 ascii
= wgDigitTransformTable
[0].split("\t");
732 localised
= wgDigitTransformTable
[1].split("\t");
733 for ( var i
= 0; i
< ascii
.length
; i
++ ) {
734 ts_number_transform_table
[localised
[i
]] = ascii
[i
];
737 // Construct regex for number identification
738 var digits
= ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\\.'];
739 var maxDigitLength
= 1;
740 for ( var digit
in ts_number_transform_table
) {
741 // Escape regex metacharacters
743 digit
.replace( /([{}()|.?*+^$\[\]\\-])/g, "\\$1" )
745 if ( digit
.length
> maxDigitLength
) {
746 maxDigitLength
= digit
.length
;
749 if ( maxDigitLength
> 1 ) {
750 var digitClass
= '(' + digits
.join( '|' ) + ')';
752 var digitClass
= '[' + digits
.join( '' ) + ']';
756 // We allow a trailing percent sign, which we just strip. This works fine
757 // if percents and regular numbers aren't being mixed.
758 ts_number_regex
= new RegExp(
760 "[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?" + // Fortran-style scientific
762 "[-+\u2212]?" + digitClass
+ "+[\s\xa0]*%?" + // Generic localised
767 window
.ts_toLowerCase = function( s
) {
768 return s
.toLowerCase();
771 window
.ts_dateToSortKey = function( date
) {
772 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
773 if ( date
.length
== 11 ) {
774 switch ( date
.substr( 3, 3 ).toLowerCase() ) {
811 // default: var month = '00';
813 return date
.substr( 7, 4 ) + month
+ date
.substr( 0, 2 );
814 } else if ( date
.length
== 10 ) {
815 if ( !ts_europeandate
) {
816 return date
.substr( 6, 4 ) + date
.substr( 0, 2 ) + date
.substr( 3, 2 );
818 return date
.substr( 6, 4 ) + date
.substr( 3, 2 ) + date
.substr( 0, 2 );
820 } else if ( date
.length
== 8 ) {
821 var yr
= date
.substr( 6, 2 );
822 if ( parseInt( yr
) < 50 ) {
827 if ( ts_europeandate
) {
828 return yr
+ date
.substr( 3, 2 ) + date
.substr( 0, 2 );
830 return yr
+ date
.substr( 0, 2 ) + date
.substr( 3, 2 );
836 window
.ts_parseFloat = function( s
) {
840 if ( ts_number_transform_table
!= false ) {
843 for ( var p
= 0; p
< s
.length
; p
++ ) {
845 if ( c
in ts_number_transform_table
) {
846 newNum
+= ts_number_transform_table
[c
];
853 var num
= parseFloat( s
.replace(/[, ]/g, '').replace("\u2212", '-') );
854 return ( isNaN( num
) ? -Infinity
: num
);
857 window
.ts_currencyToSortKey = function( s
) {
858 return ts_parseFloat(s
.replace(/[^-\u22120-9.,]/g,''));
861 window
.ts_sort_generic = function( a
, b
) {
862 return a
[1] < b
[1] ? -1 : a
[1] > b
[1] ? 1 : a
[2] - b
[2];
865 window
.ts_alternate = function( table
) {
866 // Take object table and get all it's tbodies.
867 var tableBodies
= table
.getElementsByTagName( 'tbody' );
868 // Loop through these tbodies
869 for ( var i
= 0; i
< tableBodies
.length
; i
++ ) {
870 // Take the tbody, and get all it's rows
871 var tableRows
= tableBodies
[i
].getElementsByTagName( 'tr' );
872 // Loop through these rows
873 // Start at 1 because we want to leave the heading row untouched
874 for ( var j
= 0; j
< tableRows
.length
; j
++ ) {
875 // Check if j is even, and apply classes for both possible results
876 var oldClasses
= tableRows
[j
].className
.split(' ');
877 var newClassName
= '';
878 for ( var k
= 0; k
< oldClasses
.length
; k
++ ) {
879 if ( oldClasses
[k
] != '' && oldClasses
[k
] != 'even' && oldClasses
[k
] != 'odd' ) {
880 newClassName
+= oldClasses
[k
] + ' ';
883 tableRows
[j
].className
= newClassName
+ ( j
% 2 == 0 ? 'even' : 'odd' );
889 * End of table sorting code
894 * Add a cute little box at the top of the screen to inform the user of
895 * something, replacing any preexisting message.
897 * @param message String -or- Dom Object HTML to be put inside the right div
898 * @param className String Used in adding a class; should be different for each
899 * call to allow CSS/JS to hide different boxes. null = no class used.
900 * @return Boolean True on success, false on failure
902 window
.jsMsg = function( message
, className
) {
903 if ( !document
.getElementById
) {
906 // We special-case skin structures provided by the software. Skins that
907 // choose to abandon or significantly modify our formatting can just define
908 // an mw-js-message div to start with.
909 var messageDiv
= document
.getElementById( 'mw-js-message' );
911 messageDiv
= document
.createElement( 'div' );
912 if ( document
.getElementById( 'column-content' )
913 && document
.getElementById( 'content' ) ) {
914 // MonoBook, presumably
915 document
.getElementById( 'content' ).insertBefore(
917 document
.getElementById( 'content' ).firstChild
919 } else if ( document
.getElementById( 'content' )
920 && document
.getElementById( 'article' ) ) {
921 // Non-Monobook but still recognizable (old-style)
922 document
.getElementById( 'article').insertBefore(
924 document
.getElementById( 'article' ).firstChild
931 messageDiv
.setAttribute( 'id', 'mw-js-message' );
932 messageDiv
.style
.display
= 'block';
934 messageDiv
.setAttribute( 'class', 'mw-js-message-' + className
);
937 if ( typeof message
=== 'object' ) {
938 while ( messageDiv
.hasChildNodes() ) { // Remove old content
939 messageDiv
.removeChild( messageDiv
.firstChild
);
941 messageDiv
.appendChild( message
); // Append new content
943 messageDiv
.innerHTML
= message
;
949 * Inject a cute little progress spinner after the specified element
951 * @param element Element to inject after
952 * @param id Identifier string (for use with removeSpinner(), below)
954 window
.injectSpinner = function( element
, id
) {
955 var spinner
= document
.createElement( 'img' );
956 spinner
.id
= 'mw-spinner-' + id
;
957 spinner
.src
= stylepath
+ '/common/images/spinner.gif';
958 spinner
.alt
= spinner
.title
= '...';
959 if( element
.nextSibling
) {
960 element
.parentNode
.insertBefore( spinner
, element
.nextSibling
);
962 element
.parentNode
.appendChild( spinner
);
967 * Remove a progress spinner added with injectSpinner()
969 * @param id Identifier string
971 window
.removeSpinner = function( id
) {
972 var spinner
= document
.getElementById( 'mw-spinner-' + id
);
974 spinner
.parentNode
.removeChild( spinner
);
978 window
.runOnloadHook = function() {
979 // don't run anything below this for non-dom browsers
980 if ( doneOnloadHook
|| !( document
.getElementById
&& document
.getElementsByTagName
) ) {
984 // set this before running any hooks, since any errors below
985 // might cause the function to terminate prematurely
986 doneOnloadHook
= true;
988 updateTooltipAccessKeys( null );
989 setupCheckboxShiftClick();
991 jQuery( document
).ready( sortables_init
);
993 // Run any added-on functions
994 for ( var i
= 0; i
< onloadFuncts
.length
; i
++ ) {
1000 * Add an event handler to an element
1002 * @param element Element to add handler to
1003 * @param attach String Event to attach to
1004 * @param handler callable Event handler callback
1006 window
.addHandler = function( element
, attach
, handler
) {
1007 if( element
.addEventListener
) {
1008 element
.addEventListener( attach
, handler
, false );
1009 } else if( element
.attachEvent
) {
1010 element
.attachEvent( 'on' + attach
, handler
);
1014 window
.hookEvent = function( hookName
, hookFunct
) {
1015 addHandler( window
, hookName
, hookFunct
);
1019 * Add a click event handler to an element
1021 * @param element Element to add handler to
1022 * @param handler callable Event handler callback
1024 window
.addClickHandler = function( element
, handler
) {
1025 addHandler( element
, 'click', handler
);
1029 * Removes an event handler from an element
1031 * @param element Element to remove handler from
1032 * @param remove String Event to remove
1033 * @param handler callable Event handler callback to remove
1035 window
.removeHandler = function( element
, remove
, handler
) {
1036 if( window
.removeEventListener
) {
1037 element
.removeEventListener( remove
, handler
, false );
1038 } else if( window
.detachEvent
) {
1039 element
.detachEvent( 'on' + remove
, handler
);
1042 // note: all skins should call runOnloadHook() at the end of html output,
1043 // so the below should be redundant. It's there just in case.
1044 hookEvent( 'load', runOnloadHook
);
1047 importScriptURI( stylepath
+ '/common/IEFixes.js' );