define([ 'angular', 'jquery', 'lodash', 'tether', ], function (angular, $, _, Tether) { 'use strict'; angular .module('grafana.directives') .directive('panelMenu', function($compile, linkSrv) { var linkTemplate = '' + '' + '{{ctrl.panel.title | interpolateTemplateVars:this}}' + '' + ' {{ctrl.timeInfo}}' + ''; function createExternalLinkMenu(ctrl) { var template = '
'; template += '
'; if (ctrl.panel.links) { _.each(ctrl.panel.links, function(link) { var info = linkSrv.getPanelLinkAnchorInfo(link, ctrl.panel.scopedVars); template += '' + info.title + ''; }); } return template; } function createMenuTemplate(ctrl) { var template = '
'; if (ctrl.dashboard.meta.canEdit) { template += '
'; template += '
'; if (!ctrl.dashboard.meta.fullscreen) { template += ''; template += ''; } template += ''; template += '
'; template += '
'; } template += '
'; template += ''; _.each(ctrl.getMenu(), function(item) { // skip edit actions if not editor if (item.role === 'Editor' && !ctrl.dashboard.meta.canEdit) { return; } template += ''; }); template += '
'; template += '
'; template += '
'; return template; } function getExtendedMenu(ctrl) { return ctrl.getExtendedMenu(); } return { restrict: 'A', link: function($scope, elem) { var $link = $(linkTemplate); var $panelLinksBtn = $link.find(".panel-links-btn"); var $panelContainer = elem.parents(".panel-container"); var menuScope = null; var ctrl = $scope.ctrl; var timeout = null; var $menu = null; var teather; elem.append($link); $scope.$watchCollection('ctrl.panel.links', function(newValue) { var showIcon = (newValue ? newValue.length > 0 : false) && ctrl.panel.title !== ''; $panelLinksBtn.toggle(showIcon); }); function dismiss(time, force) { clearTimeout(timeout); timeout = null; if (time) { timeout = setTimeout(dismiss, time); return; } // if hovering or draging pospone close if (force !== true) { if ($menu.is(':hover') || $scope.ctrl.dashboard.$$panelDragging) { dismiss(2200); return; } } if (menuScope) { teather.destroy(); $menu.unbind(); $menu.remove(); menuScope.$destroy(); menuScope = null; $menu = null; $panelContainer.removeClass('panel-highlight'); } } var showMenu = function(e) { // if menu item is clicked and menu was just removed from dom ignore this event if (!$.contains(document, e.target)) { return; } if ($menu) { dismiss(); return; } var menuTemplate; if ($(e.target).hasClass('fa-external-link')) { menuTemplate = createExternalLinkMenu(ctrl); } else { menuTemplate = createMenuTemplate(ctrl); } $menu = $(menuTemplate); $menu.mouseleave(function() { dismiss(1000); }); menuScope = $scope.$new(); menuScope.extendedMenu = getExtendedMenu(ctrl); menuScope.dismiss = function() { dismiss(null, true); }; $(".panel-container").removeClass('panel-highlight'); $panelContainer.toggleClass('panel-highlight'); $('.panel-menu').remove(); elem.append($menu); $scope.$apply(function() { $compile($menu.contents())(menuScope); teather = new Tether({ element: $menu, target: $panelContainer, attachment: 'bottom center', targetAttachment: 'top center', constraints: [ { to: 'window', attachment: 'together', pin: true } ] }); }); dismiss(2200); }; elem.click(showMenu); $compile(elem.contents())($scope); } }; }); });