panel_menu.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'lodash',
  5. ],
  6. function (angular, $, _) {
  7. 'use strict';
  8. angular
  9. .module('grafana.directives')
  10. .directive('panelMenu', function($compile, linkSrv) {
  11. var linkTemplate =
  12. '<span class="panel-title drag-handle pointer">' +
  13. '<span class="panel-title-text drag-handle">{{panel.title | interpolateTemplateVars:this}}</span>' +
  14. '<span class="panel-links-btn"><i class="fa fa-external-link"></i></span>' +
  15. '<span class="panel-time-info" ng-show="panelMeta.timeInfo"><i class="fa fa-clock-o"></i> {{panelMeta.timeInfo}}</span>' +
  16. '</span>';
  17. function createExternalLinkMenu($scope) {
  18. var template = '<div class="panel-menu small">';
  19. template += '<div class="panel-menu-row">';
  20. if ($scope.panel.links) {
  21. _.each($scope.panel.links, function(link) {
  22. var info = linkSrv.getPanelLinkAnchorInfo(link, $scope.panel.scopedVars);
  23. template += '<a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a>';
  24. });
  25. }
  26. return template;
  27. }
  28. function createMenuTemplate($scope) {
  29. var template = '<div class="panel-menu small">';
  30. if ($scope.dashboardMeta.canEdit) {
  31. template += '<div class="panel-menu-inner">';
  32. template += '<div class="panel-menu-row">';
  33. template += '<a class="panel-menu-icon pull-left" ng-click="updateColumnSpan(-1)"><i class="fa fa-minus"></i></a>';
  34. template += '<a class="panel-menu-icon pull-left" ng-click="updateColumnSpan(1)"><i class="fa fa-plus"></i></a>';
  35. template += '<a class="panel-menu-icon pull-right" ng-click="removePanel(panel)"><i class="fa fa-remove"></i></a>';
  36. template += '<div class="clearfix"></div>';
  37. template += '</div>';
  38. }
  39. template += '<div class="panel-menu-row">';
  40. template += '<a class="panel-menu-link" gf-dropdown="extendedMenu"><i class="fa fa-bars"></i></a>';
  41. _.each($scope.panelMeta.menu, function(item) {
  42. // skip edit actions if not editor
  43. if (item.role === 'Editor' && !$scope.dashboardMeta.canEdit) {
  44. return;
  45. }
  46. template += '<a class="panel-menu-link" ';
  47. if (item.click) { template += ' ng-click="' + item.click + '"'; }
  48. if (item.editorLink) { template += ' dash-editor-link="' + item.editorLink + '"'; }
  49. template += '>';
  50. template += item.text + '</a>';
  51. });
  52. template += '</div>';
  53. template += '</div>';
  54. template += '</div>';
  55. return template;
  56. }
  57. function getExtendedMenu($scope) {
  58. return angular.copy($scope.panelMeta.extendedMenu);
  59. }
  60. return {
  61. restrict: 'A',
  62. link: function($scope, elem) {
  63. var $link = $(linkTemplate);
  64. var $panelLinksBtn = $link.find(".panel-links-btn");
  65. var $panelContainer = elem.parents(".panel-container");
  66. var menuScope = null;
  67. var timeout = null;
  68. var $menu = null;
  69. elem.append($link);
  70. $scope.$watchCollection('panel.links', function(newValue) {
  71. var showIcon = (newValue ? newValue.length > 0 : false) && $scope.panel.title !== '';
  72. $panelLinksBtn.toggle(showIcon);
  73. });
  74. function dismiss(time, force) {
  75. clearTimeout(timeout);
  76. timeout = null;
  77. if (time) {
  78. timeout = setTimeout(dismiss, time);
  79. return;
  80. }
  81. // if hovering or draging pospone close
  82. if (force !== true) {
  83. if ($menu.is(':hover') || $scope.dashboard.$$panelDragging) {
  84. dismiss(2200);
  85. return;
  86. }
  87. }
  88. if (menuScope) {
  89. $menu.unbind();
  90. $menu.remove();
  91. menuScope.$destroy();
  92. menuScope = null;
  93. $menu = null;
  94. $panelContainer.removeClass('panel-highlight');
  95. }
  96. }
  97. var showMenu = function(e) {
  98. // if menu item is clicked and menu was just removed from dom ignore this event
  99. if (!$.contains(document, e.target)) {
  100. return;
  101. }
  102. if ($menu) {
  103. dismiss();
  104. return;
  105. }
  106. var menuTemplate;
  107. if ($(e.target).hasClass('fa-external-link')) {
  108. menuTemplate = createExternalLinkMenu($scope);
  109. } else {
  110. menuTemplate = createMenuTemplate($scope);
  111. }
  112. $menu = $(menuTemplate);
  113. $menu.mouseleave(function() {
  114. dismiss(1000);
  115. });
  116. menuScope = $scope.$new();
  117. menuScope.extendedMenu = getExtendedMenu($scope);
  118. menuScope.dismiss = function() {
  119. dismiss(null, true);
  120. };
  121. $(".panel-container").removeClass('panel-highlight');
  122. $panelContainer.toggleClass('panel-highlight');
  123. $('.panel-menu').remove();
  124. elem.append($menu);
  125. $scope.$apply(function() {
  126. $compile($menu.contents())(menuScope);
  127. var menuWidth = $menu[0].offsetWidth;
  128. var menuHeight = $menu[0].offsetHeight;
  129. var windowWidth = $(window).width();
  130. var panelLeftPos = $(elem).offset().left;
  131. var panelWidth = $(elem).width();
  132. var menuLeftPos = (panelWidth / 2) - (menuWidth/2);
  133. var stickingOut = panelLeftPos + menuLeftPos + menuWidth - windowWidth;
  134. if (stickingOut > 0) {
  135. menuLeftPos -= stickingOut + 10;
  136. }
  137. if (panelLeftPos + menuLeftPos < 0) {
  138. menuLeftPos = 0;
  139. }
  140. $menu.css({'left': menuLeftPos, top: -menuHeight});
  141. });
  142. dismiss(2200);
  143. };
  144. elem.click(showMenu);
  145. $compile(elem.contents())($scope);
  146. }
  147. };
  148. });
  149. });