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