cleanup and optimization

* reused jquery object  $( 'input#languagefilter' )
* handled the clean button click and escape press on ULS
* some code cleanup.

Change-Id: I5b8b597dceb4cf273a7eff2761f42557828e630b
This commit is contained in:
Santhosh Thottingal
2012-07-24 14:17:48 +05:30
parent 964f508533
commit 05eb91bee3

View File

@@ -32,27 +32,31 @@
} }
} }
this.shown = false; this.shown = false;
this.$languageFilter = $( 'input#languagefilter' );
this.render(); this.render();
this.listen(); this.listen();
this.ready();
}; };
ULS.prototype = { ULS.prototype = {
constructor: ULS, constructor: ULS,
ready: function() {
// Initialize with a full search.
this.$languageFilter.val( "" ).languagefilter( "search" );
},
show: function() { show: function() {
var pos = $.extend( {}, this.$element.offset(), { var pos = $.extend( {}, this.$element.offset(), {
height: this.$element[0].offsetHeight height: this.$element[0].offsetHeight
} ); } );
this.$menu.css( { this.$menu.css( {
top: pos.top + pos.height, top: pos.top + pos.height,
left: '25%' //pos.left // FIXME left: '25%'
} ); } );
this.$menu.show(); this.$menu.show();
this.shown = true; this.shown = true;
$( 'input#languagefilter' ).focus(); this.$languageFilter.focus();
return this; return this;
}, },
@@ -70,9 +74,18 @@
var that = this, $lcd; var that = this, $lcd;
// Register all event listeners to the ULS here. // Register all event listeners to the ULS here.
that.$element.on( 'click', $.proxy( that.click, that ) ); that.$element.on( 'click', $.proxy( that.click, that ) );
// Handle click on close button
$( ".icon-close" ).on( 'click', $.proxy( that.click, that ) ); $( ".icon-close" ).on( 'click', $.proxy( that.click, that ) );
$lcd = $( "div.uls-language-list" ).lcd( { // Handle key press events on the menu
that.$menu.on('keypress', $.proxy(this.keypress, this) )
.on('keyup', $.proxy(this.keyup, this) );
if ( $.browser.webkit || $.browser.msie ) {
this.$menu.on( 'keydown', $.proxy( this.keypress, this ) )
}
$lcd = $( 'div.uls-language-list' ).lcd( {
languages: that.languages, languages: that.languages,
clickhandler: function( langCode ) { clickhandler: function( langCode ) {
if ( that.options.onSelect ) { if ( that.options.onSelect ) {
@@ -80,7 +93,8 @@
} }
} }
} ).data( "lcd" ); } ).data( "lcd" );
$( "#languagefilter" ).languagefilter( {
that.$languageFilter.languagefilter( {
$target: $lcd, //$( 'ul.uls-language-filter-result' ), $target: $lcd, //$( 'ul.uls-language-filter-result' ),
languages: that.languages languages: that.languages
} ); } );
@@ -92,32 +106,36 @@
languages: that.languages, languages: that.languages,
callback: function () { callback: function () {
// clear the search field. // clear the search field.
$( "#languagefilter" ).val( "" ); that.$languageFilter.val( "" );
} }
} ); } );
// trigger a search for all languages.
$( "#languagefilter" ).languagefilter( "search" ); $( '.clear-button' ).on( 'click', function() {
// go to the ready state.
that.ready();
} );
}, },
keyup: function( e ) { keyup: function( e ) {
if ( !this.shown ) {
return;
}
switch( e.keyCode ) { switch( e.keyCode ) {
case 27: // escape case 27: // escape
if (!this.shown ) { this.hide();
return this.hide(); e.preventDefault();
}
break; break;
} }
e.stopPropagation(); e.stopPropagation();
e.preventDefault();
}, },
keypress: function( e ) { keypress: function( e ) {
if ( !this.shown ) { if ( !this.shown ) {
return; return;
} }
switch( e.keyCode ) { switch( e.keyCode ) {
case 27: // escape case 27: // escape
this.hide();
e.preventDefault(); e.preventDefault();
break; break;
} }