diff --git a/source/Ox.UI/js/Core/Focus.js b/source/Ox.UI/js/Core/Focus.js index 5a0b8da4..5479b37d 100644 --- a/source/Ox.UI/js/Core/Focus.js +++ b/source/Ox.UI/js/Core/Focus.js @@ -5,75 +5,66 @@ Ox.Focus Basic focus controller @*/ Ox.Focus = (function() { - var stack = []; - return { - _print: function() { - Ox.Log('Core', stack); - }, - _reset: function() { - $('.OxFocus').removeClass('OxFocus'); - stack = []; - }, - /*@ - blur blur element - (id) -> blur element by id - @*/ - blur: function(id) { - Ox.GarbageCollection(); - var index = stack.indexOf(id); - if (index > -1 && index == stack.length - 1) { - stack.length == 1 - // empty stack - ? stack.pop() - // swap the two last stack items - : stack.splice(stack.length - 2, 0, stack.pop()); - Ox.UI.elements[id] - .removeClass('OxFocus') - .triggerEvent('losefocus'); - if (stack.length) { - Ox.UI.elements[stack[stack.length - 1]] + + var stack = [], + + that = { + focusedElement: function() { + return Ox.elements[Ox.last(stack)]; + }, + focusedElementIsInput: function() { + var $element = that.focusedElement(); + return $element && ( + $element.hasClass('OxInput') + || $element.hasClass('OxEditableElement') + || $element.hasClass('OxAutocompleteMenu') + ); + }, + 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 .addClass('OxFocus') - .triggerEvent('gainfocus') + .triggerEvent('gainfocus'); } - Ox.Log('Core', 'blur', id, stack); - } - }, - /*@ - focus focus element - (id) -> focus element by id - @*/ - focus: function(id) { - Ox.GarbageCollection(); - var index = stack.indexOf(id); - if (index == -1 || index < stack.length - 1) { - // move the item to the end of the stack - index > -1 && stack.splice(index, 1); - stack.push(id); - if (stack.length > 1) { - Ox.UI.elements[stack[stack.length - 2]] + }, + 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); } - Ox.UI.elements[id] - .addClass('OxFocus') - .triggerEvent('gainfocus'); - Ox.Log('Core', 'focus', id, stack); } - }, - /*@ - focused return id of focused element, or null - () -> get id of currently focused element - @*/ - focused: function() { - return stack.length ? stack[stack.length - 1] : null; - }, - /*@ - remove remove - (id) -> - @*/ - remove: function(id) { - var index = stack.indexOf(id); - index > -1 && stack.splice(index, 1); - } - }; -}()); + }; + + return that; + +}()); \ No newline at end of file