From 986d25eea11c99fbd07487b0cdecb9beb82fb877 Mon Sep 17 00:00:00 2001 From: Rob Moen Date: Fri, 24 Aug 2012 10:46:06 -0700 Subject: [PATCH] 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 --- modules/jquery/jquery.multiSuggest.js | 28 ++++------ .../ve/ui/inspectors/ve.ui.LinkInspector.js | 52 +++++++++++++------ 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/modules/jquery/jquery.multiSuggest.js b/modules/jquery/jquery.multiSuggest.js index 4446a10375..06750050fa 100644 --- a/modules/jquery/jquery.multiSuggest.js +++ b/modules/jquery/jquery.multiSuggest.js @@ -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 { diff --git a/modules/ve/ui/inspectors/ve.ui.LinkInspector.js b/modules/ve/ui/inspectors/ve.ui.LinkInspector.js index 191fd95c6d..d197315769 100644 --- a/modules/ve/ui/inspectors/ve.ui.LinkInspector.js +++ b/modules/ve/ui/inspectors/ve.ui.LinkInspector.js @@ -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 } ); } };