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]);
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) {
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
);
29 // Global external objects used by this script.
30 /*extern ta, stylepath, skin */
32 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
35 if (!window
.onloadFuncts
) {
36 var onloadFuncts
= [];
39 // code that is dependent on js2 functions should use js2AddOnloadHook
40 function addOnloadHook(hookFunct
) {
41 // Allows add-on scripts to add onload functions
43 onloadFuncts
[onloadFuncts
.length
] = hookFunct
;
45 hookFunct(); // bug in MSIE script loading
50 function hookEvent(hookName
, hookFunct
) {
51 addHandler(window
, hookName
, hookFunct
);
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);
62 var loadedScripts = {}; // included-scripts tracker
63 function importScriptURI(url) {
64 if (loadedScripts[url]) {
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);
75 function importStylesheet(page) {
76 return importStylesheetURI(wgScript + '?action
=raw
&ctype
=text
/css&title=' + encodeURIComponent(page.replace(/ /g
,'_')));
79 function importStylesheetURI(url
,media
) {
80 var l
= document
.createElement('link');
84 if(media
) l
.media
= media
85 document
.getElementsByTagName('head')[0].appendChild(l
);
89 function appendCSS(text
) {
90 var s
= document
.createElement('style');
93 if (s
.styleSheet
) s
.styleSheet
.cssText
= text
//IE
94 else s
.appendChild(document
.createTextNode(text
+ '')) //Safari sometimes borks on null
95 document
.getElementsByTagName('head')[0].appendChild(s
);
99 // special stylesheet links
100 if (typeof stylepath
!= 'undefined' && typeof skin
!= 'undefined') {
101 // FIXME: This tries to load the stylesheets even for skins where they
102 // don't exist, i.e., everything but Monobook.
104 importStylesheetURI(stylepath
+'/'+skin
+'/Opera6Fixes.css');
105 } else if (opera7_bugs
) {
106 importStylesheetURI(stylepath
+'/'+skin
+'/Opera7Fixes.css');
107 } else if (opera95_bugs
) {
108 importStylesheetURI(stylepath
+'/'+skin
+'/Opera9Fixes.css');
109 } else if (ff2_bugs
) {
110 importStylesheetURI(stylepath
+'/'+skin
+'/FF2Fixes.css');
116 // Un-trap us from framesets
117 if (window
.top
!= window
) {
118 window
.top
.location
= window
.location
;
122 function showTocToggle() {
123 if (document
.createTextNode
) {
124 // Uses DOM calls to avoid document.write + XHTML issues
126 var linkHolder
= document
.getElementById('toctitle');
127 var existingLink
= document
.getElementById('togglelink');
128 if (!linkHolder
|| existingLink
) {
129 // Don't add the toggle link twice
133 var outerSpan
= document
.createElement('span');
134 outerSpan
.className
= 'toctoggle';
136 var toggleLink
= document
.createElement('a');
137 toggleLink
.id
= 'togglelink';
138 toggleLink
.className
= 'internal';
139 toggleLink
.href
= 'javascript:toggleToc()';
140 toggleLink
.appendChild(document
.createTextNode(tocHideText
));
142 outerSpan
.appendChild(document
.createTextNode('['));
143 outerSpan
.appendChild(toggleLink
);
144 outerSpan
.appendChild(document
.createTextNode(']'));
146 linkHolder
.appendChild(document
.createTextNode(' '));
147 linkHolder
.appendChild(outerSpan
);
149 var cookiePos
= document
.cookie
.indexOf("hidetoc=");
150 if (cookiePos
> -1 && document
.cookie
.charAt(cookiePos
+ 8) == 1) {
156 function changeText(el
, newText
) {
157 // Safari work around
159 el
.innerText
= newText
;
160 } else if (el
.firstChild
&& el
.firstChild
.nodeValue
) {
161 el
.firstChild
.nodeValue
= newText
;
165 function toggleToc() {
166 var tocmain
= document
.getElementById('toc');
167 var toc
= document
.getElementById('toc').getElementsByTagName('ul')[0];
168 var toggleLink
= document
.getElementById('togglelink');
170 if (toc
&& toggleLink
&& toc
.style
.display
== 'none') {
171 changeText(toggleLink
, tocHideText
);
172 toc
.style
.display
= 'block';
173 document
.cookie
= "hidetoc=0";
174 tocmain
.className
= 'toc';
176 changeText(toggleLink
, tocShowText
);
177 toc
.style
.display
= 'none';
178 document
.cookie
= "hidetoc=1";
179 tocmain
.className
= 'toc tochidden';
183 var mwEditButtons
= [];
184 var mwCustomEditButtons
= []; // eg to add in MediaWiki:Common.js
186 function escapeQuotes(text
) {
187 var re
= new RegExp("'","g");
188 text
= text
.replace(re
,"\\'");
189 re
= new RegExp("\\n","g");
190 text
= text
.replace(re
,"\\n");
191 return escapeQuotesHTML(text
);
194 function escapeQuotesHTML(text
) {
195 var re
= new RegExp('&',"g");
196 text
= text
.replace(re
,"&");
197 re
= new RegExp('"',"g");
198 text
= text
.replace(re
,""");
199 re
= new RegExp('<',"g");
200 text
= text
.replace(re
,"<");
201 re
= new RegExp('>',"g");
202 text
= text
.replace(re
,">");
208 * Set the accesskey prefix based on browser detection.
210 var tooltipAccessKeyPrefix
= 'alt-';
212 tooltipAccessKeyPrefix
= 'shift-esc-';
213 } else if (!is_safari_win
&& is_safari
&& webkit_version
> 526) {
214 tooltipAccessKeyPrefix
= 'ctrl-alt-';
215 } else if (!is_safari_win
&& (is_safari
216 || clientPC
.indexOf('mac') != -1
217 || clientPC
.indexOf('konqueror') != -1 )) {
218 tooltipAccessKeyPrefix
= 'ctrl-';
220 tooltipAccessKeyPrefix
= 'alt-shift-';
222 var tooltipAccessKeyRegexp
= /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/;
225 * Add the appropriate prefix to the accesskey shown in the tooltip.
226 * If the nodeList parameter is given, only those nodes are updated;
227 * otherwise, all the nodes that will probably have accesskeys by
228 * default are updated.
230 * @param Array nodeList -- list of elements to update
232 function updateTooltipAccessKeys( nodeList
) {
234 // Rather than scan all links on the whole page, we can just scan these
235 // containers which contain the relevant links. This is really just an
236 // optimization technique.
237 var linkContainers
= [
238 "column-one", // Monobook and Modern
239 "head", "panel", "p-logo" // Vector
241 for ( var i
in linkContainers
) {
242 var linkContainer
= document
.getElementById( linkContainers
[i
] );
243 if ( linkContainer
) {
244 updateTooltipAccessKeys( linkContainer
.getElementsByTagName("a") );
247 // these are rare enough that no such optimization is needed
248 updateTooltipAccessKeys( document
.getElementsByTagName("input") );
249 updateTooltipAccessKeys( document
.getElementsByTagName("label") );
253 for ( var i
= 0; i
< nodeList
.length
; i
++ ) {
254 var element
= nodeList
[i
];
255 var tip
= element
.getAttribute("title");
256 if ( tip
&& tooltipAccessKeyRegexp
.exec(tip
) ) {
257 tip
= tip
.replace(tooltipAccessKeyRegexp
,
258 "["+tooltipAccessKeyPrefix
+"$5]");
259 element
.setAttribute("title", tip
);
265 * Add a link to one of the portlet menus on the page, including:
267 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
268 * p-personal: Personal tools (shown at the top right of the page in Monobook)
269 * p-navigation: Navigation
272 * This function exists for the convenience of custom JS authors. All
273 * but the first three parameters are optional, though providing at
274 * least an id and a tooltip is recommended.
276 * By default the new link will be added to the end of the list. To
277 * add the link before a given existing item, pass the DOM node of
278 * that item (easily obtained with document.getElementById()) as the
279 * nextnode parameter; to add the link _after_ an existing item, pass
280 * the node's nextSibling instead.
282 * @param String portlet -- id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
283 * @param String href -- link URL
284 * @param String text -- link text (will be automatically lowercased by CSS for p-cactions in Monobook)
285 * @param String id -- id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
286 * @param String tooltip -- text to show when hovering over the link, without accesskey suffix
287 * @param String accesskey -- accesskey to activate this link (one character, try to avoid conflicts)
288 * @param Node nextnode -- the DOM node before which the new item should be added, should be another item in the same list
290 * @return Node -- the DOM node of the new item (an LI element) or null
292 function addPortletLink(portlet
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
293 var root
= document
.getElementById(portlet
);
294 if ( !root
) return null;
295 var node
= root
.getElementsByTagName( "ul" )[0];
296 if ( !node
) return null;
298 // unhide portlet if it was hidden before
299 root
.className
= root
.className
.replace( /(^| )emptyPortlet( |$)/, "$2" );
301 var span
= document
.createElement( "span" );
302 span
.appendChild( document
.createTextNode( text
) );
304 var link
= document
.createElement( "a" );
305 link
.appendChild( span
);
308 var item
= document
.createElement( "li" );
309 item
.appendChild( link
);
310 if ( id
) item
.id
= id
;
313 link
.setAttribute( "accesskey", accesskey
);
314 tooltip
+= " ["+accesskey
+"]";
317 link
.setAttribute( "title", tooltip
);
319 if ( accesskey
&& tooltip
) {
320 updateTooltipAccessKeys( new Array( link
) );
323 if ( nextnode
&& nextnode
.parentNode
== node
)
324 node
.insertBefore( item
, nextnode
);
326 node
.appendChild( item
); // IE compatibility (?)
331 function getInnerText(el
) {
332 if (typeof el
== "string") return el
;
333 if (typeof el
== "undefined") { return el
};
334 if (el
.textContent
) return el
.textContent
; // not needed but it is faster
335 if (el
.innerText
) return el
.innerText
; // IE doesn't have textContent
338 var cs
= el
.childNodes
;
340 for (var i
= 0; i
< l
; i
++) {
341 switch (cs
[i
].nodeType
) {
342 case 1: //ELEMENT_NODE
343 str
+= ts_getInnerText(cs
[i
]);
346 str
+= cs
[i
].nodeValue
;
355 * Set up accesskeys/tooltips from the deprecated ta array. If doId
356 * is specified, only set up for that id. Note that this function is
357 * deprecated and will not be supported indefinitely -- use
358 * updateTooltipAccessKey() instead.
360 * @param mixed doId string or null
362 function akeytt( doId
) {
363 // A lot of user scripts (and some of the code below) break if
364 // ta isn't defined, so we make sure it is. Explictly using
365 // window.ta avoids a "ta is not defined" error.
366 if (!window
.ta
) window
.ta
= new Array
;
368 // Make a local, possibly restricted, copy to avoid clobbering
377 // Now deal with evil deprecated ta
378 var watchCheckboxExists
= document
.getElementById( 'wpWatchthis' ) ? true : false;
380 var n
= document
.getElementById(id
);
384 // Are we putting accesskey in it
385 if (ta
[id
][0].length
> 0) {
386 // Is this object a object? If not assume it's the next child.
388 if (n
.nodeName
.toLowerCase() == "a") {
393 // Don't add an accesskey for the watch tab if the watch
394 // checkbox is also available.
395 if (a
&& ((id
!= 'ca-watch' && id
!= 'ca-unwatch') || !watchCheckboxExists
)) {
396 a
.accessKey
= ta
[id
][0];
397 ak
= ' ['+tooltipAccessKeyPrefix
+ta
[id
][0]+']';
400 // We don't care what type the object is when assigning tooltip
406 a
.title
= ta
[id
][1]+ak
;
415 function setupCheckboxShiftClick() {
418 var inputs
= document
.getElementsByTagName('input');
419 addCheckboxClickHandlers(inputs
);
422 function addCheckboxClickHandlers(inputs
, start
) {
423 if ( !start
) start
= 0;
425 var finish
= start
+ 250;
426 if ( finish
> inputs
.length
)
427 finish
= inputs
.length
;
429 for ( var i
= start
; i
< finish
; i
++ ) {
431 if ( !cb
.type
|| cb
.type
.toLowerCase() != 'checkbox' )
433 var end
= checkboxes
.length
;
434 checkboxes
[end
] = cb
;
436 cb
.onclick
= checkboxClickHandler
;
439 if ( finish
< inputs
.length
) {
440 setTimeout( function () {
441 addCheckboxClickHandlers(inputs
, finish
);
446 function checkboxClickHandler(e
) {
447 if (typeof e
== 'undefined') {
450 if ( !e
.shiftKey
|| lastCheckbox
=== null ) {
451 lastCheckbox
= this.index
;
454 var endState
= this.checked
;
456 if ( this.index
< lastCheckbox
) {
457 start
= this.index
+ 1;
458 finish
= lastCheckbox
;
460 start
= lastCheckbox
;
461 finish
= this.index
- 1;
463 for (var i
= start
; i
<= finish
; ++i
) {
464 checkboxes
[i
].checked
= endState
;
465 if( i
> start
&& typeof checkboxes
[i
].onchange
== 'function' )
466 checkboxes
[i
].onchange(); // fire triggers
468 lastCheckbox
= this.index
;
472 function toggle_element_activation(ida
,idb
) {
473 if ( !document
.getElementById
) {
476 // Show the appropriate upload size limit message
477 if( idb
== 'wpUploadFileURL' ) {
478 var e
= document
.getElementById( 'mw-upload-maxfilesize' );
479 if( e
) e
.style
.display
= "none";
481 var e
= document
.getElementById( 'mw-upload-maxfilesize-url' );
482 if( e
) e
.style
.display
= "block";
484 if( idb
== 'wpUploadFile' ) {
485 var e
= document
.getElementById( 'mw-upload-maxfilesize-url' );
486 if( e
) e
.style
.display
= "none";
488 var e
= document
.getElementById( 'mw-upload-maxfilesize' );
489 if( e
) e
.style
.display
= "block";
491 document
.getElementById( ida
).disabled
= true;
492 document
.getElementById( idb
).disabled
= false;
495 function toggle_element_check(ida
,idb
) {
496 if (!document
.getElementById
) {
499 document
.getElementById(ida
).checked
=true;
500 document
.getElementById(idb
).checked
=false;
504 Written by Jonathan Snook, http://www.snook.ca/jonathan
505 Add-ons by Robert Nyman, http://www.robertnyman.com
506 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
507 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
509 function getElementsByClassName(oElm
, strTagName
, oClassNames
){
510 var arrReturnElements
= new Array();
511 if ( typeof( oElm
.getElementsByClassName
) == "function" ) {
512 /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
513 var arrNativeReturn
= oElm
.getElementsByClassName( oClassNames
);
514 if ( strTagName
== "*" )
515 return arrNativeReturn
;
516 for ( var h
=0; h
< arrNativeReturn
.length
; h
++ ) {
517 if( arrNativeReturn
[h
].tagName
.toLowerCase() == strTagName
.toLowerCase() )
518 arrReturnElements
[arrReturnElements
.length
] = arrNativeReturn
[h
];
520 return arrReturnElements
;
522 var arrElements
= (strTagName
== "*" && oElm
.all
)? oElm
.all
: oElm
.getElementsByTagName(strTagName
);
523 var arrRegExpClassNames
= new Array();
524 if(typeof oClassNames
== "object"){
525 for(var i
=0; i
<oClassNames
.length
; i
++){
526 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
527 new RegExp("(^|\\s)" + oClassNames
[i
].replace(/\-/g, "\\-") + "(\\s|$)");
531 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
532 new RegExp("(^|\\s)" + oClassNames
.replace(/\-/g, "\\-") + "(\\s|$)");
536 for(var j
=0; j
<arrElements
.length
; j
++){
537 oElement
= arrElements
[j
];
539 for(var k
=0; k
<arrRegExpClassNames
.length
; k
++){
540 if(!arrRegExpClassNames
[k
].test(oElement
.className
)){
546 arrReturnElements
[arrReturnElements
.length
] = oElement
;
549 return (arrReturnElements
)
552 function redirectToFragment(fragment
) {
553 var match
= navigator
.userAgent
.match(/AppleWebKit\/(\d+)/);
555 var webKitVersion
= parseInt(match
[1]);
556 if (webKitVersion
< 420) {
557 // Released Safari w/ WebKit 418.9.1 messes up horribly
558 // Nightlies of 420+ are ok
563 // Mozilla needs to wait until after load, otherwise the window doesn't scroll
564 addOnloadHook(function () {
565 if (window
.location
.hash
== "")
566 window
.location
.hash
= fragment
;
569 if (window
.location
.hash
== "")
570 window
.location
.hash
= fragment
;
575 * Table sorting script based on one (c) 1997-2006 Stuart Langridge and Joost
577 * http://www.joostdevalk.nl/code/sortable-table/
578 * http://www.kryogenix.org/code/browser/sorttable/
580 * @todo don't break on colspans/rowspans (bug 8028)
581 * @todo language-specific digit grouping/decimals (bug 8063)
582 * @todo support all accepted date formats (bug 8226)
585 var ts_image_path
= stylepath
+"/common/images/";
586 var ts_image_up
= "sort_up.gif";
587 var ts_image_down
= "sort_down.gif";
588 var ts_image_none
= "sort_none.gif";
589 var ts_europeandate
= wgContentLanguage
!= "en"; // The non-American-inclined can change to "true"
590 var ts_alternate_row_colors
= false;
591 var ts_number_transform_table
= null;
592 var ts_number_regex
= null;
594 function sortables_init() {
596 // Find all tables with class sortable and make them sortable
597 var tables
= getElementsByClassName(document
, "table", "sortable");
598 for (var ti
= 0; ti
< tables
.length
; ti
++) {
599 if (!tables
[ti
].id
) {
600 tables
[ti
].setAttribute('id','sortable_table_id_'+idnum
);
603 ts_makeSortable(tables
[ti
]);
607 function ts_makeSortable(table
) {
609 if (table
.rows
&& table
.rows
.length
> 0) {
610 if (table
.tHead
&& table
.tHead
.rows
.length
> 0) {
611 firstRow
= table
.tHead
.rows
[table
.tHead
.rows
.length
-1];
613 firstRow
= table
.rows
[0];
616 if (!firstRow
) return;
618 // We have a first row: assume it's the header, and make its contents clickable links
619 for (var i
= 0; i
< firstRow
.cells
.length
; i
++) {
620 var cell
= firstRow
.cells
[i
];
621 if ((" "+cell
.className
+" ").indexOf(" unsortable ") == -1) {
622 cell
.innerHTML
+= ' '
623 + '<a href="#" class="sortheader" '
624 + 'onclick="ts_resortTable(this);return false;">'
625 + '<span class="sortarrow">'
629 + '" alt="↓"/></span></a>';
632 if (ts_alternate_row_colors
) {
637 function ts_getInnerText(el
) {
638 return getInnerText( el
);
641 function ts_resortTable(lnk
) {
643 var span
= lnk
.getElementsByTagName('span')[0];
645 var td
= lnk
.parentNode
;
646 var tr
= td
.parentNode
;
647 var column
= td
.cellIndex
;
649 var table
= tr
.parentNode
;
650 while (table
&& !(table
.tagName
&& table
.tagName
.toLowerCase() == 'table'))
651 table
= table
.parentNode
;
654 if (table
.rows
.length
<= 1) return;
656 // Generate the number transform table if it's not done already
657 if (ts_number_transform_table
== null) {
658 ts_initTransformTable();
661 // Work out a type for the column
662 // Skip the first row if that's where the headings are
663 var rowStart
= (table
.tHead
&& table
.tHead
.rows
.length
> 0 ? 0 : 1);
666 for (var i
= rowStart
; i
< table
.rows
.length
; i
++) {
667 if (table
.rows
[i
].cells
.length
> column
) {
668 itm
= ts_getInnerText(table
.rows
[i
].cells
[column
]);
669 itm
= itm
.replace(/^[\s\xa0]+/, "").replace(/[\s\xa0]+$/, "");
670 if (itm
!= "") break;
674 // TODO: bug 8226, localised date formats
675 var sortfn
= ts_sort_generic
;
676 var preprocessor
= ts_toLowerCase
;
677 if (/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/.test(itm
)) {
678 preprocessor
= ts_dateToSortKey
;
679 } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/.test(itm
)) {
680 preprocessor
= ts_dateToSortKey
;
681 } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d$/.test(itm
)) {
682 preprocessor
= ts_dateToSortKey
;
683 // pound dollar euro yen currency cents
684 } else if (/(^[\u00a3$\u20ac\u00a4\u00a5]|\u00a2$)/.test(itm
)) {
685 preprocessor
= ts_currencyToSortKey
;
686 } else if (ts_number_regex
.test(itm
)) {
687 preprocessor
= ts_parseFloat
;
690 var reverse
= (span
.getAttribute("sortdir") == 'down');
692 var newRows
= new Array();
693 var staticRows
= new Array();
694 for (var j
= rowStart
; j
< table
.rows
.length
; j
++) {
695 var row
= table
.rows
[j
];
696 if((" "+row
.className
+" ").indexOf(" unsortable ") < 0) {
697 var keyText
= ts_getInnerText(row
.cells
[column
]);
698 if(keyText
== undefined) {
701 var oldIndex
= (reverse
? -j
: j
);
702 var preprocessed
= preprocessor( keyText
.replace(/^[\s\xa0]+/, "").replace(/[\s\xa0]+$/, "") );
704 newRows
[newRows
.length
] = new Array(row
, preprocessed
, oldIndex
);
705 } else staticRows
[staticRows
.length
] = new Array(row
, false, j
-rowStart
);
708 newRows
.sort(sortfn
);
712 arrowHTML
= '<img src="'+ ts_image_path
+ ts_image_down
+ '" alt="↓"/>';
714 span
.setAttribute('sortdir','up');
716 arrowHTML
= '<img src="'+ ts_image_path
+ ts_image_up
+ '" alt="↑"/>';
717 span
.setAttribute('sortdir','down');
720 for (var i
= 0; i
< staticRows
.length
; i
++) {
721 var row
= staticRows
[i
];
722 newRows
.splice(row
[2], 0, row
);
725 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
726 // don't do sortbottom rows
727 for (var i
= 0; i
< newRows
.length
; i
++) {
728 if ((" "+newRows
[i
][0].className
+" ").indexOf(" sortbottom ") == -1)
729 table
.tBodies
[0].appendChild(newRows
[i
][0]);
731 // do sortbottom rows only
732 for (var i
= 0; i
< newRows
.length
; i
++) {
733 if ((" "+newRows
[i
][0].className
+" ").indexOf(" sortbottom ") != -1)
734 table
.tBodies
[0].appendChild(newRows
[i
][0]);
737 // Delete any other arrows there may be showing
738 var spans
= getElementsByClassName(tr
, "span", "sortarrow");
739 for (var i
= 0; i
< spans
.length
; i
++) {
740 spans
[i
].innerHTML
= '<img src="'+ ts_image_path
+ ts_image_none
+ '" alt="↓"/>';
742 span
.innerHTML
= arrowHTML
;
744 if (ts_alternate_row_colors
) {
749 function ts_initTransformTable() {
750 if ( typeof wgSeparatorTransformTable
== "undefined"
751 || ( wgSeparatorTransformTable
[0] == '' && wgDigitTransformTable
[2] == '' ) )
753 digitClass
= "[0-9,.]";
754 ts_number_transform_table
= false;
756 ts_number_transform_table
= {};
757 // Unpack the transform table
759 ascii
= wgSeparatorTransformTable
[0].split("\t");
760 localised
= wgSeparatorTransformTable
[1].split("\t");
761 for ( var i
= 0; i
< ascii
.length
; i
++ ) {
762 ts_number_transform_table
[localised
[i
]] = ascii
[i
];
765 ascii
= wgDigitTransformTable
[0].split("\t");
766 localised
= wgDigitTransformTable
[1].split("\t");
767 for ( var i
= 0; i
< ascii
.length
; i
++ ) {
768 ts_number_transform_table
[localised
[i
]] = ascii
[i
];
771 // Construct regex for number identification
772 digits
= ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\\.'];
774 for ( var digit
in ts_number_transform_table
) {
775 // Escape regex metacharacters
777 digit
.replace( /[\\\\$\*\+\?\.\(\)\|\{\}\[\]\-]/,
778 function( s
) { return '\\' + s
; } )
780 if (digit
.length
> maxDigitLength
) {
781 maxDigitLength
= digit
.length
;
784 if ( maxDigitLength
> 1 ) {
785 digitClass
= '[' + digits
.join( '', digits
) + ']';
787 digitClass
= '(' + digits
.join( '|', digits
) + ')';
791 // We allow a trailing percent sign, which we just strip. This works fine
792 // if percents and regular numbers aren't being mixed.
793 ts_number_regex
= new RegExp(
795 "[+-]?[0-9][0-9,]*(\\.[0-9,]*)?(E[+-]?[0-9][0-9,]*)?" + // Fortran-style scientific
797 "[+-]?" + digitClass
+ "+%?" + // Generic localised
802 function ts_toLowerCase( s
) {
803 return s
.toLowerCase();
806 function ts_dateToSortKey(date
) {
807 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
808 if (date
.length
== 11) {
809 switch (date
.substr(3,3).toLowerCase()) {
810 case "jan": var month
= "01"; break;
811 case "feb": var month
= "02"; break;
812 case "mar": var month
= "03"; break;
813 case "apr": var month
= "04"; break;
814 case "may": var month
= "05"; break;
815 case "jun": var month
= "06"; break;
816 case "jul": var month
= "07"; break;
817 case "aug": var month
= "08"; break;
818 case "sep": var month
= "09"; break;
819 case "oct": var month
= "10"; break;
820 case "nov": var month
= "11"; break;
821 case "dec": var month
= "12"; break;
822 // default: var month = "00";
824 return date
.substr(7,4)+month
+date
.substr(0,2);
825 } else if (date
.length
== 10) {
826 if (ts_europeandate
== false) {
827 return date
.substr(6,4)+date
.substr(0,2)+date
.substr(3,2);
829 return date
.substr(6,4)+date
.substr(3,2)+date
.substr(0,2);
831 } else if (date
.length
== 8) {
832 yr
= date
.substr(6,2);
833 if (parseInt(yr
) < 50) {
838 if (ts_europeandate
== true) {
839 return yr
+date
.substr(3,2)+date
.substr(0,2);
841 return yr
+date
.substr(0,2)+date
.substr(3,2);
847 function ts_parseFloat( s
) {
851 if (ts_number_transform_table
!= false) {
854 for ( var p
= 0; p
< s
.length
; p
++ ) {
856 if (c
in ts_number_transform_table
) {
857 newNum
+= ts_number_transform_table
[c
];
865 num
= parseFloat(s
.replace(/,/g
, ""));
866 return (isNaN(num
) ? 0 : num
);
869 function ts_currencyToSortKey( s
) {
870 return ts_parseFloat(s
.replace(/[^0-9.,]/g,''));
873 function ts_sort_generic(a
, b
) {
874 return a
[1] < b
[1] ? -1 : a
[1] > b
[1] ? 1 : a
[2] - b
[2];
877 function ts_alternate(table
) {
878 // Take object table and get all it's tbodies.
879 var tableBodies
= table
.getElementsByTagName("tbody");
880 // Loop through these tbodies
881 for (var i
= 0; i
< tableBodies
.length
; i
++) {
882 // Take the tbody, and get all it's rows
883 var tableRows
= tableBodies
[i
].getElementsByTagName("tr");
884 // Loop through these rows
885 // Start at 1 because we want to leave the heading row untouched
886 for (var j
= 0; j
< tableRows
.length
; j
++) {
887 // Check if j is even, and apply classes for both possible results
888 var oldClasses
= tableRows
[j
].className
.split(" ");
889 var newClassName
= "";
890 for (var k
= 0; k
< oldClasses
.length
; k
++) {
891 if (oldClasses
[k
] != "" && oldClasses
[k
] != "even" && oldClasses
[k
] != "odd")
892 newClassName
+= oldClasses
[k
] + " ";
894 tableRows
[j
].className
= newClassName
+ (j
% 2 == 0 ? "even" : "odd");
900 * End of table sorting code
905 * Add a cute little box at the top of the screen to inform the user of
906 * something, replacing any preexisting message.
908 * @param String -or- Dom Object message HTML to be put inside the right div
909 * @param String className Used in adding a class; should be different for each
910 * call to allow CSS/JS to hide different boxes. null = no class used.
911 * @return Boolean True on success, false on failure
913 function jsMsg( message
, className
) {
914 if ( !document
.getElementById
) {
917 // We special-case skin structures provided by the software. Skins that
918 // choose to abandon or significantly modify our formatting can just define
919 // an mw-js-message div to start with.
920 var messageDiv
= document
.getElementById( 'mw-js-message' );
922 messageDiv
= document
.createElement( 'div' );
923 if ( document
.getElementById( 'column-content' )
924 && document
.getElementById( 'content' ) ) {
925 // MonoBook, presumably
926 document
.getElementById( 'content' ).insertBefore(
928 document
.getElementById( 'content' ).firstChild
930 } else if ( document
.getElementById('content')
931 && document
.getElementById( 'article' ) ) {
932 // Non-Monobook but still recognizable (old-style)
933 document
.getElementById( 'article').insertBefore(
935 document
.getElementById( 'article' ).firstChild
942 messageDiv
.setAttribute( 'id', 'mw-js-message' );
943 messageDiv
.style
.display
= 'block';
945 messageDiv
.setAttribute( 'class', 'mw-js-message-'+className
);
948 if (typeof message
=== 'object') {
949 while (messageDiv
.hasChildNodes()) // Remove old content
950 messageDiv
.removeChild(messageDiv
.firstChild
);
951 messageDiv
.appendChild (message
); // Append new content
954 messageDiv
.innerHTML
= message
;
960 * Inject a cute little progress spinner after the specified element
962 * @param element Element to inject after
963 * @param id Identifier string (for use with removeSpinner(), below)
965 function injectSpinner( element
, id
) {
966 var spinner
= document
.createElement( "img" );
967 spinner
.id
= "mw-spinner-" + id
;
968 spinner
.src
= stylepath
+ "/common/images/spinner.gif";
969 spinner
.alt
= spinner
.title
= "...";
970 if( element
.nextSibling
) {
971 element
.parentNode
.insertBefore( spinner
, element
.nextSibling
);
973 element
.parentNode
.appendChild( spinner
);
978 * Remove a progress spinner added with injectSpinner()
980 * @param id Identifier string
982 function removeSpinner( id
) {
983 var spinner
= document
.getElementById( "mw-spinner-" + id
);
985 spinner
.parentNode
.removeChild( spinner
);
989 function runOnloadHook() {
990 // don't run anything below this for non-dom browsers
991 if (doneOnloadHook
|| !(document
.getElementById
&& document
.getElementsByTagName
)) {
995 // set this before running any hooks, since any errors below
996 // might cause the function to terminate prematurely
997 doneOnloadHook
= true;
999 updateTooltipAccessKeys( null );
1001 setupCheckboxShiftClick();
1004 // Run any added-on functions
1005 for (var i
= 0; i
< onloadFuncts
.length
; i
++) {
1011 * Add an event handler to an element
1013 * @param Element element Element to add handler to
1014 * @param String attach Event to attach to
1015 * @param callable handler Event handler callback
1017 function addHandler( element
, attach
, handler
) {
1018 if( window
.addEventListener
) {
1019 element
.addEventListener( attach
, handler
, false );
1020 } else if( window
.attachEvent
) {
1021 element
.attachEvent( 'on' + attach
, handler
);
1026 * Add a click event handler to an element
1028 * @param Element element Element to add handler to
1029 * @param callable handler Event handler callback
1031 function addClickHandler( element
, handler
) {
1032 addHandler( element
, 'click', handler
);
1036 * Removes an event handler from an element
1038 * @param Element element Element to remove handler from
1039 * @param String remove Event to remove
1040 * @param callable handler Event handler callback to remove
1042 function removeHandler( element
, remove
, handler
) {
1043 if( window
.removeEventListener
) {
1044 element
.removeEventListener( remove
, handler
, false );
1045 } else if( window
.detachEvent
) {
1046 element
.detachEvent( 'on' + remove
, handler
);
1049 //note: all skins should call runOnloadHook() at the end of html output,
1050 // so the below should be redundant. It's there just in case.
1051 hookEvent("load", runOnloadHook
);