panel_header.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ///<reference path="../../headers/common.d.ts" />
  2. import $ from 'jquery';
  3. import angular from 'angular';
  4. import {coreModule} from 'app/core/core';
  5. var template = `
  6. <span class="panel-title">
  7. <span class="icon-gf panel-alert-icon"></span>
  8. <span class="panel-title-text">{{ctrl.panel.title | interpolateTemplateVars:this}}</span>
  9. <span class="panel-menu-container dropdown">
  10. <span class="fa fa-caret-down panel-menu-toggle" data-toggle="dropdown"></span>
  11. <ul class="dropdown-menu dropdown-menu--menu panel-menu" role="menu">
  12. <li>
  13. <a ng-click="ctrl.addDataQuery(datasource);">
  14. <i class="fa fa-cog"></i> Edit <span class="dropdown-menu-item-shortcut">e</span>
  15. </a>
  16. </li>
  17. <li class="dropdown-submenu">
  18. <a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-cube"></i> Actions</a>
  19. <ul class="dropdown-menu panel-menu">
  20. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-flash"></i> Add Annotation</a></li>
  21. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-bullseye"></i> Toggle Legend</a></li>
  22. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-download"></i> Export to CSV</a></li>
  23. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-eye"></i> View JSON</a></li>
  24. </ul>
  25. </li>
  26. <li><a ng-click="ctrl.addDataQuery(datasource);"><i class="fa fa-trash"></i> Remove</a></li>
  27. </ul>
  28. </span>
  29. <span class="panel-time-info" ng-show="ctrl.timeInfo"><i class="fa fa-clock-o"></i> {{ctrl.timeInfo}}</span>
  30. </span>`;
  31. function renderMenuItem(item, ctrl) {
  32. let html = '';
  33. let listItemClass = '';
  34. if (item.divider) {
  35. return '<li class="divider"></li>';
  36. }
  37. if (item.submenu) {
  38. listItemClass = 'dropdown-submenu';
  39. }
  40. html += `<li class="${listItemClass}"><a `;
  41. if (item.click) { html += ` ng-click="${item.click}"`; }
  42. if (item.href) { html += ` href="${item.href}"`; }
  43. html += `><i class="${item.icon}"></i>`;
  44. html += `<span class="dropdown-item-text">${item.text}</span>`;
  45. if (item.shortcut) {
  46. html += `<span class="dropdown-menu-item-shortcut">${item.shortcut}</span>`;
  47. }
  48. html += `</a>`;
  49. if (item.submenu) {
  50. html += '<ul class="dropdown-menu dropdown-menu--menu panel-menu">';
  51. for (let subitem of item.submenu) {
  52. html += renderMenuItem(subitem, ctrl);
  53. }
  54. html += '</ul>';
  55. }
  56. html += `</li>`;
  57. return html;
  58. }
  59. function createMenuTemplate(ctrl) {
  60. let html = '';
  61. for (let item of ctrl.getMenu()) {
  62. html += renderMenuItem(item, ctrl);
  63. }
  64. return html;
  65. }
  66. /** @ngInject **/
  67. function panelHeader($compile) {
  68. return {
  69. restrict: 'E',
  70. template: template,
  71. link: function(scope, elem, attrs) {
  72. let menuElem = elem.find('.panel-menu');
  73. let menuScope;
  74. elem.click(function(evt) {
  75. const targetClass = evt.target.className;
  76. // remove existing scope
  77. if (menuScope) {
  78. menuScope.$destroy();
  79. }
  80. menuScope = scope.$new();
  81. let menuHtml = createMenuTemplate(scope.ctrl);
  82. menuElem.html(menuHtml);
  83. $compile(menuElem)(menuScope);
  84. if (targetClass === 'panel-title-text' || targetClass === 'panel-title') {
  85. evt.stopPropagation();
  86. elem.find('[data-toggle=dropdown]').dropdown('toggle');
  87. }
  88. });
  89. }
  90. };
  91. }
  92. coreModule.directive('panelHeader', panelHeader);