panelMenu.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. var menu = angular.copy($scope.panelMeta.extendedMenu);
  59. if ($scope.panel.links) {
  60. _.each($scope.panel.links, function(link) {
  61. var info = linkSrv.getPanelLinkAnchorInfo(link, $scope.panel.scopedVars);
  62. menu.push({text: info.title, href: info.href, target: info.target });
  63. });
  64. }
  65. return menu;
  66. }
  67. return {
  68. restrict: 'A',
  69. link: function($scope, elem) {
  70. var $link = $(linkTemplate);
  71. var $panelLinksBtn = $link.find(".panel-links-btn");
  72. var $panelContainer = elem.parents(".panel-container");
  73. var menuScope = null;
  74. var timeout = null;
  75. var $menu = null;
  76. elem.append($link);
  77. $scope.$watchCollection('panel.links', function(newValue) {
  78. var showIcon = (newValue ? newValue.length > 0 : false) && $scope.panel.title !== '';
  79. $panelLinksBtn.toggle(showIcon);
  80. });
  81. function dismiss(time, force) {
  82. clearTimeout(timeout);
  83. timeout = null;
  84. if (time) {
  85. timeout = setTimeout(dismiss, time);
  86. return;
  87. }
  88. // if hovering or draging pospone close
  89. if (force !== true) {
  90. if ($menu.is(':hover') || $scope.dashboard.$$panelDragging) {
  91. dismiss(2200);
  92. return;
  93. }
  94. }
  95. if (menuScope) {
  96. $menu.unbind();
  97. $menu.remove();
  98. menuScope.$destroy();
  99. menuScope = null;
  100. $menu = null;
  101. $panelContainer.removeClass('panel-highlight');
  102. }
  103. }
  104. var showMenu = function(e) {
  105. // if menu item is clicked and menu was just removed from dom ignore this event
  106. if (!$.contains(document, e.target)) {
  107. return;
  108. }
  109. if ($menu) {
  110. dismiss();
  111. return;
  112. }
  113. var menuTemplate;
  114. if ($(e.target).hasClass('fa-external-link')) {
  115. menuTemplate = createExternalLinkMenu($scope);
  116. } else {
  117. menuTemplate = createMenuTemplate($scope);
  118. }
  119. $menu = $(menuTemplate);
  120. $menu.mouseleave(function() {
  121. dismiss(1000);
  122. });
  123. menuScope = $scope.$new();
  124. menuScope.extendedMenu = getExtendedMenu($scope);
  125. menuScope.dismiss = function() {
  126. dismiss(null, true);
  127. };
  128. $(".panel-container").removeClass('panel-highlight');
  129. $panelContainer.toggleClass('panel-highlight');
  130. $('.panel-menu').remove();
  131. elem.append($menu);
  132. $scope.$apply(function() {
  133. $compile($menu.contents())(menuScope);
  134. var menuWidth = $menu[0].offsetWidth;
  135. var menuHeight = $menu[0].offsetHeight;
  136. var windowWidth = $(window).width();
  137. var panelLeftPos = $(elem).offset().left;
  138. var panelWidth = $(elem).width();
  139. var menuLeftPos = (panelWidth / 2) - (menuWidth/2);
  140. var stickingOut = panelLeftPos + menuLeftPos + menuWidth - windowWidth;
  141. if (stickingOut > 0) {
  142. menuLeftPos -= stickingOut + 10;
  143. }
  144. if (panelLeftPos + menuLeftPos < 0) {
  145. menuLeftPos = 0;
  146. }
  147. $menu.css({'left': menuLeftPos, top: -menuHeight});
  148. });
  149. dismiss(2200);
  150. };
  151. elem.click(showMenu);
  152. $compile(elem.contents())($scope);
  153. }
  154. };
  155. });
  156. });