r3428 committed - autocomplete: use cache when searching for the same term again; replac...

r3428 committed - autocomplete: use cache when searching for the same term again; replac...


Revision: 3428
Author: joern.zaefferer
Date: Mon Nov 9 07:54:05 2009
Log: autocomplete: use cache when searching for the same term again;
replace menu's ui-corner-all with ui-corner-bottom; fix for remote-jsonp
demo, didn't place the cache-option at the right position
http://code.google.com/p/jquery-ui/source/detail?r=3428
Modified:
/branches/dev/tests/visual/autocomplete/remote-jsonp.html
/branches/dev/ui/jquery.ui.autocomplete.js
=======================================
--- /branches/dev/tests/visual/autocomplete/remote-jsonp.html    Mon Nov 9
07:37:57 2009
+++ /branches/dev/tests/visual/autocomplete/remote-jsonp.html    Mon Nov 9
07:54:05 2009
@@ -25,11 +25,11 @@
        }
        $("#city").autocomplete({
+            cache: {
+                limit: 15
+            },
            source: function(request, response) {
                $.ajax({
-                    cache: {
-                        limit: 15
-                    },
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
@@ -51,6 +51,12 @@
            minLength: 2,
            change: function(event, ui) {
                log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected,
input was " + this.value);
+            },
+            open: function() {
+                $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
+            },
+            close: function() {
+                $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
        });
    });
@@ -58,6 +64,7 @@
    <style>
        body { font-size:62.5%; }
        .ui-autocomplete-loading { background: url(indicator.gif) no-repeat
right; }
+        #city { width: 25em; }
    </style>
</head>
<body>
=======================================
--- /branches/dev/ui/jquery.ui.autocomplete.js    Mon Nov 9 07:37:57 2009
+++ /branches/dev/ui/jquery.ui.autocomplete.js    Mon Nov 9 07:54:05 2009
@@ -98,9 +98,11 @@
            var self = this;
            this.source = (function(source) {
                return function(request) {
-                    var matcher = new
RegExp(request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|
\\])/gi, "\\$1"), "i");
+                    if (self.cache.term == request.term && self.cache.content)
+                        return self.cache.content;
                    if (new RegExp(self.cache.term).test(request.term) &&
self.cache.content && self.cache.content.length < self.options.cache.limit)
{
                        // TODO refactor with array-source above
+                        var matcher = new
RegExp(request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|
\\])/gi, "\\$1"), "i");
                        return $.grep(self.cache.content, function(value) {
                         return matcher.test(value.result)
                        });
@@ -197,7 +199,9 @@
                // TODO only trigger when focus was lost?
                self.element.focus();
            }
-        }).position({
+        })
+        .removeClass("ui-corner-all").addClass("ui-corner-bottom")
+        .position({
            my: "left top",
            at: "left bottom",
            of: this.element