2014-09-10 10:45:34 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
(function(Icinga, $) {
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var activeMenuId;
|
|
|
|
|
|
|
|
Icinga.Behaviors = Icinga.Behaviors || {};
|
|
|
|
|
|
|
|
var Navigation = function (icinga) {
|
2014-09-12 09:09:21 +02:00
|
|
|
Icinga.EventListener.call(this, icinga);
|
|
|
|
this.on('click', '#menu a', this.linkClicked, this);
|
|
|
|
this.on('click', '#menu tr[href]', this.linkClicked, this);
|
|
|
|
this.on('mouseenter', 'li.dropdown', this.dropdownHover, this);
|
|
|
|
this.on('mouseleave', 'li.dropdown', this.dropdownLeave, this);
|
|
|
|
this.on('mouseenter', '#menu > ul > li', this.menuTitleHovered, this);
|
|
|
|
this.on('mouseleave', '#sidebar', this.leaveSidebar, this);
|
|
|
|
this.on('rendered', this.onRendered);
|
2014-09-10 10:45:34 +02:00
|
|
|
};
|
2014-09-12 09:09:21 +02:00
|
|
|
Navigation.prototype = new Icinga.EventListener();
|
2014-09-10 10:45:34 +02:00
|
|
|
|
2014-09-12 09:09:21 +02:00
|
|
|
Navigation.prototype.onRendered = function(evt) {
|
|
|
|
// get original source element of the rendered-event
|
|
|
|
var el = evt.target;
|
2014-09-10 16:25:47 +02:00
|
|
|
// restore old menu state
|
2014-09-10 10:45:34 +02:00
|
|
|
if (activeMenuId) {
|
|
|
|
$('[role="navigation"] li.active', el).removeClass('active');
|
2014-09-10 16:25:47 +02:00
|
|
|
var $selectedMenu = $('#' + activeMenuId, el).addClass('active');
|
2014-09-10 10:45:34 +02:00
|
|
|
var $outerMenu = $selectedMenu.parent().closest('li');
|
|
|
|
if ($outerMenu.size()) {
|
2014-09-10 16:25:47 +02:00
|
|
|
$outerMenu.addClass('active');
|
2014-09-10 10:45:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// store menu state
|
|
|
|
var $menus = $('[role="navigation"] li.active', el);
|
|
|
|
if ($menus.size()) {
|
|
|
|
activeMenuId = $menus[0].id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Navigation.prototype.linkClicked = function(event) {
|
|
|
|
var $a = $(this);
|
|
|
|
var href = $a.attr('href');
|
|
|
|
var $li;
|
|
|
|
var icinga = event.data.self.icinga;
|
|
|
|
|
|
|
|
if (href.match(/#/)) {
|
2014-09-10 14:21:15 +02:00
|
|
|
// ...it may be a menu section without a dedicated link.
|
|
|
|
// Switch the active menu item:
|
2014-09-10 16:25:47 +02:00
|
|
|
$li = $a.closest('li');
|
|
|
|
$('#menu .active').removeClass('active');
|
|
|
|
$li.addClass('active');
|
|
|
|
activeMenuId = $($li).attr('id');
|
|
|
|
if ($li.hasClass('hover')) {
|
|
|
|
$li.removeClass('hover');
|
2014-09-10 14:21:15 +02:00
|
|
|
}
|
|
|
|
if (href === '#') {
|
|
|
|
// Allow to access dropdown menu by keyboard
|
|
|
|
if ($a.hasClass('dropdown-toggle')) {
|
|
|
|
$a.closest('li').toggleClass('hover');
|
|
|
|
}
|
2014-09-10 16:25:47 +02:00
|
|
|
return;
|
2014-09-10 10:45:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-10 16:25:47 +02:00
|
|
|
activeMenuId = $(event.target).closest('li').attr('id');
|
2014-09-10 10:45:34 +02:00
|
|
|
}
|
2014-09-10 16:25:47 +02:00
|
|
|
// update target url of the menu container to the clicked link
|
|
|
|
var $menu = $('#menu');
|
|
|
|
var menuDataUrl = icinga.utils.parseUrl($menu.data('icinga-url'));
|
|
|
|
menuDataUrl = icinga.utils.addUrlParams(menuDataUrl.path, { url: href });
|
|
|
|
$menu.data('icinga-url', menuDataUrl);
|
2014-09-10 10:45:34 +02:00
|
|
|
};
|
|
|
|
|
2014-09-10 14:21:15 +02:00
|
|
|
Navigation.prototype.menuTitleHovered = function(event) {
|
|
|
|
var $li = $(this),
|
|
|
|
delay = 800,
|
|
|
|
self = event.data.self;
|
|
|
|
|
|
|
|
if ($li.hasClass('active')) {
|
|
|
|
$li.siblings().removeClass('hover');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($li.children('ul').children('li').length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($('#menu').scrollTop() > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($('#layout').hasClass('hoveredmenu')) {
|
|
|
|
delay = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
if (! $li.is('li:hover')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($li.hasClass('active')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$li.siblings().each(function () {
|
|
|
|
var $sibling = $(this);
|
|
|
|
if ($sibling.is('li:hover')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($sibling.hasClass('hover')) {
|
|
|
|
$sibling.removeClass('hover');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.hoverElement($li);
|
|
|
|
}, delay);
|
|
|
|
};
|
|
|
|
|
|
|
|
Navigation.prototype.leaveSidebar = function (event) {
|
|
|
|
var $sidebar = $(this),
|
|
|
|
$li = $sidebar.find('li.hover'),
|
|
|
|
self = event.data.self;
|
|
|
|
if (! $li.length) {
|
|
|
|
$('#layout').removeClass('hoveredmenu');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
if ($li.is('li:hover') || $sidebar.is('sidebar:hover') ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$li.removeClass('hover');
|
|
|
|
$('#layout').removeClass('hoveredmenu');
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
Navigation.prototype.hoverElement = function ($li) {
|
|
|
|
$('#layout').addClass('hoveredmenu');
|
|
|
|
$li.addClass('hover');
|
|
|
|
};
|
|
|
|
|
|
|
|
Navigation.prototype.dropdownHover = function () {
|
|
|
|
$(this).addClass('hover');
|
|
|
|
};
|
|
|
|
|
|
|
|
Navigation.prototype.dropdownLeave = function (event) {
|
|
|
|
var $li = $(this),
|
|
|
|
self = event.data.self;
|
|
|
|
setTimeout(function () {
|
|
|
|
// TODO: make this behave well together with keyboard navigation
|
|
|
|
if (! $li.is('li:hover') /*&& ! $li.find('a:focus')*/) {
|
|
|
|
$li.removeClass('hover');
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
};
|
2014-09-10 10:45:34 +02:00
|
|
|
Icinga.Behaviors.Navigation = Navigation;
|
|
|
|
|
|
|
|
}) (Icinga, jQuery);
|