r3409 committed - autocomplete: experimental caching implementation
Revision: 3409
Author: joern.zaefferer
Date: Thu Nov 5 13:21:23 2009
Log: autocomplete: experimental caching implementation
http://code.google.com/p/jquery-ui/source/detail?r=3409
Modified:
/branches/dev/ui/jquery.ui.autocomplete.js
=======================================
--- /branches/dev/ui/jquery.ui.autocomplete.js Mon Nov 2 11:55:35 2009
+++ /branches/dev/ui/jquery.ui.autocomplete.js Thu Nov 5 13:21:23 2009
@@ -15,10 +15,11 @@
$.widget("ui.autocomplete", {
_init: function() {
var self = this;
+ this.cache = {};
this.element.attr("autocomplete", "off").addClass("ui-autocomplete")
// TODO verify these actually work as intended
.attr("role", "textbox").attr("aria-autocomplete", "list").attr("aria-haspopup", "true")
- .keydown(function(event) {
+ .bind("keydown.autocomplete", function(event) {
switch(event.keyCode) {
case $.ui.keyCode.PAGE_UP:
self.move("previousPage");
@@ -69,6 +70,10 @@
this.initSource();
},
+ destroy: function() {
+ // TODO implement
+ },
+
// TODO call when source-option is updated
initSource: function() {
if ($.isArray(this.options.source)) {
@@ -88,6 +93,22 @@
} else {
this.source = this.options.source;
}
+ // experimental caching code
+ if (this.options.cache) {
+ var self = this;
+ this.source = (function(source) {
+ return function(request) {
+ var matcher = new
RegExp(request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|
\\])/gi, "\\$1"), "i");
+ if (new RegExp(self.cache.term).test(request.term) &&
self.cache.shrinking && self.cache.content) {
+ // TODO refactor with array-source above
+ return $.grep(self.cache.content, function(value) {
+ return matcher.test(value.result)
+ });
+ }
+ return source.apply(this, arguments);
+ }
+ })(this.source);
+ }
},
search: function(value) {
@@ -102,6 +123,15 @@
self.term = this.element.val();
function response(content) {
if (content.length) {
+ content = self.normalize(content);
+ // experimental caching code
+ if (self.options.cache) {
+ // TODO invalidate cache, eg. on delete or when nothing is matched(?)
+ //if (self.cache.content && self.cache.content.length >
content.length)
+ self.cache.shrinking = self.cache.content &&
self.cache.content.length > content.length;
+ self.cache.term = value;
+ self.cache.content = content;
+ }
self._trigger("open");
self.suggest(content);
} else {
@@ -148,7 +178,6 @@
},
suggest: function(items) {
- items = this.normalize(items);
this.menu && this.menu.element.remove();
var self = this;
var ul = $("<ul/>");
@@ -207,6 +236,8 @@
});
$.ui.autocomplete.defaults = {
+ // caching isn't working yet, disable by default for now
+ cache: false,
minLength: 1,
delay: 300
}