fixing bug in focus manager that would cause keyboard events to be bound twice

This commit is contained in:
rolux 2010-07-06 09:43:12 +02:00
parent cd1bde4348
commit d6cc6fbf2f

View file

@ -212,23 +212,23 @@ requires
} }
return { return {
bind: function(id, event, callback) { bind: function(id, event, callback) {
Ox.print("a.l", arguments.length, id, event)
if (arguments.length == 2) { if (arguments.length == 2) {
callback = event; callback = event;
event = id; event = id;
Ox.print("e,c", event, callback)
} }
if (isKeyboardEvent(event)) { if (isKeyboardEvent(event)) {
keyboardEvents[id] = keyboardEvents[id] || {}; keyboardEvents[id] = keyboardEvents[id] || {};
keyboardEvents[id][event] = callback; keyboardEvents[id][event] = callback;
} }
if (!isKeyboardEvent(event) || Ox.Focus.focused() == id) { if (!isKeyboardEvent(event) || Ox.Focus.focused() == id) {
Ox.print("bind", id, event)
$eventHandler.bind(event, callback); $eventHandler.bind(event, callback);
} }
}, },
bindKeyboard: function(id) { bindKeyboard: function(id) {
$.each(keyboardEvents[id] || [], function(event, callback) { $.each(keyboardEvents[id] || [], function(event, callback) {
Ox.Event.bind(id, event, callback); Ox.Event.bind(id, event, callback);
//$eventHandler.bind(event, callback);
}); });
}, },
trigger: function(event, data) { trigger: function(event, data) {
@ -244,17 +244,19 @@ requires
} }
}); });
} }
Ox.print("unbind", id, event)
$eventHandler.unbind(event, callback); $eventHandler.unbind(event, callback);
}, },
unbindKeyboard: function(id) { unbindKeyboard: function(id) {
$.each(keyboardEvents[id] || [], function(event, callback) { $.each(keyboardEvents[id] || [], function(event, callback) {
Ox.print("unbind", id, event)
$eventHandler.unbind(event, callback); $eventHandler.unbind(event, callback);
}); });
} }
} }
}(); }();
Ox.Event_ = function() { Ox.Event_ = function() { // unused
var events = {}; var events = {};
return { return {
// make these bind, trigger, unbind // make these bind, trigger, unbind
@ -294,13 +296,13 @@ requires
var stack = []; var stack = [];
return { return {
blur: function(id) { blur: function(id) {
if (stack.indexOf(id) > -1) { if (stack.indexOf(id) == stack.length - 1) {
$elements[Ox.Focus.focused()].removeClass("OxFocus"); $elements[Ox.Focus.focused()].removeClass("OxFocus");
$(".OxFocus").removeClass("OxFocus"); // fixme: the above is better, and should work $(".OxFocus").removeClass("OxFocus"); // fixme: the above is better, and should work
stack.splice(stack.length - 2, 0, stack.pop()); stack.splice(stack.length - 2, 0, stack.pop());
Ox.Event.unbindKeyboard(id); Ox.Event.unbindKeyboard(id);
Ox.Event.bindKeyboard(stack[stack.length - 1]); Ox.Event.bindKeyboard(stack[stack.length - 1]);
Ox.print("blur", stack); Ox.print("blur", id, stack);
} }
}, },
focus: function(id) { focus: function(id) {
@ -314,7 +316,7 @@ requires
stack.push(id); stack.push(id);
$elements[Ox.Focus.focused()].addClass("OxFocus"); $elements[Ox.Focus.focused()].addClass("OxFocus");
Ox.Event.bindKeyboard(id); Ox.Event.bindKeyboard(id);
Ox.print("focus", stack); Ox.print("focus", id, stack);
}, },
focused: function() { focused: function() {
return stack[stack.length - 1]; return stack[stack.length - 1];
@ -2341,6 +2343,7 @@ requires
} }
}); });
Ox.print(self.options.id)
that.$button = new Ox.Button($.extend(self.options, { that.$button = new Ox.Button($.extend(self.options, {
id: self.buttonId, id: self.buttonId,
type: "text", // fixme: this shouldn't be necessary type: "text", // fixme: this shouldn't be necessary
@ -2679,6 +2682,7 @@ requires
if (!Ox.isUndefined(self.$items[pos])) { if (!Ox.isUndefined(self.$items[pos])) {
self.$items[pos].addClass("OxSelected"); self.$items[pos].addClass("OxSelected");
} }
Ox.print("addToSelection")
that.triggerEvent("select", { that.triggerEvent("select", {
ids: $.map(self.selected, function(v, i) { ids: $.map(self.selected, function(v, i) {
return self.ids[v]; return self.ids[v];
@ -3249,8 +3253,6 @@ requires
// Body // Body
Ox.print("s.vC", self.visibleColumns);
that.$body = new Ox.List({ that.$body = new Ox.List({
construct: constructItem, construct: constructItem,
id: self.options.id, id: self.options.id,
@ -3702,6 +3704,15 @@ requires
$item; // fixme: used? $item; // fixme: used?
// fixme: attach all private vars to self? // fixme: attach all private vars to self?
self.keyboardEvents = {
key_up: selectPreviousItem,
key_down: selectNextItem,
key_left: selectSupermenu,
key_right: selectSubmenu,
key_escape: hideMenu,
key_enter: clickSelectedItem
};
// construct // construct
that.items = []; that.items = [];
that.submenus = {}; that.submenus = {};
@ -3747,6 +3758,7 @@ requires
var item = that.items[position]; var item = that.items[position];
if (!item.options("items").length) { if (!item.options("items").length) {
if (that.options("parent")) { if (that.options("parent")) {
Ox.print("t.o.p", that.options("parent"))
that.options("parent").hideMenu().triggerEvent("click"); that.options("parent").hideMenu().triggerEvent("click");
} }
if (item.options("checked") !== null && (!item.options("group") || !item.options("checked"))) { if (item.options("checked") !== null && (!item.options("group") || !item.options("checked"))) {
@ -3882,6 +3894,11 @@ requires
return $("#" + Ox.toCamelCase(options.id + "/" + id)); return $("#" + Ox.toCamelCase(options.id + "/" + id));
} }
function hideMenu() {
// called on key_escape
that.hideMenu();
}
function isFirstEnabledItem() { function isFirstEnabledItem() {
var ret = true; var ret = true;
$.each(that.items, function(i, item) { $.each(that.items, function(i, item) {
@ -3987,6 +4004,7 @@ requires
function selectNextItem() { function selectNextItem() {
var offset, var offset,
selected = self.options.selected; selected = self.options.selected;
Ox.print("sNI", selected)
if (!isLastEnabledItem()) { if (!isLastEnabledItem()) {
if (selected == -1) { if (selected == -1) {
scrollMenuUp(); scrollMenuUp();
@ -4020,6 +4038,7 @@ requires
function selectPreviousItem() { function selectPreviousItem() {
var offset, var offset,
selected = self.options.selected; selected = self.options.selected;
Ox.print("sPI", selected)
if (selected > - 1) { if (selected > - 1) {
if (!isFirstEnabledItem()) { if (!isFirstEnabledItem()) {
that.items[selected].removeClass("OxSelected"); that.items[selected].removeClass("OxSelected");
@ -4046,6 +4065,7 @@ requires
} }
function selectSubmenu() { function selectSubmenu() {
Ox.print("selectSubmenu", self.options.selected)
if (self.options.selected > -1) { if (self.options.selected > -1) {
var submenu = that.submenus[that.items[self.options.selected].options("id")]; var submenu = that.submenus[that.items[self.options.selected].options("id")];
if (submenu && submenu.hasEnabledItems()) { if (submenu && submenu.hasEnabledItems()) {
@ -4060,6 +4080,7 @@ requires
} }
function selectSupermenu() { function selectSupermenu() {
Ox.print("selectSupermenu", self.options.selected)
if (self.options.parent) { if (self.options.parent) {
that.items[self.options.selected].trigger("mouseleave"); that.items[self.options.selected].trigger("mouseleave");
self.options.parent.gainFocus(); self.options.parent.gainFocus();
@ -4129,14 +4150,7 @@ requires
} }
that.hide() that.hide()
.loseFocus() .loseFocus()
.unbindEvent({ .unbindEvent(self.keyboardEvents)
key_up: selectPreviousItem,
key_down: selectNextItem,
key_left: selectSupermenu,
key_right: selectSubmenu,
key_escape: that.hideMenu,
key_enter: clickItem
})
.triggerEvent("hide"); .triggerEvent("hide");
that.$layer.hide(); that.$layer.hide();
$document.unbind("click", click); $document.unbind("click", click);
@ -4188,14 +4202,7 @@ requires
that.$container.height(menuHeight); that.$container.height(menuHeight);
} }
!self.options.parent && that.gainFocus(); !self.options.parent && that.gainFocus();
that.bindEvent({ that.bindEvent(self.keyboardEvents);
key_up: selectPreviousItem,
key_down: selectNextItem,
key_left: selectSupermenu,
key_right: selectSubmenu,
key_escape: that.hideMenu,
key_enter: clickSelectedItem
});
setTimeout(function() { setTimeout(function() {
$document.bind("click", click); $document.bind("click", click);
}, 100); }, 100);