oxjs/source/UI/js/Core/Focus.js
2014-12-22 22:15:54 +00:00

67 lines
No EOL
2.3 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('OxKeyboardFocus');
},
gainFocus: function($element) {
Ox.print('GAIN FOCUS', $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;
}());