don't fail if elements in focus stack are gone
This commit is contained in:
parent
310502df22
commit
0f10b4be47
1 changed files with 44 additions and 10 deletions
|
@ -9,6 +9,24 @@ Ox.Focus = (function() {
|
|||
var stack = [],
|
||||
|
||||
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() {
|
||||
return Ox.$elements[Ox.last(stack)];
|
||||
},
|
||||
|
@ -25,9 +43,13 @@ Ox.Focus = (function() {
|
|||
return $element.oxid;
|
||||
}).concat(oxid);
|
||||
if ($focusedElement) {
|
||||
try {
|
||||
$focusedElement
|
||||
.removeClass('OxFocus')
|
||||
.triggerEvent('losefocus');
|
||||
} catch(e) {
|
||||
that.cleanup();
|
||||
}
|
||||
}
|
||||
$element
|
||||
.addClass('OxFocus')
|
||||
|
@ -38,16 +60,28 @@ Ox.Focus = (function() {
|
|||
return Ox.last(stack) == $element.oxid;
|
||||
},
|
||||
loseFocus: function($element) {
|
||||
var index = stack.indexOf($element.oxid);
|
||||
var $focusedElement,
|
||||
index = stack.indexOf($element.oxid);
|
||||
if (index > -1 && index == stack.length - 1) {
|
||||
stack.pop();
|
||||
try {
|
||||
$element
|
||||
.removeClass('OxFocus')
|
||||
.triggerEvent('losefocus');
|
||||
} catch(e) {
|
||||
that.cleanup();
|
||||
}
|
||||
$focusedElement = that.focusedElement();
|
||||
if (stack.length) {
|
||||
Ox.$elements[Ox.last(stack)]
|
||||
if ($focusedElement) {
|
||||
try {
|
||||
$focusedElement
|
||||
.addClass('OxFocus')
|
||||
.triggerEvent('gainfocus');
|
||||
} catch(e) {
|
||||
that.cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue