Index: trunk/extensions/Narayam/Narayam.hooks.php |
— | — | @@ -66,18 +66,6 @@ |
67 | 67 | $wgNarayamSchemes[$userlangCode] : array(); |
68 | 68 | |
69 | 69 | $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 | | - } |
82 | 70 | |
83 | 71 | return $schemes; |
84 | 72 | } |
Index: trunk/extensions/Narayam/js/ext.narayam.core.js |
— | — | @@ -318,7 +318,7 @@ |
319 | 319 | * Enable Narayam |
320 | 320 | */ |
321 | 321 | this.enable = function() { |
322 | | - if ( !enabled && currentScheme !== null ) { |
| 322 | + if ( !enabled) { |
323 | 323 | $.cookie( 'narayam-enabled', '1', { 'path': '/', 'expires': 30 } ); |
324 | 324 | $( '#narayam-toggle' ).attr( 'checked', true ); |
325 | 325 | $( 'li#pt-narayam').removeClass( 'narayam-inactive' ); |
— | — | @@ -402,14 +402,26 @@ |
403 | 403 | * @param name String |
404 | 404 | */ |
405 | 405 | 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 | + }); |
410 | 424 | } |
411 | | - else { |
412 | | - return false; |
413 | | - } |
| 425 | + return true; |
414 | 426 | }; |
415 | 427 | |
416 | 428 | /** |
— | — | @@ -426,10 +438,14 @@ |
427 | 439 | } |
428 | 440 | |
429 | 441 | // 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' ); |
434 | 450 | } else { |
435 | 451 | //if no saved input scheme, select the first. |
436 | 452 | var $firstScheme = $( 'input.narayam-scheme:first' ); |
— | — | @@ -453,30 +469,56 @@ |
454 | 470 | } |
455 | 471 | |
456 | 472 | }; |
| 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 ); |
457 | 479 | |
| 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 | + |
458 | 491 | /** |
459 | | - * Construct the menu for Narayam |
| 492 | + * prepare the menu list for all the input methods. |
| 493 | + * @return The div containing the constructed menu. |
460 | 494 | */ |
461 | | - this.buildMenu = function() { |
| 495 | + this.buildMenuItems = function(){ |
462 | 496 | var haveSchemes = false; |
463 | 497 | // Build schemes option list |
464 | 498 | 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 | + |
465 | 518 | 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 | | - |
480 | 519 | haveSchemes = true; |
| 520 | + if ( $.inArray( scheme, seen ) > -1 ) { continue; } |
| 521 | + seen.push( scheme ); |
| 522 | + $narayamMenuItem = that.buildMenuItem(scheme); |
481 | 523 | $narayamMenuItems.append( $narayamMenuItem ); |
482 | 524 | } |
483 | 525 | |
— | — | @@ -487,8 +529,6 @@ |
488 | 530 | } |
489 | 531 | |
490 | 532 | // 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 |
493 | 533 | $( '.narayam-scheme', $( '#narayam-menu-items > ul')[0] ).live( 'click', function() { |
494 | 534 | that.setScheme( $(this).val() ); |
495 | 535 | } ); |
— | — | @@ -523,32 +563,25 @@ |
524 | 564 | for ( var lang in allImes ) { |
525 | 565 | var langschemes = allImes[lang]; |
526 | 566 | 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' ); |
542 | 572 | $narayamMenuItems.append( $narayamMenuItem ); |
543 | 573 | |
544 | 574 | } |
545 | 575 | } |
546 | 576 | |
547 | 577 | // 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 | + } |
553 | 586 | } ); |
554 | 587 | |
555 | 588 | var helppage = mw.config.get( 'wgNarayamHelpPage' ); |
— | — | @@ -566,11 +599,18 @@ |
567 | 600 | } |
568 | 601 | |
569 | 602 | $narayamMenuItems.prepend( $( '<li/>' ).append( $label ) ); |
570 | | - |
571 | 603 | var $menuItemsDiv = $( '<div id="narayam-menu-items" class="menu-items" />' ); |
572 | 604 | $menuItemsDiv |
573 | 605 | .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(); |
575 | 615 | var $menu = $( '<div id="narayam-menu" class="narayam-menu" />'); |
576 | 616 | $menu |
577 | 617 | .append( |