r101592 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101591‎ | r101592 | r101593 >
Date:09:14, 2 November 2011
Author:santhosh
Status:resolved (Comments)
Tags:
Comment:
Show the recently used 5 inputmethod in menu,- No need to select them from the big expandable menu.
-Refactored the UI construction to buildMenuItem, buildMenuItems method.
-The cookie Narayam-scheme now remembers comma seperated list of recently used inputmethods

i18n card #188.
Modified paths:
  • /trunk/extensions/Narayam/Narayam.hooks.php (modified) (history)
  • /trunk/extensions/Narayam/js/ext.narayam.core.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Narayam/Narayam.hooks.php
@@ -66,18 +66,6 @@
6767 $wgNarayamSchemes[$userlangCode] : array();
6868
6969 $schemes = $userlangSchemes + $contlangSchemes;
70 -
71 - // Get user selected scheme from cookie
72 - $lastScheme = $wgRequest->getCookie( 'narayam-scheme', '', null );
73 - // If user selected scheme is not in the array of schemes to be loaded
74 - // Add it
75 - if ( $lastScheme && !array_key_exists( $lastScheme, $schemes ) ) {
76 - // scheme names are of patten <language-code>[-<some-name>]
77 - list( $lastSchemeLanguageCode ) = explode( '-', $lastScheme, 2 );
78 - if ( isset( $wgNarayamSchemes[$lastSchemeLanguageCode][$lastScheme] ) ) {
79 - $schemes[$lastScheme] = $wgNarayamSchemes[$lastSchemeLanguageCode][$lastScheme];
80 - }
81 - }
8270
8371 return $schemes;
8472 }
Index: trunk/extensions/Narayam/js/ext.narayam.core.js
@@ -318,7 +318,7 @@
319319 * Enable Narayam
320320 */
321321 this.enable = function() {
322 - if ( !enabled && currentScheme !== null ) {
 322+ if ( !enabled) {
323323 $.cookie( 'narayam-enabled', '1', { 'path': '/', 'expires': 30 } );
324324 $( '#narayam-toggle' ).attr( 'checked', true );
325325 $( 'li#pt-narayam').removeClass( 'narayam-inactive' );
@@ -402,14 +402,26 @@
403403 * @param name String
404404 */
405405 this.setScheme = function( name ) {
406 - currentScheme = schemes[name];
407 - if ( currentScheme ){
408 - $.cookie( 'narayam-scheme', name, { 'path': '/', 'expires': 30 } );
409 - return true;
 406+ var recent = $.cookie( 'narayam-scheme' ) || [];
 407+ if ( typeof recent === "string" ) {
 408+ recent = recent.split( "," );
 409+ };
 410+ recent = $.grep(recent, function(value) {
 411+ return value != name;
 412+ });
 413+ recent.unshift( name );
 414+ recent = recent.slice( 0, 5 );
 415+ recent = recent.join( "," );
 416+ $.cookie( 'narayam-scheme', recent, { 'path': '/', 'expires': 30 } );
 417+ if (name in schemes){
 418+ currentScheme = schemes[name];
 419+ }else{
 420+ // load the rules dynamically.
 421+ mw.loader.using( "ext.narayam.rules." + name, function() {
 422+ currentScheme = schemes[name];
 423+ });
410424 }
411 - else {
412 - return false;
413 - }
 425+ return true;
414426 };
415427
416428 /**
@@ -426,10 +438,14 @@
427439 }
428440
429441 // Restore state from cookies
430 - var savedScheme = $.cookie( 'narayam-scheme' );
431 - if ( savedScheme && savedScheme in schemes ) {
432 - that.setScheme( savedScheme );
433 - $( '#narayam-' + savedScheme ).attr( 'checked', 'checked' );
 442+ var recentSchemes = $.cookie( 'narayam-scheme' );
 443+ var lastScheme = null;
 444+ if ( typeof recent === "string" ) {
 445+ lastScheme = recent.split( "," )[0];
 446+ }
 447+ if ( lastScheme) {
 448+ that.setScheme( lastScheme );
 449+ $( '#narayam-' + lastScheme ).attr( 'checked', 'checked' );
434450 } else {
435451 //if no saved input scheme, select the first.
436452 var $firstScheme = $( 'input.narayam-scheme:first' );
@@ -453,30 +469,56 @@
454470 }
455471
456472 };
 473+ /**
 474+ * Construct the menu item, for the given scheme name.
 475+ */
 476+ this.buildMenuItem =function(scheme) {
 477+ var $input = $( '<input type="radio" name="narayam-input-method" class="narayam-scheme" />' );
 478+ $input.attr( 'id', 'narayam-' + scheme ).val( scheme );
457479
 480+ var $narayamMenuItemLabel = $( '<label />' )
 481+ .attr( 'for' ,'narayam-' + scheme )
 482+ .append( $input )
 483+ .append( mw.html.escape( mw.msg( "narayam-"+ scheme ) ) );
 484+
 485+ var $narayamMenuItem = $( '<li/>' )
 486+ .append( $input )
 487+ .append( $narayamMenuItemLabel );
 488+ return $narayamMenuItem;
 489+ };
 490+
458491 /**
459 - * Construct the menu for Narayam
 492+ * prepare the menu list for all the input methods.
 493+ * @return The div containing the constructed menu.
460494 */
461 - this.buildMenu = function() {
 495+ this.buildMenuItems = function(){
462496 var haveSchemes = false;
463497 // Build schemes option list
464498 var $narayamMenuItems = $( '<ul/>' );
 499+ var count = 1;
 500+ var seen = [];
 501+
 502+ var recent = $.cookie( "narayam-scheme" ) || [];
 503+ if ( typeof recent === "string" ) {
 504+ recent = recent.split( "," );
 505+ }
 506+ // Prepare the recent inputmethods menu items
 507+ for ( var i = 0; i < recent.length; i++ ) {
 508+ var scheme = recent[i];
 509+ if ( $.inArray( scheme, seen ) > -1 ) { continue; }
 510+ seen.push( scheme );
 511+ //we show 5 recent input methods.
 512+ if ( count++ > 5 ) { break; }
 513+ $narayamMenuItem = that.buildMenuItem(scheme);
 514+ $narayamMenuItem.addClass('narayam-recent-menu-item');
 515+ $narayamMenuItems.append( $narayamMenuItem );
 516+ }
 517+
465518 for ( var scheme in schemes ) {
466 - var $input = $( '<input type="radio" name="narayam-input-method" class="narayam-scheme" />' );
467 - $input
468 - .attr( 'id', 'narayam-' + scheme )
469 - .val( scheme );
470 -
471 - var $narayamMenuItemLabel = $( '<label />' )
472 - .attr( 'for' ,'narayam-' + scheme )
473 - .append( $input )
474 - .append( mw.html.escape( mw.msg( schemes[scheme].namemsg ) ) );
475 -
476 - var $narayamMenuItem = $( '<li/>' )
477 - .append( $input )
478 - .append( $narayamMenuItemLabel );
479 -
480519 haveSchemes = true;
 520+ if ( $.inArray( scheme, seen ) > -1 ) { continue; }
 521+ seen.push( scheme );
 522+ $narayamMenuItem = that.buildMenuItem(scheme);
481523 $narayamMenuItems.append( $narayamMenuItem );
482524 }
483525
@@ -487,8 +529,6 @@
488530 }
489531
490532 // Event listener for scheme selection.
491 - // There is a plan to add a feature that allow dynamic loading of schemes.
492 - // So .live will be useful
493533 $( '.narayam-scheme', $( '#narayam-menu-items > ul')[0] ).live( 'click', function() {
494534 that.setScheme( $(this).val() );
495535 } );
@@ -523,32 +563,25 @@
524564 for ( var lang in allImes ) {
525565 var langschemes = allImes[lang];
526566 for ( var langscheme in langschemes ) {
527 -
528 - var $input = $( '<input type="radio" name="narayam-input-method" class="narayam-scheme-dynamic" />' );
529 - $input
530 - .attr( 'id', 'narayam-' + langscheme )
531 - .val( langscheme );
532 -
533 - var $narayamMenuItemLabel = $( '<label />' )
534 - .attr( 'for' ,'narayam-' + langscheme )
535 - .append( $input )
536 - .append( mw.html.escape( mw.msg( "narayam-"+ langscheme ) ) );
537 -
538 - var $narayamMenuItem = $( '<li class="narayam-scheme-dynamic-item" />' )
539 - .append( $input )
540 - .append( $narayamMenuItemLabel );
541 -
 567+ // Donot repeat the input methods in more input methods section.
 568+ // If already shown on recent items.
 569+ if ( $.inArray( langscheme, seen ) > -1 ) { continue; }
 570+ $narayamMenuItem = that.buildMenuItem(langscheme);
 571+ $narayamMenuItem.addClass( 'narayam-scheme-dynamic-item' );
542572 $narayamMenuItems.append( $narayamMenuItem );
543573
544574 }
545575 }
546576
547577 // Event listener for scheme selection - dynamic loading of rules.
548 - $( '.narayam-scheme-dynamic', $( '#narayam-menu-items > ul')[0] ).live( 'click', function() {
549 - var curVal = $(this).val();
550 - mw.loader.using( "ext.narayam.rules." + $(this).val() , function() {
551 - that.setScheme( curVal )
552 - });
 578+ $( '.narayam-scheme', $('.narayam-scheme-dynamic-item') ).live( 'click', function() {
 579+ that.setScheme( $(this).val() );
 580+ // rebuild the menu items with recent items.
 581+ $( '#narayam-menu-items' ).html( $.narayam.buildMenuItems() );
 582+ $( '#narayam-' + $(this).val() ).attr( 'checked', 'checked' );
 583+ if ( enabled ) {
 584+ $( '#narayam-toggle' ).attr( 'checked', true );
 585+ }
553586 } );
554587
555588 var helppage = mw.config.get( 'wgNarayamHelpPage' );
@@ -566,11 +599,18 @@
567600 }
568601
569602 $narayamMenuItems.prepend( $( '<li/>' ).append( $label ) );
570 -
571603 var $menuItemsDiv = $( '<div id="narayam-menu-items" class="menu-items" />' );
572604 $menuItemsDiv
573605 .append( $narayamMenuItems );
574 -
 606+ return $menuItemsDiv;
 607+ }
 608+
 609+
 610+ /**
 611+ * Construct the menu for Narayam
 612+ */
 613+ this.buildMenu = function() {
 614+ var $menuItemsDiv = that.buildMenuItems();
575615 var $menu = $( '<div id="narayam-menu" class="narayam-menu" />');
576616 $menu
577617 .append(

Follow-up revisions

RevisionCommit summaryAuthorDate
r102391Follow-up r101592....santhosh11:20, 8 November 2011

Comments

#Comment by Amire80 (talk | contribs)   08:37, 8 November 2011

1. The number 5 should probably not be literal, but constant or even a config variable.

2. Some spacing anomalies, like

  • if ( !enabled) {
  • if (name in schemes){
  • }else{
  • $narayamMenuItem = that.buildMenuItem(scheme);

Status & tagging log