panel_menu.js 6.0 KB

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