Index: trunk/phase3/resources/mediawiki/mediawiki.util.js |
— | — | @@ -1,27 +1,26 @@ |
2 | 2 | /** |
3 | | - * Utilities |
| 3 | + * Implementents |
4 | 4 | */ |
5 | 5 | ( function ( $, mw ) { |
6 | 6 | "use strict"; |
7 | 7 | |
8 | 8 | // Local cache and alias |
9 | | - var util = mw.util = { |
| 9 | + var util = { |
10 | 10 | |
11 | 11 | /** |
12 | 12 | * Initialisation |
13 | 13 | * (don't call before document ready) |
14 | 14 | */ |
15 | | - 'init' : function() { |
| 15 | + init: function() { |
16 | 16 | var profile = $.client.profile(), |
17 | | - $tocContainer = $( '#toc' ), |
18 | 17 | $tocTitle = $( '#toctitle' ), |
19 | 18 | $tocToggleLink = $( '#togglelink' ), |
20 | 19 | hideTocCookie; |
21 | 20 | |
22 | 21 | /* Set up $.messageBox */ |
23 | 22 | $.messageBoxNew( { |
24 | | - 'id': 'mw-js-message', |
25 | | - 'parent': '#content' |
| 23 | + id: 'mw-js-message', |
| 24 | + parent: '#content' |
26 | 25 | } ); |
27 | 26 | |
28 | 27 | /* Set tooltipAccessKeyPrefix */ |
— | — | @@ -78,14 +77,14 @@ |
79 | 78 | |
80 | 79 | // Table of contents toggle |
81 | 80 | // Only add it if there is a TOC and there is no toggle added already |
82 | | - if ( $tocContainer.length && $tocTitle.length && !$tocToggleLink.length ) { |
| 81 | + if ( $( '#toc' ).length && $tocTitle.length && !$tocToggleLink.length ) { |
83 | 82 | hideTocCookie = $.cookie( 'mw_hidetoc' ); |
84 | 83 | $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' ) |
85 | 84 | .text( mw.msg( 'hidetoc' ) ) |
86 | | - .click( function(e){ |
| 85 | + .click( function( e ) { |
87 | 86 | e.preventDefault(); |
88 | 87 | util.toggleToc( $(this) ); |
89 | | - } ); |
| 88 | + } ); |
90 | 89 | $tocTitle.append( |
91 | 90 | $tocToggleLink |
92 | 91 | .wrap( '<span class="toctoggle"></span>' ) |
— | — | @@ -94,9 +93,8 @@ |
95 | 94 | .append( '] ' ) |
96 | 95 | ); |
97 | 96 | |
98 | | - if ( hideTocCookie == '1' ) { |
99 | | - // Cookie says user want toc hidden |
100 | | - $tocToggleLink.click(); |
| 97 | + if ( hideTocCookie === '1' ) { |
| 98 | + util.toggleToc( $tocToggleLink ); |
101 | 99 | } |
102 | 100 | } |
103 | 101 | }, |
— | — | @@ -108,8 +106,8 @@ |
109 | 107 | * |
110 | 108 | * @param str string String to be encoded |
111 | 109 | */ |
112 | | - 'rawurlencode' : function( str ) { |
113 | | - str = ( str + '' ).toString(); |
| 110 | + rawurlencode: function( str ) { |
| 111 | + str = String( str ); |
114 | 112 | return encodeURIComponent( str ) |
115 | 113 | .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' ) |
116 | 114 | .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /~/g, '%7E' ); |
— | — | @@ -122,7 +120,7 @@ |
123 | 121 | * |
124 | 122 | * @param str string String to be encoded |
125 | 123 | */ |
126 | | - 'wikiUrlencode' : function( str ) { |
| 124 | + wikiUrlencode: function( str ) { |
127 | 125 | return this.rawurlencode( str ) |
128 | 126 | .replace( /%20/g, '_' ).replace( /%3A/g, ':' ).replace( /%2F/g, '/' ); |
129 | 127 | }, |
— | — | @@ -133,7 +131,7 @@ |
134 | 132 | * @param str string Page name to get the link for. |
135 | 133 | * @return string Location for a page with name of 'str' or boolean false on error. |
136 | 134 | */ |
137 | | - 'wikiGetlink' : function( str ) { |
| 135 | + wikiGetlink: function( str ) { |
138 | 136 | return mw.config.get( 'wgArticlePath' ).replace( '$1', |
139 | 137 | this.wikiUrlencode( str || mw.config.get( 'wgPageName' ) ) ); |
140 | 138 | }, |
— | — | @@ -145,7 +143,7 @@ |
146 | 144 | * @param str string Name of script (eg. 'api'), defaults to 'index' |
147 | 145 | * @return string Address to script (eg. '/w/api.php' ) |
148 | 146 | */ |
149 | | - 'wikiScript' : function( str ) { |
| 147 | + wikiScript: function( str ) { |
150 | 148 | return mw.config.get( 'wgScriptPath' ) + '/' + ( str || 'index' ) + |
151 | 149 | mw.config.get( 'wgScriptExtension' ); |
152 | 150 | }, |
— | — | @@ -156,7 +154,7 @@ |
157 | 155 | * @param text string CSS to be appended |
158 | 156 | * @return CSSStyleSheet |
159 | 157 | */ |
160 | | - 'addCSS' : function( text ) { |
| 158 | + addCSS: function( text ) { |
161 | 159 | var s = document.createElement( 'style' ); |
162 | 160 | s.type = 'text/css'; |
163 | 161 | s.rel = 'stylesheet'; |
— | — | @@ -180,7 +178,7 @@ |
181 | 179 | * @return mixed Boolean visibility of the toc (true if it's visible) |
182 | 180 | * or Null if there was no table of contents. |
183 | 181 | */ |
184 | | - 'toggleToc' : function( $toggleLink, callback ) { |
| 182 | + toggleToc: function( $toggleLink, callback ) { |
185 | 183 | var $tocList = $( '#toc ul:first' ); |
186 | 184 | |
187 | 185 | // This function shouldn't be called if there's no TOC, |
— | — | @@ -218,7 +216,7 @@ |
219 | 217 | * @param url string URL to search through (optional) |
220 | 218 | * @return mixed Parameter value or null. |
221 | 219 | */ |
222 | | - 'getParamValue' : function( param, url ) { |
| 220 | + getParamValue: function( param, url ) { |
223 | 221 | url = url || document.location.href; |
224 | 222 | // Get last match, stop at hash |
225 | 223 | var re = new RegExp( '^[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' ), |
— | — | @@ -236,13 +234,13 @@ |
237 | 235 | * Access key prefix. Will be re-defined based on browser/operating system |
238 | 236 | * detection in mw.util.init(). |
239 | 237 | */ |
240 | | - 'tooltipAccessKeyPrefix' : 'alt-', |
| 238 | + tooltipAccessKeyPrefix: 'alt-', |
241 | 239 | |
242 | 240 | /** |
243 | 241 | * @var RegExp |
244 | 242 | * Regex to match accesskey tooltips. |
245 | 243 | */ |
246 | | - 'tooltipAccessKeyRegexp': /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/, |
| 244 | + tooltipAccessKeyRegexp: /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/, |
247 | 245 | |
248 | 246 | /** |
249 | 247 | * Add the appropriate prefix to the accesskey shown in the tooltip. |
— | — | @@ -250,37 +248,25 @@ |
251 | 249 | * otherwise, all the nodes that will probably have accesskeys by |
252 | 250 | * default are updated. |
253 | 251 | * |
254 | | - * @param nodeList {Array|jQuery} [optional] A jQuery object, or array |
| 252 | + * @param $nodes {Array|jQuery} [optional] A jQuery object, or array |
255 | 253 | * of elements to update. |
256 | 254 | */ |
257 | | - 'updateTooltipAccessKeys' : function( nodeList ) { |
258 | | - var $nodes; |
259 | | - if ( !nodeList ) { |
260 | | - |
261 | | - // Rather than scanning all links, just the elements that |
262 | | - // contain the relevant links |
263 | | - this.updateTooltipAccessKeys( |
264 | | - $( '#column-one a, #mw-head a, #mw-panel a, #p-logo a' ) ); |
265 | | - |
266 | | - // these are rare enough that no such optimization is needed |
267 | | - this.updateTooltipAccessKeys( $( 'input' ) ); |
268 | | - this.updateTooltipAccessKeys( $( 'label' ) ); |
269 | | - |
270 | | - return; |
271 | | - |
272 | | - } else if ( nodeList instanceof $ ) { |
273 | | - $nodes = nodeList; |
274 | | - } else { |
275 | | - $nodes = $( nodeList ); |
| 255 | + updateTooltipAccessKeys: function( $nodes ) { |
| 256 | + if ( !$nodes ) { |
| 257 | + // Rather than going into a loop of all anchor tags, limit to few elements that |
| 258 | + // contain the relevant anchor tags. |
| 259 | + // Input and label are rare enough that no such optimization is needed |
| 260 | + $nodes = $( '#column-one a, #mw-head a, #mw-panel a, #p-logo a, input, label' ); |
| 261 | + } else if ( !( $nodes instanceof $ ) ) { |
| 262 | + $nodes = $( $nodes ); |
276 | 263 | } |
277 | 264 | |
278 | | - $nodes.each( function ( i ) { |
279 | | - var tip = $(this).attr( 'title' ); |
280 | | - if ( !!tip && util.tooltipAccessKeyRegexp.exec( tip ) ) { |
281 | | - tip = tip.replace( util.tooltipAccessKeyRegexp, |
282 | | - '[' + util.tooltipAccessKeyPrefix + "$5]" ); |
283 | | - $(this).attr( 'title', tip ); |
| 265 | + $nodes.attr( 'title', function( i, val ) { |
| 266 | + if ( val && util.tooltipAccessKeyRegexp.exec( val ) ) { |
| 267 | + return val.replace( util.tooltipAccessKeyRegexp, |
| 268 | + '[' + util.tooltipAccessKeyPrefix + '$5]' ); |
284 | 269 | } |
| 270 | + return val; |
285 | 271 | } ); |
286 | 272 | }, |
287 | 273 | |
— | — | @@ -289,7 +275,7 @@ |
290 | 276 | * A jQuery object that refers to the page-content element |
291 | 277 | * Populated by init(). |
292 | 278 | */ |
293 | | - '$content' : null, |
| 279 | + $content: null, |
294 | 280 | |
295 | 281 | /** |
296 | 282 | * Add a link to a portlet menu on the page, such as: |
— | — | @@ -326,7 +312,7 @@ |
327 | 313 | * @return mixed The DOM Node of the added item (a ListItem or Anchor element, |
328 | 314 | * depending on the skin) or null if no element was added to the document. |
329 | 315 | */ |
330 | | - 'addPortletLink' : function( portlet, href, text, id, tooltip, accesskey, nextnode ) { |
| 316 | + addPortletLink: function( portlet, href, text, id, tooltip, accesskey, nextnode ) { |
331 | 317 | var $item, $link, $portlet, $ul; |
332 | 318 | |
333 | 319 | // Check if there's atleast 3 arguments to prevent a TypeError |
— | — | @@ -347,7 +333,7 @@ |
348 | 334 | $( '#quickbar' ).append( $link.after( '<br/>' ) ); |
349 | 335 | return $link[0]; |
350 | 336 | case 'nostalgia' : |
351 | | - $( '#searchform' ).before( $link).before( ' | ' ); |
| 337 | + $( '#searchform' ).before( $link ).before( ' | ' ); |
352 | 338 | return $link[0]; |
353 | 339 | default : // Skins like chick, modern, monobook, myskin, simple, vector... |
354 | 340 | |
— | — | @@ -410,7 +396,6 @@ |
411 | 397 | } else if ( typeof nextnode === 'string' && $ul.find( nextnode ).length !== 0 ) { |
412 | 398 | $ul.find( nextnode ).eq( 0 ).before( $item ); |
413 | 399 | |
414 | | - |
415 | 400 | // If the jQuery selector isn't found within the <ul>, |
416 | 401 | // or if nextnode was invalid or not passed at all, |
417 | 402 | // then just append it at the end of the <ul> (this is the default behaviour) |
— | — | @@ -433,10 +418,8 @@ |
434 | 419 | * to allow CSS/JS to hide different boxes. null = no class used. |
435 | 420 | * @return boolean True on success, false on failure. |
436 | 421 | */ |
437 | | - 'jsMessage' : function( message, className ) { |
438 | | - |
| 422 | + jsMessage: function( message, className ) { |
439 | 423 | if ( !arguments.length || message === '' || message === null ) { |
440 | | - |
441 | 424 | $( '#mw-js-message' ).empty().hide(); |
442 | 425 | return true; // Emptying and hiding message is intended behaviour, return true |
443 | 426 | |
— | — | @@ -481,7 +464,7 @@ |
482 | 465 | * @return mixed Null if mailtxt was an empty string, otherwise true/false |
483 | 466 | * is determined by validation. |
484 | 467 | */ |
485 | | - 'validateEmail' : function( mailtxt ) { |
| 468 | + validateEmail: function( mailtxt ) { |
486 | 469 | if( mailtxt === '' ) { |
487 | 470 | return null; |
488 | 471 | } |
— | — | @@ -555,7 +538,7 @@ |
556 | 539 | * @param allowBlock boolean |
557 | 540 | * @return boolean |
558 | 541 | */ |
559 | | - 'isIPv4Address' : function( address, allowBlock ) { |
| 542 | + isIPv4Address: function( address, allowBlock ) { |
560 | 543 | if ( typeof address !== 'string' ) { |
561 | 544 | return false; |
562 | 545 | } |
— | — | @@ -574,7 +557,7 @@ |
575 | 558 | * @param allowBlock boolean |
576 | 559 | * @return boolean |
577 | 560 | */ |
578 | | - 'isIPv6Address' : function( address, allowBlock ) { |
| 561 | + isIPv6Address: function( address, allowBlock ) { |
579 | 562 | if ( typeof address !== 'string' ) { |
580 | 563 | return false; |
581 | 564 | } |
— | — | @@ -599,7 +582,8 @@ |
600 | 583 | return address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) !== -1 |
601 | 584 | && address.search( /::/ ) !== -1 && address.search( /::.*::/ ) === -1; |
602 | 585 | } |
603 | | - |
604 | 586 | }; |
605 | 587 | |
| 588 | + mw.util = util; |
| 589 | + |
606 | 590 | } )( jQuery, mediaWiki ); |