panel_ctrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (!this.editModeInitiated) {
  55. this.editorTabs = [];
  56. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  57. this.initEditMode();
  58. }
  59. this.changeView(true, true);
  60. }
  61. exitFullscreen() {
  62. this.changeView(false, false);
  63. }
  64. initEditMode() {
  65. return;
  66. }
  67. addEditorTab(title, directiveFn, index?) {
  68. var editorTab = {title, directiveFn};
  69. if (_.isString(directiveFn)) {
  70. editorTab.directiveFn = function() {
  71. return {templateUrl: directiveFn};
  72. };
  73. }
  74. if (index) {
  75. this.editorTabs.splice(index, 0, editorTab);
  76. } else {
  77. this.editorTabs.push(editorTab);
  78. }
  79. }
  80. getMenu() {
  81. let menu = [];
  82. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  83. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  84. menu.push({text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  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: './app/features/dashboard/partials/shareModal.html',
  146. scope: shareScope
  147. });
  148. }
  149. }