Index: trunk/phase3/resources/mediawiki/mediawiki.util.js |
— | — | @@ -1,7 +1,8 @@ |
2 | 2 | /** |
3 | 3 | * Utilities |
4 | 4 | */ |
5 | | -( function( $ ) { |
| 5 | +( function ( $, mw ) { |
| 6 | +"use strict"; |
6 | 7 | |
7 | 8 | // Local cache and alias |
8 | 9 | var util = mw.util = { |
— | — | @@ -11,6 +12,11 @@ |
12 | 13 | * (don't call before document ready) |
13 | 14 | */ |
14 | 15 | 'init' : function() { |
| 16 | + var profile = $.client.profile(), |
| 17 | + $tocContainer = $( '#toc' ), |
| 18 | + $tocTitle = $( '#toctitle' ), |
| 19 | + $tocToggleLink = $( '#togglelink' ), |
| 20 | + hideTocCookie; |
15 | 21 | |
16 | 22 | /* Set up $.messageBox */ |
17 | 23 | $.messageBoxNew( { |
— | — | @@ -18,37 +24,34 @@ |
19 | 25 | 'parent': '#content' |
20 | 26 | } ); |
21 | 27 | |
22 | | - // Shortcut to client profile return |
23 | | - var profile = $.client.profile(); |
24 | | - |
25 | 28 | /* Set tooltipAccessKeyPrefix */ |
26 | 29 | |
27 | 30 | // Opera on any platform |
28 | | - if ( profile.name == 'opera' ) { |
| 31 | + if ( profile.name === 'opera' ) { |
29 | 32 | util.tooltipAccessKeyPrefix = 'shift-esc-'; |
30 | 33 | |
31 | 34 | // Chrome on any platform |
32 | | - } else if ( profile.name == 'chrome' ) { |
| 35 | + } else if ( profile.name === 'chrome' ) { |
33 | 36 | // Chrome on Mac or Chrome on other platform ? |
34 | | - util.tooltipAccessKeyPrefix = ( profile.platform == 'mac' |
| 37 | + util.tooltipAccessKeyPrefix = ( profile.platform === 'mac' |
35 | 38 | ? 'ctrl-option-' : 'alt-' ); |
36 | 39 | |
37 | 40 | // Non-Windows Safari with webkit_version > 526 |
38 | 41 | } else if ( profile.platform !== 'win' |
39 | | - && profile.name == 'safari' |
| 42 | + && profile.name === 'safari' |
40 | 43 | && profile.layoutVersion > 526 ) { |
41 | 44 | util.tooltipAccessKeyPrefix = 'ctrl-alt-'; |
42 | 45 | |
43 | 46 | // Safari/Konqueror on any platform, or any browser on Mac |
44 | 47 | // (but not Safari on Windows) |
45 | | - } else if ( !( profile.platform == 'win' && profile.name == 'safari' ) |
46 | | - && ( profile.name == 'safari' |
47 | | - || profile.platform == 'mac' |
48 | | - || profile.name == 'konqueror' ) ) { |
| 48 | + } else if ( !( profile.platform === 'win' && profile.name === 'safari' ) |
| 49 | + && ( profile.name === 'safari' |
| 50 | + || profile.platform === 'mac' |
| 51 | + || profile.name === 'konqueror' ) ) { |
49 | 52 | util.tooltipAccessKeyPrefix = 'ctrl-'; |
50 | 53 | |
51 | 54 | // Firefox 2.x and later |
52 | | - } else if ( profile.name == 'firefox' && profile.versionBase > '1' ) { |
| 55 | + } else if ( profile.name === 'firefox' && profile.versionBase > '1' ) { |
53 | 56 | util.tooltipAccessKeyPrefix = 'alt-shift-'; |
54 | 57 | } |
55 | 58 | |
— | — | @@ -73,20 +76,23 @@ |
74 | 77 | util.$content = $( '#content' ); |
75 | 78 | } |
76 | 79 | |
77 | | - /* Table of Contents toggle */ |
78 | | - var $tocContainer = $( '#toc' ), |
79 | | - $tocTitle = $( '#toctitle' ), |
80 | | - $tocToggleLink = $( '#togglelink' ); |
| 80 | + // Table of contents toggle |
81 | 81 | // Only add it if there is a TOC and there is no toggle added already |
82 | 82 | if ( $tocContainer.length && $tocTitle.length && !$tocToggleLink.length ) { |
83 | | - var hideTocCookie = $.cookie( 'mw_hidetoc' ); |
| 83 | + hideTocCookie = $.cookie( 'mw_hidetoc' ); |
84 | 84 | $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' ) |
85 | 85 | .text( mw.msg( 'hidetoc' ) ) |
86 | 86 | .click( function(e){ |
87 | 87 | e.preventDefault(); |
88 | 88 | util.toggleToc( $(this) ); |
89 | 89 | } ); |
90 | | - $tocTitle.append( $tocToggleLink.wrap( '<span class="toctoggle"></span>' ).parent().prepend( ' [' ).append( '] ' ) ); |
| 90 | + $tocTitle.append( |
| 91 | + $tocToggleLink |
| 92 | + .wrap( '<span class="toctoggle"></span>' ) |
| 93 | + .parent() |
| 94 | + .prepend( ' [' ) |
| 95 | + .append( '] ' ) |
| 96 | + ); |
91 | 97 | |
92 | 98 | if ( hideTocCookie == '1' ) { |
93 | 99 | // Cookie says user want toc hidden |
— | — | @@ -140,7 +146,8 @@ |
141 | 147 | * @return string Address to script (eg. '/w/api.php' ) |
142 | 148 | */ |
143 | 149 | 'wikiScript' : function( str ) { |
144 | | - return mw.config.get( 'wgScriptPath' ) + '/' + ( str || 'index' ) + mw.config.get( 'wgScriptExtension' ); |
| 150 | + return mw.config.get( 'wgScriptPath' ) + '/' + ( str || 'index' ) + |
| 151 | + mw.config.get( 'wgScriptExtension' ); |
145 | 152 | }, |
146 | 153 | |
147 | 154 | /** |
— | — | @@ -156,7 +163,8 @@ |
157 | 164 | if ( s.styleSheet ) { |
158 | 165 | s.styleSheet.cssText = text; // IE |
159 | 166 | } else { |
160 | | - s.appendChild( document.createTextNode( text + '' ) ); // Safari sometimes borks on null |
| 167 | + // Safari sometimes borks on null |
| 168 | + s.appendChild( document.createTextNode( text + '' ) ); |
161 | 169 | } |
162 | 170 | document.getElementsByTagName('head')[0].appendChild( s ); |
163 | 171 | return s.sheet || s; |
— | — | @@ -210,10 +218,10 @@ |
211 | 219 | * @return mixed Parameter value or null. |
212 | 220 | */ |
213 | 221 | 'getParamValue' : function( param, url ) { |
214 | | - url = url ? url : document.location.href; |
| 222 | + url = url || document.location.href; |
215 | 223 | // Get last match, stop at hash |
216 | | - var re = new RegExp( '^[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' ); |
217 | | - var m = re.exec( url ); |
| 224 | + var re = new RegExp( '^[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' ), |
| 225 | + m = re.exec( url ); |
218 | 226 | if ( m && m.length > 1 ) { |
219 | 227 | // Beware that decodeURIComponent is not required to understand '+' |
220 | 228 | // by spec, as encodeURIComponent does not produce it. |
— | — | @@ -241,7 +249,8 @@ |
242 | 250 | * otherwise, all the nodes that will probably have accesskeys by |
243 | 251 | * default are updated. |
244 | 252 | * |
245 | | - * @param nodeList {Array|jQuery} (optional) A jQuery object, or array of elements to update. |
| 253 | + * @param nodeList {Array|jQuery} [optional] A jQuery object, or array |
| 254 | + * of elements to update. |
246 | 255 | */ |
247 | 256 | 'updateTooltipAccessKeys' : function( nodeList ) { |
248 | 257 | var $nodes; |
— | — | @@ -317,13 +326,14 @@ |
318 | 327 | * depending on the skin) or null if no element was added to the document. |
319 | 328 | */ |
320 | 329 | 'addPortletLink' : function( portlet, href, text, id, tooltip, accesskey, nextnode ) { |
| 330 | + var $item, $link, $portlet, $ul; |
321 | 331 | |
322 | 332 | // Check if there's atleast 3 arguments to prevent a TypeError |
323 | 333 | if ( arguments.length < 3 ) { |
324 | 334 | return null; |
325 | 335 | } |
326 | 336 | // Setup the anchor tag |
327 | | - var $link = $( '<a></a>' ).attr( 'href', href ).text( text ); |
| 337 | + $link = $( '<a>' ).attr( 'href', href ).text( text ); |
328 | 338 | if ( tooltip ) { |
329 | 339 | $link.attr( 'title', tooltip ); |
330 | 340 | } |
— | — | @@ -341,12 +351,12 @@ |
342 | 352 | default : // Skins like chick, modern, monobook, myskin, simple, vector... |
343 | 353 | |
344 | 354 | // Select the specified portlet |
345 | | - var $portlet = $( '#' + portlet ); |
| 355 | + $portlet = $( '#' + portlet ); |
346 | 356 | if ( $portlet.length === 0 ) { |
347 | 357 | return null; |
348 | 358 | } |
349 | 359 | // Select the first (most likely only) unordered list inside the portlet |
350 | | - var $ul = $portlet.find( 'ul' ); |
| 360 | + $ul = $portlet.find( 'ul' ); |
351 | 361 | |
352 | 362 | // If it didn't have an unordered list yet, create it |
353 | 363 | if ( $ul.length === 0 ) { |
— | — | @@ -371,7 +381,6 @@ |
372 | 382 | |
373 | 383 | // Wrap the anchor tag in a list item (and a span if $portlet is a Vector tab) |
374 | 384 | // and back up the selector to the list item |
375 | | - var $item; |
376 | 385 | if ( $portlet.hasClass( 'vectorTabs' ) ) { |
377 | 386 | $item = $link.wrap( '<li><span></span></li>' ).parent().parent(); |
378 | 387 | } else { |
— | — | @@ -392,12 +401,12 @@ |
393 | 402 | } |
394 | 403 | |
395 | 404 | // Where to put our node ? |
396 | | - // - nextnode is a DOM element (before MW 1.17, in wikibits.js, this was the only option) |
397 | | - if ( nextnode && nextnode.parentNode == $ul[0] ) { |
| 405 | + // - nextnode is a DOM element (was the only option before MW 1.17, in wikibits.js) |
| 406 | + if ( nextnode && nextnode.parentNode === $ul[0] ) { |
398 | 407 | $(nextnode).before( $item ); |
399 | 408 | |
400 | 409 | // - nextnode is a CSS selector for jQuery |
401 | | - } else if ( typeof nextnode == 'string' && $ul.find( nextnode ).length !== 0 ) { |
| 410 | + } else if ( typeof nextnode === 'string' && $ul.find( nextnode ).length !== 0 ) { |
402 | 411 | $ul.find( nextnode ).eq( 0 ).before( $item ); |
403 | 412 | |
404 | 413 | |
— | — | @@ -436,7 +445,7 @@ |
437 | 446 | // an mw-js-message div to start with. |
438 | 447 | var $messageDiv = $( '#mw-js-message' ); |
439 | 448 | if ( !$messageDiv.length ) { |
440 | | - $messageDiv = $( '<div id="mw-js-message">' ); |
| 449 | + $messageDiv = $( '<div id="mw-js-message"></div>' ); |
441 | 450 | if ( util.$content.parent().length ) { |
442 | 451 | util.$content.parent().prepend( $messageDiv ); |
443 | 452 | } else { |
— | — | @@ -549,11 +558,14 @@ |
550 | 559 | if ( typeof address !== 'string' ) { |
551 | 560 | return false; |
552 | 561 | } |
553 | | - var block = allowBlock ? '(?:\\/(?:3[0-2]|[12]?\\d))?' : ''; |
554 | | - var RE_IP_BYTE = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])'; |
555 | | - var RE_IP_ADD = '(?:' + RE_IP_BYTE + '\\.){3}' + RE_IP_BYTE; |
556 | | - return address.search( new RegExp( '^' + RE_IP_ADD + block + '$' ) ) != -1; |
| 562 | + |
| 563 | + var block = allowBlock ? '(?:\\/(?:3[0-2]|[12]?\\d))?' : '', |
| 564 | + RE_IP_BYTE = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])', |
| 565 | + RE_IP_ADD = '(?:' + RE_IP_BYTE + '\\.){3}' + RE_IP_BYTE; |
| 566 | + |
| 567 | + return address.search( new RegExp( '^' + RE_IP_ADD + block + '$' ) ) !== -1; |
557 | 568 | }, |
| 569 | + |
558 | 570 | /** |
559 | 571 | * Note: borrows from IP::isIPv6 |
560 | 572 | * |
— | — | @@ -565,8 +577,9 @@ |
566 | 578 | if ( typeof address !== 'string' ) { |
567 | 579 | return false; |
568 | 580 | } |
569 | | - var block = allowBlock ? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : ''; |
570 | | - var RE_IPV6_ADD = |
| 581 | + |
| 582 | + var block = allowBlock ? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : '', |
| 583 | + RE_IPV6_ADD = |
571 | 584 | '(?:' + // starts with "::" (including "::") |
572 | 585 | ':(?::|(?::' + '[0-9A-Fa-f]{1,4}' + '){1,7})' + |
573 | 586 | '|' + // ends with "::" (except "::") |
— | — | @@ -574,15 +587,18 @@ |
575 | 588 | '|' + // contains no "::" |
576 | 589 | '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){7}' + |
577 | 590 | ')'; |
578 | | - if ( address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1 ) { |
| 591 | + |
| 592 | + if ( address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) !== -1 ) { |
579 | 593 | return true; |
580 | 594 | } |
| 595 | + |
581 | 596 | RE_IPV6_ADD = // contains one "::" in the middle (single '::' check below) |
582 | 597 | '[0-9A-Fa-f]{1,4}' + '(?:::?' + '[0-9A-Fa-f]{1,4}' + '){1,6}'; |
583 | | - return address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1 |
584 | | - && address.search( /::/ ) != -1 && address.search( /::.*::/ ) == -1; |
| 598 | + |
| 599 | + return address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) !== -1 |
| 600 | + && address.search( /::/ ) !== -1 && address.search( /::.*::/ ) === -1; |
585 | 601 | } |
586 | 602 | |
587 | 603 | }; |
588 | 604 | |
589 | | -} )( jQuery ); |
| 605 | +} )( jQuery, mediaWiki ); |