From 27cc14db36e00745365f1885fc07098ef482f912 Mon Sep 17 00:00:00 2001 From: Rolux Date: Thu, 18 Feb 2010 20:41:14 +0530 Subject: [PATCH] autocomplete, continued --- build/js/ox.ui.js | 37 ++++++++++++++----------------------- demos/test/index.html | 2 +- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/build/js/ox.ui.js b/build/js/ox.ui.js index ae80941..43d282a 100644 --- a/build/js/ox.ui.js +++ b/build/js/ox.ui.js @@ -1564,7 +1564,6 @@ requires }, size: self.options.size }); - self.value = "", that.bindEvent("click_" + self.menuId, onClick); that.bindEvent("deselect_" + self.menuId, onDeselect); that.bindEvent("select_" + self.menuId, onSelect); @@ -1575,18 +1574,22 @@ requires }); } function autocomplete(items) { + var selected = -1; if (items.length) { - items = $.map(items, function(title) { + items = $.map(items, function(title, position) { + if (that.val().toLowerCase() == title.toLowerCase()) { + selected = position; + } return { id: title.toLowerCase(), // fixme: need function to do lowercase, underscores etc? title: title }; }); self.menu.options({ - items: items + items: items, + selected: selected }).showMenu(); } else { - Ox.print("hiding") self.menu.hideMenu(); } } @@ -1615,21 +1618,12 @@ requires } function keypress(event) { setTimeout(function() { - var val = that.val(); - if (self.options.autocomplete && val != self.options.value) { - self.options.value = val; - if (val === "") { - self.menu.hideMenu(); - } else { - self.options.autocomplete(val, autocomplete); - } + var value = that.val(); + if (self.options.autocomplete && value != self.value) { + self.value = value; + self.options.autocomplete(self.value, autocomplete); } }, 50); - /* - if (event.keyCode == 38 || event.keyCode == 40) { - return false; - } - */ } function onClick(event, data) { Ox.print("onClick", data) @@ -1637,14 +1631,11 @@ requires self.menu.hideMenu(); } function onDeselect(event, data) { - that.val(self.value); + //that.val(self.value); } function onSelect(event, data) { - self.value = that.val().substr(0, selection()[0]); - var position = self.value.length; - that.val(data.title); - selection(position); - self.element.setSelectionRange(position, data.title.length); + //self.value = that.val(); + //that.val(data.title); } function selection() { var start, end; diff --git a/demos/test/index.html b/demos/test/index.html index 8f9f1eb..19db1a1 100644 --- a/demos/test/index.html +++ b/demos/test/index.html @@ -341,7 +341,7 @@ "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", - "Vermont", "Virginia", "Washington", "West Virgina", "Wisconsin", + "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming" ]; if (value === "") {