r107289 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107288‎ | r107289 | r107290 >
Date:20:32, 25 December 2011
Author:krinkle
Status:ok
Tags:
Comment:
[Core JS] Clean up and optimization mediawiki.util
* partial rewrite of updateTooltipAccessKeys, much shorter and a bit faster
-- Remove self-calling logic, totally unnecessary here
-- removing the .each() loop, using .attr()'s callback function instead
* directly calling util.toggleToc instead of triggering a click event
* strict comparison
* unquoted keys
* substitute single-use variable
Modified paths:
  • /trunk/phase3/resources/mediawiki/mediawiki.util.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki/mediawiki.util.js
@@ -1,27 +1,26 @@
22 /**
3 - * Utilities
 3+ * Implementents
44 */
55 ( function ( $, mw ) {
66 "use strict";
77
88 // Local cache and alias
9 - var util = mw.util = {
 9+ var util = {
1010
1111 /**
1212 * Initialisation
1313 * (don't call before document ready)
1414 */
15 - 'init' : function() {
 15+ init: function() {
1616 var profile = $.client.profile(),
17 - $tocContainer = $( '#toc' ),
1817 $tocTitle = $( '#toctitle' ),
1918 $tocToggleLink = $( '#togglelink' ),
2019 hideTocCookie;
2120
2221 /* Set up $.messageBox */
2322 $.messageBoxNew( {
24 - 'id': 'mw-js-message',
25 - 'parent': '#content'
 23+ id: 'mw-js-message',
 24+ parent: '#content'
2625 } );
2726
2827 /* Set tooltipAccessKeyPrefix */
@@ -78,14 +77,14 @@
7978
8079 // Table of contents toggle
8180 // 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 ) {
8382 hideTocCookie = $.cookie( 'mw_hidetoc' );
8483 $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
8584 .text( mw.msg( 'hidetoc' ) )
86 - .click( function(e){
 85+ .click( function( e ) {
8786 e.preventDefault();
8887 util.toggleToc( $(this) );
89 - } );
 88+ } );
9089 $tocTitle.append(
9190 $tocToggleLink
9291 .wrap( '<span class="toctoggle"></span>' )
@@ -94,9 +93,8 @@
9594 .append( ']&nbsp;' )
9695 );
9796
98 - if ( hideTocCookie == '1' ) {
99 - // Cookie says user want toc hidden
100 - $tocToggleLink.click();
 97+ if ( hideTocCookie === '1' ) {
 98+ util.toggleToc( $tocToggleLink );
10199 }
102100 }
103101 },
@@ -108,8 +106,8 @@
109107 *
110108 * @param str string String to be encoded
111109 */
112 - 'rawurlencode' : function( str ) {
113 - str = ( str + '' ).toString();
 110+ rawurlencode: function( str ) {
 111+ str = String( str );
114112 return encodeURIComponent( str )
115113 .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
116114 .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /~/g, '%7E' );
@@ -122,7 +120,7 @@
123121 *
124122 * @param str string String to be encoded
125123 */
126 - 'wikiUrlencode' : function( str ) {
 124+ wikiUrlencode: function( str ) {
127125 return this.rawurlencode( str )
128126 .replace( /%20/g, '_' ).replace( /%3A/g, ':' ).replace( /%2F/g, '/' );
129127 },
@@ -133,7 +131,7 @@
134132 * @param str string Page name to get the link for.
135133 * @return string Location for a page with name of 'str' or boolean false on error.
136134 */
137 - 'wikiGetlink' : function( str ) {
 135+ wikiGetlink: function( str ) {
138136 return mw.config.get( 'wgArticlePath' ).replace( '$1',
139137 this.wikiUrlencode( str || mw.config.get( 'wgPageName' ) ) );
140138 },
@@ -145,7 +143,7 @@
146144 * @param str string Name of script (eg. 'api'), defaults to 'index'
147145 * @return string Address to script (eg. '/w/api.php' )
148146 */
149 - 'wikiScript' : function( str ) {
 147+ wikiScript: function( str ) {
150148 return mw.config.get( 'wgScriptPath' ) + '/' + ( str || 'index' ) +
151149 mw.config.get( 'wgScriptExtension' );
152150 },
@@ -156,7 +154,7 @@
157155 * @param text string CSS to be appended
158156 * @return CSSStyleSheet
159157 */
160 - 'addCSS' : function( text ) {
 158+ addCSS: function( text ) {
161159 var s = document.createElement( 'style' );
162160 s.type = 'text/css';
163161 s.rel = 'stylesheet';
@@ -180,7 +178,7 @@
181179 * @return mixed Boolean visibility of the toc (true if it's visible)
182180 * or Null if there was no table of contents.
183181 */
184 - 'toggleToc' : function( $toggleLink, callback ) {
 182+ toggleToc: function( $toggleLink, callback ) {
185183 var $tocList = $( '#toc ul:first' );
186184
187185 // This function shouldn't be called if there's no TOC,
@@ -218,7 +216,7 @@
219217 * @param url string URL to search through (optional)
220218 * @return mixed Parameter value or null.
221219 */
222 - 'getParamValue' : function( param, url ) {
 220+ getParamValue: function( param, url ) {
223221 url = url || document.location.href;
224222 // Get last match, stop at hash
225223 var re = new RegExp( '^[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' ),
@@ -236,13 +234,13 @@
237235 * Access key prefix. Will be re-defined based on browser/operating system
238236 * detection in mw.util.init().
239237 */
240 - 'tooltipAccessKeyPrefix' : 'alt-',
 238+ tooltipAccessKeyPrefix: 'alt-',
241239
242240 /**
243241 * @var RegExp
244242 * Regex to match accesskey tooltips.
245243 */
246 - 'tooltipAccessKeyRegexp': /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/,
 244+ tooltipAccessKeyRegexp: /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/,
247245
248246 /**
249247 * Add the appropriate prefix to the accesskey shown in the tooltip.
@@ -250,37 +248,25 @@
251249 * otherwise, all the nodes that will probably have accesskeys by
252250 * default are updated.
253251 *
254 - * @param nodeList {Array|jQuery} [optional] A jQuery object, or array
 252+ * @param $nodes {Array|jQuery} [optional] A jQuery object, or array
255253 * of elements to update.
256254 */
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 );
276263 }
277264
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]' );
284269 }
 270+ return val;
285271 } );
286272 },
287273
@@ -289,7 +275,7 @@
290276 * A jQuery object that refers to the page-content element
291277 * Populated by init().
292278 */
293 - '$content' : null,
 279+ $content: null,
294280
295281 /**
296282 * Add a link to a portlet menu on the page, such as:
@@ -326,7 +312,7 @@
327313 * @return mixed The DOM Node of the added item (a ListItem or Anchor element,
328314 * depending on the skin) or null if no element was added to the document.
329315 */
330 - 'addPortletLink' : function( portlet, href, text, id, tooltip, accesskey, nextnode ) {
 316+ addPortletLink: function( portlet, href, text, id, tooltip, accesskey, nextnode ) {
331317 var $item, $link, $portlet, $ul;
332318
333319 // Check if there's atleast 3 arguments to prevent a TypeError
@@ -347,7 +333,7 @@
348334 $( '#quickbar' ).append( $link.after( '<br/>' ) );
349335 return $link[0];
350336 case 'nostalgia' :
351 - $( '#searchform' ).before( $link).before( ' &#124; ' );
 337+ $( '#searchform' ).before( $link ).before( ' &#124; ' );
352338 return $link[0];
353339 default : // Skins like chick, modern, monobook, myskin, simple, vector...
354340
@@ -410,7 +396,6 @@
411397 } else if ( typeof nextnode === 'string' && $ul.find( nextnode ).length !== 0 ) {
412398 $ul.find( nextnode ).eq( 0 ).before( $item );
413399
414 -
415400 // If the jQuery selector isn't found within the <ul>,
416401 // or if nextnode was invalid or not passed at all,
417402 // then just append it at the end of the <ul> (this is the default behaviour)
@@ -433,10 +418,8 @@
434419 * to allow CSS/JS to hide different boxes. null = no class used.
435420 * @return boolean True on success, false on failure.
436421 */
437 - 'jsMessage' : function( message, className ) {
438 -
 422+ jsMessage: function( message, className ) {
439423 if ( !arguments.length || message === '' || message === null ) {
440 -
441424 $( '#mw-js-message' ).empty().hide();
442425 return true; // Emptying and hiding message is intended behaviour, return true
443426
@@ -481,7 +464,7 @@
482465 * @return mixed Null if mailtxt was an empty string, otherwise true/false
483466 * is determined by validation.
484467 */
485 - 'validateEmail' : function( mailtxt ) {
 468+ validateEmail: function( mailtxt ) {
486469 if( mailtxt === '' ) {
487470 return null;
488471 }
@@ -555,7 +538,7 @@
556539 * @param allowBlock boolean
557540 * @return boolean
558541 */
559 - 'isIPv4Address' : function( address, allowBlock ) {
 542+ isIPv4Address: function( address, allowBlock ) {
560543 if ( typeof address !== 'string' ) {
561544 return false;
562545 }
@@ -574,7 +557,7 @@
575558 * @param allowBlock boolean
576559 * @return boolean
577560 */
578 - 'isIPv6Address' : function( address, allowBlock ) {
 561+ isIPv6Address: function( address, allowBlock ) {
579562 if ( typeof address !== 'string' ) {
580563 return false;
581564 }
@@ -599,7 +582,8 @@
600583 return address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) !== -1
601584 && address.search( /::/ ) !== -1 && address.search( /::.*::/ ) === -1;
602585 }
603 -
604586 };
605587
 588+ mw.util = util;
 589+
606590 } )( jQuery, mediaWiki );

Follow-up revisions

RevisionCommit summaryAuthorDate
r107291typo; follows-up r107289krinkle20:34, 25 December 2011

Status & tagging log