oxjs/source/js/Ox.Focus.js
2011-04-23 00:54:53 +02:00

41 lines
1.4 KiB
JavaScript

//vim: et:ts=4:sw=4:sts=4:ft=js
Ox.Focus = function() {
/***
Ox.Focus
Basic focus handler
Methods
blur(id) blur element
focus(id) focus element
focused() return id of focused element, or null
***/
var stack = [];
return {
_print: function() {
Ox.print(stack);
},
blur: function(id) {
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());
//$elements[id].removeClass('OxFocus');
$('.OxFocus').removeClass('OxFocus'); // fixme: the above is better, and should work
stack.length && Ox.UI.elements[stack[stack.length - 1]].addClass('OxFocus');
Ox.print('blur', id, stack);
}
},
focus: function(id) {
var index = stack.indexOf(id);
if (index == -1 || index < stack.length - 1) {
index > -1 && stack.splice(index, 1);
stack.push(id);
$('.OxFocus').removeClass('OxFocus'); // fixme: see above
Ox.UI.elements[id].addClass('OxFocus');
Ox.print('focus', id, stack);
}
},
focused: function() {
return stack.length ? stack[stack.length - 1] : null;
}
};
}();