Misc. performance tweaks
* Do not hit search API if there are results to show from front end * Avoid the lcd exists check * slightly reduce the timeout of search term typing * show autofill for search api results too. * correct the tooltip text of results. Change-Id: Id102fbf3bc09eb926d9e36f125ed6291943007f9
This commit is contained in:
@@ -74,7 +74,7 @@
|
|||||||
delay( function() {
|
delay( function() {
|
||||||
that.options.$target.empty();
|
that.options.$target.empty();
|
||||||
that.search();
|
that.search();
|
||||||
}, 500 );
|
}, 300 );
|
||||||
this.toggleClear();
|
this.toggleClear();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -114,7 +114,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Also do a search by search API
|
// Also do a search by search API
|
||||||
if( this.options.searchAPI && query ) {
|
if( !this.resultCount && this.options.searchAPI && query ) {
|
||||||
this.searchAPI( query );
|
this.searchAPI( query );
|
||||||
} else {
|
} else {
|
||||||
this.resultHandler( query );
|
this.resultHandler( query );
|
||||||
@@ -125,6 +125,10 @@
|
|||||||
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 ) {
|
||||||
|
// Autofill the first result.
|
||||||
|
that.autofill( code, name );
|
||||||
|
}
|
||||||
that.render( code, name );
|
that.render( code, name );
|
||||||
that.resultCount++;
|
that.resultCount++;
|
||||||
} );
|
} );
|
||||||
@@ -145,7 +149,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
autofill: function( langCode ) {
|
autofill: function( langCode, languageName ) {
|
||||||
if ( !this.$suggestion.length ) {
|
if ( !this.$suggestion.length ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -153,8 +157,8 @@
|
|||||||
this.$suggestion.val( '' );
|
this.$suggestion.val( '' );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
languageName = languageName || this.options.languages[langCode];
|
||||||
var autonym,
|
var autonym,
|
||||||
languageName = this.options.languages[langCode],
|
|
||||||
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 !== languageName ) {
|
if ( suggestion !== languageName ) {
|
||||||
@@ -178,8 +182,7 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
escapeRegex: function( value ) {
|
escapeRegex: function( value ) {
|
||||||
// This is a prefix search.
|
return value.replace( /[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&" );
|
||||||
return value.replace( /^[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&" );
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,7 +195,7 @@
|
|||||||
*/
|
*/
|
||||||
filter: function( langCode, searchTerm ) {
|
filter: function( langCode, searchTerm ) {
|
||||||
// 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.autonym( langCode ) ) ||
|
matcher.test( $.uls.data.autonym( langCode ) ) ||
|
||||||
|
|||||||
@@ -35,16 +35,7 @@
|
|||||||
var that = this;
|
var that = this;
|
||||||
this.addToRegion( langCode, regionCode, languageName );
|
this.addToRegion( langCode, regionCode, languageName );
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* Check whether a language code is already displayed or not.
|
|
||||||
* @param langCode
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
exists: function( langCode ) {
|
|
||||||
return this.$element.find( 'li' ).filter(function() {
|
|
||||||
return $(this).data('code') === langCode;
|
|
||||||
} ).length > 0;
|
|
||||||
},
|
|
||||||
/**
|
/**
|
||||||
* Add the language to a region.
|
* Add the language to a region.
|
||||||
* If the region parameter is given , add to that region alone
|
* If the region parameter is given , add to that region alone
|
||||||
@@ -55,10 +46,7 @@
|
|||||||
*/
|
*/
|
||||||
addToRegion: function( langCode, region, languageName) {
|
addToRegion: function( langCode, region, languageName) {
|
||||||
var that = this;
|
var that = this;
|
||||||
if ( that.exists( langCode ) ) {
|
var language = that.options.languages[langCode],
|
||||||
return;
|
|
||||||
}
|
|
||||||
var language = $.uls.data.languages[langCode],
|
|
||||||
langName = languageName
|
langName = languageName
|
||||||
|| $.uls.data.autonym( langCode )
|
|| $.uls.data.autonym( langCode )
|
||||||
|| that.options.languages[langCode]
|
|| that.options.languages[langCode]
|
||||||
@@ -125,6 +113,10 @@
|
|||||||
this.$element.find( 'div' ).hide();
|
this.$element.find( 'div' ).hide();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
focus: function() {
|
||||||
|
this.$element.focus();
|
||||||
|
},
|
||||||
|
|
||||||
listen: function() {
|
listen: function() {
|
||||||
var that = this;
|
var that = this;
|
||||||
// The region section need to be in sync with the map filter.
|
// The region section need to be in sync with the map filter.
|
||||||
|
|||||||
Reference in New Issue
Block a user