define([ 'angular', 'jquery', 'lodash', 'tether', ], function (angular, $, _, Tether) { 'use strict'; angular .module('grafana.directives') .directive('panelMenu', function($compile) { var linkTemplate = '' + '' + '{{ctrl.panel.title | interpolateTemplateVars:this}}' + ' {{ctrl.timeInfo}}' + ''; function createMenuTemplate(ctrl) { var template = '
'; return template; } function getExtendedMenu(ctrl) { return ctrl.getExtendedMenu(); } return { restrict: 'A', link: function($scope, elem) { var $link = $(linkTemplate); var $panelContainer = elem.parents(".panel-container"); var menuScope = null; var ctrl = $scope.ctrl; var timeout = null; var $menu = null; var teather; elem.append($link); 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; 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); } }; }); });