panel_ctrl.ts 4.5 KB

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