panel_ctrl.ts 8.0 KB

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