panel_header.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import $ from 'jquery';
  2. import { coreModule } from 'app/core/core';
  3. const template = `
  4. <span class="panel-title">
  5. <span class="icon-gf panel-alert-icon"></span>
  6. <span class="panel-title-text">{{ctrl.panel.title | interpolateTemplateVars:this}}</span>
  7. <span class="panel-menu-container dropdown">
  8. <span class="fa fa-caret-down panel-menu-toggle" data-toggle="dropdown"></span>
  9. <ul class="dropdown-menu dropdown-menu--menu panel-menu" role="menu">
  10. <li>
  11. <a ng-click="ctrl.addDataQuery(datasource);">
  12. <i class="fa fa-cog"></i> Edit <span class="dropdown-menu-item-shortcut">e</span>
  13. </a>
  14. </li>
  15. <li class="dropdown-submenu">
  16. <a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-cube"></i> Actions</a>
  17. <ul class="dropdown-menu panel-menu">
  18. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-flash"></i> Add Annotation</a></li>
  19. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-bullseye"></i> Toggle Legend</a></li>
  20. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-download"></i> Export to CSV</a></li>
  21. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-eye"></i> View JSON</a></li>
  22. </ul>
  23. </li>
  24. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-trash"></i> Remove</a></li>
  25. </ul>
  26. </span>
  27. <span class="panel-time-info" ng-if="ctrl.timeInfo"><i class="fa fa-clock-o"></i> {{ctrl.timeInfo}}</span>
  28. </span>`;
  29. function renderMenuItem(item, ctrl) {
  30. let html = '';
  31. let listItemClass = '';
  32. if (item.divider) {
  33. return '<li class="divider"></li>';
  34. }
  35. if (item.submenu) {
  36. listItemClass = 'dropdown-submenu';
  37. }
  38. html += `<li class="${listItemClass}"><a `;
  39. if (item.click) {
  40. html += ` ng-click="${item.click}"`;
  41. }
  42. if (item.href) {
  43. html += ` href="${item.href}"`;
  44. }
  45. html += `><i class="${item.icon}"></i>`;
  46. html += `<span class="dropdown-item-text">${item.text}</span>`;
  47. if (item.shortcut) {
  48. html += `<span class="dropdown-menu-item-shortcut">${item.shortcut}</span>`;
  49. }
  50. html += `</a>`;
  51. if (item.submenu) {
  52. html += '<ul class="dropdown-menu dropdown-menu--menu panel-menu">';
  53. for (const subitem of item.submenu) {
  54. html += renderMenuItem(subitem, ctrl);
  55. }
  56. html += '</ul>';
  57. }
  58. html += `</li>`;
  59. return html;
  60. }
  61. function createMenuTemplate(ctrl) {
  62. let html = '';
  63. for (const item of ctrl.getMenu()) {
  64. html += renderMenuItem(item, ctrl);
  65. }
  66. return html;
  67. }
  68. /** @ngInject */
  69. function panelHeader($compile) {
  70. return {
  71. restrict: 'E',
  72. template: template,
  73. link: function(scope, elem, attrs) {
  74. const menuElem = elem.find('.panel-menu');
  75. let menuScope;
  76. let isDragged;
  77. elem.click(function(evt) {
  78. const targetClass = evt.target.className;
  79. // remove existing scope
  80. if (menuScope) {
  81. menuScope.$destroy();
  82. }
  83. menuScope = scope.$new();
  84. const menuHtml = createMenuTemplate(scope.ctrl);
  85. menuElem.html(menuHtml);
  86. $compile(menuElem)(menuScope);
  87. if (targetClass.indexOf('panel-title-text') >= 0 || targetClass.indexOf('panel-title') >= 0) {
  88. togglePanelMenu(evt);
  89. }
  90. });
  91. elem.find('.panel-menu-toggle').click(() => {
  92. togglePanelStackPosition();
  93. });
  94. function togglePanelMenu(e) {
  95. if (!isDragged) {
  96. e.stopPropagation();
  97. togglePanelStackPosition();
  98. elem.find('[data-toggle=dropdown]').dropdown('toggle');
  99. }
  100. }
  101. /**
  102. * Hack for adding special class 'dropdown-menu-open' to the panel.
  103. * This class sets z-index for panel and prevents menu overlapping.
  104. */
  105. function togglePanelStackPosition() {
  106. const menuOpenClass = 'dropdown-menu-open';
  107. const panelGridClass = '.react-grid-item.panel';
  108. let panelElem = elem
  109. .find('[data-toggle=dropdown]')
  110. .parentsUntil('.panel')
  111. .parent();
  112. const menuElem = elem.find('[data-toggle=dropdown]').parent();
  113. panelElem = panelElem && panelElem.length ? panelElem[0] : undefined;
  114. if (panelElem) {
  115. panelElem = $(panelElem);
  116. $(panelGridClass).removeClass(menuOpenClass);
  117. const state = !menuElem.hasClass('open');
  118. panelElem.toggleClass(menuOpenClass, state);
  119. }
  120. }
  121. let mouseX, mouseY;
  122. elem.mousedown(e => {
  123. mouseX = e.pageX;
  124. mouseY = e.pageY;
  125. });
  126. elem.mouseup(e => {
  127. if (mouseX === e.pageX && mouseY === e.pageY) {
  128. isDragged = false;
  129. } else {
  130. isDragged = true;
  131. }
  132. });
  133. },
  134. };
  135. }
  136. coreModule.directive('panelHeader', panelHeader);