new Focus handler

This commit is contained in:
rlx 2014-09-23 21:13:07 +02:00
parent cc183b6198
commit a90cc87234

View file

@ -5,75 +5,66 @@ Ox.Focus <o> Basic focus controller
@*/ @*/
Ox.Focus = (function() { Ox.Focus = (function() {
var stack = [];
return { var stack = [],
_print: function() {
Ox.Log('Core', stack); that = {
}, focusedElement: function() {
_reset: function() { return Ox.elements[Ox.last(stack)];
$('.OxFocus').removeClass('OxFocus'); },
stack = []; focusedElementIsInput: function() {
}, var $element = that.focusedElement();
/*@ return $element && (
blur <f> blur element $element.hasClass('OxInput')
(id) -> <u> blur element by id || $element.hasClass('OxEditableElement')
@*/ || $element.hasClass('OxAutocompleteMenu')
blur: function(id) { );
Ox.GarbageCollection(); },
var index = stack.indexOf(id); gainFocus: function($element) {
if (index > -1 && index == stack.length - 1) { var $focusedElement = that.focusedElement(),
stack.length == 1 oxid = $element.oxid,
// empty stack index = stack.indexOf(oxid);
? stack.pop() if (index == -1 || index < stack.length - 1) {
// swap the two last stack items stack = $element.parentElements().map(function($element) {
: stack.splice(stack.length - 2, 0, stack.pop()); return $element.oxid;
Ox.UI.elements[id] }).concat(oxid);
.removeClass('OxFocus') if ($focusedElement) {
.triggerEvent('losefocus'); $focusedElement
if (stack.length) { .removeClass('OxFocus')
Ox.UI.elements[stack[stack.length - 1]] .triggerEvent('losefocus');
}
$element
.addClass('OxFocus') .addClass('OxFocus')
.triggerEvent('gainfocus') .triggerEvent('gainfocus');
} }
Ox.Log('Core', 'blur', id, stack); },
} hasFocus: function($element) {
}, return Ox.last(stack) == $element.oxid;
/*@ },
focus <f> focus element loseFocus: function($element) {
(id) -> <u> focus element by id var index = stack.indexOf($element.oxid);
@*/ if (index > -1 && index == stack.length - 1) {
focus: function(id) { stack.pop();
Ox.GarbageCollection(); $element
var index = stack.indexOf(id);
if (index == -1 || index < stack.length - 1) {
// move the item to the end of the stack
index > -1 && stack.splice(index, 1);
stack.push(id);
if (stack.length > 1) {
Ox.UI.elements[stack[stack.length - 2]]
.removeClass('OxFocus') .removeClass('OxFocus')
.triggerEvent('losefocus'); .triggerEvent('losefocus');
if (stack.length) {
Ox.elements[Ox.last(stack)]
.addClass('OxFocus')
.triggerEvent('gainfocus');
}
}
},
removeElement: function($element) {
var index = stack.indexOf($element.oxid);
if (index == stack.length - 1) {
that.loseFocus($element);
} else if (index > -1) {
stack.splice(index, 1);
} }
Ox.UI.elements[id]
.addClass('OxFocus')
.triggerEvent('gainfocus');
Ox.Log('Core', 'focus', id, stack);
} }
}, };
/*@
focused <f> return id of focused element, or null return that;
() -> <s> get id of currently focused element
@*/ }());
focused: function() {
return stack.length ? stack[stack.length - 1] : null;
},
/*@
remove <f> remove
(id) -> <u>
@*/
remove: function(id) {
var index = stack.indexOf(id);
index > -1 && stack.splice(index, 1);
}
};
}());