Merge "Now caching build params in jquery.multiSuggest."

This commit is contained in:
Trevor Parscal 2012-08-22 17:36:52 +00:00 committed by Gerrit Code Review
commit 33abde3bcf

View file

@ -70,8 +70,9 @@
visible = false, visible = false,
focused = false, focused = false,
$input = $( this ), $input = $( this ),
cachedInput = '', currentInput = '',
$multiSuggest; $multiSuggest,
cache = {};
// Merge options with default configuration. // Merge options with default configuration.
$.extend( { $.extend( {
@ -98,11 +99,36 @@
} }
// Call configured input method and supply the private build method as callback. // Call configured input method and supply the private build method as callback.
function onInput() { function onInput() {
if ( typeof options.input === 'function' ) { // Throttle
options.input.call( $input, function( params, callback ){ clearTimeout( inputTimer );
build( params ); inputTimer = setTimeout( function() {
} ); var txt = $input.val().toLowerCase();
} if ( txt !== '' ) {
// Check for query in cache object.
if (
!( txt in cache ) &&
typeof options.input === 'function'
) {
options.input.call( $input, function( params, callback ){
build( params );
cache[txt] = params;
} );
} else {
// Rebuild from cache only if query has changed.
// This prevents disrupting the menu on keypress.
if ( txt !== currentInput ) {
build( cache[txt] );
}
}
// Set current input.
currentInput = txt;
} else {
// No Text, close.
if ( visible ) {
close();
}
}
}, 250 );
} }
// Opens the MultiSuggest dropdown. // Opens the MultiSuggest dropdown.
function open() { function open() {
@ -110,9 +136,9 @@
// Call input method if cached value is stale // Call input method if cached value is stale
if ( if (
$input.val() !== '' && $input.val() !== '' &&
$input.val() !== cachedInput $input.val() !== currentInput
) { ) {
cachedInput = $input.val(); currentInput = $input.val();
onInput(); onInput();
} else { } else {
// Show if there are suggestions. // Show if there are suggestions.
@ -135,7 +161,7 @@
// When an item is selected in the dropdown. // When an item is selected in the dropdown.
function select( text ) { function select( text ) {
// Cache input. // Cache input.
cachedInput = text; currentInput = text;
$input.val( text ); $input.val( text );
close(); close();
} }
@ -215,26 +241,7 @@
// Bind target input events // Bind target input events
$input.on( { $input.on( {
// Handle any change to the input. // Handle any change to the input.
'keyup change cut paste': function( e ) { 'keyup change cut paste': onInput,
var input = this;
// Throttle input.
clearTimeout( inputTimer );
inputTimer = setTimeout( function() {
if ( $input.val() !== '' ) {
// Check for difference.
if ( $input.val() !== cachedInput ) {
onInput();
}
} else if ( $input.val() === '' ) {
// No Text, close.
if ( visible ) {
close();
}
}
// Cache
cachedInput = $input.val();
}, 250 );
},
// Handle arrow up and down keys. // Handle arrow up and down keys.
'keydown': function( e ) { 'keydown': function( e ) {
var $item, var $item,