don't fail if elements in focus stack are gone

This commit is contained in:
j 2016-09-07 11:28:58 +02:00
parent 310502df22
commit 0f10b4be47

View file

@ -9,6 +9,24 @@ Ox.Focus = (function() {
var stack = [], var stack = [],
that = { that = {
cleanup: function() {
stack = stack.filter(function(item) {
try {
return !!Ox.$elements[item]
} catch(e) {
return false;
}
});
},
debug: function() {
return stack.map(function(item) {
try {
return Ox.$elements[item][0];
} catch(e) {
return item;
}
});
},
focusedElement: function() { focusedElement: function() {
return Ox.$elements[Ox.last(stack)]; return Ox.$elements[Ox.last(stack)];
}, },
@ -25,9 +43,13 @@ Ox.Focus = (function() {
return $element.oxid; return $element.oxid;
}).concat(oxid); }).concat(oxid);
if ($focusedElement) { if ($focusedElement) {
try {
$focusedElement $focusedElement
.removeClass('OxFocus') .removeClass('OxFocus')
.triggerEvent('losefocus'); .triggerEvent('losefocus');
} catch(e) {
that.cleanup();
}
} }
$element $element
.addClass('OxFocus') .addClass('OxFocus')
@ -38,16 +60,28 @@ Ox.Focus = (function() {
return Ox.last(stack) == $element.oxid; return Ox.last(stack) == $element.oxid;
}, },
loseFocus: function($element) { loseFocus: function($element) {
var index = stack.indexOf($element.oxid); var $focusedElement,
index = stack.indexOf($element.oxid);
if (index > -1 && index == stack.length - 1) { if (index > -1 && index == stack.length - 1) {
stack.pop(); stack.pop();
try {
$element $element
.removeClass('OxFocus') .removeClass('OxFocus')
.triggerEvent('losefocus'); .triggerEvent('losefocus');
} catch(e) {
that.cleanup();
}
$focusedElement = that.focusedElement();
if (stack.length) { if (stack.length) {
Ox.$elements[Ox.last(stack)] if ($focusedElement) {
try {
$focusedElement
.addClass('OxFocus') .addClass('OxFocus')
.triggerEvent('gainfocus'); .triggerEvent('gainfocus');
} catch(e) {
that.cleanup();
}
}
} }
} }
}, },