diff --git a/public/js/icinga/loader.js b/public/js/icinga/loader.js index 36f6805ba..78f753d1b 100644 --- a/public/js/icinga/loader.js +++ b/public/js/icinga/loader.js @@ -726,6 +726,16 @@ var scrollPos = false; var self = this; var containerId = $container.attr('id'); + + var activeElementPath = false; + if (document.activeElement + && document.activeElement !== document.body + && $.contains($container[0], document.activeElement) + ) { + // Active element in container + activeElementPath = this.icinga.utils.getCSSPath($(document.activeElement)); + } + if (typeof containerId !== 'undefined') { if (autorefresh) { scrollPos = $container.scrollTop(); @@ -733,9 +743,6 @@ scrollPos = 0; } } - if (autorefresh && $.contains($container[0], document.activeElement)) { - var origFocus = self.icinga.utils.getDomPath(document.activeElement); - } $container.trigger('beforerender'); @@ -788,9 +795,28 @@ } this.icinga.ui.assignUniqueContainerIds(); - if (origFocus && origFocus.length > 0 && origFocus[0] !== '') { + if (! activeElementPath) { + // Active element was not in this container + if (! autorefresh) { + setTimeout(function() { + if (typeof $container.attr('tabindex') === 'undefined') { + $container.attr('tabindex', -1); + } + $container.focus(); + }, 0); + } + } else { setTimeout(function() { - $(self.icinga.utils.getElementByDomPath(origFocus)).focus(); + var $activeElement = $(activeElementPath); + + if ($activeElement.length) { + $activeElement.focus(); + } else if (! autorefresh) { + if (typeof $container.attr('tabindex') === 'undefined') { + $container.attr('tabindex', -1); + } + $container.focus(); + } }, 0); }