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

73 lines
2.3 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 10:49:48 +00:00
/*@
Ox.Focus <o> Basic focus handler
@*/
Ox.Focus = (function() {
2011-04-22 22:03:10 +00:00
var stack = [];
return {
_print: function() {
2011-11-04 15:54:28 +00:00
Ox.Log('Core', stack);
2011-04-22 22:03:10 +00:00
},
2011-05-12 20:02:22 +00:00
_reset: function() {
$('.OxFocus').removeClass('OxFocus');
stack = [];
},
2011-05-16 10:49:48 +00:00
/*@
blur <f> blur element
(id) -> <u> blur element by id
@*/
2011-04-22 22:03:10 +00:00
blur: function(id) {
Ox.GarbageCollection();
2011-04-22 22:03:10 +00:00
var index = stack.indexOf(id);
if (index > -1 && index == stack.length - 1) {
stack.length == 1
? stack.pop()
: stack.splice(stack.length - 2, 0, stack.pop());
2012-01-27 14:29:11 +00:00
Ox.UI.elements[id]
.removeClass('OxFocus')
.triggerEvent('losefocus');
if (stack.length) {
Ox.UI.elements[stack[stack.length - 1]]
.addClass('OxFocus')
.triggerEvent('gainfocus')
}
2011-11-04 15:54:28 +00:00
Ox.Log('Core', 'blur', id, stack);
2011-04-22 22:03:10 +00:00
}
},
2011-05-16 10:49:48 +00:00
/*@
focus <f> focus element
(id) -> <u> focus element by id
@*/
2011-04-22 22:03:10 +00:00
focus: function(id) {
Ox.GarbageCollection();
2011-04-22 22:03:10 +00:00
var index = stack.indexOf(id);
if (index == -1 || index < stack.length - 1) {
index > -1 && stack.splice(index, 1);
stack.push(id);
2012-01-27 14:29:11 +00:00
if (stack.length > 1) {
Ox.UI.elements[stack[stack.length - 2]]
.removeClass('OxFocus')
.triggerEvent('losefocus');
2012-01-27 14:29:11 +00:00
}
Ox.UI.elements[id]
2012-01-27 14:29:11 +00:00
.addClass('OxFocus')
.triggerEvent('gainfocus');
Ox.Log('Core', 'focus', id, stack);
2011-04-22 22:03:10 +00:00
}
},
2011-05-16 10:49:48 +00:00
/*@
focused <f> return id of focused element, or null
() -> <s> get id of currently focused element
@*/
2011-04-22 22:03:10 +00:00
focused: function() {
return stack.length ? stack[stack.length - 1] : null;
},
remove: function(id) {
var index = stack.indexOf(id);
index > -1 && stack.splice(index, 1);
2011-04-22 22:03:10 +00:00
}
};
}());