2 * MediaWiki legacy wikibits
4 /*jshint quotmark:false, onevar:false */
7 ua
= navigator
.userAgent
.toLowerCase(),
8 uaMsg
= 'Use feature detection or module jquery.client instead.';
11 * User-agent sniffing.
12 * To be removed in MediaWiki 1.23.
14 * @deprecated since 1.17 Use jquery.client instead.
16 mw
.log
.deprecate( window
, 'clientPC', ua
, uaMsg
);
38 mw
.log
.deprecate( window
, key
, false, uaMsg
);
41 if ( /msie ([0-9]{1,}[\.0-9]{0,})/.exec( ua
) && parseFloat( RegExp
.$1 ) <= 6.0 ) {
44 isGecko
= /gecko/.test( ua
) && !/khtml|spoofer|netscape\/7\.0/.test( ua
);
46 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
47 window
.doneOnloadHook
= undefined;
49 if ( !window
.onloadFuncts
) {
50 window
.onloadFuncts
= [];
53 window
.addOnloadHook = function( hookFunct
) {
54 // Allows add-on scripts to add onload functions
55 if( !window
.doneOnloadHook
) {
56 window
.onloadFuncts
[window
.onloadFuncts
.length
] = hookFunct
;
58 hookFunct(); // bug in MSIE script loading
62 window
.importScript = function( page
) {
63 var uri
= mw
.config
.get( 'wgScript' ) + '?title=' +
64 mw
.util
.wikiUrlencode( page
) +
65 '&action=raw&ctype=text/javascript';
66 return window
.importScriptURI( uri
);
69 window
.loadedScripts
= {}; // included-scripts tracker
70 window
.importScriptURI = function( url
) {
71 if ( window
.loadedScripts
[url
] ) {
74 window
.loadedScripts
[url
] = true;
75 var s
= document
.createElement( 'script' );
76 s
.setAttribute( 'src', url
);
77 s
.setAttribute( 'type', 'text/javascript' );
78 document
.getElementsByTagName('head')[0].appendChild( s
);
82 window
.importStylesheet = function( page
) {
83 return window
.importStylesheetURI( mw
.config
.get( 'wgScript' ) + '?action=raw&ctype=text/css&title=' + mw
.util
.wikiUrlencode( page
) );
86 window
.importStylesheetURI = function( url
, media
) {
87 var l
= document
.createElement( 'link' );
93 document
.getElementsByTagName('head')[0].appendChild( l
);
97 window
.appendCSS = function( text
) {
98 var s
= document
.createElement( 'style' );
100 s
.rel
= 'stylesheet';
101 if ( s
.styleSheet
) {
102 s
.styleSheet
.cssText
= text
; // IE
104 s
.appendChild( document
.createTextNode( text
+ '' ) ); // Safari sometimes borks on null
106 document
.getElementsByTagName('head')[0].appendChild( s
);
110 if ( mw
.config
.get( 'wgBreakFrames' ) ) {
111 // Note: In IE < 9 strict comparison to window is non-standard (the standard didn't exist yet)
112 // it works only comparing to window.self or window.window (http://stackoverflow.com/q/4850978/319266)
113 if ( window
.top
!== window
.self
) {
114 // Un-trap us from framesets
115 window
.top
.location
= window
.location
;
119 window
.changeText = function( el
, newText
) {
120 // Safari work around
121 if ( el
.innerText
) {
122 el
.innerText
= newText
;
123 } else if ( el
.firstChild
&& el
.firstChild
.nodeValue
) {
124 el
.firstChild
.nodeValue
= newText
;
128 window
.killEvt = function( evt
) {
129 evt
= evt
|| window
.event
|| window
.Event
; // W3C, IE, Netscape
130 if ( typeof evt
.preventDefault
!== 'undefined' ) {
131 evt
.preventDefault(); // Don't follow the link
132 evt
.stopPropagation();
134 evt
.cancelBubble
= true; // IE
136 return false; // Don't follow the link (IE)
139 window
.mwEditButtons
= [];
140 window
.mwCustomEditButtons
= []; // eg to add in MediaWiki:Common.js
142 window
.escapeQuotes = function( text
) {
143 var re
= new RegExp( "'", "g" );
144 text
= text
.replace( re
, "\\'" );
145 re
= new RegExp( "\\n", "g" );
146 text
= text
.replace( re
, "\\n" );
147 return window
.escapeQuotesHTML( text
);
150 window
.escapeQuotesHTML = function( text
) {
151 var re
= new RegExp( '&', "g" );
152 text
= text
.replace( re
, "&" );
153 re
= new RegExp( '"', "g" );
154 text
= text
.replace( re
, """ );
155 re
= new RegExp( '<', "g" );
156 text
= text
.replace( re
, "<" );
157 re
= new RegExp( '>', "g" );
158 text
= text
.replace( re
, ">" );
163 * Accesskey prefix utilities.
164 * To be removed in MediaWiki 1.23.
166 * @deprecated since 1.17 Use mediawiki.util instead.
168 mw
.log
.deprecate( window
, 'tooltipAccessKeyPrefix', 'alt-', 'Use mediawiki.util instead.' );
169 mw
.log
.deprecate( window
, 'tooltipAccessKeyRegexp', /\[(alt-)?(.)\]$/, 'Use mediawiki.util instead.' );
170 mw
.log
.deprecate( window
, 'updateTooltipAccessKeys', mw
.util
.updateTooltipAccessKeys
, 'Use mediawiki.util instead.' );
173 * Add a link to one of the portlet menus on the page, including:
175 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
176 * p-personal: Personal tools (shown at the top right of the page in Monobook)
177 * p-navigation: Navigation
180 * This function exists for the convenience of custom JS authors. All
181 * but the first three parameters are optional, though providing at
182 * least an id and a tooltip is recommended.
184 * By default the new link will be added to the end of the list. To
185 * add the link before a given existing item, pass the DOM node of
186 * that item (easily obtained with document.getElementById()) as the
187 * nextnode parameter; to add the link _after_ an existing item, pass
188 * the node's nextSibling instead.
190 * @param portlet String id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
191 * @param href String link URL
192 * @param text String link text (will be automatically lowercased by CSS for p-cactions in Monobook)
193 * @param id String id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
194 * @param tooltip String text to show when hovering over the link, without accesskey suffix
195 * @param accesskey String accesskey to activate this link (one character, try to avoid conflicts)
196 * @param nextnode Node the DOM node before which the new item should be added, should be another item in the same list
198 * @return Node -- the DOM node of the new item (an LI element) or null
200 window
.addPortletLink = function( portlet
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
201 var root
= document
.getElementById( portlet
);
205 var uls
= root
.getElementsByTagName( 'ul' );
207 if ( uls
.length
> 0 ) {
210 node
= document
.createElement( 'ul' );
211 var lastElementChild
= null;
212 for ( var i
= 0; i
< root
.childNodes
.length
; ++i
) { /* get root.lastElementChild */
213 if ( root
.childNodes
[i
].nodeType
=== 1 ) {
214 lastElementChild
= root
.childNodes
[i
];
217 if ( lastElementChild
&& lastElementChild
.nodeName
.match( /div/i ) ) {
218 /* Insert into the menu divs */
219 lastElementChild
.appendChild( node
);
221 root
.appendChild( node
);
228 // unhide portlet if it was hidden before
229 root
.className
= root
.className
.replace( /(^| )emptyPortlet( |$)/, "$2" );
231 var link
= document
.createElement( 'a' );
232 link
.appendChild( document
.createTextNode( text
) );
235 // Wrap in a span - make it work with vector tabs and has no effect on any other portlets
236 var span
= document
.createElement( 'span' );
237 span
.appendChild( link
);
239 var item
= document
.createElement( 'li' );
240 item
.appendChild( span
);
246 link
.setAttribute( 'accesskey', accesskey
);
247 tooltip
+= ' [' + accesskey
+ ']';
250 link
.setAttribute( 'title', tooltip
);
252 if ( accesskey
&& tooltip
) {
253 mw
.util
.updateTooltipAccessKeys( [link
] );
256 if ( nextnode
&& nextnode
.parentNode
=== node
) {
257 node
.insertBefore( item
, nextnode
);
259 node
.appendChild( item
); // IE compatibility (?)
265 window
.getInnerText = function( el
) {
266 if ( typeof el
=== 'string' ) {
269 if ( typeof el
=== 'undefined' ) {
272 // Custom sort value through 'data-sort-value' attribute
273 // (no need to prepend hidden text to change sort value)
274 if ( el
.nodeType
&& el
.getAttribute( 'data-sort-value' ) !== null ) {
275 // Make sure it's a valid DOM element (.nodeType) and that the attribute is set (!null)
276 return el
.getAttribute( 'data-sort-value' );
278 if ( el
.textContent
) {
279 return el
.textContent
; // not needed but it is faster
281 if ( el
.innerText
) {
282 return el
.innerText
; // IE doesn't have textContent
286 var cs
= el
.childNodes
;
288 for ( var i
= 0; i
< l
; i
++ ) {
289 switch ( cs
[i
].nodeType
) {
290 case 1: // ELEMENT_NODE
291 str
+= window
.getInnerText( cs
[i
] );
294 str
+= cs
[i
].nodeValue
;
302 * Toggle checkboxes with shift selection.
303 * To be removed in MediaWiki 1.23.
305 * @deprecated since 1.17 Use jquery.checkboxShiftClick instead.
310 setupCheckboxShiftClick
: $.noop
,
311 addCheckboxClickHandlers
: $.noop
,
312 checkboxClickHandler
: $.noop
313 }, function ( key
, val
) {
314 mw
.log
.deprecate( window
, key
, val
, 'Use jquery.checkboxShiftClick instead.' );
318 Written by Jonathan Snook, http://www.snook.ca/jonathan
319 Add-ons by Robert Nyman, http://www.robertnyman.com
320 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
321 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
323 window
.getElementsByClassName = function( oElm
, strTagName
, oClassNames
) {
324 var arrReturnElements
= [];
325 if ( typeof oElm
.getElementsByClassName
=== 'function' ) {
326 /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
327 var arrNativeReturn
= oElm
.getElementsByClassName( oClassNames
);
328 if ( strTagName
=== '*' ) {
329 return arrNativeReturn
;
331 for ( var h
= 0; h
< arrNativeReturn
.length
; h
++ ) {
332 if( arrNativeReturn
[h
].tagName
.toLowerCase() === strTagName
.toLowerCase() ) {
333 arrReturnElements
[arrReturnElements
.length
] = arrNativeReturn
[h
];
336 return arrReturnElements
;
338 var arrElements
= ( strTagName
=== '*' && oElm
.all
) ? oElm
.all
: oElm
.getElementsByTagName( strTagName
);
339 var arrRegExpClassNames
= [];
340 if( typeof oClassNames
=== 'object' ) {
341 for( var i
= 0; i
< oClassNames
.length
; i
++ ) {
342 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
343 new RegExp("(^|\\s)" + oClassNames
[i
].replace(/\-/g, "\\-") + "(\\s|$)");
346 arrRegExpClassNames
[arrRegExpClassNames
.length
] =
347 new RegExp("(^|\\s)" + oClassNames
.replace(/\-/g, "\\-") + "(\\s|$)");
351 for( var j
= 0; j
< arrElements
.length
; j
++ ) {
352 oElement
= arrElements
[j
];
354 for( var k
= 0; k
< arrRegExpClassNames
.length
; k
++ ) {
355 if( !arrRegExpClassNames
[k
].test( oElement
.className
) ) {
361 arrReturnElements
[arrReturnElements
.length
] = oElement
;
364 return ( arrReturnElements
);
367 window
.redirectToFragment = function( fragment
) {
369 match
= navigator
.userAgent
.match(/AppleWebKit\/(\d+)/);
371 webKitVersion
= parseInt( match
[1], 10 );
372 if ( webKitVersion
< 420 ) {
373 // Released Safari w/ WebKit 418.9.1 messes up horribly
374 // Nightlies of 420+ are ok
378 if ( !window
.location
.hash
) {
379 window
.location
.hash
= fragment
;
381 // Mozilla needs to wait until after load, otherwise the window doesn't
382 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
383 // There's no obvious way to detect this programmatically, so we use
384 // version-testing. If Firefox fixes the bug, they'll jump twice, but
385 // better twice than not at all, so make the fix hit future versions as
389 if ( window
.location
.hash
=== fragment
) {
390 window
.location
.hash
= fragment
;
398 * Add a cute little box at the top of the screen to inform the user of
399 * something, replacing any preexisting message.
401 * @deprecated since 1.17 Use the 'mediawiki.notify' module instead.
402 * @param {string|HTMLElement} message To be put inside the message box.
404 mw
.log
.deprecate( window
, 'jsMsg', mw
.util
.jsMessage
, 'Use mediawiki.notify instead.' );
407 * Inject a cute little progress spinner after the specified element
409 * @param element Element to inject after
410 * @param id Identifier string (for use with removeSpinner(), below)
412 window
.injectSpinner = function( element
, id
) {
413 var spinner
= document
.createElement( 'img' );
414 spinner
.id
= 'mw-spinner-' + id
;
415 spinner
.src
= mw
.config
.get( 'stylepath' ) + '/common/images/spinner.gif';
416 spinner
.alt
= spinner
.title
= '...';
417 if( element
.nextSibling
) {
418 element
.parentNode
.insertBefore( spinner
, element
.nextSibling
);
420 element
.parentNode
.appendChild( spinner
);
425 * Remove a progress spinner added with injectSpinner()
427 * @param id Identifier string
429 window
.removeSpinner = function( id
) {
430 var spinner
= document
.getElementById( 'mw-spinner-' + id
);
432 spinner
.parentNode
.removeChild( spinner
);
436 window
.runOnloadHook = function() {
437 // don't run anything below this for non-dom browsers
438 if ( window
.doneOnloadHook
|| !( document
.getElementById
&& document
.getElementsByTagName
) ) {
442 // set this before running any hooks, since any errors below
443 // might cause the function to terminate prematurely
444 window
.doneOnloadHook
= true;
446 // Run any added-on functions
447 for ( var i
= 0; i
< window
.onloadFuncts
.length
; i
++ ) {
448 window
.onloadFuncts
[i
]();
453 * Add an event handler to an element
455 * @param element Element to add handler to
456 * @param attach String Event to attach to
457 * @param handler callable Event handler callback
459 window
.addHandler = function( element
, attach
, handler
) {
460 if( element
.addEventListener
) {
461 element
.addEventListener( attach
, handler
, false );
462 } else if( element
.attachEvent
) {
463 element
.attachEvent( 'on' + attach
, handler
);
467 window
.hookEvent = function( hookName
, hookFunct
) {
468 window
.addHandler( window
, hookName
, hookFunct
);
472 * Add a click event handler to an element
474 * @param element Element to add handler to
475 * @param handler callable Event handler callback
477 window
.addClickHandler = function( element
, handler
) {
478 window
.addHandler( element
, 'click', handler
);
482 * Removes an event handler from an element
484 * @param element Element to remove handler from
485 * @param remove String Event to remove
486 * @param handler callable Event handler callback to remove
488 window
.removeHandler = function( element
, remove
, handler
) {
489 if( window
.removeEventListener
) {
490 element
.removeEventListener( remove
, handler
, false );
491 } else if( window
.detachEvent
) {
492 element
.detachEvent( 'on' + remove
, handler
);
495 window
.hookEvent( 'load', window
.runOnloadHook
);
498 window
.importScriptURI( mw
.config
.get( 'stylepath' ) + '/common/IEFixes.js' );
501 }( mediaWiki
, jQuery
) );