diff options
author | Christopher Lenz <cmlenz@apache.org> | 2009-01-21 19:40:19 +0000 |
---|---|---|
committer | Christopher Lenz <cmlenz@apache.org> | 2009-01-21 19:40:19 +0000 |
commit | 03d03bdaf3d86de48444ca768a024362fa63ca24 (patch) | |
tree | 8323822cd8724d325a0a874dd5690116a04d9dc1 /share/www/script/jquery.suggest.js | |
parent | a3fec89cdff0deda1c30d15bd0503af2f9b824e2 (diff) |
Add caching to the Futon autocompletion feature, and fix the positioning code.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@736387 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share/www/script/jquery.suggest.js')
-rw-r--r-- | share/www/script/jquery.suggest.js | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/share/www/script/jquery.suggest.js b/share/www/script/jquery.suggest.js index 3a9d0ead..da592317 100644 --- a/share/www/script/jquery.suggest.js +++ b/share/www/script/jquery.suggest.js @@ -15,13 +15,15 @@ suggest = function(elem, options) { var timer = null; var prevVal = null; + var cache = {}; + var cacheKeys = []; var input = $(elem).attr("autocomplete", "off"); - var offset = input.offset(); - var dropdown = $('<ul style="z-index: 10000"></ul>') - .addClass(options.dropdownClass).appendTo("body").css({ - top: (offset.top + elem.offsetHeight) + "px", - left: offset.left + "px", + var pos = input.position(); + var dropdown = $('<ul style="display: none; position: absolute; z-index: 10000"></ul>') + .addClass(options.dropdownClass).insertAfter(input).css({ + top: (pos.top + input.outerHeight()) + "px", + left: pos.left + "px", minWidth: input.css("width") }); @@ -65,9 +67,20 @@ var newVal = $.trim(input.val()); if (force || newVal != prevVal) { if (force || newVal.length >= options.minChars) { - options.callback.apply(elem, [$.trim(input.val()), function(items, render) { - show(items, render); - }]); + if (options.cache && cache.hasOwnProperty(newVal)) { + show(cache[newVal].items, cache[newVal].render); + } else { + options.callback.apply(elem, [newVal, function(items, render) { + if (options.cache) { + if (cacheKeys.length >= options.cacheLimit) { + delete cache[cacheKeys.shift()]; + } + cache[newVal] = {items: items, render: render}; + cacheKeys.push(newVal); + } + show(items, render); + }]); + } } else { dropdown.hide(); } @@ -91,7 +104,7 @@ item.appendTo(dropdown); } dropdown.slideDown("fast"); - dropdown.children('li').click(function(e) { + dropdown.children("li").click(function(e) { $(this).addClass("selected"); commit(); }); @@ -133,8 +146,10 @@ $.fn.suggest = function(callback, options) { options = $.extend({ + cache: true, + cacheLimit: 10, callback: callback, - delay: 100, + delay: 250, dropdownClass: "suggest-dropdown", minChars: 1, select: null |