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 is_safari
= clientPC
.indexOf('applewebkit') != -1 &&
7 clientPC
.indexOf('spoofer') == -1;
8 var is_khtml
= navigator
.vendor
== 'KDE' ||
9 ( document
.childNodes
&& !document
.all
&& !navigator
.taintEnabled
);
10 // For accesskeys; note that FF3+ is included here!
11 var is_ff2
= /firefox\/[2-9]|minefield\/3/.test( clientPC
);
12 // These aren't used here, but some custom scripts rely on them
13 var is_ff2_win
= is_ff2
&& clientPC
.indexOf('windows') != -1;
14 var is_ff2_x11
= is_ff2
&& clientPC
.indexOf('x11') != -1;
15 if (clientPC
.indexOf('opera') != -1) {
17 var is_opera_preseven
= window
.opera
&& !document
.childNodes
;
18 var is_opera_seven
= window
.opera
&& document
.childNodes
;
19 var is_opera_95
= /opera\/(9.[5-9]|[1-9][0-9])/.test( clientPC
);
22 // Global external objects used by this script.
23 /*extern ta, stylepath, skin */
25 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
28 if (!window
.onloadFuncts
) {
29 var onloadFuncts
= [];
32 function addOnloadHook(hookFunct
) {
33 // Allows add-on scripts to add onload functions
34 onloadFuncts
[onloadFuncts
.length
] = hookFunct
;
37 function hookEvent(hookName
, hookFunct
) {
38 if (window
.addEventListener
) {
39 window
.addEventListener(hookName
, hookFunct
, false);
40 } else if (window
.attachEvent
) {
41 window
.attachEvent("on" + hookName
, hookFunct
);
45 // document.write special stylesheet links
46 if (typeof stylepath
!= 'undefined' && typeof skin
!= 'undefined') {
47 if (is_opera_preseven
) {
48 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera6Fixes.css">');
49 } else if (is_opera_seven
&& !is_opera_95
) {
50 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera7Fixes.css">');
51 } else if (is_khtml
) {
52 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/KHTMLFixes.css">');
57 // Un-trap us from framesets
58 if (window
.top
!= window
) {
59 window
.top
.location
= window
.location
;
63 // for enhanced RecentChanges
64 function toggleVisibility(_levelId
, _otherId
, _linkId
) {
65 var thisLevel
= document
.getElementById(_levelId
);
66 var otherLevel
= document
.getElementById(_otherId
);
67 var linkLevel
= document
.getElementById(_linkId
);
68 if (thisLevel
.style
.display
== 'none') {
69 thisLevel
.style
.display
= 'block';
70 otherLevel
.style
.display
= 'none';
71 linkLevel
.style
.display
= 'inline';
73 thisLevel
.style
.display
= 'none';
74 otherLevel
.style
.display
= 'inline';
75 linkLevel
.style
.display
= 'none';
79 function showTocToggle() {
80 if (document
.createTextNode
) {
81 // Uses DOM calls to avoid document.write + XHTML issues
83 var linkHolder
= document
.getElementById('toctitle');
88 var outerSpan
= document
.createElement('span');
89 outerSpan
.className
= 'toctoggle';
91 var toggleLink
= document
.createElement('a');
92 toggleLink
.id
= 'togglelink';
93 toggleLink
.className
= 'internal';
94 toggleLink
.href
= 'javascript:toggleToc()';
95 toggleLink
.appendChild(document
.createTextNode(tocHideText
));
97 outerSpan
.appendChild(document
.createTextNode('['));
98 outerSpan
.appendChild(toggleLink
);
99 outerSpan
.appendChild(document
.createTextNode(']'));
101 linkHolder
.appendChild(document
.createTextNode(' '));
102 linkHolder
.appendChild(outerSpan
);
104 var cookiePos
= document
.cookie
.indexOf("hidetoc=");
105 if (cookiePos
> -1 && document
.cookie
.charAt(cookiePos
+ 8) == 1) {
111 function changeText(el
, newText
) {
112 // Safari work around
114 el
.innerText
= newText
;
115 } else if (el
.firstChild
&& el
.firstChild
.nodeValue
) {
116 el
.firstChild
.nodeValue
= newText
;
120 function toggleToc() {
121 var toc
= document
.getElementById('toc').getElementsByTagName('ul')[0];
122 var toggleLink
= document
.getElementById('togglelink');
124 if (toc
&& toggleLink
&& toc
.style
.display
== 'none') {
125 changeText(toggleLink
, tocHideText
);
126 toc
.style
.display
= 'block';
127 document
.cookie
= "hidetoc=0";
129 changeText(toggleLink
, tocShowText
);
130 toc
.style
.display
= 'none';
131 document
.cookie
= "hidetoc=1";
135 var mwEditButtons
= [];
136 var mwCustomEditButtons
= []; // eg to add in MediaWiki:Common.js
138 // this function generates the actual toolbar buttons with localized text
139 // we use it to avoid creating the toolbar where javascript is not enabled
140 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
, imageId
) {
141 // Don't generate buttons for browsers which don't fully
143 mwEditButtons
[mwEditButtons
.length
] =
145 "imageFile": imageFile
,
146 "speedTip": speedTip
,
148 "tagClose": tagClose
,
149 "sampleText": sampleText
};
152 // this function generates the actual toolbar buttons with localized text
153 // we use it to avoid creating the toolbar where javascript is not enabled
154 function mwInsertEditButton(parent
, item
) {
155 var image
= document
.createElement("img");
158 image
.className
= "mw-toolbar-editbutton";
159 if (item
.imageId
) image
.id
= item
.imageId
;
160 image
.src
= item
.imageFile
;
162 image
.alt
= item
.speedTip
;
163 image
.title
= item
.speedTip
;
164 image
.style
.cursor
= "pointer";
165 image
.onclick = function() {
166 insertTags(item
.tagOpen
, item
.tagClose
, item
.sampleText
);
170 parent
.appendChild(image
);
174 function mwSetupToolbar() {
175 var toolbar
= document
.getElementById('toolbar');
176 if (!toolbar
) { return false; }
178 var textbox
= document
.getElementById('wpTextbox1');
179 if (!textbox
) { return false; }
181 // Don't generate buttons for browsers which don't fully
183 if (!(document
.selection
&& document
.selection
.createRange
)
184 && textbox
.selectionStart
=== null) {
188 for (var i
= 0; i
< mwEditButtons
.length
; i
++) {
189 mwInsertEditButton(toolbar
, mwEditButtons
[i
]);
191 for (var i
= 0; i
< mwCustomEditButtons
.length
; i
++) {
192 mwInsertEditButton(toolbar
, mwCustomEditButtons
[i
]);
197 function escapeQuotes(text
) {
198 var re
= new RegExp("'","g");
199 text
= text
.replace(re
,"\\'");
200 re
= new RegExp("\\n","g");
201 text
= text
.replace(re
,"\\n");
202 return escapeQuotesHTML(text
);
205 function escapeQuotesHTML(text
) {
206 var re
= new RegExp('&',"g");
207 text
= text
.replace(re
,"&");
208 re
= new RegExp('"',"g");
209 text
= text
.replace(re
,""");
210 re
= new RegExp('<',"g");
211 text
= text
.replace(re
,"<");
212 re
= new RegExp('>',"g");
213 text
= text
.replace(re
,">");
217 // apply tagOpen/tagClose to selection in textarea,
218 // use sampleText instead of selection if there is none
219 function insertTags(tagOpen
, tagClose
, sampleText
) {
221 if (document
.editform
) {
222 txtarea
= document
.editform
.wpTextbox1
;
224 // some alternate form? take the first one we can find
225 var areas
= document
.getElementsByTagName('textarea');
228 var selText
, isSample
= false;
230 if (document
.selection
&& document
.selection
.createRange
) { // IE/Opera
232 //save window scroll position
233 if (document
.documentElement
&& document
.documentElement
.scrollTop
)
234 var winScroll
= document
.documentElement
.scrollTop
235 else if (document
.body
)
236 var winScroll
= document
.body
.scrollTop
;
237 //get current selection
239 var range
= document
.selection
.createRange();
240 selText
= range
.text
;
243 range
.text
= tagOpen
+ selText
+ tagClose
;
244 //mark sample text as selected
245 if (isSample
&& range
.moveStart
) {
247 tagClose
= tagClose
.replace(/\n/g,'');
248 range
.moveStart('character', - tagClose
.length
- selText
.length
);
249 range
.moveEnd('character', - tagClose
.length
);
252 //restore window scroll position
253 if (document
.documentElement
&& document
.documentElement
.scrollTop
)
254 document
.documentElement
.scrollTop
= winScroll
255 else if (document
.body
)
256 document
.body
.scrollTop
= winScroll
;
258 } else if (txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') { // Mozilla
260 //save textarea scroll position
261 var textScroll
= txtarea
.scrollTop
;
262 //get current selection
264 var startPos
= txtarea
.selectionStart
;
265 var endPos
= txtarea
.selectionEnd
;
266 selText
= txtarea
.value
.substring(startPos
, endPos
);
269 txtarea
.value
= txtarea
.value
.substring(0, startPos
)
270 + tagOpen
+ selText
+ tagClose
271 + txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
274 txtarea
.selectionStart
= startPos
+ tagOpen
.length
;
275 txtarea
.selectionEnd
= startPos
+ tagOpen
.length
+ selText
.length
;
277 txtarea
.selectionStart
= startPos
+ tagOpen
.length
+ selText
.length
+ tagClose
.length
;
278 txtarea
.selectionEnd
= txtarea
.selectionStart
;
280 //restore textarea scroll position
281 txtarea
.scrollTop
= textScroll
;
284 function checkSelectedText(){
286 selText
= sampleText
;
288 } else if (selText
.charAt(selText
.length
- 1) == ' ') { //exclude ending space char
289 selText
= selText
.substring(0, selText
.length
- 1);
298 * Set the accesskey prefix based on browser detection.
300 var tooltipAccessKeyPrefix
= 'alt-';
302 tooltipAccessKeyPrefix
= 'shift-esc-';
304 || navigator
.userAgent
.toLowerCase().indexOf('mac') != -1
305 || navigator
.userAgent
.toLowerCase().indexOf('konqueror') != -1 ) {
306 tooltipAccessKeyPrefix
= 'ctrl-';
308 tooltipAccessKeyPrefix
= 'alt-shift-';
310 var tooltipAccessKeyRegexp
= /\[(ctrl-)?(alt-)?(shift-)?(esc-)?.\]$/;
313 * Add the appropriate prefix to the accesskey shown in the tooltip.
314 * If the nodeList parameter is given, only those nodes are updated;
315 * otherwise, all the nodes that will probably have accesskeys by
316 * default are updated.
318 * @param Array nodeList -- list of elements to update
320 function updateTooltipAccessKeys( nodeList
) {
322 // skins without a "column-one" element don't seem to have links with accesskeys either
323 var columnOne
= document
.getElementById("column-one");
325 updateTooltipAccessKeys( columnOne
.getElementsByTagName("a") );
326 // these are rare enough that no such optimization is needed
327 updateTooltipAccessKeys( document
.getElementsByTagName("input") );
328 updateTooltipAccessKeys( document
.getElementsByTagName("label") );
332 for ( var i
= 0; i
< nodeList
.length
; i
++ ) {
333 var element
= nodeList
[i
];
334 var tip
= element
.getAttribute("title");
335 var key
= element
.getAttribute("accesskey");
336 if ( key
&& tooltipAccessKeyRegexp
.exec(tip
) ) {
337 tip
= tip
.replace(tooltipAccessKeyRegexp
,
338 "["+tooltipAccessKeyPrefix
+key
+"]");
339 element
.setAttribute("title", tip
);
345 * Add a link to one of the portlet menus on the page, including:
347 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
348 * p-personal: Personal tools (shown at the top right of the page in Monobook)
349 * p-navigation: Navigation
352 * This function exists for the convenience of custom JS authors. All
353 * but the first three parameters are optional, though providing at
354 * least an id and a tooltip is recommended.
356 * By default the new link will be added to the end of the list. To
357 * add the link before a given existing item, pass the DOM node of
358 * that item (easily obtained with document.getElementById()) as the
359 * nextnode parameter; to add the link _after_ an existing item, pass
360 * the node's nextSibling instead.
362 * @param String portlet -- id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
363 * @param String href -- link URL
364 * @param String text -- link text (will be automatically lowercased by CSS for p-cactions in Monobook)
365 * @param String id -- id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
366 * @param String tooltip -- text to show when hovering over the link, without accesskey suffix
367 * @param String accesskey -- accesskey to activate this link (one character, try to avoid conflicts)
368 * @param Node nextnode -- the DOM node before which the new item should be added, should be another item in the same list
370 * @return Node -- the DOM node of the new item (an LI element) or null
372 function addPortletLink(portlet
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
373 var node
= document
.getElementById(portlet
);
374 if ( !node
) return null;
375 node
= node
.getElementsByTagName( "ul" )[0];
376 if ( !node
) return null;
378 var link
= document
.createElement( "a" );
379 link
.appendChild( document
.createTextNode( text
) );
382 var item
= document
.createElement( "li" );
383 item
.appendChild( link
);
384 if ( id
) item
.id
= id
;
387 link
.setAttribute( "accesskey", accesskey
);
388 tooltip
+= " ["+accesskey
+"]";
391 link
.setAttribute( "title", tooltip
);
393 if ( accesskey
&& tooltip
) {
394 updateTooltipAccessKeys( new Array( link
) );
397 if ( nextnode
&& nextnode
.parentNode
== node
)
398 node
.insertBefore( item
, nextnode
);
400 node
.appendChild( item
); // IE compatibility (?)
407 * Set up accesskeys/tooltips from the deprecated ta array. If doId
408 * is specified, only set up for that id. Note that this function is
409 * deprecated and will not be supported indefinitely -- use
410 * updateTooltipAccessKey() instead.
412 * @param mixed doId string or null
414 function akeytt( doId
) {
415 // A lot of user scripts (and some of the code below) break if
416 // ta isn't defined, so we make sure it is. Explictly using
417 // window.ta avoids a "ta is not defined" error.
418 if (!window
.ta
) window
.ta
= new Array
;
420 // Make a local, possibly restricted, copy to avoid clobbering
429 // Now deal with evil deprecated ta
430 var watchCheckboxExists
= document
.getElementById( 'wpWatchthis' ) ? true : false;
432 var n
= document
.getElementById(id
);
436 // Are we putting accesskey in it
437 if (ta
[id
][0].length
> 0) {
438 // Is this object a object? If not assume it's the next child.
440 if (n
.nodeName
.toLowerCase() == "a") {
445 // Don't add an accesskey for the watch tab if the watch
446 // checkbox is also available.
447 if (a
&& ((id
!= 'ca-watch' && id
!= 'ca-unwatch') || !watchCheckboxExists
)) {
448 a
.accessKey
= ta
[id
][0];
449 ak
= ' ['+tooltipAccessKeyPrefix
+ta
[id
][0]+']';
452 // We don't care what type the object is when assigning tooltip
458 a
.title
= ta
[id
][1]+ak
;
467 function setupCheckboxShiftClick() {
470 var inputs
= document
.getElementsByTagName('input');
471 addCheckboxClickHandlers(inputs
);
474 function addCheckboxClickHandlers(inputs
, start
) {
475 if ( !start
) start
= 0;
477 var finish
= start
+ 250;
478 if ( finish
> inputs
.length
)
479 finish
= inputs
.length
;
481 for ( var i
= start
; i
< finish
; i
++ ) {
483 if ( !cb
.type
|| cb
.type
.toLowerCase() != 'checkbox' )
485 var end
= checkboxes
.length
;
486 checkboxes
[end
] = cb
;
488 cb
.onclick
= checkboxClickHandler
;
491 if ( finish
< inputs
.length
) {
492 setTimeout( function () {
493 addCheckboxClickHandlers(inputs
, finish
);
498 function checkboxClickHandler(e
) {
499 if (typeof e
== 'undefined') {
502 if ( !e
.shiftKey
|| lastCheckbox
=== null ) {
503 lastCheckbox
= this.index
;
506 var endState
= this.checked
;
508 if ( this.index
< lastCheckbox
) {
509 start
= this.index
+ 1;
510 finish
= lastCheckbox
;
512 start
= lastCheckbox
;
513 finish
= this.index
- 1;
515 for (var i
= start
; i
<= finish
; ++i
) {
516 checkboxes
[i
].checked
= endState
;
518 lastCheckbox
= this.index
;
522 function toggle_element_activation(ida
,idb
) {
523 if (!document
.getElementById
) {
526 document
.getElementById(ida
).disabled
=true;
527 document
.getElementById(idb
).disabled
=false;
530 function toggle_element_check(ida
,idb
) {
531 if (!document
.getElementById
) {
534 document
.getElementById(ida
).checked
=true;
535 document
.getElementById(idb
).checked
=false;
539 * Restore the edit box scroll state following a preview operation,
540 * and set up a form submission handler to remember this state
542 function scrollEditBox() {
543 var editBox
= document
.getElementById( 'wpTextbox1' );
544 var scrollTop
= document
.getElementById( 'wpScrolltop' );
545 var editForm
= document
.getElementById( 'editform' );
546 if( editBox
&& scrollTop
) {
547 if( scrollTop
.value
)
548 editBox
.scrollTop
= scrollTop
.value
;
549 addHandler( editForm
, 'submit', function() {
550 document
.getElementById( 'wpScrolltop' ).value
= document
.getElementById( 'wpTextbox1' ).scrollTop
;
554 hookEvent( 'load', scrollEditBox
);
557 Written by Jonathan Snook, http://www.snook.ca/jonathan
558 Add-ons by Robert Nyman, http://www.robertnyman.com
559 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
560 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
562 function getElementsByClassName(oElm
, strTagName
, oClassNames
){
563 var arrElements
= (strTagName
== "*" && oElm
.all
)? oElm
.all
: oElm
.getElementsByTagName(strTagName
);
564 var arrReturnElements
= new Array();
565 var arrRegExpClassNames
= new Array();
566 if(typeof oClassNames
== "object"){
567 for(var i
=0; i
<oClassNames
.length
; i
++){
568 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
569 new RegExp("(^|\\s)" + oClassNames
[i
].replace(/\-/g, "\\-") + "(\\s|$)");
573 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
574 new RegExp("(^|\\s)" + oClassNames
.replace(/\-/g, "\\-") + "(\\s|$)");
578 for(var j
=0; j
<arrElements
.length
; j
++){
579 oElement
= arrElements
[j
];
581 for(var k
=0; k
<arrRegExpClassNames
.length
; k
++){
582 if(!arrRegExpClassNames
[k
].test(oElement
.className
)){
588 arrReturnElements
[arrReturnElements
.length
] = oElement
;
591 return (arrReturnElements
)
594 function redirectToFragment(fragment
) {
595 var match
= navigator
.userAgent
.match(/AppleWebKit\/(\d+)/);
597 var webKitVersion
= parseInt(match
[1]);
598 if (webKitVersion
< 420) {
599 // Released Safari w/ WebKit 418.9.1 messes up horribly
600 // Nightlies of 420+ are ok
605 // Mozilla needs to wait until after load, otherwise the window doesn't scroll
606 addOnloadHook(function () {
607 if (window
.location
.hash
== "")
608 window
.location
.hash
= fragment
;
611 if (window
.location
.hash
== "")
612 window
.location
.hash
= fragment
;
617 * Table sorting script by Joost de Valk, check it out at http://www.joostdevalk.nl/code/sortable-table/.
618 * Based on a script from http://www.kryogenix.org/code/browser/sorttable/.
619 * Distributed under the MIT license: http://www.kryogenix.org/code/browser/licence.html .
621 * Copyright (c) 1997-2006 Stuart Langridge, Joost de Valk.
623 * @todo don't break on colspans/rowspans (bug 8028)
624 * @todo language-specific digit grouping/decimals (bug 8063)
625 * @todo support all accepted date formats (bug 8226)
628 var ts_image_path
= stylepath
+"/common/images/";
629 var ts_image_up
= "sort_up.gif";
630 var ts_image_down
= "sort_down.gif";
631 var ts_image_none
= "sort_none.gif";
632 var ts_europeandate
= wgContentLanguage
!= "en"; // The non-American-inclined can change to "true"
633 var ts_alternate_row_colors
= true;
634 var SORT_COLUMN_INDEX
;
636 function sortables_init() {
638 // Find all tables with class sortable and make them sortable
639 var tables
= getElementsByClassName(document
, "table", "sortable");
640 for (var ti
= 0; ti
< tables
.length
; ti
++) {
641 if (!tables
[ti
].id
) {
642 tables
[ti
].setAttribute('id','sortable_table_id_'+idnum
);
645 ts_makeSortable(tables
[ti
]);
649 function ts_makeSortable(table
) {
651 if (table
.rows
&& table
.rows
.length
> 0) {
652 if (table
.tHead
&& table
.tHead
.rows
.length
> 0) {
653 firstRow
= table
.tHead
.rows
[table
.tHead
.rows
.length
-1];
655 firstRow
= table
.rows
[0];
658 if (!firstRow
) return;
660 // We have a first row: assume it's the header, and make its contents clickable links
661 for (var i
= 0; i
< firstRow
.cells
.length
; i
++) {
662 var cell
= firstRow
.cells
[i
];
663 if ((" "+cell
.className
+" ").indexOf(" unsortable ") == -1) {
664 cell
.innerHTML
+= ' <a href="#" class="sortheader" onclick="ts_resortTable(this);return false;"><span class="sortarrow"><img src="'+ ts_image_path
+ ts_image_none
+ '" alt="↓"/></span></a>';
667 if (ts_alternate_row_colors
) {
672 function ts_getInnerText(el
) {
673 if (typeof el
== "string") return el
;
674 if (typeof el
== "undefined") { return el
};
675 if (el
.textContent
) return el
.textContent
; // not needed but it is faster
676 if (el
.innerText
) return el
.innerText
; // IE doesn't have textContent
679 var cs
= el
.childNodes
;
681 for (var i
= 0; i
< l
; i
++) {
682 switch (cs
[i
].nodeType
) {
683 case 1: //ELEMENT_NODE
684 str
+= ts_getInnerText(cs
[i
]);
687 str
+= cs
[i
].nodeValue
;
694 function ts_resortTable(lnk
) {
696 var span
= lnk
.getElementsByTagName('span')[0];
698 var td
= lnk
.parentNode
;
699 var tr
= td
.parentNode
;
700 var column
= td
.cellIndex
;
702 var table
= tr
.parentNode
;
703 while (table
&& !(table
.tagName
&& table
.tagName
.toLowerCase() == 'table'))
704 table
= table
.parentNode
;
707 // Work out a type for the column
708 if (table
.rows
.length
<= 1) return;
710 // Skip the first row if that's where the headings are
711 var rowStart
= (table
.tHead
&& table
.tHead
.rows
.length
> 0 ? 0 : 1);
714 for (var i
= rowStart
; i
< table
.rows
.length
; i
++) {
715 if (table
.rows
[i
].cells
.length
> column
) {
716 itm
= ts_getInnerText(table
.rows
[i
].cells
[column
]);
717 itm
= itm
.replace(/^[\s\xa0]+/, "").replace(/[\s\xa0]+$/, "");
718 if (itm
!= "") break;
722 sortfn
= ts_sort_caseinsensitive
;
723 if (itm
.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/))
724 sortfn
= ts_sort_date
;
725 if (itm
.match(/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/))
726 sortfn
= ts_sort_date
;
727 if (itm
.match(/^\d\d[\/.-]\d\d[\/.-]\d\d$/))
728 sortfn
= ts_sort_date
;
729 if (itm
.match(/^[\u00a3$\u20ac]/)) // pound dollar euro
730 sortfn
= ts_sort_currency
;
731 if (itm
.match(/^[\d.,]+\%?$/))
732 sortfn
= ts_sort_numeric
;
734 var reverse
= (span
.getAttribute("sortdir") == 'down');
736 var newRows
= new Array();
737 for (var j
= rowStart
; j
< table
.rows
.length
; j
++) {
738 var row
= table
.rows
[j
];
739 var keyText
= ts_getInnerText(row
.cells
[column
]);
740 var oldIndex
= (reverse
? -j
: j
);
742 newRows
[newRows
.length
] = new Array(row
, keyText
, oldIndex
);
745 newRows
.sort(sortfn
);
749 arrowHTML
= '<img src="'+ ts_image_path
+ ts_image_down
+ '" alt="↓"/>';
751 span
.setAttribute('sortdir','up');
753 arrowHTML
= '<img src="'+ ts_image_path
+ ts_image_up
+ '" alt="↑"/>';
754 span
.setAttribute('sortdir','down');
757 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
758 // don't do sortbottom rows
759 for (var i
= 0; i
< newRows
.length
; i
++) {
760 if ((" "+newRows
[i
][0].className
+" ").indexOf(" sortbottom ") == -1)
761 table
.tBodies
[0].appendChild(newRows
[i
][0]);
763 // do sortbottom rows only
764 for (var i
= 0; i
< newRows
.length
; i
++) {
765 if ((" "+newRows
[i
][0].className
+" ").indexOf(" sortbottom ") != -1)
766 table
.tBodies
[0].appendChild(newRows
[i
][0]);
769 // Delete any other arrows there may be showing
770 var spans
= getElementsByClassName(tr
, "span", "sortarrow");
771 for (var i
= 0; i
< spans
.length
; i
++) {
772 spans
[i
].innerHTML
= '<img src="'+ ts_image_path
+ ts_image_none
+ '" alt="↓"/>';
774 span
.innerHTML
= arrowHTML
;
779 function ts_dateToSortKey(date
) {
780 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
781 if (date
.length
== 11) {
782 switch (date
.substr(3,3).toLowerCase()) {
783 case "jan": var month
= "01"; break;
784 case "feb": var month
= "02"; break;
785 case "mar": var month
= "03"; break;
786 case "apr": var month
= "04"; break;
787 case "may": var month
= "05"; break;
788 case "jun": var month
= "06"; break;
789 case "jul": var month
= "07"; break;
790 case "aug": var month
= "08"; break;
791 case "sep": var month
= "09"; break;
792 case "oct": var month
= "10"; break;
793 case "nov": var month
= "11"; break;
794 case "dec": var month
= "12"; break;
795 // default: var month = "00";
797 return date
.substr(7,4)+month
+date
.substr(0,2);
798 } else if (date
.length
== 10) {
799 if (ts_europeandate
== false) {
800 return date
.substr(6,4)+date
.substr(0,2)+date
.substr(3,2);
802 return date
.substr(6,4)+date
.substr(3,2)+date
.substr(0,2);
804 } else if (date
.length
== 8) {
805 yr
= date
.substr(6,2);
806 if (parseInt(yr
) < 50) {
811 if (ts_europeandate
== true) {
812 return yr
+date
.substr(3,2)+date
.substr(0,2);
814 return yr
+date
.substr(0,2)+date
.substr(3,2);
820 function ts_parseFloat(num
) {
822 num
= parseFloat(num
.replace(/,/g
, ""));
823 return (isNaN(num
) ? 0 : num
);
826 function ts_sort_date(a
,b
) {
827 var aa
= ts_dateToSortKey(a
[1]);
828 var bb
= ts_dateToSortKey(b
[1]);
829 return (aa
< bb
? -1 : aa
> bb
? 1 : a
[2] - b
[2]);
832 function ts_sort_currency(a
,b
) {
833 var aa
= ts_parseFloat(a
[1].replace(/[^0-9.]/g,''));
834 var bb
= ts_parseFloat(b
[1].replace(/[^0-9.]/g,''));
835 return (aa
!= bb
? aa
- bb
: a
[2] - b
[2]);
838 function ts_sort_numeric(a
,b
) {
839 var aa
= ts_parseFloat(a
[1]);
840 var bb
= ts_parseFloat(b
[1]);
841 return (aa
!= bb
? aa
- bb
: a
[2] - b
[2]);
844 function ts_sort_caseinsensitive(a
,b
) {
845 var aa
= a
[1].toLowerCase();
846 var bb
= b
[1].toLowerCase();
847 return (aa
< bb
? -1 : aa
> bb
? 1 : a
[2] - b
[2]);
850 function ts_sort_default(a
,b
) {
851 return (a
[1] < b
[1] ? -1 : a
[1] > b
[1] ? 1 : a
[2] - b
[2]);
854 function ts_alternate(table
) {
855 // Take object table and get all it's tbodies.
856 var tableBodies
= table
.getElementsByTagName("tbody");
857 // Loop through these tbodies
858 for (var i
= 0; i
< tableBodies
.length
; i
++) {
859 // Take the tbody, and get all it's rows
860 var tableRows
= tableBodies
[i
].getElementsByTagName("tr");
861 // Loop through these rows
862 // Start at 1 because we want to leave the heading row untouched
863 for (var j
= 0; j
< tableRows
.length
; j
++) {
864 // Check if j is even, and apply classes for both possible results
865 var oldClasses
= tableRows
[j
].className
.split(" ");
866 var newClassName
= "";
867 for (var k
= 0; k
< oldClasses
.length
; k
++) {
868 if (oldClasses
[k
] != "" && oldClasses
[k
] != "even" && oldClasses
[k
] != "odd")
869 newClassName
+= oldClasses
[k
] + " ";
871 tableRows
[j
].className
= newClassName
+ (j
% 2 == 0 ? "even" : "odd");
877 * End of table sorting code
882 * Add a cute little box at the top of the screen to inform the user of
883 * something, replacing any preexisting message.
885 * @param String message HTML to be put inside the right div
886 * @param String className Used in adding a class; should be different for each
887 * call to allow CSS/JS to hide different boxes. null = no class used.
888 * @return Boolean True on success, false on failure
890 function jsMsg( message
, className
) {
891 if ( !document
.getElementById
) {
894 // We special-case skin structures provided by the software. Skins that
895 // choose to abandon or significantly modify our formatting can just define
896 // an mw-js-message div to start with.
897 var messageDiv
= document
.getElementById( 'mw-js-message' );
899 messageDiv
= document
.createElement( 'div' );
900 if ( document
.getElementById( 'column-content' )
901 && document
.getElementById( 'content' ) ) {
902 // MonoBook, presumably
903 document
.getElementById( 'content' ).insertBefore(
905 document
.getElementById( 'content' ).firstChild
907 } else if ( document
.getElementById('content')
908 && document
.getElementById( 'article' ) ) {
909 // Non-Monobook but still recognizable (old-style)
910 document
.getElementById( 'article').insertBefore(
912 document
.getElementById( 'article' ).firstChild
919 messageDiv
.setAttribute( 'id', 'mw-js-message' );
921 messageDiv
.setAttribute( 'class', 'mw-js-message-'+className
);
923 messageDiv
.innerHTML
= message
;
928 * Inject a cute little progress spinner after the specified element
930 * @param element Element to inject after
931 * @param id Identifier string (for use with removeSpinner(), below)
933 function injectSpinner( element
, id
) {
934 var spinner
= document
.createElement( "img" );
935 spinner
.id
= "mw-spinner-" + id
;
936 spinner
.src
= stylepath
+ "/common/images/spinner.gif";
937 spinner
.alt
= spinner
.title
= "...";
938 if( element
.nextSibling
) {
939 element
.parentNode
.insertBefore( spinner
, element
.nextSibling
);
941 element
.parentNode
.appendChild( spinner
);
946 * Remove a progress spinner added with injectSpinner()
948 * @param id Identifier string
950 function removeSpinner( id
) {
951 var spinner
= document
.getElementById( "mw-spinner-" + id
);
953 spinner
.parentNode
.removeChild( spinner
);
957 function runOnloadHook() {
958 // don't run anything below this for non-dom browsers
959 if (doneOnloadHook
|| !(document
.getElementById
&& document
.getElementsByTagName
)) {
963 // set this before running any hooks, since any errors below
964 // might cause the function to terminate prematurely
965 doneOnloadHook
= true;
967 updateTooltipAccessKeys( null );
970 setupCheckboxShiftClick();
973 // Run any added-on functions
974 for (var i
= 0; i
< onloadFuncts
.length
; i
++) {
980 * Add an event handler to an element
982 * @param Element element Element to add handler to
983 * @param String attach Event to attach to
984 * @param callable handler Event handler callback
986 function addHandler( element
, attach
, handler
) {
987 if( window
.addEventListener
) {
988 element
.addEventListener( attach
, handler
, false );
989 } else if( window
.attachEvent
) {
990 element
.attachEvent( 'on' + attach
, handler
);
995 * Add a click event handler to an element
997 * @param Element element Element to add handler to
998 * @param callable handler Event handler callback
1000 function addClickHandler( element
, handler
) {
1001 addHandler( element
, 'click', handler
);
1003 //note: all skins should call runOnloadHook() at the end of html output,
1004 // so the below should be redundant. It's there just in case.
1005 hookEvent("load", runOnloadHook
);
1006 hookEvent("load", mwSetupToolbar
);