autocomplete menu improvements (left/right keys while menu is visible, properly set selected in all cases)

This commit is contained in:
rolux 2011-05-20 08:40:57 +02:00
parent 3087690d89
commit 0ebd4efc72

View file

@ -352,7 +352,9 @@ Ox.Input = function(options, self) {
}; };
} }
return ret; return ret;
}), })
});
self.$autocompleteMenu.options({
selected: selected selected: selected
}).showMenu(); }).showMenu();
} else { } else {
@ -378,7 +380,23 @@ Ox.Input = function(options, self) {
} }
//size: self.options.size //size: self.options.size
}) })
.bindEvent('click', clickMenu); .bindEvent({
click: clickMenu,
key_left: function() {
cursor(cursor()[0] - 1);
},
key_right: function() {
cursor(cursor()[0] + 1);
},
key_shift_left: function() {
var cursor = cursor();
cursor(cursor[0] - 1, cursor[1]);
},
key_shift_right: function() {
var cursor = cursor();
cursor(cursor[0], cursor[1] + 1);
}
});
if (self.options.autocompleteReplace) { if (self.options.autocompleteReplace) {
menu.bindEvent({ menu.bindEvent({
deselect: deselectMenu, deselect: deselectMenu,
@ -632,7 +650,11 @@ Ox.Input = function(options, self) {
hasDeletedSelectedEnd = (event.keyCode == 8 || event.keyCode == 46) && hasDeletedSelectedEnd = (event.keyCode == 8 || event.keyCode == 46) &&
oldCursor[0] < oldCursor[1] && oldCursor[1] == oldValue.length; oldCursor[0] < oldCursor[1] && oldCursor[1] == oldValue.length;
//Ox.print('keypress', event.keyCode) //Ox.print('keypress', event.keyCode)
if (event.keyCode != 9 && (self.options.type == 'textarea' || event.keyCode != 13) && event.keyCode != 27) { // fixme: can't 13 and 27 return false? if (
event.keyCode != 9 &&
(self.options.type == 'textarea' || event.keyCode != 13) &&
event.keyCode != 27
) { // fixme: can't 13 and 27 return false?
setTimeout(function() { // wait for val to be set setTimeout(function() { // wait for val to be set
var value = self.$input.val(); var value = self.$input.val();
if (self.options.autocompleteReplaceCorrect && hasDeletedSelectedEnd) { if (self.options.autocompleteReplaceCorrect && hasDeletedSelectedEnd) {
@ -652,8 +674,12 @@ Ox.Input = function(options, self) {
} }
}, 0); }, 0);
} }
if ((event.keyCode == 38 || event.keyCode == 40) && self.options.autocompleteSelect && self.$autocompleteMenu.is(':visible')) { if (
return false; (event.keyCode == 38 || event.keyCode == 40) &&
self.options.autocompleteSelect &&
self.$autocompleteMenu.is(':visible')
) {
//return false;
} }
} }