panelMenu.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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) {
  11. var linkTemplate = '<span class="panel-title drag-handle pointer">{{panel.title | interpolateTemplateVars}}</span>';
  12. function createMenuTemplate($scope) {
  13. var template = '<div class="panel-menu small">';
  14. template += '<div class="panel-menu-inner">';
  15. template += '<div class="panel-menu-row">';
  16. template += '<a class="panel-menu-icon pull-left" ng-click="updateColumnSpan(-1)"><i class="icon-minus"></i></a>';
  17. template += '<a class="panel-menu-icon pull-left" ng-click="updateColumnSpan(1)"><i class="icon-plus"></i></a>';
  18. template += '<a class="panel-menu-icon pull-right" ng-click="remove_panel_from_row(row, panel)"><i class="icon-remove"></i></a>';
  19. template += '<div class="clearfix"></div>';
  20. template += '</div>';
  21. template += '<div class="panel-menu-row">';
  22. template += '<a class="panel-menu-link" bs-dropdown="panelMeta.extendedMenu"><i class="icon-th-list"></i></a>';
  23. _.each($scope.panelMeta.menu, function(item) {
  24. template += '<a class="panel-menu-link" ';
  25. if (item.click) { template += ' ng-click="' + item.click + '"'; }
  26. if (item.editorLink) { template += ' dash-editor-link="' + item.editorLink + '"'; }
  27. template += '>';
  28. template += item.text + '</a>';
  29. });
  30. template += '</div>';
  31. template += '</div>';
  32. template += '</div>';
  33. return template;
  34. }
  35. return {
  36. restrict: 'A',
  37. link: function($scope, elem) {
  38. var $link = $(linkTemplate);
  39. var $panelContainer = elem.parents(".panel-container");
  40. var menuWidth = $scope.panelMeta.menu.length === 5 ? 246 : 201;
  41. var menuScope = null;
  42. var timeout = null;
  43. var $menu = null;
  44. elem.append($link);
  45. function dismiss(time) {
  46. clearTimeout(timeout);
  47. timeout = null;
  48. if (time) {
  49. timeout = setTimeout(dismiss, time);
  50. return;
  51. }
  52. // if hovering or draging pospone close
  53. if ($menu.is(':hover') || $scope.dashboard.$$panelDragging) {
  54. dismiss(2500);
  55. return;
  56. }
  57. if (menuScope) {
  58. $menu.unbind();
  59. $menu.remove();
  60. menuScope.$destroy();
  61. menuScope = null;
  62. $menu = null;
  63. $panelContainer.removeClass('panel-highlight');
  64. }
  65. }
  66. var showMenu = function() {
  67. if ($menu) {
  68. dismiss();
  69. return;
  70. }
  71. var windowWidth = $(window).width();
  72. var panelLeftPos = $(elem).offset().left;
  73. var panelWidth = $(elem).width();
  74. var menuLeftPos = (panelWidth / 2) - (menuWidth/2);
  75. var stickingOut = panelLeftPos + menuLeftPos + menuWidth - windowWidth;
  76. if (stickingOut > 0) {
  77. menuLeftPos -= stickingOut + 10;
  78. }
  79. if (panelLeftPos + menuLeftPos < 0) {
  80. menuLeftPos = 0;
  81. }
  82. var menuTemplate = createMenuTemplate($scope);
  83. $menu = $(menuTemplate);
  84. $menu.css('left', menuLeftPos);
  85. $menu.mouseleave(function() {
  86. //dismiss(1000);
  87. });
  88. menuScope = $scope.$new();
  89. $('.panel-menu').remove();
  90. elem.append($menu);
  91. $scope.$apply(function() {
  92. $compile($menu.contents())(menuScope);
  93. });
  94. $(".panel-container").removeClass('panel-highlight');
  95. $panelContainer.toggleClass('panel-highlight');
  96. //dismiss(2500);
  97. };
  98. if ($scope.panelMeta.titlePos && $scope.panel.title) {
  99. elem.css('text-align', 'left');
  100. $link.css('padding-left', '10px');
  101. }
  102. elem.click(showMenu);
  103. $compile(elem.contents())($scope);
  104. }
  105. };
  106. });
  107. });