panel_menu.js 5.8 KB

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