panel_ctrl.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. import angular from 'angular';
  5. import $ from 'jquery';
  6. import {profiler} from 'app/core/profiler';
  7. import Remarkable from 'remarkable';
  8. import {CELL_HEIGHT, CELL_VMARGIN} from '../dashboard/model';
  9. const TITLE_HEIGHT = 25;
  10. const EMPTY_TITLE_HEIGHT = 9;
  11. const PANEL_PADDING = 5;
  12. const PANEL_BORDER = 2;
  13. import {Emitter} from 'app/core/core';
  14. export class PanelCtrl {
  15. panel: any;
  16. error: any;
  17. dashboard: any;
  18. editorTabIndex: number;
  19. pluginName: string;
  20. pluginId: string;
  21. editorTabs: any;
  22. $scope: any;
  23. $injector: any;
  24. $timeout: any;
  25. fullscreen: boolean;
  26. inspector: any;
  27. editModeInitiated: boolean;
  28. editMode: any;
  29. height: any;
  30. containerHeight: any;
  31. events: Emitter;
  32. timing: any;
  33. constructor($scope, $injector) {
  34. this.$injector = $injector;
  35. this.$scope = $scope;
  36. this.$timeout = $injector.get('$timeout');
  37. this.editorTabIndex = 0;
  38. this.events = new Emitter();
  39. this.timing = {};
  40. var plugin = config.panels[this.panel.type];
  41. if (plugin) {
  42. this.pluginId = plugin.id;
  43. this.pluginName = plugin.name;
  44. }
  45. $scope.$on("refresh", () => this.refresh());
  46. $scope.$on("render", () => this.render());
  47. $scope.$on("$destroy", () => {
  48. this.events.emit('panel-teardown');
  49. this.events.removeAllListeners();
  50. });
  51. // we should do something interesting
  52. // with newly added panels
  53. if (this.panel.isNew) {
  54. delete this.panel.isNew;
  55. }
  56. }
  57. init() {
  58. this.calculatePanelHeight();
  59. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  60. this.events.emit('panel-initialized');
  61. }
  62. renderingCompleted() {
  63. profiler.renderingCompleted(this.panel.id, this.timing);
  64. }
  65. refresh() {
  66. this.events.emit('refresh', null);
  67. }
  68. publishAppEvent(evtName, evt) {
  69. this.$scope.$root.appEvent(evtName, evt);
  70. }
  71. changeView(fullscreen, edit) {
  72. this.publishAppEvent('panel-change-view', {
  73. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  74. });
  75. }
  76. viewPanel() {
  77. this.changeView(true, false);
  78. }
  79. editPanel() {
  80. this.changeView(true, true);
  81. }
  82. exitFullscreen() {
  83. this.changeView(false, false);
  84. }
  85. initEditMode() {
  86. this.editorTabs = [];
  87. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  88. this.editModeInitiated = true;
  89. this.events.emit('init-edit-mode', null);
  90. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  91. if (urlTab) {
  92. this.editorTabs.forEach((tab, i) => {
  93. if (tab.title.toLowerCase() === urlTab) {
  94. this.editorTabIndex = i;
  95. }
  96. });
  97. }
  98. }
  99. changeTab(newIndex) {
  100. this.editorTabIndex = newIndex;
  101. var route = this.$injector.get('$route');
  102. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  103. route.updateParams();
  104. }
  105. addEditorTab(title, directiveFn, index?) {
  106. var editorTab = {title, directiveFn};
  107. if (_.isString(directiveFn)) {
  108. editorTab.directiveFn = function() {
  109. return {templateUrl: directiveFn};
  110. };
  111. }
  112. if (index) {
  113. this.editorTabs.splice(index, 0, editorTab);
  114. } else {
  115. this.editorTabs.push(editorTab);
  116. }
  117. }
  118. getMenu() {
  119. let menu = [];
  120. menu.push({text: 'View', click: 'ctrl.viewPanel();', icon: "fa fa-fw fa-eye", shortcut: "v"});
  121. menu.push({text: 'Edit', click: 'ctrl.editPanel();', role: 'Editor', icon: "fa fa-fw fa-edit", shortcut: "e"});
  122. menu.push({text: 'Share', click: 'ctrl.sharePanel();', icon: "fa fa-fw fa-share", shortcut: "p s"});
  123. if (!this.fullscreen) {
  124. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor', icon: "fa fa-fw fa-copy" });
  125. }
  126. menu.push({divider: true});
  127. let extendedMenu = this.getExtendedMenu();
  128. menu.push({text: 'More ...', click: 'ctrl.removePanel();', icon: "fa fa-fw fa-cube", submenu: extendedMenu});
  129. menu.push({divider: true, role: 'Editor'});
  130. menu.push({text: 'Remove', click: 'ctrl.removePanel();', role: 'Editor', icon: "fa fa-fw fa-trash", shortcut: "p r"});
  131. return menu;
  132. }
  133. getExtendedMenu() {
  134. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  135. this.events.emit('init-panel-actions', actions);
  136. return actions;
  137. }
  138. otherPanelInFullscreenMode() {
  139. return this.dashboard.meta.fullscreen && !this.fullscreen;
  140. }
  141. calculatePanelHeight() {
  142. if (this.fullscreen) {
  143. var docHeight = $(window).height();
  144. var editHeight = Math.floor(docHeight * 0.4);
  145. var fullscreenHeight = Math.floor(docHeight * 0.8);
  146. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  147. } else {
  148. this.containerHeight = this.panel.height * CELL_HEIGHT + ((this.panel.height-1) * CELL_VMARGIN);
  149. }
  150. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  151. }
  152. render(payload?) {
  153. // ignore if other panel is in fullscreen mode
  154. if (this.otherPanelInFullscreenMode()) {
  155. return;
  156. }
  157. this.calculatePanelHeight();
  158. this.timing.renderStart = new Date().getTime();
  159. this.events.emit('render', payload);
  160. }
  161. duplicate() {
  162. this.dashboard.duplicatePanel(this.panel);
  163. this.$timeout(() => {
  164. this.$scope.$root.$broadcast('render');
  165. });
  166. }
  167. removePanel() {
  168. this.dashboard.removePanel(this.panel);
  169. }
  170. editPanelJson() {
  171. this.publishAppEvent('show-json-editor', {
  172. object: this.panel,
  173. updateHandler: this.replacePanel.bind(this)
  174. });
  175. }
  176. replacePanel(newPanel, oldPanel) {
  177. var index = _.indexOf(this.dashboard.panels, oldPanel);
  178. this.dashboard.panels.splice(index, 1);
  179. // adding it back needs to be done in next digest
  180. this.$timeout(() => {
  181. newPanel.id = oldPanel.id;
  182. newPanel.width = oldPanel.width;
  183. this.dashboard.panels.splice(index, 0, newPanel);
  184. });
  185. }
  186. sharePanel() {
  187. var shareScope = this.$scope.$new();
  188. shareScope.panel = this.panel;
  189. shareScope.dashboard = this.dashboard;
  190. this.publishAppEvent('show-modal', {
  191. src: 'public/app/features/dashboard/partials/shareModal.html',
  192. scope: shareScope
  193. });
  194. }
  195. getInfoMode() {
  196. if (this.error) {
  197. return 'error';
  198. }
  199. if (!!this.panel.description) {
  200. return 'info';
  201. }
  202. if (this.panel.links && this.panel.links.length) {
  203. return 'links';
  204. }
  205. return '';
  206. }
  207. getInfoContent(options) {
  208. var markdown = this.panel.description;
  209. if (options.mode === 'tooltip') {
  210. markdown = this.error || this.panel.description;
  211. }
  212. var linkSrv = this.$injector.get('linkSrv');
  213. var templateSrv = this.$injector.get('templateSrv');
  214. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  215. var html = '<div class="markdown-html">';
  216. html += new Remarkable().render(interpolatedMarkdown);
  217. if (this.panel.links && this.panel.links.length > 0) {
  218. html += '<ul>';
  219. for (let link of this.panel.links) {
  220. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  221. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  222. }
  223. html += '</ul>';
  224. }
  225. return html + '</div>';
  226. }
  227. openInspector() {
  228. var modalScope = this.$scope.$new();
  229. modalScope.panel = this.panel;
  230. modalScope.dashboard = this.dashboard;
  231. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  232. modalScope.inspector = $.extend(true, {}, this.inspector);
  233. this.publishAppEvent('show-modal', {
  234. src: 'public/app/features/dashboard/partials/inspector.html',
  235. scope: modalScope
  236. });
  237. }
  238. }