grafana/public/app/features/panel/panel_ctrl.ts

308 lines
7.2 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import _ from 'lodash';
import { sanitize, escapeHtml } from 'app/core/utils/text';
2018-11-14 06:20:19 -06:00
import config from 'app/core/config';
import { profiler } from 'app/core/core';
2018-11-14 06:20:19 -06:00
import { Emitter } from 'app/core/core';
import getFactors from 'app/core/utils/factors';
import {
duplicatePanel,
2019-02-03 10:15:23 -06:00
removePanel,
copyPanel as copyPanelUtil,
editPanelJson as editPanelJsonUtil,
sharePanel as sharePanelUtil,
calculateInnerPanelHeight,
} from 'app/features/dashboard/utils/panel';
import { GRID_COLUMN_COUNT } from 'app/core/constants';
import { auto } from 'angular';
import { TemplateSrv } from '../templating/template_srv';
import { getPanelLinksSupplier } from './panellinks/linkSuppliers';
import { renderMarkdown, AppEvent, PanelEvents, PanelPluginMeta } from '@grafana/data';
import { getLocationSrv } from '@grafana/runtime';
Graph: Add data links feature (click on graph) (#17267) * WIP: initial panel links editor * WIP: Added dashboard migration to new panel drilldown link schema * Make link_srv interpolate new variables * Fix failing tests * Drilldown: Add context menu to graph viz (#17284) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Move graph context menu controller to separate file * Drilldown: datapoint variables interpolation (#17328) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Add util for absolute time range transformation * Add series name and datapoint timestamp interpolation * Rename drilldown link variables tot snake case, use const values instead of strings in tests * Bring LinkSrv.getPanelLinkAnchorInfo for compatibility reasons and add deprecation warning * Rename seriesLabel to seriesName * Drilldown: use separate editors for panel and series links (#17355) * Use correct target ini context menu links * Rename PanelLinksEditor to DrilldownLinksEditor and mote it to grafana/ui * Expose DrilldownLinksEditor as an angular directive * Enable visualization specifix drilldown links * Props interfaces rename * Drilldown: Add variables suggestion and syntax highlighting for drilldown link editor (#17391) * Add variables suggestion in drilldown link editor * Enable prism * Fix backspace not working * Move slate value helpers to grafana/ui * Add syntax higlighting for links input * Rename drilldown link components to data links * Add template variabe suggestions * Bugfix * Fix regexp not working in Firefox * Display correct links in panel header corner * bugfix * bugfix * Bugfix * Context menu UI tweaks * Use data link terminology instead of drilldown * DataLinks: changed autocomplete syntax * Use singular form for data link * Use the same syntax higlighting for built-in and template variables in data links editor * UI improvements to context menu * UI review tweaks * Tweak layout of data link editor * Fix vertical spacing * Remove data link header in context menu * Remove pointer cursor from series label in context menu * Fix variable selection on click * DataLinks: migrations for old links * Update docs about data links * Use value time instead of time range when interpolating datapoint timestamp * Remove not used util * Update docs * Moved icon a bit more down * Interpolate value ts only when using __value_time variable * Bring href property back to LinkModel * Add any type annotations * Fix TS error on slate's Value type * minor changes
2019-06-25 04:38:51 -05:00
2016-01-24 17:44:21 -06:00
export class PanelCtrl {
panel: any;
error: any;
2016-01-24 17:44:21 -06:00
dashboard: any;
pluginName: string;
pluginId: string;
2016-01-24 17:44:21 -06:00
editorTabs: any;
$scope: any;
$injector: auto.IInjectorService;
$location: any;
$timeout: any;
editModeInitiated: boolean;
height: any;
containerHeight: any;
events: Emitter;
loading: boolean;
timing: any;
maxPanelsPerRowOptions: number[];
2016-01-24 17:44:21 -06:00
/** @ngInject */
constructor($scope: any, $injector: auto.IInjectorService) {
2016-01-25 14:58:24 -06:00
this.$injector = $injector;
this.$location = $injector.get('$location');
2016-01-24 17:44:21 -06:00
this.$scope = $scope;
2017-12-20 05:33:33 -06:00
this.$timeout = $injector.get('$timeout');
this.editorTabs = [];
this.events = this.panel.events;
this.timing = {}; // not used but here to not break plugins
2016-01-25 10:41:20 -06:00
2018-08-29 07:26:50 -05:00
const plugin = config.panels[this.panel.type];
2016-01-27 16:16:00 -06:00
if (plugin) {
this.pluginId = plugin.id;
this.pluginName = plugin.name;
}
$scope.$on(PanelEvents.componentDidMount.name, () => this.panelDidMount());
2016-01-25 10:41:20 -06:00
}
2017-11-22 06:32:54 -06:00
panelDidMount() {
this.events.emit(PanelEvents.componentDidMount);
2018-09-10 09:19:28 -05:00
this.dashboard.panelInitialized(this.panel);
}
2016-01-25 16:58:41 -06:00
renderingCompleted() {
2019-05-13 02:38:19 -05:00
profiler.renderingCompleted();
2016-01-25 16:58:41 -06:00
}
2016-01-25 10:41:20 -06:00
refresh() {
this.panel.refresh();
2016-01-24 17:44:21 -06:00
}
publishAppEvent<T>(event: AppEvent<T>, payload?: T) {
this.$scope.$root.appEvent(event, payload);
2016-01-24 17:44:21 -06:00
}
changeView(fullscreen: boolean, edit: boolean) {
this.publishAppEvent(PanelEvents.panelChangeView, {
fullscreen,
edit,
2017-12-20 05:33:33 -06:00
panelId: this.panel.id,
2017-10-11 09:32:05 -05:00
});
2016-01-24 17:44:21 -06:00
}
viewPanel() {
this.changeView(true, false);
}
editPanel() {
this.changeView(true, true);
}
exitFullscreen() {
this.changeView(false, false);
}
initEditMode() {
if (!this.editModeInitiated) {
this.editModeInitiated = true;
this.events.emit(PanelEvents.editModeInitialized);
this.maxPanelsPerRowOptions = getFactors(GRID_COLUMN_COUNT);
}
}
addEditorTab(title: string, directiveFn: any, index?: number, icon?: any) {
const editorTab = { title, directiveFn, icon };
if (_.isString(directiveFn)) {
editorTab.directiveFn = () => {
2017-12-19 07:55:03 -06:00
return { templateUrl: directiveFn };
};
}
2018-06-19 09:57:55 -05:00
2016-01-28 15:48:37 -06:00
if (index) {
this.editorTabs.splice(index, 0, editorTab);
} else {
this.editorTabs.push(editorTab);
}
2016-01-24 17:44:21 -06:00
}
async getMenu() {
const menu = [];
2017-12-19 07:55:03 -06:00
menu.push({
2017-12-20 05:33:33 -06:00
text: 'View',
click: 'ctrl.viewPanel();',
icon: 'gicon gicon-viewer',
2017-12-20 05:33:33 -06:00
shortcut: 'v',
2017-12-19 07:55:03 -06:00
});
2017-12-11 09:28:57 -06:00
if (this.dashboard.meta.canEdit) {
2017-12-19 07:55:03 -06:00
menu.push({
2017-12-20 05:33:33 -06:00
text: 'Edit',
click: 'ctrl.editPanel();',
role: 'Editor',
icon: 'gicon gicon-editor',
2017-12-20 05:33:33 -06:00
shortcut: 'e',
2017-12-19 07:55:03 -06:00
});
2017-12-11 09:28:57 -06:00
}
2017-12-19 07:55:03 -06:00
menu.push({
2017-12-20 05:33:33 -06:00
text: 'Share',
click: 'ctrl.sharePanel();',
icon: 'fa fa-fw fa-share',
shortcut: 'p s',
2017-12-19 07:55:03 -06:00
});
2017-08-02 02:22:22 -05:00
if (config.featureToggles.inspect) {
menu.push({
text: 'Inspect',
icon: 'fa fa-fw fa-info-circle',
click: 'ctrl.inspectPanel();',
shortcut: 'p i',
});
}
// Additional items from sub-class
menu.push(...(await this.getAdditionalMenuItems()));
const extendedMenu = this.getExtendedMenu();
2017-12-19 07:55:03 -06:00
menu.push({
2017-12-20 05:33:33 -06:00
text: 'More ...',
click: '',
icon: 'fa fa-fw fa-cube',
submenu: extendedMenu,
2017-12-19 07:55:03 -06:00
});
2017-08-03 08:19:06 -05:00
2017-12-11 09:28:57 -06:00
if (this.dashboard.meta.canEdit) {
2017-12-20 05:33:33 -06:00
menu.push({ divider: true, role: 'Editor' });
2017-12-19 07:55:03 -06:00
menu.push({
2017-12-20 05:33:33 -06:00
text: 'Remove',
click: 'ctrl.removePanel();',
role: 'Editor',
icon: 'fa fa-fw fa-trash',
shortcut: 'p r',
2017-12-19 07:55:03 -06:00
});
2017-12-11 09:28:57 -06:00
}
2016-01-24 17:44:21 -06:00
return menu;
}
2016-01-27 16:16:00 -06:00
getExtendedMenu() {
const menu = [];
2018-06-19 09:57:55 -05:00
if (!this.panel.fullscreen && this.dashboard.meta.canEdit) {
2017-12-19 07:55:03 -06:00
menu.push({
2017-12-20 05:33:33 -06:00
text: 'Duplicate',
click: 'ctrl.duplicate()',
role: 'Editor',
shortcut: 'p d',
2017-12-19 07:55:03 -06:00
});
menu.push({
text: 'Copy',
click: 'ctrl.copyPanel()',
role: 'Editor',
});
2017-10-14 14:17:16 -05:00
}
2017-12-19 07:55:03 -06:00
menu.push({
2017-12-20 05:33:33 -06:00
text: 'Panel JSON',
click: 'ctrl.editPanelJson(); dismiss();',
2017-12-19 07:55:03 -06:00
});
this.events.emit(PanelEvents.initPanelActions, menu);
2017-10-14 14:17:16 -05:00
return menu;
2016-01-27 16:16:00 -06:00
}
// Override in sub-class to add items before extended menu
async getAdditionalMenuItems(): Promise<any[]> {
return [];
}
otherPanelInFullscreenMode() {
2018-06-19 09:57:55 -05:00
return this.dashboard.meta.fullscreen && !this.panel.fullscreen;
}
calculatePanelHeight(containerHeight: number) {
this.containerHeight = containerHeight;
this.height = calculateInnerPanelHeight(this.panel, containerHeight);
}
render(payload?: any) {
this.events.emit(PanelEvents.render, payload);
}
2016-01-26 17:08:08 -06:00
duplicate() {
2018-10-30 08:38:18 -05:00
duplicatePanel(this.dashboard, this.panel);
}
removePanel() {
2019-02-03 10:15:23 -06:00
removePanel(this.dashboard, this.panel, true);
}
2016-01-26 17:08:08 -06:00
2016-01-27 16:16:00 -06:00
editPanelJson() {
editPanelJsonUtil(this.dashboard, this.panel);
2016-01-27 16:16:00 -06:00
}
copyPanel() {
copyPanelUtil(this.panel);
2016-01-27 16:16:00 -06:00
}
2016-01-28 17:05:49 -06:00
sharePanel() {
sharePanelUtil(this.dashboard, this.panel);
}
inspectPanel() {
getLocationSrv().update({
query: {
inspect: this.panel.id,
},
partial: true,
});
}
getInfoMode() {
if (this.error) {
2017-12-20 05:33:33 -06:00
return 'error';
}
if (!!this.panel.description) {
2017-12-20 05:33:33 -06:00
return 'info';
}
if (this.panel.links && this.panel.links.length) {
2017-12-20 05:33:33 -06:00
return 'links';
}
2017-12-20 05:33:33 -06:00
return '';
}
getInfoContent(options: { mode: string }) {
const { panel } = this;
let markdown = panel.description || '';
2017-12-20 05:33:33 -06:00
if (options.mode === 'tooltip') {
markdown = this.error || panel.description || '';
}
const templateSrv: TemplateSrv = this.$injector.get('templateSrv');
const interpolatedMarkdown = templateSrv.replace(markdown, panel.scopedVars);
Graph: Add data links feature (click on graph) (#17267) * WIP: initial panel links editor * WIP: Added dashboard migration to new panel drilldown link schema * Make link_srv interpolate new variables * Fix failing tests * Drilldown: Add context menu to graph viz (#17284) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Move graph context menu controller to separate file * Drilldown: datapoint variables interpolation (#17328) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Add util for absolute time range transformation * Add series name and datapoint timestamp interpolation * Rename drilldown link variables tot snake case, use const values instead of strings in tests * Bring LinkSrv.getPanelLinkAnchorInfo for compatibility reasons and add deprecation warning * Rename seriesLabel to seriesName * Drilldown: use separate editors for panel and series links (#17355) * Use correct target ini context menu links * Rename PanelLinksEditor to DrilldownLinksEditor and mote it to grafana/ui * Expose DrilldownLinksEditor as an angular directive * Enable visualization specifix drilldown links * Props interfaces rename * Drilldown: Add variables suggestion and syntax highlighting for drilldown link editor (#17391) * Add variables suggestion in drilldown link editor * Enable prism * Fix backspace not working * Move slate value helpers to grafana/ui * Add syntax higlighting for links input * Rename drilldown link components to data links * Add template variabe suggestions * Bugfix * Fix regexp not working in Firefox * Display correct links in panel header corner * bugfix * bugfix * Bugfix * Context menu UI tweaks * Use data link terminology instead of drilldown * DataLinks: changed autocomplete syntax * Use singular form for data link * Use the same syntax higlighting for built-in and template variables in data links editor * UI improvements to context menu * UI review tweaks * Tweak layout of data link editor * Fix vertical spacing * Remove data link header in context menu * Remove pointer cursor from series label in context menu * Fix variable selection on click * DataLinks: migrations for old links * Update docs about data links * Use value time instead of time range when interpolating datapoint timestamp * Remove not used util * Update docs * Moved icon a bit more down * Interpolate value ts only when using __value_time variable * Bring href property back to LinkModel * Add any type annotations * Fix TS error on slate's Value type * minor changes
2019-06-25 04:38:51 -05:00
let html = '<div class="markdown-html panel-info-content">';
const md = renderMarkdown(interpolatedMarkdown);
html += config.disableSanitizeHtml ? md : sanitize(md);
if (panel.links && panel.links.length > 0) {
const interpolatedLinks = getPanelLinksSupplier(panel).getLinks();
Graph: Add data links feature (click on graph) (#17267) * WIP: initial panel links editor * WIP: Added dashboard migration to new panel drilldown link schema * Make link_srv interpolate new variables * Fix failing tests * Drilldown: Add context menu to graph viz (#17284) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Move graph context menu controller to separate file * Drilldown: datapoint variables interpolation (#17328) * Add simple context menu for adding graph annotations and showing drilldown links * Close graph context menu when user start scrolling * Move context menu component to grafana/ui * Make graph context menu appear on click, use cmd/ctrl click for quick annotations * Add util for absolute time range transformation * Add series name and datapoint timestamp interpolation * Rename drilldown link variables tot snake case, use const values instead of strings in tests * Bring LinkSrv.getPanelLinkAnchorInfo for compatibility reasons and add deprecation warning * Rename seriesLabel to seriesName * Drilldown: use separate editors for panel and series links (#17355) * Use correct target ini context menu links * Rename PanelLinksEditor to DrilldownLinksEditor and mote it to grafana/ui * Expose DrilldownLinksEditor as an angular directive * Enable visualization specifix drilldown links * Props interfaces rename * Drilldown: Add variables suggestion and syntax highlighting for drilldown link editor (#17391) * Add variables suggestion in drilldown link editor * Enable prism * Fix backspace not working * Move slate value helpers to grafana/ui * Add syntax higlighting for links input * Rename drilldown link components to data links * Add template variabe suggestions * Bugfix * Fix regexp not working in Firefox * Display correct links in panel header corner * bugfix * bugfix * Bugfix * Context menu UI tweaks * Use data link terminology instead of drilldown * DataLinks: changed autocomplete syntax * Use singular form for data link * Use the same syntax higlighting for built-in and template variables in data links editor * UI improvements to context menu * UI review tweaks * Tweak layout of data link editor * Fix vertical spacing * Remove data link header in context menu * Remove pointer cursor from series label in context menu * Fix variable selection on click * DataLinks: migrations for old links * Update docs about data links * Use value time instead of time range when interpolating datapoint timestamp * Remove not used util * Update docs * Moved icon a bit more down * Interpolate value ts only when using __value_time variable * Bring href property back to LinkModel * Add any type annotations * Fix TS error on slate's Value type * minor changes
2019-06-25 04:38:51 -05:00
html += '<ul class="panel-info-corner-links">';
for (const link of interpolatedLinks) {
2017-12-19 07:55:03 -06:00
html +=
'<li><a class="panel-menu-link" href="' +
escapeHtml(link.href) +
2017-12-19 07:55:03 -06:00
'" target="' +
escapeHtml(link.target) +
2017-12-19 07:55:03 -06:00
'">' +
escapeHtml(link.title) +
2017-12-20 05:33:33 -06:00
'</a></li>';
}
2017-12-20 05:33:33 -06:00
html += '</ul>';
}
html += '</div>';
return html;
}
// overriden from react
onPluginTypeChange = (plugin: PanelPluginMeta) => {};
2016-01-24 17:44:21 -06:00
}