r3318 committed - autocomplete: implemented events (search event not yet cancellable)

r3318 committed - autocomplete: implemented events (search event not yet cancellable)


Revision: 3318
Author: joern.zaefferer
Date: Tue Sep 29 01:11:04 2009
Log: autocomplete: implemented events (search event not yet cancellable)
http://code.google.com/p/jquery-ui/source/detail?r=3318
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    Sat Sep 26
04:12:14 2009
+++ /branches/dev/tests/visual/autocomplete/default.html    Tue Sep 29
01:11:04 2009
@@ -19,11 +19,26 @@
            top: 10
        }).appendTo(document.body).themeswitcher();
+        function log(message) {
+            $("<div/>").text(message).appendTo("#log");
+        }
+
        $("#tags").autocomplete({
            source:
["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"],
+            search: function() {
+                log("Searching for: " + this.value);
+            },
+            open: function() {
+                log("Found something");
+            },
+            focus: function(event, ui) {
+                log("Moving focus to " + ui.item.label);
+            },
+            close: function() {
+                log("Hiding suggestions");
+            },
            change: function(event, ui) {
-                var message = ui.item ? ("Selected: " + ui.item.result) : "Nothing
selected";
-                $("<div/>").text(message).appendTo("#log");
+                log(ui.item ? ("Selected: " + ui.item.result) : "Nothing selected,
input was " + this.value);
            }
        }).focus();
    });
=======================================
--- /branches/dev/ui/jquery.ui.autocomplete.js    Sat Sep 26 04:12:14 2009
+++ /branches/dev/ui/jquery.ui.autocomplete.js    Tue Sep 29 01:11:04 2009
@@ -30,11 +30,21 @@
                self.search();
                break;
            }
+        }).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");
+            }, 150);
        });
        this.initSource();
    },
-    // call when source-option is updated
+    // TODO call when source-option is updated
    initSource: function() {
        if ($.isArray(this.options.source)) {
            var array = this.options.source;
@@ -59,7 +69,10 @@
        var self = this;
        var value = this.element.val();
        if (value.length >= this.options.minLength) {
+            // 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);
            });
        } else {
@@ -79,10 +92,15 @@
        ul.css({
            position: "absolute"
        }).appendTo(document.body).menu({
+            focus: function(event, ui) {
+                self._trigger("focus", null, { item: ui.item.data("item.autocomplete")
});
+            },
            selected: function(event, ui) {
                var data = ui.item.data("item.autocomplete");
                self.element.val( data.result );
-                ul.remove();
+                self._trigger("close");
+                self.menu.remove();
+                self.menu = null;
                self._trigger("change", null, { item: data });
            }
        }).position({
=======================================
--- /branches/dev/ui/jquery.ui.menu.js    Sat Sep 26 04:12:14 2009
+++ /branches/dev/ui/jquery.ui.menu.js    Tue Sep 29 01:11:04 2009
@@ -34,6 +34,7 @@
    },
    activate: function(item) {
+        this._trigger("focus", null, { item: item.parent() });
        this.activeitem =
item.addClass("ui-state-hover").attr("id", "ui-active-menuitem");
    },