From 3ab5b5b72101ea52a795702537943803ca7c911f Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 18 Jan 2016 11:36:50 +0100 Subject: [PATCH] Fix skip to content jump link --- public/js/icinga/events.js | 4 ++-- public/js/icinga/ui.js | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/public/js/icinga/events.js b/public/js/icinga/events.js index 9910c2d46..42eba1b72 100644 --- a/public/js/icinga/events.js +++ b/public/js/icinga/events.js @@ -441,7 +441,6 @@ if (href.match(remote)) { return true; } - // window.open is used as return true; didn't work reliable if (linkTarget === '_blank' || linkTarget === '_self') { window.open(href, linkTarget); @@ -472,7 +471,8 @@ // This is an anchor only if (href.substr(0, 1) === '#' && href.length > 1 - && href.substr(1, 1) !== '!') { + && href.substr(1, 1) !== '!' + ) { icinga.ui.focusElement(href.substr(1), $a.closest('.container')); return; } diff --git a/public/js/icinga/ui.js b/public/js/icinga/ui.js index 3c5b1fcf6..09b18857b 100644 --- a/public/js/icinga/ui.js +++ b/public/js/icinga/ui.js @@ -132,17 +132,21 @@ /** * Focus the given element and scroll to its position * - * @param {string} element The name or id of the element to focus - * @param {object} $container The container containing the element + * @param {string} element The name or id of the element to focus + * @param {object} [$container] The container containing the element */ focusElement: function(element, $container) { - var $element = $('#' + element, $container); + var $element = $('#' + element); if (! $element.length) { // The name attribute is actually deprecated, on anchor tags, // but we'll possibly handle links from another source // (module etc) so that's used as a fallback - $element = $('[name="' + element.replace(/'/, '\\\'') + '"]', $container); + if ($container && $container.length) { + $element = $container.find('[name="' + element.replace(/'/, '\\\'') + '"]'); + } else { + $element = $('[name="' + element.replace(/'/, '\\\'') + '"]'); + } } if ($element.length) { @@ -151,8 +155,11 @@ } $element.focus(); - $container.scrollTop(0); - $container.scrollTop($element.first().position().top); + + if ($container && $container.length) { + $container.scrollTop(0); + $container.scrollTop($element.first().position().top); + } } },