From 7ede8124cba760cce6c454978d3bb9313cde0bb6 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 25 Jan 2016 07:09:15 +0100 Subject: [PATCH] Pagination: Focus active after next or prev exceeded I'd like to see behaviors for components instead of patching loader/events all the time. --- public/js/icinga/behavior/autofocus.js | 8 +++++--- public/js/icinga/loader.js | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/public/js/icinga/behavior/autofocus.js b/public/js/icinga/behavior/autofocus.js index 23236f072..35614a46e 100644 --- a/public/js/icinga/behavior/autofocus.js +++ b/public/js/icinga/behavior/autofocus.js @@ -14,9 +14,11 @@ Autofocus.prototype = new Icinga.EventListener(); Autofocus.prototype.onRendered = function(e) { - if (document.activeElement === document.body) { - $(e.target).find('.autofocus').focus(); - } + setTimeout(function() { + if (document.activeElement === e.target) { + $(e.target).find('.autofocus').focus(); + } + }, 0); }; Icinga.Behaviors.Autofocus = Autofocus; diff --git a/public/js/icinga/loader.js b/public/js/icinga/loader.js index 78bdee93a..198bc643f 100644 --- a/public/js/icinga/loader.js +++ b/public/js/icinga/loader.js @@ -729,6 +729,7 @@ var containerId = $container.attr('id'); var activeElementPath = false; + var focusFallback = false; if (forceFocus && forceFocus.length) { activeElementPath = this.icinga.utils.getCSSPath($(forceFocus)); } else if (document.activeElement @@ -736,7 +737,15 @@ && $.contains($container[0], document.activeElement) ) { // Active element in container - activeElementPath = this.icinga.utils.getCSSPath($(document.activeElement)); + var $activeElement = $(document.activeElement); + var $pagination = $activeElement.closest('.pagination-control'); + if ($pagination.length) { + focusFallback = { + 'parent': this.icinga.utils.getCSSPath($pagination), + 'child': '.active > a' + }; + } + activeElementPath = this.icinga.utils.getCSSPath($activeElement); } if (typeof containerId !== 'undefined') { @@ -815,7 +824,9 @@ if ($activeElement.length && $activeElement.is(':visible')) { $activeElement.focus(); } else if (! autorefresh) { - if (typeof $container.attr('tabindex') === 'undefined') { + if (focusFallback) { + $(focusFallback.parent).find(focusFallback.child).focus(); + } else if (typeof $container.attr('tabindex') === 'undefined') { $container.attr('tabindex', -1); } $container.focus();