autocomplete, continued

This commit is contained in:
Rolux 2010-02-18 20:41:14 +05:30
parent f71f0ac5ee
commit 27cc14db36
2 changed files with 15 additions and 24 deletions

View File

@ -1564,7 +1564,6 @@ requires
}, },
size: self.options.size size: self.options.size
}); });
self.value = "",
that.bindEvent("click_" + self.menuId, onClick); that.bindEvent("click_" + self.menuId, onClick);
that.bindEvent("deselect_" + self.menuId, onDeselect); that.bindEvent("deselect_" + self.menuId, onDeselect);
that.bindEvent("select_" + self.menuId, onSelect); that.bindEvent("select_" + self.menuId, onSelect);
@ -1575,18 +1574,22 @@ requires
}); });
} }
function autocomplete(items) { function autocomplete(items) {
var selected = -1;
if (items.length) { if (items.length) {
items = $.map(items, function(title) { items = $.map(items, function(title, position) {
if (that.val().toLowerCase() == title.toLowerCase()) {
selected = position;
}
return { return {
id: title.toLowerCase(), // fixme: need function to do lowercase, underscores etc? id: title.toLowerCase(), // fixme: need function to do lowercase, underscores etc?
title: title title: title
}; };
}); });
self.menu.options({ self.menu.options({
items: items items: items,
selected: selected
}).showMenu(); }).showMenu();
} else { } else {
Ox.print("hiding")
self.menu.hideMenu(); self.menu.hideMenu();
} }
} }
@ -1615,21 +1618,12 @@ requires
} }
function keypress(event) { function keypress(event) {
setTimeout(function() { setTimeout(function() {
var val = that.val(); var value = that.val();
if (self.options.autocomplete && val != self.options.value) { if (self.options.autocomplete && value != self.value) {
self.options.value = val; self.value = value;
if (val === "") { self.options.autocomplete(self.value, autocomplete);
self.menu.hideMenu();
} else {
self.options.autocomplete(val, autocomplete);
}
} }
}, 50); }, 50);
/*
if (event.keyCode == 38 || event.keyCode == 40) {
return false;
}
*/
} }
function onClick(event, data) { function onClick(event, data) {
Ox.print("onClick", data) Ox.print("onClick", data)
@ -1637,14 +1631,11 @@ requires
self.menu.hideMenu(); self.menu.hideMenu();
} }
function onDeselect(event, data) { function onDeselect(event, data) {
that.val(self.value); //that.val(self.value);
} }
function onSelect(event, data) { function onSelect(event, data) {
self.value = that.val().substr(0, selection()[0]); //self.value = that.val();
var position = self.value.length; //that.val(data.title);
that.val(data.title);
selection(position);
self.element.setSelectionRange(position, data.title.length);
} }
function selection() { function selection() {
var start, end; var start, end;

View File

@ -341,7 +341,7 @@
"New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virgina", "Wisconsin", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming" "Wyoming"
]; ];
if (value === "") { if (value === "") {