Some minor changes for code readability

This commit is contained in:
Santhosh Thottingal
2012-11-19 11:15:44 +05:30
parent c95fab6923
commit 5626a4cf92

View File

@@ -34,11 +34,13 @@
this.$suggestion = this.$element.parents().find( '#' + this.$element.data( 'suggestion' ) ); this.$suggestion = this.$element.parents().find( '#' + this.$element.data( 'suggestion' ) );
this.$clear = this.$element.parents().find( '#'+ this.$element.data( 'clear' ) ); this.$clear = this.$element.parents().find( '#'+ this.$element.data( 'clear' ) );
this.selectedLanguage = null; this.selectedLanguage = null;
this.listen(); this.listen();
}; };
var delay = ( function() { var delay = ( function() {
var timer = 0; var timer = 0;
return function( callback, milliseconds ) { return function( callback, milliseconds ) {
clearTimeout( timer ); clearTimeout( timer );
timer = setTimeout( callback, milliseconds ); timer = setTimeout( callback, milliseconds );
@@ -50,12 +52,15 @@
listen: function() { listen: function() {
this.$element.on( 'keypress', $.proxy( this.keyup, this ) ) this.$element.on( 'keypress', $.proxy( this.keyup, this ) )
.on( 'keyup', $.proxy( this.keyup, this ) ); .on( 'keyup', $.proxy( this.keyup, this ) );
if ( this.eventSupported( 'keydown' ) ) { if ( this.eventSupported( 'keydown' ) ) {
this.$element.on( 'keydown', $.proxy( this.keyup, this ) ); this.$element.on( 'keydown', $.proxy( this.keyup, this ) );
} }
if ( this.$clear.length ) { if ( this.$clear.length ) {
this.$clear.on( 'click' , $.proxy( this.clear, this ) ); this.$clear.on( 'click' , $.proxy( this.clear, this ) );
} }
this.toggleClear(); this.toggleClear();
}, },
@@ -63,6 +68,7 @@
switch( e.keyCode ) { switch( e.keyCode ) {
case 9: // Tab -> Autocomplete case 9: // Tab -> Autocomplete
var suggestion = this.$suggestion.val(); var suggestion = this.$suggestion.val();
if ( suggestion && suggestion !== this.$element.val() ) { if ( suggestion && suggestion !== this.$element.val() ) {
this.$element.val( suggestion ); this.$element.val( suggestion );
e.preventDefault(); e.preventDefault();
@@ -73,7 +79,9 @@
if ( !this.options.onSelect ) { if ( !this.options.onSelect ) {
break; break;
} }
var query = $.trim( this.$element.val() ).toLowerCase(); var query = $.trim( this.$element.val() ).toLowerCase();
if ( this.selectedLanguage ) { if ( this.selectedLanguage ) {
// this.selectLanguage will be populated from a matching search // this.selectLanguage will be populated from a matching search
this.options.onSelect( this.selectedLanguage ); this.options.onSelect( this.selectedLanguage );
@@ -82,10 +90,13 @@
// but we have a matching language code. // but we have a matching language code.
this.options.onSelect( query ); this.options.onSelect( query );
} }
break; break;
default: default:
var that = this; var that = this;
this.selectedLanguage = null; this.selectedLanguage = null;
delay( function() { delay( function() {
if ( !that.$element.val() ) { if ( !that.$element.val() ) {
that.clear(); that.clear();
@@ -94,6 +105,7 @@
that.search(); that.search();
} }
}, 300 ); }, 300 );
this.toggleClear(); this.toggleClear();
} }
}, },
@@ -108,6 +120,7 @@
if ( !$.fn.uls.Constructor.prototype.isMobile() ) { if ( !$.fn.uls.Constructor.prototype.isMobile() ) {
this.$element.focus(); this.$element.focus();
} }
this.toggleClear(); this.toggleClear();
this.autofill(); this.autofill();
}, },
@@ -140,25 +153,33 @@
var query = $.trim( this.$element.val() ), var query = $.trim( this.$element.val() ),
languages = $.uls.data.getLanguagesByScriptGroup( this.options.languages ), languages = $.uls.data.getLanguagesByScriptGroup( this.options.languages ),
scriptGroup, langNum, langCode; scriptGroup, langNum, langCode;
this.resultCount = 0; this.resultCount = 0;
for ( scriptGroup in languages ) { for ( scriptGroup in languages ) {
var languagesInScript = languages[scriptGroup]; var languagesInScript = languages[scriptGroup];
languagesInScript.sort( $.uls.data.sortByAutonym ); languagesInScript.sort( $.uls.data.sortByAutonym );
for ( langNum = 0; langNum < languagesInScript.length; langNum++ ) { for ( langNum = 0; langNum < languagesInScript.length; langNum++ ) {
langCode = languagesInScript[langNum]; langCode = languagesInScript[langNum];
if ( query === "" || this.filter( langCode, query ) ) { if ( query === "" || this.filter( langCode, query ) ) {
if ( this.resultCount === 0 ) { if ( this.resultCount === 0 ) {
// Autofill the first result. // Autofill the first result.
this.autofill( langCode ); this.autofill( langCode );
} }
if ( query.toLowerCase() === langCode ) { if ( query.toLowerCase() === langCode ) {
this.selectedLanguage = langCode; this.selectedLanguage = langCode;
} }
this.render( langCode ); this.render( langCode );
this.resultCount++; this.resultCount++;
} }
} }
} }
// Also do a search by search API // Also do a search by search API
if( !this.resultCount && this.options.searchAPI && query ) { if( !this.resultCount && this.options.searchAPI && query ) {
this.searchAPI( query ); this.searchAPI( query );
@@ -169,6 +190,7 @@
searchAPI: function( query ) { searchAPI: function( query ) {
var that = this; var that = this;
$.get( that.options.searchAPI, { search: query }, function( result ) { $.get( that.options.searchAPI, { search: query }, function( result ) {
$.each( result['languagesearch'], function( code, name ) { $.each( result['languagesearch'], function( code, name ) {
if ( that.resultCount === 0 ) { if ( that.resultCount === 0 ) {
@@ -200,15 +222,19 @@
if ( !this.$suggestion.length ) { if ( !this.$suggestion.length ) {
return; return;
} }
if ( !this.$element.val() ) { if ( !this.$element.val() ) {
this.$suggestion.val( '' ); this.$suggestion.val( '' );
return; return;
} }
this.selectedLanguage = langCode; this.selectedLanguage = langCode;
languageName = languageName || this.options.languages[langCode]; languageName = languageName || this.options.languages[langCode];
var autonym, var autonym,
userInput = this.$element.val(), userInput = this.$element.val(),
suggestion = userInput + languageName.substring( userInput.length, languageName.length ); suggestion = userInput + languageName.substring( userInput.length, languageName.length );
if ( suggestion.toLowerCase() !== languageName.toLowerCase() ) { if ( suggestion.toLowerCase() !== languageName.toLowerCase() ) {
// see if it was autonym match // see if it was autonym match
autonym = $.uls.data.getAutonym( langCode ) || ''; autonym = $.uls.data.getAutonym( langCode ) || '';
@@ -218,18 +244,22 @@
suggestion = ""; suggestion = "";
} }
} }
// Make sure that it is a visual prefix. // Make sure that it is a visual prefix.
if ( !isVisualPrefix( userInput, suggestion ) ) { if ( !isVisualPrefix( userInput, suggestion ) ) {
suggestion = ""; suggestion = "";
} }
this.$suggestion.val( suggestion ); this.$suggestion.val( suggestion );
}, },
render: function( langCode ) { render: function( langCode ) {
var $target = this.options.$target; var $target = this.options.$target;
if ( !$target ) { if ( !$target ) {
return; return;
} }
$target.append( langCode, null ); $target.append( langCode, null );
}, },
@@ -249,6 +279,7 @@
// FIXME script is ISO 15924 code. We might need actual name of script. // FIXME script is ISO 15924 code. We might need actual name of script.
var matcher = new RegExp( "^" + this.escapeRegex( searchTerm ), 'i' ), var matcher = new RegExp( "^" + this.escapeRegex( searchTerm ), 'i' ),
languageName = this.options.languages[langCode]; languageName = this.options.languages[langCode];
return matcher.test( languageName ) || return matcher.test( languageName ) ||
matcher.test( $.uls.data.getAutonym( langCode ) ) || matcher.test( $.uls.data.getAutonym( langCode ) ) ||
matcher.test( langCode ) || matcher.test( langCode ) ||
@@ -273,9 +304,11 @@
var $this = $( this ), var $this = $( this ),
data = $this.data( 'languagefilter' ), data = $this.data( 'languagefilter' ),
options = typeof option === 'object' && option; options = typeof option === 'object' && option;
if ( !data ) { if ( !data ) {
$this.data( 'languagefilter', ( data = new LanguageFilter( this, options ) ) ); $this.data( 'languagefilter', ( data = new LanguageFilter( this, options ) ) );
} }
if ( typeof option === 'string' ) { if ( typeof option === 'string' ) {
data[option](); data[option]();
} }