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

105 lines
3.5 KiB
JavaScript
Raw Permalink 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 = {
cleanup: function() {
stack = stack.filter(function(item) {
try {
return !!Ox.$elements[item]
} catch(e) {
return false;
}
});
},
debug: function() {
return stack.map(function(item) {
try {
return Ox.$elements[item][0];
} catch(e) {
return item;
}
});
},
2014-09-23 19:13:07 +00:00
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) {
try {
$focusedElement
.removeClass('OxFocus')
.triggerEvent('losefocus');
} catch(e) {
that.cleanup();
}
}
try {
$element
.addClass('OxFocus')
.triggerEvent('gainfocus');
} catch(e) {
that.cleanup();
2014-09-23 19:13:07 +00:00
}
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 $focusedElement,
index = stack.indexOf($element.oxid);
2014-09-23 19:13:07 +00:00
if (index > -1 && index == stack.length - 1) {
stack.pop();
try {
$element
.removeClass('OxFocus')
.triggerEvent('losefocus');
} catch(e) {
that.cleanup();
}
$focusedElement = that.focusedElement();
2014-09-23 19:13:07 +00:00
if (stack.length) {
if ($focusedElement) {
try {
$focusedElement
.addClass('OxFocus')
.triggerEvent('gainfocus');
} catch(e) {
that.cleanup();
}
}
2014-09-23 19:13:07 +00:00
}
}
},
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;
}());