panel_ctrl.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { auto } from 'angular';
  2. import { isString } from 'lodash';
  3. import {
  4. AppEvent,
  5. PanelEvents,
  6. PanelPluginMeta,
  7. AngularPanelMenuItem,
  8. EventBusExtended,
  9. EventBusSrv,
  10. } from '@grafana/data';
  11. import { AngularLocationWrapper } from 'app/angular/AngularLocationWrapper';
  12. import config from 'app/core/config';
  13. import { profiler } from 'app/core/core';
  14. import { DashboardModel } from '../../features/dashboard/state';
  15. export class PanelCtrl {
  16. panel: any;
  17. error: any;
  18. declare dashboard: DashboardModel;
  19. pluginName = '';
  20. pluginId = '';
  21. editorTabs: any;
  22. $scope: any;
  23. $injector: auto.IInjectorService;
  24. $timeout: any;
  25. editModeInitiated = false;
  26. declare height: number;
  27. declare width: number;
  28. containerHeight: any;
  29. events: EventBusExtended;
  30. loading = false;
  31. timing: any;
  32. $location: AngularLocationWrapper;
  33. constructor($scope: any, $injector: auto.IInjectorService) {
  34. this.panel = this.panel ?? $scope.$parent.panel;
  35. this.dashboard = this.dashboard ?? $scope.$parent.dashboard;
  36. this.$injector = $injector;
  37. this.$scope = $scope;
  38. this.$timeout = $injector.get('$timeout');
  39. this.editorTabs = [];
  40. this.$location = new AngularLocationWrapper();
  41. this.events = new EventBusSrv();
  42. this.timing = {}; // not used but here to not break plugins
  43. const plugin = config.panels[this.panel.type];
  44. if (plugin) {
  45. this.pluginId = plugin.id;
  46. this.pluginName = plugin.name;
  47. }
  48. $scope.$on(PanelEvents.componentDidMount.name, () => this.panelDidMount());
  49. }
  50. panelDidMount() {
  51. this.events.emit(PanelEvents.componentDidMount);
  52. this.events.emit(PanelEvents.initialized);
  53. this.dashboard.panelInitialized(this.panel);
  54. }
  55. renderingCompleted() {
  56. profiler.renderingCompleted();
  57. }
  58. refresh() {
  59. this.panel.refresh();
  60. }
  61. publishAppEvent<T>(event: AppEvent<T>, payload?: T) {
  62. this.$scope.$root.appEvent(event, payload);
  63. }
  64. initEditMode() {
  65. if (!this.editModeInitiated) {
  66. this.editModeInitiated = true;
  67. this.events.emit(PanelEvents.editModeInitialized);
  68. }
  69. }
  70. addEditorTab(title: string, directiveFn: any, index?: number, icon?: any) {
  71. const editorTab = { title, directiveFn, icon };
  72. if (isString(directiveFn)) {
  73. editorTab.directiveFn = () => {
  74. return { templateUrl: directiveFn };
  75. };
  76. }
  77. if (index) {
  78. this.editorTabs.splice(index, 0, editorTab);
  79. } else {
  80. this.editorTabs.push(editorTab);
  81. }
  82. }
  83. getExtendedMenu() {
  84. const menu: AngularPanelMenuItem[] = [];
  85. this.events.emit(PanelEvents.initPanelActions, menu);
  86. return menu;
  87. }
  88. // Override in sub-class to add items before extended menu
  89. async getAdditionalMenuItems(): Promise<any[]> {
  90. return [];
  91. }
  92. otherPanelInFullscreenMode() {
  93. return this.dashboard.otherPanelInFullscreen(this.panel);
  94. }
  95. render(payload?: any) {
  96. this.events.emit(PanelEvents.render, payload);
  97. }
  98. // overriden from react
  99. onPluginTypeChange = (plugin: PanelPluginMeta) => {};
  100. }