panelMenu.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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}}</span>' +
  14. '<span class="panel-links-icon"></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 createMenuTemplate($scope) {
  18. var template = '<div class="panel-menu small">';
  19. template += '<div class="panel-menu-inner">';
  20. template += '<div class="panel-menu-row">';
  21. template += '<a class="panel-menu-icon pull-left" ng-click="updateColumnSpan(-1)"><i class="fa fa-minus"></i></a>';
  22. template += '<a class="panel-menu-icon pull-left" ng-click="updateColumnSpan(1)"><i class="fa fa-plus"></i></a>';
  23. template += '<a class="panel-menu-icon pull-right" ng-click="removePanel(panel)"><i class="fa fa-remove"></i></a>';
  24. template += '<div class="clearfix"></div>';
  25. template += '</div>';
  26. template += '<div class="panel-menu-row">';
  27. template += '<a class="panel-menu-link" gf-dropdown="extendedMenu"><i class="fa fa-bars"></i></a>';
  28. _.each($scope.panelMeta.menu, function(item) {
  29. template += '<a class="panel-menu-link" ';
  30. if (item.click) { template += ' ng-click="' + item.click + '"'; }
  31. if (item.editorLink) { template += ' dash-editor-link="' + item.editorLink + '"'; }
  32. template += '>';
  33. template += item.text + '</a>';
  34. });
  35. template += '</div>';
  36. template += '</div>';
  37. template += '</div>';
  38. return template;
  39. }
  40. function getExtendedMenu($scope) {
  41. var menu = angular.copy($scope.panelMeta.extendedMenu);
  42. if ($scope.panel.links) {
  43. _.each($scope.panel.links, function(link) {
  44. var info = linkSrv.getPanelLinkAnchorInfo(link);
  45. menu.push({text: info.title, href: info.href, target: info.target });
  46. });
  47. }
  48. return menu;
  49. }
  50. return {
  51. restrict: 'A',
  52. link: function($scope, elem) {
  53. var $link = $(linkTemplate);
  54. var $panelContainer = elem.parents(".panel-container");
  55. var menuWidth = $scope.panelMeta.menu.length === 4 ? 236 : 191;
  56. var menuScope = null;
  57. var timeout = null;
  58. var $menu = null;
  59. elem.append($link);
  60. $scope.$watchCollection('panel.links', function(newValue) {
  61. var showIcon = (newValue ? newValue.length > 0 : false) && $scope.panel.title !== '';
  62. $link.toggleClass('has-panel-links', showIcon);
  63. });
  64. function dismiss(time, force) {
  65. clearTimeout(timeout);
  66. timeout = null;
  67. if (time) {
  68. timeout = setTimeout(dismiss, time);
  69. return;
  70. }
  71. // if hovering or draging pospone close
  72. if (force !== true) {
  73. if ($menu.is(':hover') || $scope.dashboard.$$panelDragging) {
  74. dismiss(2200);
  75. return;
  76. }
  77. }
  78. if (menuScope) {
  79. $menu.unbind();
  80. $menu.remove();
  81. menuScope.$destroy();
  82. menuScope = null;
  83. $menu = null;
  84. $panelContainer.removeClass('panel-highlight');
  85. }
  86. }
  87. var showMenu = function(e) {
  88. // if menu item is clicked and menu was just removed from dom ignore this event
  89. if (!$.contains(document, e.target)) {
  90. return;
  91. }
  92. if ($menu) {
  93. dismiss();
  94. return;
  95. }
  96. var windowWidth = $(window).width();
  97. var panelLeftPos = $(elem).offset().left;
  98. var panelWidth = $(elem).width();
  99. var menuLeftPos = (panelWidth / 2) - (menuWidth/2);
  100. var stickingOut = panelLeftPos + menuLeftPos + menuWidth - windowWidth;
  101. if (stickingOut > 0) {
  102. menuLeftPos -= stickingOut + 10;
  103. }
  104. if (panelLeftPos + menuLeftPos < 0) {
  105. menuLeftPos = 0;
  106. }
  107. var menuTemplate = createMenuTemplate($scope);
  108. $menu = $(menuTemplate);
  109. $menu.css('left', menuLeftPos);
  110. $menu.mouseleave(function() {
  111. dismiss(1000);
  112. });
  113. menuScope = $scope.$new();
  114. menuScope.extendedMenu = getExtendedMenu($scope);
  115. menuScope.dismiss = function() {
  116. dismiss(null, true);
  117. };
  118. $('.panel-menu').remove();
  119. elem.append($menu);
  120. $scope.$apply(function() {
  121. $compile($menu.contents())(menuScope);
  122. });
  123. $(".panel-container").removeClass('panel-highlight');
  124. $panelContainer.toggleClass('panel-highlight');
  125. dismiss(2200);
  126. };
  127. elem.click(showMenu);
  128. $compile(elem.contents())($scope);
  129. }
  130. };
  131. });
  132. });