panel_ctrl.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. export class PanelCtrl {
  5. panel: any;
  6. row: any;
  7. dashboard: any;
  8. editorTabIndex: number;
  9. pluginName: string;
  10. pluginId: string;
  11. icon: string;
  12. editorTabs: any;
  13. $scope: any;
  14. $injector: any;
  15. $timeout: any;
  16. fullscreen: boolean;
  17. inspector: any;
  18. editModeInitiated: boolean;
  19. editorHelpIndex: number;
  20. constructor($scope, $injector) {
  21. this.$injector = $injector;
  22. this.$scope = $scope;
  23. this.$timeout = $injector.get('$timeout');
  24. this.editorTabIndex = 0;
  25. var plugin = config.panels[this.panel.type];
  26. if (plugin) {
  27. this.pluginId = plugin.id;
  28. this.pluginName = plugin.name;
  29. }
  30. $scope.$on("refresh", () => this.refresh());
  31. }
  32. init() {
  33. this.publishAppEvent('panel-instantiated', {scope: this.$scope});
  34. this.refresh();
  35. }
  36. renderingCompleted() {
  37. this.$scope.$root.performance.panelsRendered++;
  38. }
  39. refresh() {
  40. return;
  41. }
  42. publishAppEvent(evtName, evt) {
  43. this.$scope.$root.appEvent(evtName, evt);
  44. }
  45. changeView(fullscreen, edit) {
  46. this.publishAppEvent('panel-change-view', {
  47. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  48. });
  49. }
  50. viewPanel() {
  51. this.changeView(true, false);
  52. }
  53. editPanel() {
  54. this.changeView(true, true);
  55. }
  56. exitFullscreen() {
  57. this.changeView(false, false);
  58. }
  59. initEditMode() {
  60. this.editorTabs = [];
  61. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  62. this.editModeInitiated = true;
  63. }
  64. addEditorTab(title, directiveFn, index?) {
  65. var editorTab = {title, directiveFn};
  66. if (_.isString(directiveFn)) {
  67. editorTab.directiveFn = function() {
  68. return {templateUrl: directiveFn};
  69. };
  70. }
  71. if (index) {
  72. this.editorTabs.splice(index, 0, editorTab);
  73. } else {
  74. this.editorTabs.push(editorTab);
  75. }
  76. }
  77. getMenu() {
  78. let menu = [];
  79. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  80. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  81. menu.push({text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  82. menu.push({text: 'Share', click: 'ctrl.sharePanel(); dismiss();'});
  83. return menu;
  84. }
  85. getExtendedMenu() {
  86. return [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  87. }
  88. otherPanelInFullscreenMode() {
  89. return this.dashboard.meta.fullscreen && !this.fullscreen;
  90. }
  91. broadcastRender(arg1?, arg2?) {
  92. this.$scope.$broadcast('render', arg1, arg2);
  93. }
  94. toggleEditorHelp(index) {
  95. if (this.editorHelpIndex === index) {
  96. this.editorHelpIndex = null;
  97. return;
  98. }
  99. this.editorHelpIndex = index;
  100. }
  101. duplicate() {
  102. this.dashboard.duplicatePanel(this.panel, this.row);
  103. }
  104. updateColumnSpan(span) {
  105. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  106. this.$timeout(() => {
  107. this.broadcastRender();
  108. });
  109. }
  110. removePanel() {
  111. this.publishAppEvent('confirm-modal', {
  112. title: 'Are you sure you want to remove this panel?',
  113. icon: 'fa-trash',
  114. yesText: 'Delete',
  115. onConfirm: () => {
  116. this.row.panels = _.without(this.row.panels, this.panel);
  117. }
  118. });
  119. }
  120. editPanelJson() {
  121. this.publishAppEvent('show-json-editor', {
  122. object: this.panel,
  123. updateHandler: this.replacePanel.bind(this)
  124. });
  125. }
  126. replacePanel(newPanel, oldPanel) {
  127. var row = this.row;
  128. var index = _.indexOf(this.row.panels, oldPanel);
  129. this.row.panels.splice(index, 1);
  130. // adding it back needs to be done in next digest
  131. this.$timeout(() => {
  132. newPanel.id = oldPanel.id;
  133. newPanel.span = oldPanel.span;
  134. this.row.panels.splice(index, 0, newPanel);
  135. });
  136. }
  137. sharePanel() {
  138. var shareScope = this.$scope.$new();
  139. shareScope.panel = this.panel;
  140. shareScope.dashboard = this.dashboard;
  141. this.publishAppEvent('show-modal', {
  142. src: 'public/app/features/dashboard/partials/shareModal.html',
  143. scope: shareScope
  144. });
  145. }
  146. }