oxjs/source/Ox.UI/js/Core/Focus.js

66 lines
2.2 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2012-05-21 10:38:18 +00:00
2011-05-16 10:49:48 +00:00
/*@
Ox.Focus <o> Basic focus controller
2011-05-16 10:49:48 +00:00
@*/
Ox.Focus = (function() {
2014-09-23 19:13:07 +00:00
var stack = [],
that = {
focusedElement: function() {
2014-09-25 16:47:29 +00:00
return Ox.$elements[Ox.last(stack)];
2014-09-23 19:13:07 +00:00
},
focusedElementIsInput: function() {
var $element = that.focusedElement();
return $element && $element.hasClass('OxKeyboardFocus');
2014-09-23 19:13:07 +00:00
},
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
2012-01-27 14:29:11 +00:00
.addClass('OxFocus')
2014-09-23 19:13:07 +00:00
.triggerEvent('gainfocus');
2012-01-27 14:29:11 +00:00
}
2014-09-23 19:13:07 +00:00
},
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
2012-01-27 14:29:11 +00:00
.removeClass('OxFocus')
.triggerEvent('losefocus');
2014-09-23 19:13:07 +00:00
if (stack.length) {
2014-09-25 16:47:29 +00:00
Ox.$elements[Ox.last(stack)]
2014-09-23 19:13:07 +00:00
.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);
2012-01-27 14:29:11 +00:00
}
2011-04-22 22:03:10 +00:00
}
2014-09-23 19:13:07 +00:00
};
return that;
}());