Merge pull request #276 from wikimedia/search

Improve search code
This commit is contained in:
Santhosh Thottingal
2017-11-29 15:51:16 +05:30
committed by GitHub

View File

@@ -167,76 +167,69 @@
}, },
search: function () { search: function () {
var langCode, scriptGroup, langNum, languagesInScript, var languages = Object.keys( this.options.languages ),
languages = $.uls.data.getLanguagesByScriptGroup( this.options.languages ), results = [],
query = $.trim( this.$element.val() ); query = $.trim( this.$element.val() ).toLowerCase();
this.resultCount = 0; if ( query === '' ) {
for ( scriptGroup in languages ) { languages.map( this.render.bind( this ) );
languagesInScript = languages[ scriptGroup ]; this.resultHandler( query, languages );
languagesInScript.sort( $.uls.data.sortByAutonym ); return;
for ( langNum = 0; langNum < languagesInScript.length; langNum++ ) {
langCode = languagesInScript[ langNum ];
if ( query === '' || this.filter( langCode, query ) ) {
if ( this.resultCount === 0 ) {
// Autofill the first result.
this.autofill( langCode );
}
if ( query.toLowerCase() === langCode ) {
this.selectedLanguage = langCode;
}
if ( this.render( langCode ) ) {
this.resultCount++;
}
}
}
} }
// Also do a search by search API // Local search results
if ( !this.resultCount && this.options.searchAPI && query ) { results = languages.filter( function ( langCode ) {
this.searchAPI( query ); return this.filter( langCode, query );
}.bind( this ) );
// Use the searchAPI if available, assuming that it has superior search results.
if ( this.options.searchAPI ) {
this.searchAPI( query )
.done( this.resultHandler.bind( this ) )
.fail( this.resultHandler.bind( this, query, results, undefined ) );
} else { } else {
this.resultHandler( query ); this.resultHandler( query, results );
} }
}, },
searchAPI: function ( query ) { searchAPI: function ( query ) {
var languageFilter = this; return $.get( this.options.searchAPI, { search: query } ).then( function ( result ) {
var autofillLabel,
results = [];
$.get( languageFilter.options.searchAPI, {
search: query
}, function ( result ) {
$.each( result.languagesearch, function ( code, name ) { $.each( result.languagesearch, function ( code, name ) {
if ( languageFilter.resultCount === 0 ) { if ( this.options.languages[ code ] ) {
// Autofill the first result. autofillLabel = autofillLabel || name;
languageFilter.autofill( code, name ); results.push( code );
} }
}.bind( this ) );
if ( languageFilter.options.languages[ code ] && return $.Deferred().resolve( query, results, autofillLabel );
languageFilter.render( code )
) {
languageFilter.resultCount++;
}
} );
languageFilter.resultHandler( query ); }.bind( this ) );
} );
}, },
/** /**
* Handler method to be called once search is over. * Handler method to be called once search is over.
* Based on search result triggers resultsfound or noresults events * Based on search result triggers resultsfound or noresults events
* @param query string * @param {string} query
* @param {number} resultCount
* @param {string} [autofillLabel]
*/ */
resultHandler: function ( query ) { resultHandler: function ( query, results, autofillLabel ) {
if ( this.resultCount === 0 ) { if ( results.length === 0 ) {
this.$suggestion.val( '' ); this.$suggestion.val( '' );
this.$element.trigger( 'noresults.uls', query ); this.$element.trigger( 'noresults.uls', query );
} else { return;
this.$element.trigger( 'resultsfound.uls', [ query, this.resultCount ] );
} }
if ( query ) {
this.selectedLanguage = results[ 0 ];
this.autofill( results[ 0 ], autofillLabel );
}
results.map( this.render.bind( this ) );
this.$element.trigger( 'resultsfound.uls', [ query, results.length ] );
}, },
autofill: function ( langCode, languageName ) { autofill: function ( langCode, languageName ) {
@@ -251,7 +244,6 @@
return; return;
} }
this.selectedLanguage = langCode;
languageName = languageName || this.options.languages[ langCode ]; languageName = languageName || this.options.languages[ langCode ];
if ( !languageName ) { if ( !languageName ) {
@@ -281,6 +273,7 @@
}, },
render: function ( langCode ) { render: function ( langCode ) {
// This is actually instance of LanguageCategoryDisplay and not jQuery!
var $target = this.options.$target; var $target = this.options.$target;
if ( !$target ) { if ( !$target ) {