70 lines
No EOL
2.4 KiB
JavaScript
70 lines
No EOL
2.4 KiB
JavaScript
'use strict';
|
|
|
|
/*@
|
|
Ox.Focus <o> Basic focus controller
|
|
@*/
|
|
|
|
Ox.Focus = (function() {
|
|
|
|
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');
|
|
}
|
|
},
|
|
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);
|
|
}
|
|
}
|
|
};
|
|
|
|
return that;
|
|
|
|
}()); |