panel_ctrl.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, templateUrl) {
  68. this.editorTabs.push({
  69. title: title,
  70. directiveFn: function() {
  71. return {templateUrl: templateUrl};
  72. }
  73. });
  74. }
  75. getMenu() {
  76. let menu = [];
  77. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  78. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  79. menu.push({text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  80. menu.push({text: 'Share', click: 'ctrl.share(); dismiss();'});
  81. return menu;
  82. }
  83. getExtendedMenu() {
  84. return [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  85. }
  86. otherPanelInFullscreenMode() {
  87. return this.dashboard.meta.fullscreen && !this.fullscreen;
  88. }
  89. broadcastRender(arg1?, arg2?) {
  90. this.$scope.$broadcast('render', arg1, arg2);
  91. }
  92. toggleEditorHelp(index) {
  93. if (this.editorHelpIndex === index) {
  94. this.editorHelpIndex = null;
  95. return;
  96. }
  97. this.editorHelpIndex = index;
  98. }
  99. duplicate() {
  100. this.dashboard.duplicatePanel(this.panel, this.row);
  101. }
  102. updateColumnSpan(span) {
  103. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  104. this.$timeout(() => {
  105. this.broadcastRender();
  106. });
  107. }
  108. removePanel() {
  109. this.publishAppEvent('confirm-modal', {
  110. title: 'Are you sure you want to remove this panel?',
  111. icon: 'fa-trash',
  112. yesText: 'Delete',
  113. onConfirm: () => {
  114. this.row.panels = _.without(this.row.panels, this.panel);
  115. }
  116. });
  117. }
  118. editPanelJson() {
  119. this.publishAppEvent('show-json-editor', {
  120. object: this.panel,
  121. updateHandler: this.replacePanel.bind(this)
  122. });
  123. }
  124. replacePanel(newPanel, oldPanel) {
  125. var row = this.row;
  126. var index = _.indexOf(this.row.panels, oldPanel);
  127. this.row.panels.splice(index, 1);
  128. // adding it back needs to be done in next digest
  129. this.$timeout(() => {
  130. newPanel.id = oldPanel.id;
  131. newPanel.span = oldPanel.span;
  132. this.row.panels.splice(index, 0, newPanel);
  133. });
  134. }
  135. }