r103642 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103641‎ | r103642 | r103643 >
Date:06:04, 19 November 2011
Author:nikerabbit
Status:ok
Tags:
Comment:
Tidy up to match coding conventions
Modified paths:
  • /trunk/extensions/Narayam/js/ext.narayam.core.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Narayam/js/ext.narayam.core.js
@@ -32,7 +32,7 @@
3333 var availableSchemes = mw.config.get( 'wgNarayamAvailableSchemes' ) || {};
3434 // All input methods. This will be used for selecting input methods from languages
3535 // other than uselang- optionally
36 - var allImes = mw.config.get( 'wgNarayamAllSchemes' ) || {};
 36+ var allImes = mw.config.get( 'wgNarayamAllSchemes' ) || {};
3737 // Currently selected scheme
3838 var currentScheme = null;
3939 // Shortcut key for turning Narayam on and off
@@ -51,14 +51,14 @@
5252 function transliterate( str, keyBuffer, useExtended ) {
5353 var rules = currentScheme.extended_keyboard && useExtended ?
5454 currentScheme.rules_x : currentScheme.rules;
55 - for ( var i = 0; i < rules.length; i++ ) {
 55+ for ( var i = 0; i < rules.length; i++ ) {
5656 var regex = new RegExp( rules[i][0] + '$' );
57 - if ( regex.test( str ) // Input string match
 57+ if ( regex.test( str ) // Input string match
5858 &&
5959 (
6060 rules[i][1].length == 0 // Keybuffer match not required
6161 ||
62 - ( // Keybuffer match specified, so it should be met
 62+ ( // Keybuffer match specified, so it should be met
6363 rules[i][1].length > 0
6464 && rules[i][1].length <= keyBuffer.length
6565 && new RegExp( rules[i][1] + '$' ).test( keyBuffer )
@@ -83,8 +83,7 @@
8484 function lastNChars( str, pos, n ) {
8585 if ( n === 0 ) {
8686 return '';
87 - }
88 - if ( pos <= n ) {
 87+ } else if ( pos <= n ) {
8988 return str.substr( 0, pos );
9089 } else {
9190 return str.substr( pos - n, n );
@@ -133,13 +132,13 @@
134133 key: 'm'
135134 }
136135 // Browser sniffing to determine the available shortcutKey
137 - // Refer: mediawiki.util.js and en.wikipedia.org/wiki/Access_key
 136+ // Refer: mediawiki.util.js and en.wikipedia.org/wiki/Access_key
138137 var profile = $.client.profile();
139138 // Safari/Konqueror on any platform, but not Safari on Windows
140139 // or any browser on Mac except chrome and opera
141140 if ( !( profile.platform == 'win' && profile.name == 'safari' ) &&
142141 ( profile.name == 'safari'|| profile.platform == 'mac' || profile.name == 'konqueror' )
143 - && !(profile.name == 'opera' || profile.name == 'chrome' ) ) {
 142+ && !( profile.name == 'opera' || profile.name == 'chrome' ) ) {
144143 defaultShortcut.key = 'g';
145144 }
146145 // For Opera in OSX, shortcut is control+command+m.
@@ -177,8 +176,6 @@
178177 * current state of Narayam
179178 */
180179 function changeVisual( $element ) {
181 - // It is simple in working
182 - // If Narayam is active add narayam class otherwise remove narayam class
183180 if ( enabled ) {
184181 $element.addClass( 'narayam-input' );
185182 } else {
@@ -280,14 +277,13 @@
281278 replacement = replacement.substring( divergingPos );
282279
283280 $this.textSelection( 'encapsulateSelection', {
284 - 'peri': replacement,
285 - 'replace': true,
286 - 'selectPeri': false,
287 - 'selectionStart': startPos - input.length + 1,
288 - 'selectionEnd': endPos
 281+ peri: replacement,
 282+ replace: true,
 283+ selectPeri: false,
 284+ selectionStart: startPos - input.length + 1,
 285+ selectionEnd: endPos
 286+ } );
289287
290 - } );
291 -
292288 e.stopPropagation();
293289 return false;
294290 }
@@ -297,7 +293,7 @@
298294 * @param e Event object
299295 */
300296 function onfocus( e ) {
301 - if (!$( this ).data( 'narayamKeyBuffer' )) {
 297+ if ( !$( this ).data( 'narayamKeyBuffer' ) ) {
302298 // First-time focus on the input field
303299 // So, initialise a key buffer for it
304300 $( this ).data( 'narayamKeyBuffer', '' );
@@ -322,7 +318,7 @@
323319 * or an array of DOM elements, or a single DOM element, or a selector
324320 */
325321 this.addInputs = function( inputs ) {
326 - if (typeof( inputs ) == "string" ) {
 322+ if ( typeof( inputs ) === "string" ) {
327323 // If a string is passed, it is CSS selector
328324 // We can use jQuery's .live() instead of .bind()
329325 // So Narayam can work on elements added later to DOM too
@@ -337,8 +333,8 @@
338334 $newInputs
339335 .bind( 'keydown.narayam', onkeydown )
340336 .bind( 'keypress.narayam', onkeypress )
341 - .bind( 'focus', onfocus)
342 - .bind( 'blur', onblur);
 337+ .bind( 'focus', onfocus )
 338+ .bind( 'blur', onblur );
343339 }
344340 };
345341
@@ -347,9 +343,9 @@
348344 */
349345 this.enable = function() {
350346 if ( !enabled ) {
351 - $.cookie( 'narayam-enabled', '1', { 'path': '/', 'expires': 30 } );
 347+ $.cookie( 'narayam-enabled', '1', { path: '/', expires: 30 } );
352348 $( '#narayam-toggle' ).prop( 'checked', true );
353 - $( 'li#pt-narayam')
 349+ $( 'li#pt-narayam' )
354350 .removeClass( 'narayam-inactive' )
355351 .addClass( 'narayam-active' );
356352 enabled = true;
@@ -361,9 +357,9 @@
362358 */
363359 this.disable = function() {
364360 if ( enabled ) {
365 - $.cookie( 'narayam-enabled', '0', { 'path': '/', 'expires': 30 } );
 361+ $.cookie( 'narayam-enabled', '0', { path: '/', expires: 30 } );
366362 $( '#narayam-toggle' ).prop( 'checked', false );
367 - $( 'li#pt-narayam')
 363+ $( 'li#pt-narayam' )
368364 .removeClass( 'narayam-active' )
369365 .addClass( 'narayam-inactive' );
370366 enabled = false;
@@ -424,7 +420,6 @@
425421 this.addScheme = function( name, data ) {
426422 schemes[name] = data;
427423 return true;
428 -
429424 };
430425
431426 /**
@@ -436,21 +431,20 @@
437432 if ( typeof recent === "string" ) {
438433 recent = recent.split( "," );
439434 };
440 - recent = $.grep(recent, function(value) {
 435+ recent = $.grep( recent, function( value ) {
441436 return value != name;
442 - });
 437+ } );
443438 recent.unshift( name );
444439 recent = recent.slice( 0, recentItemsLength );
445440 recent = recent.join( "," );
446 - $.cookie( 'narayam-scheme', recent, { 'path': '/', 'expires': 30 } );
 441+ $.cookie( 'narayam-scheme', recent, { path: '/', expires: 30 } );
447442 if ( name in schemes ) {
448443 currentScheme = schemes[name];
449 - }
450 - else {
 444+ } else {
451445 // load the rules dynamically.
452 - mw.loader.using( "ext.narayam.rules." + name, function() {
 446+ mw.loader.using( "ext.narayam.rules." + name, function() {
453447 currentScheme = schemes[name];
454 - });
 448+ } );
455449 }
456450 return true;
457451 };
@@ -461,20 +455,14 @@
462456 * from a cookie or wgNarayamEnableByDefault
463457 */
464458 this.setup = function() {
465 - // Build the menu
466 - if ( !that.buildMenu() ) {
467 - // /buildMenu() returned false
468 - // No need to proceed
469 - return;
470 - }
471 -
 459+ that.buildMenu();
472460 // Restore state from cookies
473461 var recentSchemes = $.cookie( 'narayam-scheme' );
474462 var lastScheme = null;
475463 if ( typeof recent === "string" ) {
476464 lastScheme = recent.split( "," )[0];
477465 }
478 - if ( lastScheme) {
 466+ if ( lastScheme ) {
479467 that.setScheme( lastScheme );
480468 $( '#narayam-' + lastScheme ).prop( 'checked', true );
481469 } else {
@@ -487,30 +475,26 @@
488476 var enabledCookie = $.cookie( 'narayam-enabled' );
489477 if ( enabledCookie == '1' || ( mw.config.get( 'wgNarayamEnabledByDefault' ) && enabledCookie !== '0' ) ) {
490478 that.enable();
491 - }
492 - else {
 479+ } else {
493480 $( 'li#pt-narayam' ).addClass( 'narayam-inactive' );
494481 }
495482 // Renew the narayam-enabled cookie. narayam-scheme is renewed by setScheme()
496483 if ( enabledCookie ) {
497 - $.cookie( 'narayam-enabled', enabledCookie, {
498 - 'path': '/',
499 - 'expires': 30
500 - } );
 484+ $.cookie( 'narayam-enabled', enabledCookie, { path: '/', expires: 30 } );
501485 }
502486
503487 };
504488 /**
505489 * Construct the menu item, for the given scheme name.
506490 */
507 - this.buildMenuItem =function(scheme) {
 491+ this.buildMenuItem = function( scheme ) {
508492 var $input = $( '<input type="radio" name="narayam-input-method" class="narayam-scheme" />' );
509493 $input.attr( 'id', 'narayam-' + scheme ).val( scheme );
510494
511495 var $narayamMenuItemLabel = $( '<label>' )
512 - .attr( 'for' ,'narayam-' + scheme )
513 - .append( $input )
514 - .append( mw.message( "narayam-"+ scheme ).escaped() );
 496+ .attr( 'for' ,'narayam-' + scheme )
 497+ .append( $input )
 498+ .append( mw.message( "narayam-"+ scheme ).escaped() );
515499
516500 var $narayamMenuItem = $( '<li>' )
517501 .append( $input )
@@ -539,8 +523,8 @@
540524 if ( $.inArray( scheme, seen ) > -1 ) { continue; }
541525 seen.push( scheme );
542526 if ( count++ > recentItemsLength ) { break; }
543 - $narayamMenuItem = that.buildMenuItem(scheme);
544 - $narayamMenuItem.addClass('narayam-recent-menu-item');
 527+ $narayamMenuItem = that.buildMenuItem( scheme );
 528+ $narayamMenuItem.addClass( 'narayam-recent-menu-item' );
545529 $narayamMenuItems.append( $narayamMenuItem );
546530 }
547531 // menu items for the language of wiki.
@@ -551,21 +535,21 @@
552536 if ( !langschemes ) continue;
553537 for ( var scheme in langschemes ) {
554538 haveSchemes = true;
555 - if ( $.inArray( scheme, seen ) > -1 ) { continue; }
 539+ if ( $.inArray( scheme, seen ) !== -1 ) { continue; }
556540 seen.push( scheme );
557 - $narayamMenuItem = that.buildMenuItem(scheme);
 541+ $narayamMenuItem = that.buildMenuItem( scheme );
558542 $narayamMenuItems.append( $narayamMenuItem );
559543 }
560544 }
 545+
561546 if ( !haveSchemes ) {
562547 // No schemes available, don't show the tool
563 - // So return false
564548 return false;
565549 }
566550
567551 // Event listener for scheme selection.
568552 $( '.narayam-scheme', $( '#narayam-menu-items > ul')[0] ).live( 'click', function() {
569 - that.setScheme( $(this).val() );
 553+ that.setScheme( $( this ).val() );
570554 } );
571555
572556 // Build enable/disable checkbox and label
@@ -579,20 +563,21 @@
580564 .text( mw.msg( 'narayam-toggle-ime', shortcutText() ) )
581565 .prepend( $checkbox )
582566 .prop( 'title', mw.msg( 'narayam-checkbox-tooltip' ) );
 567+
 568+ var $moreLink = $( '<a>' )
 569+ .text( mw.msg( 'narayam-more-imes' ) )
 570+ .click( function() {
 571+ $('.narayam-scheme-dynamic-item').toggle( 'fast' );
 572+ if ( $('li.narayam-more-imes-link').hasClass( 'open' ) ) {
 573+ $('li.narayam-more-imes-link').removeClass( 'open' );
 574+ } else {
 575+ $('li.narayam-more-imes-link').addClass( 'open' );
 576+ }
 577+ } );
583578
584 - $narayamMenuItems.append( $( '<li>' ).addClass( 'narayam-more-imes-link' )
585 - .append(
586 - $( '<a>' )
587 - .text( mw.msg( 'narayam-more-imes' ) )
588 - .click( function() {
589 - $('.narayam-scheme-dynamic-item').toggle('fast');
590 - if( $('li.narayam-more-imes-link').hasClass( 'open' )){
591 - $('li.narayam-more-imes-link').removeClass('open');
592 - }else{
593 - $('li.narayam-more-imes-link').addClass('open');
594 - }
595 - })
596 - )
 579+ $narayamMenuItems.append( $( '<li>' )
 580+ .addClass( 'narayam-more-imes-link' )
 581+ .append( $moreLink )
597582 );
598583
599584 for ( var lang in allImes ) {
@@ -601,7 +586,7 @@
602587 // Donot repeat the input methods in more input methods section.
603588 // If already shown on recent items.
604589 if ( $.inArray( langscheme, seen ) > -1 ) { continue; }
605 - $narayamMenuItem = that.buildMenuItem(langscheme);
 590+ $narayamMenuItem = that.buildMenuItem( langscheme );
606591 $narayamMenuItem.addClass( 'narayam-scheme-dynamic-item' );
607592 $narayamMenuItems.append( $narayamMenuItem );
608593
@@ -610,10 +595,10 @@
611596
612597 // Event listener for scheme selection - dynamic loading of rules.
613598 $( '.narayam-scheme', $('.narayam-scheme-dynamic-item') ).live( 'click', function() {
614 - that.setScheme( $(this).val() );
 599+ that.setScheme( $( this ).val() );
615600 // rebuild the menu items with recent items.
616601 $( '#narayam-menu-items' ).html( $.narayam.buildMenuItems() );
617 - $( '#narayam-' + $(this).val() ).prop( 'checked', true );
 602+ $( '#narayam-' + $( this ).val() ).prop( 'checked', true );
618603 if ( enabled ) {
619604 $( '#narayam-toggle' ).prop( 'checked', true );
620605 }
@@ -624,7 +609,7 @@
625610 var $link = $( '<a>' )
626611 .text( mw.msg( 'narayam-help' ) )
627612 .prop( 'href', mw.util.wikiGetlink( helppage ) );
628 - var $li = $( '<li>' ).addClass( 'narayam-help-link' );
 613+ var $li = $( '<li>' ).addClass( 'narayam-help-link' );
629614 $narayamMenuItems.append( $li.append( $link ) );
630615 }
631616

Status & tagging log