Move caching mechanism out of multiSuggest plugin to linkInspector

Fixed issue where overlay would be incorrectly positioned if built
from previous caching method.

Change-Id: Id7c3ba211d5344292da47917a9bd06654823ee87
This commit is contained in:
Rob Moen 2012-08-24 10:46:06 -07:00
parent de9f0eb0ca
commit 986d25eea1
2 changed files with 45 additions and 35 deletions

View file

@ -71,8 +71,7 @@
focused = false,
$input = $( this ),
currentInput = '',
$multiSuggest,
cache = {};
$multiSuggest;
// Merge options with default configuration.
$.extend( {
@ -102,23 +101,16 @@
// Throttle
clearTimeout( inputTimer );
inputTimer = setTimeout( function() {
var txt = $input.val().toLowerCase();
var txt = $input.val();
if ( txt !== '' ) {
// Be sure that input has changed.
if ( txt !== currentInput ) {
// Build fresh if query not in cache.
if (
!( txt in cache ) &&
typeof options.input === 'function'
) {
options.input.call( $input, function( params, callback ){
build( params );
cache[txt] = params;
} );
} else {
// Rebuild from cache.
build( cache[txt] );
}
if (
txt !== currentInput &&
typeof options.input === 'function'
) {
options.input.call( $input, function( params, callback ){
build( params );
} );
}
} else {
// No Text, close.
@ -137,7 +129,7 @@
// Call input method if cached value is stale
if (
$input.val() !== '' &&
$input.val().toLowerCase() !== currentInput
$input.val() !== currentInput
) {
onInput();
} else {

View file

@ -245,6 +245,7 @@ ve.ui.LinkInspector.prototype.initMultiSuggest = function() {
var inspector = this,
context = inspector.context,
$overlay = context.$iframeOverlay,
cache = {},
options;
// Multi Suggest configuration.
@ -306,24 +307,41 @@ ve.ui.LinkInspector.prototype.initMultiSuggest = function() {
'input': function( callback ) {
var $input = $( this ),
query = $input.val(),
cKey = query.toLowerCase(),
api = null;
// Set overlay position.
options.position();
// Build from cache.
if ( cache[cKey] !== undefined ) {
callback( {
query: query,
results: cache[cKey]
} );
} else {
// No cache, build fresh.
api = new mw.Api();
// Make AJAX Request.
api.get( {
action: 'opensearch',
search: query
}, {
ok: function( data ) {
// Position the iframe overlay below the input.
context.positionIframeOverlay( {
overlay: $overlay,
below: $input
} );
// Build
callback( {
query: query,
results: data[1]
} );
}
// MW api request.
api.get( {
action: 'opensearch',
search: query
}, {
ok: function( data ) {
cache[cKey] = data[1];
// Build
callback( {
query: query,
results: data[1]
} );
}
} );
}
},
// Position the iframe overlay below the input.
'position': function() {
context.positionIframeOverlay( {
overlay: $overlay,
below: inspector.$locationInput
} );
}
};