Handle anchors in links

This commit is contained in:
Thomas Gelf 2014-03-20 11:24:15 +00:00
parent 1a47f617ee
commit edc3deeec8
3 changed files with 35 additions and 7 deletions

View File

@ -188,6 +188,7 @@
var $li; var $li;
var $target; var $target;
var isMenuLink = $a.closest('#menu').length > 0; var isMenuLink = $a.closest('#menu').length > 0;
var formerUrl;
// TODO: Let remote links pass through. Right now they only work // TODO: Let remote links pass through. Right now they only work
// combined with target="_blank" or target="_self" // combined with target="_blank" or target="_self"
@ -201,8 +202,8 @@
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
// If link is hash tag... // If link has hash tag...
if (href === '#') { if (href.match(/#/)) {
// ...it may be a menu section without a dedicated link. // ...it may be a menu section without a dedicated link.
// Switch the active menu item: // Switch the active menu item:
if (isMenuLink) { if (isMenuLink) {
@ -210,14 +211,25 @@
$('#menu .active').removeClass('active'); $('#menu .active').removeClass('active');
$li.addClass('active'); $li.addClass('active');
} }
if (href === '#') {
return false;
}
// Stop here for all hash tags $target = self.getLinkTargetFor($a);
// TODO: handle anchors
return false; formerUrl = $target.data('icingaUrl');
if (typeof formerUrl !== 'undefined' && formerUrl.split(/#/)[0] === href.split(/#/)[0]) {
icinga.ui.scrollContainerToAnchor($target, href.split(/#/)[1]);
$target.data('icingaUrl', href);
if (formerUrl !== href) {
icinga.history.pushCurrentState();
}
return false;
}
} else {
$target = self.getLinkTargetFor($a);
} }
$target = self.getLinkTargetFor($a);
// Load link URL // Load link URL
icinga.loader.loadUrl(href, $target); icinga.loader.loadUrl(href, $target);

View File

@ -388,6 +388,9 @@
*/ */
this.renderContentToContainer($resp, req.$target); this.renderContentToContainer($resp, req.$target);
if (url.match(/#/)) {
this.icinga.ui.scrollContainerToAnchor(req.$target, url.split(/#/)[1]);
}
if (newBody) { if (newBody) {
this.icinga.ui.fixDebugVisibility().triggerWindowResize(); this.icinga.ui.fixDebugVisibility().triggerWindowResize();
} }
@ -515,6 +518,7 @@
$container.html(content); $container.html(content);
} }
} }
if (scrollPos !== false) { if (scrollPos !== false) {
$container.scrollTop(scrollPos); $container.scrollTop(scrollPos);
} }

View File

@ -120,6 +120,18 @@
$col.data('icingaModule', backup['data']['data-icinga-module']); $col.data('icingaModule', backup['data']['data-icinga-module']);
}, },
scrollContainerToAnchor: function ($container, anchorName) {
// TODO: Generic issue -> we probably should escape attribute value selectors!?
var $anchor = $("a[name='" + anchorName + "']", $container);
if ($anchor.length) {
$container.scrollTop(0);
$container.scrollTop($anchor.first().position().top);
this.icinga.logger.debug('Scrolling ', $container, ' to ', anchorName);
} else {
this.icinga.logger.info('Anchor "' + anchorName + '" not found in ', $container);
}
},
triggerWindowResize: function () { triggerWindowResize: function () {
this.onWindowResize({data: {self: this}}); this.onWindowResize({data: {self: this}});
}, },