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