4fb94e682abff3dc5d59ef325285b6c5f44f1fec
2 * MediaWiki legacy wikibits
6 window
.clientPC
= navigator
.userAgent
.toLowerCase(); // Get client info
7 window
.is_gecko
= /gecko/.test( clientPC
) &&
8 !/khtml|spoofer|netscape\/7\.0/.test(clientPC
);
10 window
.is_safari
= window
.is_safari_win
= window
.webkit_version
=
11 window
.is_chrome
= window
.is_chrome_mac
= false;
12 window
.webkit_match
= clientPC
.match(/applewebkit\/(\d+)/);
14 window
.is_safari
= clientPC
.indexOf('applewebkit') != -1 &&
15 clientPC
.indexOf('spoofer') == -1;
16 window
.is_safari_win
= is_safari
&& clientPC
.indexOf('windows') != -1;
17 window
.webkit_version
= parseInt(webkit_match
[1]);
18 // Tests for chrome here, to avoid breaking old scripts safari left alone
19 // This is here for accesskeys
20 window
.is_chrome
= clientPC
.indexOf('chrome') !== -1 &&
21 clientPC
.indexOf('spoofer') === -1;
22 window
.is_chrome_mac
= is_chrome
&& clientPC
.indexOf('mac') !== -1
25 // For accesskeys; note that FF3+ is included here!
26 window
.is_ff2
= /firefox\/[2-9]|minefield\/3/.test( clientPC
);
27 window
.ff2_bugs
= /firefox\/2/.test( clientPC
);
28 // These aren't used here, but some custom scripts rely on them
29 window
.is_ff2_win
= is_ff2
&& clientPC
.indexOf('windows') != -1;
30 window
.is_ff2_x11
= is_ff2
&& clientPC
.indexOf('x11') != -1;
32 window
.is_opera
= window
.is_opera_preseven
= window
.is_opera_95
=
33 window
.opera6_bugs
= window
.opera7_bugs
= window
.opera95_bugs
= false;
34 if (clientPC
.indexOf('opera') != -1) {
35 window
.is_opera
= true;
36 window
.is_opera_preseven
= window
.opera
&& !document
.childNodes
;
37 window
.is_opera_seven
= window
.opera
&& document
.childNodes
;
38 window
.is_opera_95
= /opera\/(9\.[5-9]|[1-9][0-9])/.test( clientPC
);
39 window
.opera6_bugs
= is_opera_preseven
;
40 window
.opera7_bugs
= is_opera_seven
&& !is_opera_95
;
41 window
.opera95_bugs
= /opera\/(9\.5)/.test( clientPC
);
43 // As recommended by <http://msdn.microsoft.com/en-us/library/ms537509.aspx>,
44 // avoiding false positives from moronic extensions that append to the IE UA
46 window
.ie6_bugs
= false;
47 if ( /msie ([0-9]{1,}[\.0-9]{0,})/.exec( clientPC
) != null
48 && parseFloat( RegExp
.$1 ) <= 6.0 ) {
52 // Global external objects used by this script.
55 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
56 window
.doneOnloadHook
= undefined;
58 if (!window
.onloadFuncts
) {
59 window
.onloadFuncts
= [];
62 window
.addOnloadHook = function( hookFunct
) {
63 // Allows add-on scripts to add onload functions
64 if( !doneOnloadHook
) {
65 onloadFuncts
[onloadFuncts
.length
] = hookFunct
;
67 hookFunct(); // bug in MSIE script loading
71 window
.importScript = function( page
) {
72 // TODO: might want to introduce a utility function to match wfUrlencode() in PHP
73 var uri
= wgScript
+ '?title=' +
74 encodeURIComponent(page
.replace(/ /g,'_')).replace(/%2F
/ig,'/').replace(/%3A/ig,':') +
75 '&action
=raw
&ctype
=text
/javascript
';
76 return importScriptURI( uri );
79 window.loadedScripts = {}; // included-scripts tracker
80 window.importScriptURI = function( url ) {
81 if ( loadedScripts[url] ) {
84 loadedScripts[url] = true;
85 var s = document.createElement( 'script
' );
86 s.setAttribute( 'src
', url );
87 s.setAttribute( 'type
', 'text
/javascript
' );
88 document.getElementsByTagName('head
')[0].appendChild( s );
92 window.importStylesheet = function( page ) {
93 return importStylesheetURI( wgScript + '?action
=raw
&ctype
=text
/css&title=' + encodeURIComponent( page.replace(/ /g
,'_') ) );
96 window
.importStylesheetURI = function( url
, media
) {
97 var l
= document
.createElement( 'link' );
104 document
.getElementsByTagName('head')[0].appendChild( l
);
108 window
.appendCSS = function( text
) {
109 var s
= document
.createElement( 'style' );
111 s
.rel
= 'stylesheet';
112 if ( s
.styleSheet
) {
113 s
.styleSheet
.cssText
= text
; // IE
115 s
.appendChild( document
.createTextNode( text
+ '' ) ); // Safari sometimes borks on null
117 document
.getElementsByTagName('head')[0].appendChild( s
);
121 // Special stylesheet links for Monobook only (see bug 14717)
122 var skinpath
= mw
.config
.get( 'stylepath' ) + '/' + mw
.config
.get( 'skin' );
123 if ( mw
.config
.get( 'skin' ) === 'monobook' ) {
125 importStylesheetURI( skinpath
+ '/Opera6Fixes.css' );
126 } else if ( opera7_bugs
) {
127 importStylesheetURI( skinpath
+ '/Opera7Fixes.css' );
128 } else if ( opera95_bugs
) {
129 importStylesheetURI( skinpath
+ '/Opera9Fixes.css' );
130 } else if ( ff2_bugs
) {
131 importStylesheetURI( skinpath
+ '/FF2Fixes.css' );
135 if ( mw
.config
.get( 'wgBreakFrames' ) ) {
136 // Un-trap us from framesets
137 if ( window
.top
!= window
) {
138 window
.top
.location
= window
.location
;
142 window
.changeText = function( el
, newText
) {
143 // Safari work around
144 if ( el
.innerText
) {
145 el
.innerText
= newText
;
146 } else if ( el
.firstChild
&& el
.firstChild
.nodeValue
) {
147 el
.firstChild
.nodeValue
= newText
;
151 window
.killEvt = function( evt
) {
152 evt
= evt
|| window
.event
|| window
.Event
; // W3C, IE, Netscape
153 if ( typeof ( evt
.preventDefault
) != 'undefined' ) {
154 evt
.preventDefault(); // Don't follow the link
155 evt
.stopPropagation();
157 evt
.cancelBubble
= true; // IE
159 return false; // Don't follow the link (IE)
162 window
.mwEditButtons
= [];
163 window
.mwCustomEditButtons
= []; // eg to add in MediaWiki:Common.js
165 window
.escapeQuotes = function( text
) {
166 var re
= new RegExp( "'", "g" );
167 text
= text
.replace( re
, "\\'" );
168 re
= new RegExp( "\\n", "g" );
169 text
= text
.replace( re
, "\\n" );
170 return escapeQuotesHTML( text
);
173 window
.escapeQuotesHTML = function( text
) {
174 var re
= new RegExp( '&', "g" );
175 text
= text
.replace( re
, "&" );
176 re
= new RegExp( '"', "g" );
177 text
= text
.replace( re
, """ );
178 re
= new RegExp( '<', "g" );
179 text
= text
.replace( re
, "<" );
180 re
= new RegExp( '>', "g" );
181 text
= text
.replace( re
, ">" );
186 * Set the accesskey prefix based on browser detection.
188 window
.tooltipAccessKeyPrefix
= 'alt-';
190 tooltipAccessKeyPrefix
= 'shift-esc-';
191 } else if ( is_chrome
) {
192 tooltipAccessKeyPrefix
= is_chrome_mac
? 'ctrl-option-' : 'alt-';
193 } else if ( !is_safari_win
&& is_safari
&& webkit_version
> 526 ) {
194 tooltipAccessKeyPrefix
= 'ctrl-alt-';
195 } else if ( !is_safari_win
&& ( is_safari
196 || clientPC
.indexOf('mac') != -1
197 || clientPC
.indexOf('konqueror') != -1 ) ) {
198 tooltipAccessKeyPrefix
= 'ctrl-';
199 } else if ( is_ff2
) {
200 tooltipAccessKeyPrefix
= 'alt-shift-';
202 window
.tooltipAccessKeyRegexp
= /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/;
205 * Add the appropriate prefix to the accesskey shown in the tooltip.
206 * If the nodeList parameter is given, only those nodes are updated;
207 * otherwise, all the nodes that will probably have accesskeys by
208 * default are updated.
210 * @param nodeList Array list of elements to update
212 window
.updateTooltipAccessKeys = function( nodeList
) {
214 // Rather than scan all links on the whole page, we can just scan these
215 // containers which contain the relevant links. This is really just an
216 // optimization technique.
217 var linkContainers
= [
218 'column-one', // Monobook and Modern
219 'mw-head', 'mw-panel', 'p-logo' // Vector
221 for ( var i
in linkContainers
) {
222 var linkContainer
= document
.getElementById( linkContainers
[i
] );
223 if ( linkContainer
) {
224 updateTooltipAccessKeys( linkContainer
.getElementsByTagName( 'a' ) );
227 // these are rare enough that no such optimization is needed
228 updateTooltipAccessKeys( document
.getElementsByTagName( 'input' ) );
229 updateTooltipAccessKeys( document
.getElementsByTagName( 'label' ) );
233 for ( var i
= 0; i
< nodeList
.length
; i
++ ) {
234 var element
= nodeList
[i
];
235 var tip
= element
.getAttribute( 'title' );
236 if ( tip
&& tooltipAccessKeyRegexp
.exec( tip
) ) {
237 tip
= tip
.replace(tooltipAccessKeyRegexp
,
238 '[' + tooltipAccessKeyPrefix
+ "$5]");
239 element
.setAttribute( 'title', tip
);
245 * Add a link to one of the portlet menus on the page, including:
247 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
248 * p-personal: Personal tools (shown at the top right of the page in Monobook)
249 * p-navigation: Navigation
252 * This function exists for the convenience of custom JS authors. All
253 * but the first three parameters are optional, though providing at
254 * least an id and a tooltip is recommended.
256 * By default the new link will be added to the end of the list. To
257 * add the link before a given existing item, pass the DOM node of
258 * that item (easily obtained with document.getElementById()) as the
259 * nextnode parameter; to add the link _after_ an existing item, pass
260 * the node's nextSibling instead.
262 * @param portlet String id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
263 * @param href String link URL
264 * @param text String link text (will be automatically lowercased by CSS for p-cactions in Monobook)
265 * @param id String id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
266 * @param tooltip String text to show when hovering over the link, without accesskey suffix
267 * @param accesskey String accesskey to activate this link (one character, try to avoid conflicts)
268 * @param nextnode Node the DOM node before which the new item should be added, should be another item in the same list
270 * @return Node -- the DOM node of the new item (an LI element) or null
272 window
.addPortletLink = function( portlet
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
273 var root
= document
.getElementById( portlet
);
277 var uls
= root
.getElementsByTagName( 'ul' );
279 if ( uls
.length
> 0 ) {
282 node
= document
.createElement( 'ul' );
283 var lastElementChild
= null;
284 for ( var i
= 0; i
< root
.childNodes
.length
; ++i
) { /* get root.lastElementChild */
285 if ( root
.childNodes
[i
].nodeType
== 1 ) {
286 lastElementChild
= root
.childNodes
[i
];
289 if ( lastElementChild
&& lastElementChild
.nodeName
.match( /div/i ) ) {
290 /* Insert into the menu divs */
291 lastElementChild
.appendChild( node
);
293 root
.appendChild( node
);
300 // unhide portlet if it was hidden before
301 root
.className
= root
.className
.replace( /(^| )emptyPortlet( |$)/, "$2" );
303 var link
= document
.createElement( 'a' );
304 link
.appendChild( document
.createTextNode( text
) );
307 // Wrap in a span - make it work with vector tabs and has no effect on any other portlets
308 var span
= document
.createElement( 'span' );
309 span
.appendChild( link
);
311 var item
= document
.createElement( 'li' );
312 item
.appendChild( span
);
318 link
.setAttribute( 'accesskey', accesskey
);
319 tooltip
+= ' [' + accesskey
+ ']';
322 link
.setAttribute( 'title', tooltip
);
324 if ( accesskey
&& tooltip
) {
325 updateTooltipAccessKeys( [link
] );
328 if ( nextnode
&& nextnode
.parentNode
== node
) {
329 node
.insertBefore( item
, nextnode
);
331 node
.appendChild( item
); // IE compatibility (?)
337 window
.getInnerText = function( el
) {
338 if ( typeof el
== 'string' ) {
341 if ( typeof el
== 'undefined' ) {
344 // Custom sort value through 'data-sort-value' attribute
345 // (no need to prepend hidden text to change sort value)
346 if ( el
.nodeType
&& el
.getAttribute( 'data-sort-value' ) !== null ) {
347 // Make sure it's a valid DOM element (.nodeType) and that the attribute is set (!null)
348 return el
.getAttribute( 'data-sort-value' );
350 if ( el
.textContent
) {
351 return el
.textContent
; // not needed but it is faster
353 if ( el
.innerText
) {
354 return el
.innerText
; // IE doesn't have textContent
358 var cs
= el
.childNodes
;
360 for ( var i
= 0; i
< l
; i
++ ) {
361 switch ( cs
[i
].nodeType
) {
362 case 1: // ELEMENT_NODE
363 str
+= getInnerText( cs
[i
] );
366 str
+= cs
[i
].nodeValue
;
373 window
.checkboxes
= undefined;
374 window
.lastCheckbox
= undefined;
376 window
.setupCheckboxShiftClick = function() {
379 var inputs
= document
.getElementsByTagName( 'input' );
380 addCheckboxClickHandlers( inputs
);
383 window
.addCheckboxClickHandlers = function( inputs
, start
) {
388 var finish
= start
+ 250;
389 if ( finish
> inputs
.length
) {
390 finish
= inputs
.length
;
393 for ( var i
= start
; i
< finish
; i
++ ) {
395 if ( !cb
.type
|| cb
.type
.toLowerCase() != 'checkbox' || ( ' ' + cb
.className
+ ' ' ).indexOf( ' noshiftselect ' ) != -1 ) {
398 var end
= checkboxes
.length
;
399 checkboxes
[end
] = cb
;
401 addClickHandler( cb
, checkboxClickHandler
);
404 if ( finish
< inputs
.length
) {
405 setTimeout( function() {
406 addCheckboxClickHandlers( inputs
, finish
);
411 window
.checkboxClickHandler = function( e
) {
412 if ( typeof e
== 'undefined' ) {
415 if ( !e
.shiftKey
|| lastCheckbox
=== null ) {
416 lastCheckbox
= this.index
;
419 var endState
= this.checked
;
421 if ( this.index
< lastCheckbox
) {
422 start
= this.index
+ 1;
423 finish
= lastCheckbox
;
425 start
= lastCheckbox
;
426 finish
= this.index
- 1;
428 for ( var i
= start
; i
<= finish
; ++i
) {
429 checkboxes
[i
].checked
= endState
;
430 if( i
> start
&& typeof checkboxes
[i
].onchange
== 'function' ) {
431 checkboxes
[i
].onchange(); // fire triggers
434 lastCheckbox
= this.index
;
440 Written by Jonathan Snook, http://www.snook.ca/jonathan
441 Add-ons by Robert Nyman, http://www.robertnyman.com
442 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
443 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
445 window
.getElementsByClassName = function( oElm
, strTagName
, oClassNames
) {
446 var arrReturnElements
= [];
447 if ( typeof( oElm
.getElementsByClassName
) == 'function' ) {
448 /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
449 var arrNativeReturn
= oElm
.getElementsByClassName( oClassNames
);
450 if ( strTagName
== '*' ) {
451 return arrNativeReturn
;
453 for ( var h
= 0; h
< arrNativeReturn
.length
; h
++ ) {
454 if( arrNativeReturn
[h
].tagName
.toLowerCase() == strTagName
.toLowerCase() ) {
455 arrReturnElements
[arrReturnElements
.length
] = arrNativeReturn
[h
];
458 return arrReturnElements
;
460 var arrElements
= ( strTagName
== '*' && oElm
.all
) ? oElm
.all
: oElm
.getElementsByTagName( strTagName
);
461 var arrRegExpClassNames
= [];
462 if( typeof oClassNames
== 'object' ) {
463 for( var i
= 0; i
< oClassNames
.length
; i
++ ) {
464 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
465 new RegExp("(^|\\s)" + oClassNames
[i
].replace(/\-/g, "\\-") + "(\\s|$)");
468 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
469 new RegExp("(^|\\s)" + oClassNames
.replace(/\-/g, "\\-") + "(\\s|$)");
473 for( var j
= 0; j
< arrElements
.length
; j
++ ) {
474 oElement
= arrElements
[j
];
476 for( var k
= 0; k
< arrRegExpClassNames
.length
; k
++ ) {
477 if( !arrRegExpClassNames
[k
].test( oElement
.className
) ) {
483 arrReturnElements
[arrReturnElements
.length
] = oElement
;
486 return ( arrReturnElements
);
489 window
.redirectToFragment = function( fragment
) {
490 var match
= navigator
.userAgent
.match(/AppleWebKit\/(\d+)/);
492 var webKitVersion
= parseInt( match
[1] );
493 if ( webKitVersion
< 420 ) {
494 // Released Safari w/ WebKit 418.9.1 messes up horribly
495 // Nightlies of 420+ are ok
499 if ( window
.location
.hash
== '' ) {
500 window
.location
.hash
= fragment
;
502 // Mozilla needs to wait until after load, otherwise the window doesn't
503 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
504 // There's no obvious way to detect this programmatically, so we use
505 // version-testing. If Firefox fixes the bug, they'll jump twice, but
506 // better twice than not at all, so make the fix hit future versions as
509 addOnloadHook(function() {
510 if ( window
.location
.hash
== fragment
) {
511 window
.location
.hash
= fragment
;
519 * Add a cute little box at the top of the screen to inform the user of
520 * something, replacing any preexisting message.
522 * @param message String -or- Dom Object HTML to be put inside the right div
523 * @param className String Used in adding a class; should be different for each
524 * call to allow CSS/JS to hide different boxes. null = no class used.
525 * @return Boolean True on success, false on failure
527 window
.jsMsg = function( message
, className
) {
528 if ( !document
.getElementById
) {
531 // We special-case skin structures provided by the software. Skins that
532 // choose to abandon or significantly modify our formatting can just define
533 // an mw-js-message div to start with.
534 var messageDiv
= document
.getElementById( 'mw-js-message' );
536 messageDiv
= document
.createElement( 'div' );
537 if ( document
.getElementById( 'column-content' )
538 && document
.getElementById( 'content' ) ) {
539 // MonoBook, presumably
540 document
.getElementById( 'content' ).insertBefore(
542 document
.getElementById( 'content' ).firstChild
544 } else if ( document
.getElementById( 'content' )
545 && document
.getElementById( 'article' ) ) {
546 // Non-Monobook but still recognizable (old-style)
547 document
.getElementById( 'article').insertBefore(
549 document
.getElementById( 'article' ).firstChild
556 messageDiv
.setAttribute( 'id', 'mw-js-message' );
557 messageDiv
.style
.display
= 'block';
559 messageDiv
.setAttribute( 'class', 'mw-js-message-' + className
);
562 if ( typeof message
=== 'object' ) {
563 while ( messageDiv
.hasChildNodes() ) { // Remove old content
564 messageDiv
.removeChild( messageDiv
.firstChild
);
566 messageDiv
.appendChild( message
); // Append new content
568 messageDiv
.innerHTML
= message
;
574 * Inject a cute little progress spinner after the specified element
576 * @param element Element to inject after
577 * @param id Identifier string (for use with removeSpinner(), below)
579 window
.injectSpinner = function( element
, id
) {
580 var spinner
= document
.createElement( 'img' );
581 spinner
.id
= 'mw-spinner-' + id
;
582 spinner
.src
= mw
.config
.get( 'stylepath' ) + '/common/images/spinner.gif';
583 spinner
.alt
= spinner
.title
= '...';
584 if( element
.nextSibling
) {
585 element
.parentNode
.insertBefore( spinner
, element
.nextSibling
);
587 element
.parentNode
.appendChild( spinner
);
592 * Remove a progress spinner added with injectSpinner()
594 * @param id Identifier string
596 window
.removeSpinner = function( id
) {
597 var spinner
= document
.getElementById( 'mw-spinner-' + id
);
599 spinner
.parentNode
.removeChild( spinner
);
603 window
.runOnloadHook = function() {
604 // don't run anything below this for non-dom browsers
605 if ( doneOnloadHook
|| !( document
.getElementById
&& document
.getElementsByTagName
) ) {
609 // set this before running any hooks, since any errors below
610 // might cause the function to terminate prematurely
611 doneOnloadHook
= true;
613 // Run any added-on functions
614 for ( var i
= 0; i
< onloadFuncts
.length
; i
++ ) {
620 * Add an event handler to an element
622 * @param element Element to add handler to
623 * @param attach String Event to attach to
624 * @param handler callable Event handler callback
626 window
.addHandler = function( element
, attach
, handler
) {
627 if( element
.addEventListener
) {
628 element
.addEventListener( attach
, handler
, false );
629 } else if( element
.attachEvent
) {
630 element
.attachEvent( 'on' + attach
, handler
);
634 window
.hookEvent = function( hookName
, hookFunct
) {
635 addHandler( window
, hookName
, hookFunct
);
639 * Add a click event handler to an element
641 * @param element Element to add handler to
642 * @param handler callable Event handler callback
644 window
.addClickHandler = function( element
, handler
) {
645 addHandler( element
, 'click', handler
);
649 * Removes an event handler from an element
651 * @param element Element to remove handler from
652 * @param remove String Event to remove
653 * @param handler callable Event handler callback to remove
655 window
.removeHandler = function( element
, remove
, handler
) {
656 if( window
.removeEventListener
) {
657 element
.removeEventListener( remove
, handler
, false );
658 } else if( window
.detachEvent
) {
659 element
.detachEvent( 'on' + remove
, handler
);
662 // note: all skins should call runOnloadHook() at the end of html output,
663 // so the below should be redundant. It's there just in case.
664 hookEvent( 'load', runOnloadHook
);
667 importScriptURI( mw
.config
.get( 'stylepath' ) + '/common/IEFixes.js' );