11 if ( this.initialised
=== false ) {
12 this.initialised
= true;
14 // Any initialisation after the DOM is ready
17 /* Set up $.messageBox */
19 'id': 'mw-js-message',
23 // Shortcut to client profile return
24 var profile
= $.client
.profile();
26 /* Set tooltipAccessKeyPrefix */
28 // Opera on any platform
29 if ( profile
.name
== 'opera' ) {
30 mw
.util
.tooltipAccessKeyPrefix
= 'shift-esc-';
32 // Chrome on any platform
33 } else if ( profile
.name
== 'chrome' ) {
34 // Chrome on Mac or Chrome on other platform ?
35 mw
.util
.tooltipAccessKeyPrefix
= ( profile
.platform
== 'mac'
36 ? 'ctrl-option-' : 'alt-' );
38 // Non-Windows Safari with webkit_version > 526
39 } else if ( profile
.platform
!== 'win'
40 && profile
.name
== 'safari'
41 && profile
.layoutVersion
> 526 ) {
42 mw
.util
.tooltipAccessKeyPrefix
= 'ctrl-alt-';
44 // Safari/Konqueror on any platform, or any browser on Mac
45 // (but not Safari on Windows)
46 } else if ( !( profile
.platform
== 'win' && profile
.name
== 'safari' )
47 && ( profile
.name
== 'safari'
48 || profile
.platform
== 'mac'
49 || profile
.name
== 'konqueror' ) ) {
50 mw
.util
.tooltipAccessKeyPrefix
= 'ctrl-';
53 } else if ( profile
.name
== 'firefox' && profile
.versionBase
== '2' ) {
54 mw
.util
.tooltipAccessKeyPrefix
= 'alt-shift-';
57 /* Enable CheckboxShiftClick */
58 $( 'input[type=checkbox]:not(.noshiftselect)' ).checkboxShiftClick();
60 /* Enable Tablesorting */
61 $( 'table.sortable' ).tablesorter();
63 /* Emulate placeholder if not supported by browser */
64 if ( !( 'placeholder' in document
.createElement( 'input' ) ) ) {
65 $( 'input[placeholder]' ).placeholder();
68 /* Fill $content var */
69 if ( $( '#bodyContent' ).length
) {
70 // Vector, Monobook, Chick etc.
71 mw
.util
.$content
= $( '#bodyContent' );
73 } else if ( $( '#mw_contentholder' ).length
) {
75 mw
.util
.$content
= $( '#mw_contentholder' );
77 } else if ( $( '#article' ).length
) {
78 // Standard, CologneBlue
79 mw
.util
.$content
= $( '#article' );
82 // #content is present on almost all if not all skins. Most skins (the above cases)
83 // have #content too, but as an outer wrapper instead of the article text container.
84 // The skins that don't have an outer wrapper do have #content for everything
85 // so it's a good fallback
86 mw
.util
.$content
= $( '#content' );
89 /* Enable makeCollapse */
90 $( '.mw-collapsible' ).makeCollapsible();
92 /* Table of Contents toggle */
93 var $tocContainer
= $( '#toc' ),
94 $tocTitle
= $( '#toctitle' ),
95 $tocToggleLink
= $( '#togglelink' );
96 // Only add it if there is a TOC and there is no toggle added already
97 if ( $tocContainer
.size() && $tocTitle
.size() && !$tocToggleLink
.size() ) {
98 var hideTocCookie
= $.cookie( 'mw_hidetoc' );
99 $tocToggleLink
= $( '<a href="#" class="internal" id="togglelink">' ).text( mw
.msg( 'hidetoc' ) ).click( function(e
){
101 mw
.util
.toggleToc( $(this) );
103 $tocTitle
.append( $tocToggleLink
.wrap( '<span class="toctoggle">' ).parent().prepend( ' [' ).append( '] ' ) );
105 if ( hideTocCookie
== '1' ) {
106 // Cookie says user want toc hidden
107 $tocToggleLink
.click();
120 * Encode the string like PHP's rawurlencode
122 * @param str String to be encoded
124 'rawurlencode' : function( str
) {
125 str
= ( str
+ '' ).toString();
126 return encodeURIComponent( str
)
127 .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
128 .replace( /\)/g, '%29' ).replace( /\*/g, '%2A
' ).replace( /~/g, '%7E
' );
132 * Encode page titles for use in a URL
133 * We want / and : to be included as literal characters in our title URLs
134 * as they otherwise fatally break the title
136 * @param str String to be encoded
138 'wikiUrlencode
' : function( str ) {
139 return this.rawurlencode( str )
140 .replace( /%20/g, '_
' ).replace( /%3A/g, ':' ).replace( /%2F/g, '/' );
144 * Append a new style block to the head
146 * @param text String CSS to be appended
147 * @return CSSStyleSheet object
149 'addCSS
' : function( text ) {
150 var s = document.createElement( 'style
' );
152 s.rel = 'stylesheet
';
153 if ( s.styleSheet ) {
154 s.styleSheet.cssText = text; // IE
156 s.appendChild( document.createTextNode( text + '' ) ); // Safari sometimes borks on null
158 document.getElementsByTagName("head")[0].appendChild( s );
163 * Hide/show the table of contents element
165 * @param $toggleLink jQuery object of the toggle link
166 * @return String boolean visibility of the toc (true means it's visible
)
168 'toggleToc' : function( $toggleLink
) {
169 var $tocList
= $( '#toc ul:first' );
171 // This function shouldn't be called if there's no TOC,
172 // but just in case...
173 if ( $tocList
.size() ) {
174 if ( $tocList
.is( ':hidden' ) ) {
175 $tocList
.slideDown( 'fast' );
176 $toggleLink
.text( mw
.msg( 'hidetoc' ) );
177 $.cookie( 'mw_hidetoc', null, {
183 $tocList
.slideUp( 'fast' );
184 $toggleLink
.text( mw
.msg( 'showtoc' ) );
185 $.cookie( 'mw_hidetoc', '1', {
197 * Get the full URL to a page name
199 * @param str Page name to link to
200 * @return Full URL for page with name of 'str' or false on error
202 'wikiGetlink' : function( str
) {
204 return mw
.config
.get( 'wgArticlePath' ).replace( '$1', this.wikiUrlencode( str
) );
208 * Grab the URL parameter value for the given parameter.
209 * Returns null if not found.
211 * @param param The parameter name
212 * @param url URL to search through (optional)
214 'getParamValue' : function( param
, url
) {
215 url
= url
? url
: document
.location
.href
;
216 // Get last match, stop at hash
217 var re
= new RegExp( '[^#]*[&?]' + $.escapeRE( param
) + '=([^&#]*)' );
218 var m
= re
.exec( url
);
219 if ( m
&& m
.length
> 1 ) {
220 return decodeURIComponent( m
[1] );
225 // Access key prefix.
226 // Will be re-defined based on browser/operating system detection in
228 'tooltipAccessKeyPrefix' : 'alt-',
230 // Regex to match accesskey tooltips
231 'tooltipAccessKeyRegexp': /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/,
234 * Add the appropriate prefix to the accesskey shown in the tooltip.
235 * If the nodeList parameter is given, only those nodes are updated;
236 * otherwise, all the nodes that will probably have accesskeys by
237 * default are updated.
239 * @param nodeList jQuery object, or array of elements
241 'updateTooltipAccessKeys' : function( nodeList
) {
243 if ( nodeList
instanceof jQuery
) {
245 } else if ( nodeList
) {
246 $nodes
= $( nodeList
);
248 // Rather than scanning all links, just the elements that
249 // contain the relevant links
250 this.updateTooltipAccessKeys(
251 $( '#column-one a, #mw-head a, #mw-panel a, #p-logo a' ) );
253 // these are rare enough that no such optimization is needed
254 this.updateTooltipAccessKeys( $( 'input' ) );
255 this.updateTooltipAccessKeys( $( 'label' ) );
259 $nodes
.each( function ( i
) {
260 var tip
= $(this).attr( 'title' );
261 if ( !!tip
&& mw
.util
.tooltipAccessKeyRegexp
.exec( tip
) ) {
262 tip
= tip
.replace( mw
.util
.tooltipAccessKeyRegexp
,
263 '[' + mw
.util
.tooltipAccessKeyPrefix
+ "$5]" );
264 $(this).attr( 'title', tip
);
269 // jQuery object that refers to the page-content element
270 // Populated by init()
274 * Add a link to a portlet menu on the page, such as:
276 * p-cactions (Content actions), p-personal (Personal tools),
277 * p-navigation (Navigation), p-tb (Toolbox)
279 * The first three paramters are required, others are optionals. Though
280 * providing an id and tooltip is recommended.
282 * By default the new link will be added to the end of the list. To
283 * add the link before a given existing item, pass the DOM node
284 * (document.getElementById( 'foobar' )) or the jQuery-selector
285 * ( '#foobar' ) of that item.
287 * @example mw.util.addPortletLink(
288 * 'p-tb', 'http://mediawiki.org/',
289 * 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', '#t-print'
292 * @param portlet ID of the target portlet ( 'p-cactions' or 'p-personal' etc.)
293 * @param href Link URL
294 * @param text Link text
295 * @param id ID of the new item, should be unique and preferably have
296 * the appropriate prefix ( 'ca-', 'pt-', 'n-' or 't-' )
297 * @param tooltip Text to show when hovering over the link, without accesskey suffix
298 * @param accesskey Access key to activate this link (one character, try
299 * to avoid conflicts. Use $( '[accesskey=x]' ).get() in the console to
300 * see if 'x' is already used.
301 * @param nextnode DOM node or jQuery-selector string of the item that the new
302 * item should be added before, should be another item in the same
303 * list, it will be ignored otherwise
305 * @return The DOM node of the new item (a LI element, or A element for
306 * older skins) or null.
308 'addPortletLink' : function( portlet
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
310 // Check if there's atleast 3 arguments to prevent a TypeError
311 if ( arguments
.length
< 3 ) {
314 // Setup the anchor tag
315 var $link
= $( '<a></a>' ).attr( 'href', href
).text( text
);
317 $link
.attr( 'title', tooltip
);
320 // Some skins don't have any portlets
321 // just add it to the bottom of their 'sidebar' element as a fallback
322 switch ( mw
.config
.get( 'skin' ) ) {
325 $( '#quickbar' ).append( $link
.after( '<br />' ) );
328 $( '#searchform' ).before( $link
).before( ' | ' );
330 default : // Skins like chick, modern, monobook, myskin, simple, vector...
332 // Select the specified portlet
333 var $portlet
= $( '#' + portlet
);
334 if ( $portlet
.length
=== 0 ) {
337 // Select the first (most likely only) unordered list inside the portlet
338 var $ul
= $portlet
.find( 'ul' );
340 // If it didn't have an unordered list yet, create it
341 if ( $ul
.length
=== 0 ) {
342 // If there's no <div> inside, append it to the portlet directly
343 if ( $portlet
.find( 'div:first' ).length
=== 0 ) {
344 $portlet
.append( '<ul></ul>' );
346 // otherwise if there's a div (such as div.body or div.pBody)
347 // append the <ul> to last (most likely only) div
348 $portlet
.find( 'div' ).eq( -1 ).append( '<ul></ul>' );
350 // Select the created element
351 $ul
= $portlet
.find( 'ul' ).eq( 0 );
354 if ( $ul
.length
=== 0 ) {
358 // Unhide portlet if it was hidden before
359 $portlet
.removeClass( 'emptyPortlet' );
361 // Wrap the anchor tag in a <span> and create a list item for it
362 // and back up the selector to the list item
363 var $item
= $link
.wrap( '<li><span></span></li>' ).parent().parent();
365 // Implement the properties passed to the function
367 $item
.attr( 'id', id
);
370 $link
.attr( 'accesskey', accesskey
);
371 tooltip
+= ' [' + accesskey
+ ']';
372 $link
.attr( 'title', tooltip
);
374 if ( accesskey
&& tooltip
) {
375 this.updateTooltipAccessKeys( $link
);
378 // Append using DOM-element passing
379 if ( nextnode
&& nextnode
.parentNode
== $ul
[0] ) {
380 $(nextnode
).before( $item
);
382 // If the jQuery selector isn't found within the <ul>, just
383 // append it at the end
384 if ( $ul
.find( nextnode
).length
=== 0 ) {
387 // Append using jQuery CSS selector
388 $ul
.find( nextnode
).eq( 0 ).before( $item
);
397 * Add a little box at the top of the screen to inform the user of
398 * something, replacing any previous message.
399 * Calling with no arguments, with an empty string or null will hide the message
401 * @param message mixed The DOM-element or HTML-string to be put inside the message box.
402 * @param className string Used in adding a class; should be different for each call
403 * to allow CSS/JS to hide different boxes. null = no class used.
404 * @return boolean True on success, false on failure
406 'jsMessage' : function( message
, className
) {
408 if ( !arguments
.length
|| message
=== '' || message
=== null ) {
410 $( '#mw-js-message' ).empty().hide();
411 return true; // Emptying and hiding message is intended behaviour, return true
414 // We special-case skin structures provided by the software. Skins that
415 // choose to abandon or significantly modify our formatting can just define
416 // an mw-js-message div to start with.
417 var $messageDiv
= $( '#mw-js-message' );
418 if ( !$messageDiv
.length
) {
419 $messageDiv
= $( '<div id="mw-js-message">' );
420 if ( mw
.util
.$content
.parent().length
) {
421 mw
.util
.$content
.parent().prepend( $messageDiv
);
428 $messageDiv
.attr( 'class', 'mw-js-message-' + className
);
431 if ( typeof message
=== 'object' ) {
433 $messageDiv
.append( message
); // Append new content
435 $messageDiv
.html( message
);
438 $messageDiv
.slideDown();
444 * Validate a string as representing a valid e-mail address
445 * according to HTML5 specification. Please note the specification
446 * does not validate a domain with one character.
448 * FIXME: should be moved to or replaced by a JavaScript validation module.
450 'validateEmail' : function( mailtxt
) {
451 if( mailtxt
=== '' ) {
456 * HTML5 defines a string as valid e-mail address if it matches
458 * 1 * ( atext / "." ) "@" ldh-str 1*( "." ldh-str )
460 * - atext : defined in RFC 5322 section 3.2.3
461 * - ldh-str : defined in RFC 1034 section 3.5
463 * (see STD 68 / RFC 5234 http://tools.ietf.org/html/std68):
467 * First, define the RFC 5322 'atext' which is pretty easy :
468 * atext = ALPHA / DIGIT / ; Printable US-ASCII
469 "!" / "#" / ; characters not including
470 "$" / "%" / ; specials. Used for atoms.
480 var rfc5322_atext
= "a-z0-9!#$%&'*+\\-/=?^_`{|}~",
483 * Next define the RFC 1034 'ldh-str'
484 * <domain> ::= <subdomain> | " "
485 * <subdomain> ::= <label> | <subdomain> "." <label>
486 * <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
487 * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
488 * <let-dig-hyp> ::= <let-dig> | "-"
489 * <let-dig> ::= <letter> | <digit>
491 rfc1034_ldh_str
= "a-z0-9\\-",
493 HTML5_email_regexp
= new RegExp(
497 // User part which is liberal :p
498 '[' + rfc5322_atext
+ '\\.]+'
504 '[' + rfc1034_ldh_str
+ ']+'
506 // Optional second part and following are separated by a dot
507 '(?:\\.[' + rfc1034_ldh_str
+ ']+)*'
511 // RegExp is case insensitive
514 return (null !== mailtxt
.match( HTML5_email_regexp
) );
516 // Note: borrows from IP::isIPv4
517 'isIPv4Address' : function( address
, allowBlock
) {
518 var block
= allowBlock
? '(?:\\/(?:3[0-2]|[12]?\\d))?' : '';
519 var RE_IP_BYTE
= '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])';
520 var RE_IP_ADD
= '(?:' + RE_IP_BYTE
+ '\\.){3}' + RE_IP_BYTE
;
521 return address
.search( new RegExp( '^' + RE_IP_ADD
+ block
+ '$' ) ) != -1;
523 // Note: borrows from IP::isIPv6
524 'isIPv6Address' : function( address
, allowBlock
) {
525 var block
= allowBlock
? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : '';
527 '(?:' + // starts with "::" (including "::")
528 ':(?::|(?::' + '[0-9A-Fa-f]{1,4}' + '){1,7})' +
529 '|' + // ends with "::" (except "::")
530 '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){0,6}::' +
531 '|' + // contains no "::"
532 '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){7}' +
534 if ( address
.search( new RegExp( '^' + RE_IPV6_ADD
+ block
+ '$' ) ) != -1 ) {
537 RE_IPV6_ADD
= // contains one "::" in the middle (single '::' check below)
538 '[0-9A-Fa-f]{1,4}' + '(?:::?' + '[0-9A-Fa-f]{1,4}' + '){1,6}';
539 return address
.search( new RegExp( '^' + RE_IPV6_ADD
+ block
+ '$' ) ) != -1
540 && address
.search( /::/ ) != -1 && address
.search( /::.*::/ ) == -1;
547 } )( jQuery
, mediaWiki
);