r93364 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93363‎ | r93364 | r93365 >
Date:05:53, 28 July 2011
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
Applying code conventions:
* size() -> length
* strict comparison to undefined instead of typeof + string comparison
* merge var statements
* strict comparison to 0 and ''
* dot notation
* trailing whitespace
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.messageBox.js (modified) (history)
  • /trunk/phase3/resources/jquery/jquery.suggestions.js (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.util.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.suggestions.js
@@ -4,36 +4,36 @@
55 * Usage:
66 *
77 * Set options:
8 - * $('#textbox').suggestions( { option1: value1, option2: value2 } );
9 - * $('#textbox').suggestions( option, value );
 8+ * $( '#textbox' ).suggestions( { option1: value1, option2: value2 } );
 9+ * $( '#textbox' ).suggestions( option, value );
1010 * Get option:
11 - * value = $('#textbox').suggestions( option );
 11+ * value = $( '#textbox' ).suggestions( option );
1212 * Initialize:
13 - * $('#textbox').suggestions();
 13+ * $( '#textbox' ).suggestions();
1414 *
1515 * Options:
1616 *
1717 * fetch(query): Callback that should fetch suggestions and set the suggestions property. Executed in the context of the
18 - * textbox
19 - * Type: Function
 18+ * textbox
 19+ * Type: Function
2020 * cancel: Callback function to call when any pending asynchronous suggestions fetches should be canceled.
21 - * Executed in the context of the textbox
 21+ * Executed in the context of the textbox
2222 * Type: Function
2323 * special: Set of callbacks for rendering and selecting
2424 * Type: Object of Functions 'render' and 'select'
2525 * result: Set of callbacks for rendering and selecting
2626 * Type: Object of Functions 'render' and 'select'
2727 * $region: jQuery selection of element to place the suggestions below and match width of
28 - * Type: jQuery Object, Default: $(this)
 28+ * Type: jQuery Object, Default: $(this)
2929 * suggestions: Suggestions to display
30 - * Type: Array of strings
 30+ * Type: Array of strings
3131 * maxRows: Maximum number of suggestions to display at one time
32 - * Type: Number, Range: 1 - 100, Default: 7
 32+ * Type: Number, Range: 1 - 100, Default: 7
3333 * delay: Number of ms to wait for the user to stop typing
34 - * Type: Number, Range: 0 - 1200, Default: 120
 34+ * Type: Number, Range: 0 - 1200, Default: 120
3535 * submitOnClick: Whether to submit the form containing the textbox when a suggestion is clicked
3636 * Type: Boolean, Default: false
37 - * maxExpandFactor: Maximum suggestions box width relative to the textbox width. If set to e.g. 2, the suggestions box
 37+ * maxExpandFactor: Maximum suggestions box width relative to the textbox width. If set to e.g. 2, the suggestions box
3838 * will never be grown beyond 2 times the width of the textbox.
3939 * Type: Number, Range: 1 - infinity, Default: 3
4040 * positionFromLeft: Whether to position the suggestion box with the left attribute or the right
@@ -52,7 +52,7 @@
5353 if ( context.data.timerID != null ) {
5454 clearTimeout( context.data.timerID );
5555 }
56 - if ( typeof context.config.cancel == 'function' ) {
 56+ if ( $.isFunction( context.config.cancel ) ) {
5757 context.config.cancel.call( context.data.$textbox );
5858 }
5959 },
@@ -66,7 +66,7 @@
6767 },
6868 /**
6969 * Ask the user-specified callback for new suggestions. Any previous delayed call to this function still pending
70 - * will be canceled. If the value in the textbox is empty or hasn't changed since the last time suggestions were fetched, this
 70+ * will be canceled. If the value in the textbox is empty or hasn't changed since the last time suggestions were fetched, this
7171 * function does nothing.
7272 * @param {Boolean} delayed Whether or not to delay this by the currently configured amount of time
7373 */
@@ -74,12 +74,12 @@
7575 // Only fetch if the value in the textbox changed and is not empty
7676 // if the textbox is empty then clear the result div, but leave other settings intouched
7777 function maybeFetch() {
78 - if ( context.data.$textbox.val().length == 0 ) {
 78+ if ( context.data.$textbox.val().length === 0 ) {
7979 context.data.$container.hide();
8080 context.data.prevText = '';
8181 } else if ( context.data.$textbox.val() !== context.data.prevText ) {
82 - if ( typeof context.config.fetch == 'function' ) {
83 - context.data.prevText = context.data.$textbox.val();
 82+ if ( typeof context.config.fetch === 'function' ) {
 83+ context.data.prevText = context.data.$textbox.val();
8484 context.config.fetch.call( context.data.$textbox, context.data.$textbox.val() );
8585 }
8686 }
@@ -99,7 +99,7 @@
100100 },
101101 special: function( context ) {
102102 // Allow custom rendering - but otherwise don't do any rendering
103 - if ( typeof context.config.special.render == 'function' ) {
 103+ if ( typeof context.config.special.render === 'function' ) {
104104 // Wait for the browser to update the value
105105 setTimeout( function() {
106106 // Render special
@@ -126,8 +126,8 @@
127127 case 'suggestions':
128128 context.config[property] = value;
129129 // Update suggestions
130 - if ( typeof context.data !== 'undefined' ) {
131 - if ( context.data.$textbox.val().length == 0 ) {
 130+ if ( context.data !== undefined ) {
 131+ if ( context.data.$textbox.val().length === 0 ) {
132132 // Hide the div when no suggestion exist
133133 context.data.$container.hide();
134134 } else {
@@ -135,17 +135,17 @@
136136 context.data.$container.show();
137137 // Update the size and position of the list
138138 var newCSS = {
139 - 'top': context.config.$region.offset().top + context.config.$region.outerHeight(),
140 - 'bottom': 'auto',
141 - 'width': context.config.$region.outerWidth(),
142 - 'height': 'auto'
 139+ top: context.config.$region.offset().top + context.config.$region.outerHeight(),
 140+ bottom: 'auto',
 141+ width: context.config.$region.outerWidth(),
 142+ height: 'auto'
143143 };
144144 if ( context.config.positionFromLeft ) {
145 - newCSS['left'] = context.config.$region.offset().left;
146 - newCSS['right'] = 'auto';
 145+ newCSS.left = context.config.$region.offset().left;
 146+ newCSS.right = 'auto';
147147 } else {
148 - newCSS['left'] = 'auto';
149 - newCSS['right'] = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
 148+ newCSS.left = 'auto';
 149+ newCSS.right = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
150150 }
151151 context.data.$container.css( newCSS );
152152 var $results = context.data.$container.children( '.suggestions-results' );
@@ -155,7 +155,7 @@
156156 var matchedText = null;
157157 for ( var i = 0; i < context.config.suggestions.length; i++ ) {
158158 var text = context.config.suggestions[i];
159 - var $result = $( '<div />' )
 159+ var $result = $( '<div>' )
160160 .addClass( 'suggestions-result' )
161161 .attr( 'rel', i )
162162 .data( 'text', context.config.suggestions[i] )
@@ -167,18 +167,18 @@
168168 } )
169169 .appendTo( $results );
170170 // Allow custom rendering
171 - if ( typeof context.config.result.render == 'function' ) {
 171+ if ( typeof context.config.result.render === 'function' ) {
172172 context.config.result.render.call( $result, context.config.suggestions[i] );
173173 } else {
174174 // Add <span> with text
175175 if( context.config.highlightInput ) {
176176 matchedText = context.data.prevText;
177177 }
178 - $result.append( $( '<span />' )
 178+ $result.append( $( '<span>' )
179179 .css( 'whiteSpace', 'nowrap' )
180180 .text( text )
181181 );
182 -
 182+
183183 // Widen results box if needed
184184 // New width is only calculated here, applied later
185185 var $span = $result.children( 'span' );
@@ -223,25 +223,25 @@
224224 highlight: function( context, result, updateTextbox ) {
225225 var selected = context.data.$container.find( '.suggestions-result-current' );
226226 if ( !result.get || selected.get( 0 ) != result.get( 0 ) ) {
227 - if ( result == 'prev' ) {
 227+ if ( result === 'prev' ) {
228228 if( selected.is( '.suggestions-special' ) ) {
229 - result = context.data.$container.find( '.suggestions-result:last' )
 229+ result = context.data.$container.find( '.suggestions-result:last' );
230230 } else {
231231 result = selected.prev();
232 - if ( selected.length == 0 ) {
 232+ if ( selected.length === 0 ) {
233233 // we are at the beginning, so lets jump to the last item
234 - if ( context.data.$container.find( '.suggestions-special' ).html() != "" ) {
 234+ if ( context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
235235 result = context.data.$container.find( '.suggestions-special' );
236236 } else {
237237 result = context.data.$container.find( '.suggestions-results div:last' );
238238 }
239239 }
240240 }
241 - } else if ( result == 'next' ) {
242 - if ( selected.length == 0 ) {
 241+ } else if ( result === 'next' ) {
 242+ if ( selected.length === 0 ) {
243243 // No item selected, go to the first one
244244 result = context.data.$container.find( '.suggestions-results div:first' );
245 - if ( result.length == 0 && context.data.$container.find( '.suggestions-special' ).html() != "" ) {
 245+ if ( result.length === 0 && context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
246246 // No suggestion exists, go to the special one directly
247247 result = context.data.$container.find( '.suggestions-special' );
248248 }
@@ -250,8 +250,8 @@
251251 if ( selected.is( '.suggestions-special' ) ) {
252252 result = $( [] );
253253 } else if (
254 - result.length == 0 &&
255 - context.data.$container.find( '.suggestions-special' ).html() != ""
 254+ result.length === 0 &&
 255+ context.data.$container.find( '.suggestions-special' ).html() !== ''
256256 ) {
257257 // We were at the last item, jump to the specials!
258258 result = context.data.$container.find( '.suggestions-special' );
@@ -262,7 +262,7 @@
263263 result.addClass( 'suggestions-result-current' );
264264 }
265265 if ( updateTextbox ) {
266 - if ( result.length == 0 || result.is( '.suggestions-special' ) ) {
 266+ if ( result.length === 0 || result.is( '.suggestions-special' ) ) {
267267 $.suggestions.restore( context );
268268 } else {
269269 context.data.$textbox.val( result.data( 'text' ) );
@@ -278,8 +278,8 @@
279279 * @param key Integer Code of key pressed
280280 */
281281 keypress: function( e, context, key ) {
282 - var wasVisible = context.data.$container.is( ':visible' );
283 - var preventDefault = false;
 282+ var wasVisible = context.data.$container.is( ':visible' ),
 283+ preventDefault = false;
284284 switch ( key ) {
285285 // Arrow down
286286 case 40:
@@ -312,17 +312,17 @@
313313 context.data.$container.hide();
314314 preventDefault = wasVisible;
315315 selected = context.data.$container.find( '.suggestions-result-current' );
316 - if ( selected.size() == 0 || context.data.selectedWithMouse ) {
317 - // if nothing is selected OR if something was selected with the mouse,
 316+ if ( selected.length === 0 || context.data.selectedWithMouse ) {
 317+ // if nothing is selected OR if something was selected with the mouse,
318318 // cancel any current requests and submit the form
319319 $.suggestions.cancel( context );
320320 context.config.$region.closest( 'form' ).submit();
321321 } else if ( selected.is( '.suggestions-special' ) ) {
322 - if ( typeof context.config.special.select == 'function' ) {
 322+ if ( typeof context.config.special.select === 'function' ) {
323323 context.config.special.select.call( selected, context.data.$textbox );
324324 }
325325 } else {
326 - if ( typeof context.config.result.select == 'function' ) {
 326+ if ( typeof context.config.result.select === 'function' ) {
327327 $.suggestions.highlight( context, selected, true );
328328 context.config.result.select.call( selected, context.data.$textbox );
329329 } else {
@@ -341,17 +341,17 @@
342342 }
343343 };
344344 $.fn.suggestions = function() {
345 -
 345+
346346 // Multi-context fields
347347 var returnValue = null;
348348 var args = arguments;
349 -
 349+
350350 $(this).each( function() {
351351
352352 /* Construction / Loading */
353 -
 353+
354354 var context = $(this).data( 'suggestions-context' );
355 - if ( typeof context == 'undefined' || context == null ) {
 355+ if ( context === undefined || context === null ) {
356356 context = {
357357 config: {
358358 'fetch' : function() {},
@@ -369,17 +369,17 @@
370370 }
371371 };
372372 }
373 -
 373+
374374 /* API */
375 -
 375+
376376 // Handle various calling styles
377377 if ( args.length > 0 ) {
378 - if ( typeof args[0] == 'object' ) {
 378+ if ( typeof args[0] === 'object' ) {
379379 // Apply set of properties
380380 for ( var key in args[0] ) {
381381 $.suggestions.configure( context, key, args[0][key] );
382382 }
383 - } else if ( typeof args[0] == 'string' ) {
 383+ } else if ( typeof args[0] === 'string' ) {
384384 if ( args.length > 1 ) {
385385 // Set property values
386386 $.suggestions.configure( context, args[0], args[1] );
@@ -389,10 +389,10 @@
390390 }
391391 }
392392 }
393 -
 393+
394394 /* Initialization */
395 -
396 - if ( typeof context.data == 'undefined' ) {
 395+
 396+ if ( context.data === undefined ) {
397397 context.data = {
398398 // ID of running timer
399399 'timerID': null,
@@ -407,23 +407,23 @@
408408 };
409409 // Setup the css for positioning the results box
410410 var newCSS = {
411 - 'top': Math.round( context.data.$textbox.offset().top + context.data.$textbox.outerHeight() ),
412 - 'width': context.data.$textbox.outerWidth(),
413 - 'display': 'none'
 411+ top: Math.round( context.data.$textbox.offset().top + context.data.$textbox.outerHeight() ),
 412+ width: context.data.$textbox.outerWidth(),
 413+ display: 'none'
414414 };
415415 if ( context.config.positionFromLeft ) {
416 - newCSS['left'] = context.config.$region.offset().left;
417 - newCSS['right'] = 'auto';
 416+ newCSS.left = context.config.$region.offset().left;
 417+ newCSS.right = 'auto';
418418 } else {
419 - newCSS['left'] = 'auto';
420 - newCSS['right'] = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
 419+ newCSS.left = 'auto';
 420+ newCSS.right = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
421421 }
422 -
423 - context.data.$container = $( '<div />' )
 422+
 423+ context.data.$container = $( '<div>' )
424424 .css( newCSS )
425425 .addClass( 'suggestions' )
426426 .append(
427 - $( '<div />' ).addClass( 'suggestions-results' )
 427+ $( '<div>' ).addClass( 'suggestions-results' )
428428 // Can't use click() because the container div is hidden when the textbox loses focus. Instead,
429429 // listen for a mousedown followed by a mouseup on the same div
430430 .mousedown( function( e ) {
@@ -438,14 +438,14 @@
439439 }
440440 $.suggestions.highlight( context, $result, true );
441441 context.data.$container.hide();
442 - if ( typeof context.config.result.select == 'function' ) {
 442+ if ( typeof context.config.result.select === 'function' ) {
443443 context.config.result.select.call( $result, context.data.$textbox );
444444 }
445445 context.data.$textbox.focus();
446446 } )
447447 )
448448 .append(
449 - $( '<div />' ).addClass( 'suggestions-special' )
 449+ $( '<div>' ).addClass( 'suggestions-special' )
450450 // Can't use click() because the container div is hidden when the textbox loses focus. Instead,
451451 // listen for a mousedown followed by a mouseup on the same div
452452 .mousedown( function( e ) {
@@ -459,7 +459,7 @@
460460 return;
461461 }
462462 context.data.$container.hide();
463 - if ( typeof context.config.special.select == 'function' ) {
 463+ if ( typeof context.config.special.select === 'function' ) {
464464 context.config.special.select.call( $special, context.data.$textbox );
465465 }
466466 context.data.$textbox.focus();
@@ -477,9 +477,9 @@
478478 .attr( 'autocomplete', 'off')
479479 .keydown( function( e ) {
480480 // Store key pressed to handle later
481 - context.data.keypressed = ( e.keyCode == undefined ) ? e.which : e.keyCode;
 481+ context.data.keypressed = ( e.keyCode === undefined ) ? e.which : e.keyCode;
482482 context.data.keypressedCount = 0;
483 -
 483+
484484 switch ( context.data.keypressed ) {
485485 // This preventDefault logic is duplicated from
486486 // $.suggestions.keypress(), which sucks
@@ -503,7 +503,7 @@
504504 .keyup( function( e ) {
505505 // Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a
506506 // keypress in between, solve it
507 - if ( context.data.keypressedCount == 0 ) {
 507+ if ( context.data.keypressedCount === 0 ) {
508508 $.suggestions.keypress( e, context, context.data.keypressed );
509509 }
510510 } )
Index: trunk/phase3/resources/jquery/jquery.messageBox.js
@@ -11,7 +11,7 @@
1212 * @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0>
1313 * @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
1414 */
15 -( function( $, mw ) {
 15+( function( $ ) {
1616 // @return jQuery object of the message box
1717 $.messageBoxNew = function( options ) {
1818 options = $.extend( {
@@ -19,16 +19,16 @@
2020 'parent': 'body', // jQuery/CSS selector
2121 'insert': 'prepend' // 'prepend' or 'append'
2222 }, options );
23 - var $curBox = $( '#'+ options.id );
 23+ var $curBox = $( '#' + options.id );
2424 // Only create a new box if it doesn't exist already
25 - if ( $curBox.size() > 0 ) {
 25+ if ( $curBox.length > 0 ) {
2626 if ( $curBox.hasClass( 'js-messagebox' ) ) {
2727 return $curBox;
2828 } else {
2929 return $curBox.addClass( 'js-messagebox' );
3030 }
3131 } else {
32 - var $newBox = $( '<div/>', {
 32+ var $newBox = $( '<div>', {
3333 'id': options.id,
3434 'class': 'js-messagebox',
3535 'css': {
@@ -63,8 +63,8 @@
6464 var groupID = options.target + '-' + options.group;
6565 var $group = $( '#' + groupID );
6666 // Create group container if not existant
67 - if ( $group.size() < 1 ) {
68 - $group = $( '<div/>', {
 67+ if ( $group.length < 1 ) {
 68+ $group = $( '<div>', {
6969 'id': groupID,
7070 'class': 'js-messagebox-group'
7171 });
@@ -79,12 +79,12 @@
8080 $group.hide();
8181 } else {
8282 // Actual message addition
83 - $group.prepend( $( '<p/>' ).append( options.message ) ).show();
 83+ $group.prepend( $( '<p>' ).append( options.message ) ).show();
8484 $target.slideDown();
8585 }
8686 // If the last visible group was just hidden, slide the entire box up
8787 // Othere wise slideDown (if already visible nothing will happen)
88 - if ( $target.find( '> *:visible' ).size() === 0 ) {
 88+ if ( $target.find( '> *:visible' ).length === 0 ) {
8989 // to avoid a sudden dissapearance of the last group followed by
9090 // a slide up of only the outline, show it for a second
9191 $group.show();
@@ -95,4 +95,4 @@
9696 }
9797 return $group;
9898 };
99 -} )( jQuery, mediaWiki );
\ No newline at end of file
 99+} )( jQuery );
Index: trunk/phase3/resources/mediawiki/mediawiki.util.js
@@ -84,15 +84,15 @@
8585 $tocTitle = $( '#toctitle' ),
8686 $tocToggleLink = $( '#togglelink' );
8787 // Only add it if there is a TOC and there is no toggle added already
88 - if ( $tocContainer.size() && $tocTitle.size() && !$tocToggleLink.size() ) {
 88+ if ( $tocContainer.length && $tocTitle.length && !$tocToggleLink.length ) {
8989 var hideTocCookie = $.cookie( 'mw_hidetoc' );
90 - $tocToggleLink = $( '<a href="#" class="internal" id="togglelink">' )
 90+ $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
9191 .text( mw.msg( 'hidetoc' ) )
9292 .click( function(e){
9393 e.preventDefault();
9494 util.toggleToc( $(this) );
9595 } );
96 - $tocTitle.append( $tocToggleLink.wrap( '<span class="toctoggle">' ).parent().prepend( '&nbsp;[' ).append( ']&nbsp;' ) );
 96+ $tocTitle.append( $tocToggleLink.wrap( '<span class="toctoggle"></span>' ).parent().prepend( '&nbsp;[' ).append( ']&nbsp;' ) );
9797
9898 if ( hideTocCookie == '1' ) {
9999 // Cookie says user want toc hidden
@@ -187,7 +187,7 @@
188188
189189 // This function shouldn't be called if there's no TOC,
190190 // but just in case...
191 - if ( $tocList.size() ) {
 191+ if ( $tocList.length ) {
192192 if ( $tocList.is( ':hidden' ) ) {
193193 $tocList.slideDown( 'fast', callback );
194194 $toggleLink.text( mw.msg( 'hidetoc' ) );
@@ -255,7 +255,7 @@
256256 'updateTooltipAccessKeys' : function( nodeList ) {
257257 var $nodes;
258258 if ( !nodeList ) {
259 -
 259+
260260 // Rather than scanning all links, just the elements that
261261 // contain the relevant links
262262 this.updateTooltipAccessKeys(

Follow-up revisions

RevisionCommit summaryAuthorDate
r103961* (bug 32582) Fix TOC show/hide link regression on IE 8...brion21:44, 22 November 2011
r103962* (bug 32582) Fix TOC show/hide link regression on IE 8...brion21:46, 22 November 2011

Comments

#Comment by Nikerabbit (talk | contribs)   09:31, 28 July 2011

Sigh. Google Chrome is again broken, showing it's own suggestion on top of our ones. (Just thinking out loud, it has nothing to do with this commit.)

Status & tagging log