Merge "Cache the region filter results"

This commit is contained in:
Amire80
2012-08-21 14:14:17 +00:00
committed by Gerrit Code Review

View File

@@ -33,6 +33,7 @@
this.options = $.extend( {}, $.fn.regionselector.defaults, options ); this.options = $.extend( {}, $.fn.regionselector.defaults, options );
this.$element.addClass( 'regionselector' ); this.$element.addClass( 'regionselector' );
this.regions = []; this.regions = [];
this.cache= null;
this.regionGroup = this.$element.data( 'regiongroup' ); this.regionGroup = this.$element.data( 'regiongroup' );
this.init(); this.init();
this.listen(); this.listen();
@@ -57,6 +58,7 @@
region = this.regions[i]; region = this.regions[i];
if ( $.inArray( region, langRegions ) >= 0 ) { if ( $.inArray( region, langRegions ) >= 0 ) {
this.render( langCode, region ); this.render( langCode, region );
this.cache[langCode] = region;
return; return;
} }
} }
@@ -69,17 +71,30 @@
// if there is a region group, make it active. // if there is a region group, make it active.
this.$element.addClass( 'active' ); this.$element.addClass( 'active' );
} }
// Re-populate the list of languages // Re-populate the list of languages
this.options.$target.empty(); this.options.$target.empty();
if ( this.cache ) {
// If the result cache is present, render the results from there.
var result = null;
for ( result in this.cache ) {
this.render( result, this.cache[result] );
}
} else {
this.cache = {};
// Get the languages grouped by script group
var languagesByScriptGroup = $.uls.data.languagesByScriptGroup( this.options.languages ); var languagesByScriptGroup = $.uls.data.languagesByScriptGroup( this.options.languages );
for ( var scriptGroup in languagesByScriptGroup ) { for ( var scriptGroup in languagesByScriptGroup ) {
// Get the languages for the script group
var languages = languagesByScriptGroup[scriptGroup]; var languages = languagesByScriptGroup[scriptGroup];
// Sort it based on autonym
languages.sort( $.uls.data.sortByAutonym ); languages.sort( $.uls.data.sortByAutonym );
for ( var i = 0; i < languages.length; i++ ) { for ( var i = 0; i < languages.length; i++ ) {
// Check whether it belongs to the region
this.test( languages[i] ); this.test( languages[i] );
} }
} }
}
if ( this.options.success ) { if ( this.options.success ) {
this.options.success.call(); this.options.success.call();