r3321 committed - autocomplete: bugfix for selecting items; improved log in visual test;...

r3321 committed - autocomplete: bugfix for selecting items; improved log in visual test;...


Revision: 3321
Author: joern.zaefferer
Date: Tue Sep 29 12:30:10 2009
Log: autocomplete: bugfix for selecting items; improved log in visual test;
properly open and close menu; trigger chance only for updated content
http://code.google.com/p/jquery-ui/source/detail?r=3321
Modified:
/branches/dev/tests/visual/autocomplete/default.html
/branches/dev/ui/jquery.ui.autocomplete.js
/branches/dev/ui/jquery.ui.menu.js
=======================================
--- /branches/dev/tests/visual/autocomplete/default.html    Tue Sep 29
01:11:04 2009
+++ /branches/dev/tests/visual/autocomplete/default.html    Tue Sep 29
12:30:10 2009
@@ -20,7 +20,8 @@
        }).appendTo(document.body).themeswitcher();
        function log(message) {
-            $("<div/>").text(message).appendTo("#log");
+            $("<div/>").text(message).prependTo("#log");
+            $("#log").attr("scrollTop", 0);
        }
        $("#tags").autocomplete({
@@ -51,7 +52,10 @@
    <input id="tags" />
</div>
-<div id="log"></div>
+<div style="margin-top:2em; font-family:Arial">
+    Log:
+    <div id="log" style="height: 400px; width: 300px; overflow: auto;"
class="ui-widget-content"></div>
+</div>
</body>
</html>
=======================================
--- /branches/dev/ui/jquery.ui.autocomplete.js    Tue Sep 29 01:11:04 2009
+++ /branches/dev/ui/jquery.ui.autocomplete.js    Tue Sep 29 12:30:10 2009
@@ -30,15 +30,16 @@
                self.search();
                break;
            }
+        }).focus(function() {
+            self.previous = self.element.val();
        }).blur(function() {
            // TODO replace with a more reliable variant
            // without the timeout, the menu is removed before the click is
triggered on it, which also causes the blur
            setTimeout(function() {
-                if (!self.menu) { return; }
-                self._trigger("close");
-                self.menu.remove();
-                // TODO only trigger when content actually changed
-                self._trigger("change");
+                self.close();
+                if (self.previous != self.element.val()) {
+                    self._trigger("change");
+                }
            }, 150);
        });
        this.initSource();
@@ -72,18 +73,28 @@
            // TODO check return value to make search-event cancellable
            this._trigger("search");
            this.source({ term: value }, function response(content) {
-                self._trigger("open");
-                self.suggest(content);
+                if (content.length) {
+                    self._trigger("open");
+                    self.suggest(content);
+                } else {
+                    self.close();
+                }
            });
        } else {
-            this.menu && this.menu.remove();
+            this.close();
        }
    },
+    close: function() {
+        if (!this.menu)
+            return;
+        this._trigger("close");
+        this.menu.remove();
+        this.menu = null;
+    },
+
    suggest: function(items) {
-        if (this.menu) {
-            this.menu.remove();
-        }
+        this.menu && this.menu.remove();
        var self = this;
        var ul = this.menu = $("<ul/>");
        $.each(items, function(index, item) {
=======================================
--- /branches/dev/ui/jquery.ui.menu.js    Tue Sep 29 06:11:02 2009
+++ /branches/dev/ui/jquery.ui.menu.js    Tue Sep 29 12:30:10 2009
@@ -26,7 +26,7 @@
            self._trigger("selected", null, { item: $(this).parent() });
        })
        .mouseenter(function() {
-            self.deactivate(self.activeitem)
+            self.activeitem && self.deactivate(self.activeitem);
            self.activate($(this));
        });