panel_ctrl.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. menu.push({text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  83. menu.push({text: 'Share', click: 'ctrl.sharePanel(); dismiss();'});
  84. return menu;
  85. }
  86. getExtendedMenu() {
  87. return [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  88. }
  89. otherPanelInFullscreenMode() {
  90. return this.dashboard.meta.fullscreen && !this.fullscreen;
  91. }
  92. broadcastRender(arg1?, arg2?) {
  93. this.$scope.$broadcast('render', arg1, arg2);
  94. }
  95. toggleEditorHelp(index) {
  96. if (this.editorHelpIndex === index) {
  97. this.editorHelpIndex = null;
  98. return;
  99. }
  100. this.editorHelpIndex = index;
  101. }
  102. duplicate() {
  103. this.dashboard.duplicatePanel(this.panel, this.row);
  104. }
  105. updateColumnSpan(span) {
  106. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  107. this.$timeout(() => {
  108. this.broadcastRender();
  109. });
  110. }
  111. removePanel() {
  112. this.publishAppEvent('confirm-modal', {
  113. title: 'Are you sure you want to remove this panel?',
  114. icon: 'fa-trash',
  115. yesText: 'Delete',
  116. onConfirm: () => {
  117. this.row.panels = _.without(this.row.panels, this.panel);
  118. }
  119. });
  120. }
  121. editPanelJson() {
  122. this.publishAppEvent('show-json-editor', {
  123. object: this.panel,
  124. updateHandler: this.replacePanel.bind(this)
  125. });
  126. }
  127. replacePanel(newPanel, oldPanel) {
  128. var row = this.row;
  129. var index = _.indexOf(this.row.panels, oldPanel);
  130. this.row.panels.splice(index, 1);
  131. // adding it back needs to be done in next digest
  132. this.$timeout(() => {
  133. newPanel.id = oldPanel.id;
  134. newPanel.span = oldPanel.span;
  135. this.row.panels.splice(index, 0, newPanel);
  136. });
  137. }
  138. sharePanel() {
  139. var shareScope = this.$scope.$new();
  140. shareScope.panel = this.panel;
  141. shareScope.dashboard = this.dashboard;
  142. this.publishAppEvent('show-modal', {
  143. src: 'public/app/features/dashboard/partials/shareModal.html',
  144. scope: shareScope
  145. });
  146. }
  147. openInspector() {
  148. var modalScope = this.$scope.$new();
  149. modalScope.panel = this.panel;
  150. modalScope.dashboard = this.dashboard;
  151. modalScope.inspector = angular.copy(this.inspector);
  152. this.publishAppEvent('show-modal', {
  153. src: 'public/app/partials/inspector.html',
  154. scope: modalScope
  155. });
  156. }
  157. }