2019-02-06 00:15:21 +08:00
|
|
|
// Libraries
|
2017-10-16 15:55:55 +08:00
|
|
|
import React from 'react';
|
2017-10-16 16:39:50 +08:00
|
|
|
import _ from 'lodash';
|
2019-02-06 00:15:21 +08:00
|
|
|
|
|
|
|
|
// Utils
|
2017-10-16 15:55:55 +08:00
|
|
|
import config from 'app/core/config';
|
2019-02-06 00:15:21 +08:00
|
|
|
import store from 'app/core/store';
|
|
|
|
|
|
|
|
|
|
// Store
|
|
|
|
|
import { store as reduxStore } from 'app/store/store';
|
|
|
|
|
import { updateLocation } from 'app/core/actions';
|
|
|
|
|
|
|
|
|
|
// Types
|
2019-02-05 19:47:42 +08:00
|
|
|
import { PanelModel } from '../../state';
|
|
|
|
|
import { DashboardModel } from '../../state';
|
2017-12-26 20:20:45 +08:00
|
|
|
import { LS_PANEL_COPY_KEY } from 'app/core/constants';
|
2019-06-04 22:20:55 +08:00
|
|
|
import { LocationUpdate } from '@grafana/runtime';
|
2017-10-16 15:55:55 +08:00
|
|
|
|
2019-01-24 16:23:48 +08:00
|
|
|
export interface Props {
|
2017-10-16 15:55:55 +08:00
|
|
|
panel: PanelModel;
|
2018-01-03 20:33:54 +08:00
|
|
|
dashboard: DashboardModel;
|
2017-10-16 15:55:55 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 16:23:48 +08:00
|
|
|
export interface State {
|
2018-02-21 21:51:28 +08:00
|
|
|
copiedPanelPlugins: any[];
|
2017-10-16 15:55:55 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 16:23:48 +08:00
|
|
|
export class AddPanelWidget extends React.Component<Props, State> {
|
2017-10-16 15:55:55 +08:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2018-01-09 22:27:53 +08:00
|
|
|
this.handleCloseAddPanel = this.handleCloseAddPanel.bind(this);
|
2017-10-16 15:55:55 +08:00
|
|
|
|
|
|
|
|
this.state = {
|
2018-12-14 21:33:49 +08:00
|
|
|
copiedPanelPlugins: this.getCopiedPanelPlugins(),
|
2017-10-16 15:55:55 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
getCopiedPanelPlugins() {
|
2018-08-26 23:14:40 +08:00
|
|
|
const panels = _.chain(config.panels)
|
2018-02-21 21:51:28 +08:00
|
|
|
.filter({ hideFromList: false })
|
|
|
|
|
.map(item => item)
|
|
|
|
|
.value();
|
2018-12-14 21:33:49 +08:00
|
|
|
const copiedPanels = [];
|
2018-02-21 21:51:28 +08:00
|
|
|
|
2018-08-26 23:14:40 +08:00
|
|
|
const copiedPanelJson = store.get(LS_PANEL_COPY_KEY);
|
2017-12-26 20:20:45 +08:00
|
|
|
if (copiedPanelJson) {
|
2018-08-26 23:14:40 +08:00
|
|
|
const copiedPanel = JSON.parse(copiedPanelJson);
|
2019-04-15 18:11:52 +08:00
|
|
|
const pluginInfo: any = _.find(panels, { id: copiedPanel.type });
|
2017-12-26 20:20:45 +08:00
|
|
|
if (pluginInfo) {
|
2018-08-26 23:14:40 +08:00
|
|
|
const pluginCopy = _.cloneDeep(pluginInfo);
|
2017-12-26 20:20:45 +08:00
|
|
|
pluginCopy.name = copiedPanel.title;
|
|
|
|
|
pluginCopy.sort = -1;
|
|
|
|
|
pluginCopy.defaults = copiedPanel;
|
2018-02-21 21:51:28 +08:00
|
|
|
copiedPanels.push(pluginCopy);
|
2017-12-26 20:20:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-05 19:47:42 +08:00
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
return _.sortBy(copiedPanels, 'sort');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleCloseAddPanel(evt) {
|
|
|
|
|
evt.preventDefault();
|
2019-06-19 19:12:04 +08:00
|
|
|
this.props.dashboard.removePanel(this.props.panel);
|
2018-12-14 21:33:49 +08:00
|
|
|
}
|
2017-12-26 20:20:45 +08:00
|
|
|
|
2019-02-05 00:18:46 +08:00
|
|
|
onCreateNewPanel = (tab = 'queries') => {
|
2018-01-03 20:33:54 +08:00
|
|
|
const dashboard = this.props.dashboard;
|
2017-12-22 01:10:24 +08:00
|
|
|
const { gridPos } = this.props.panel;
|
2017-10-16 15:55:55 +08:00
|
|
|
|
2018-08-27 02:19:23 +08:00
|
|
|
const newPanel: any = {
|
2018-12-15 03:53:42 +08:00
|
|
|
type: 'graph',
|
2017-10-16 15:55:55 +08:00
|
|
|
title: 'Panel Title',
|
2017-12-22 01:10:24 +08:00
|
|
|
gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
|
2017-10-16 16:39:50 +08:00
|
|
|
};
|
|
|
|
|
|
2017-12-26 20:20:45 +08:00
|
|
|
dashboard.addPanel(newPanel);
|
2017-12-22 01:10:24 +08:00
|
|
|
dashboard.removePanel(this.props.panel);
|
2018-12-15 03:53:42 +08:00
|
|
|
|
2019-02-06 00:15:21 +08:00
|
|
|
const location: LocationUpdate = {
|
2019-02-05 04:26:49 +08:00
|
|
|
query: {
|
|
|
|
|
panelId: newPanel.id,
|
|
|
|
|
edit: true,
|
|
|
|
|
fullscreen: true,
|
|
|
|
|
},
|
|
|
|
|
partial: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (tab === 'visualization') {
|
2019-02-06 00:15:21 +08:00
|
|
|
location.query.tab = 'visualization';
|
2019-02-13 18:14:53 +08:00
|
|
|
location.query.openVizPicker = true;
|
2019-02-05 04:26:49 +08:00
|
|
|
}
|
2019-02-06 00:15:21 +08:00
|
|
|
|
|
|
|
|
reduxStore.dispatch(updateLocation(location));
|
2017-12-26 20:20:45 +08:00
|
|
|
};
|
2017-12-22 01:10:24 +08:00
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
onPasteCopiedPanel = panelPluginInfo => {
|
|
|
|
|
const dashboard = this.props.dashboard;
|
2019-03-06 22:19:58 +08:00
|
|
|
const { gridPos } = this.props.panel;
|
2018-02-21 22:39:15 +08:00
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
const newPanel: any = {
|
|
|
|
|
type: panelPluginInfo.id,
|
|
|
|
|
title: 'Panel Title',
|
2019-03-06 22:19:58 +08:00
|
|
|
gridPos: {
|
|
|
|
|
x: gridPos.x,
|
|
|
|
|
y: gridPos.y,
|
|
|
|
|
w: panelPluginInfo.defaults.gridPos.w,
|
|
|
|
|
h: panelPluginInfo.defaults.gridPos.h,
|
|
|
|
|
},
|
2018-12-14 21:33:49 +08:00
|
|
|
};
|
2017-10-16 15:55:55 +08:00
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
// apply panel template / defaults
|
|
|
|
|
if (panelPluginInfo.defaults) {
|
|
|
|
|
_.defaults(newPanel, panelPluginInfo.defaults);
|
|
|
|
|
newPanel.title = panelPluginInfo.defaults.title;
|
|
|
|
|
store.delete(LS_PANEL_COPY_KEY);
|
|
|
|
|
}
|
2018-02-22 16:58:52 +08:00
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
dashboard.addPanel(newPanel);
|
|
|
|
|
dashboard.removePanel(this.props.panel);
|
|
|
|
|
};
|
2018-06-07 09:08:39 +08:00
|
|
|
|
2018-12-15 03:53:42 +08:00
|
|
|
onCreateNewRow = () => {
|
2018-12-14 21:33:49 +08:00
|
|
|
const dashboard = this.props.dashboard;
|
2018-02-21 21:51:28 +08:00
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
const newRow: any = {
|
2018-12-15 03:53:42 +08:00
|
|
|
type: 'row',
|
2018-12-14 21:33:49 +08:00
|
|
|
title: 'Row title',
|
|
|
|
|
gridPos: { x: 0, y: 0 },
|
|
|
|
|
};
|
2018-02-21 21:51:28 +08:00
|
|
|
|
2018-12-14 21:33:49 +08:00
|
|
|
dashboard.addPanel(newRow);
|
|
|
|
|
dashboard.removePanel(this.props.panel);
|
|
|
|
|
};
|
2018-02-21 21:51:28 +08:00
|
|
|
|
2019-05-08 22:50:21 +08:00
|
|
|
renderOptionLink = (icon: string, text: string, onClick) => {
|
2019-02-04 23:48:27 +08:00
|
|
|
return (
|
|
|
|
|
<div>
|
2019-05-08 22:50:21 +08:00
|
|
|
<a
|
|
|
|
|
href="#"
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
className="add-panel-widget__link btn btn-inverse"
|
|
|
|
|
aria-label={`${text} CTA button`}
|
|
|
|
|
>
|
2019-02-04 23:48:27 +08:00
|
|
|
<div className="add-panel-widget__icon">
|
|
|
|
|
<i className={`gicon gicon-${icon}`} />
|
|
|
|
|
</div>
|
|
|
|
|
<span>{text}</span>
|
|
|
|
|
</a>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2018-02-21 21:51:28 +08:00
|
|
|
|
2019-02-04 23:48:27 +08:00
|
|
|
render() {
|
2019-02-05 19:47:42 +08:00
|
|
|
const { copiedPanelPlugins } = this.state;
|
|
|
|
|
|
2017-10-16 15:55:55 +08:00
|
|
|
return (
|
2019-01-24 16:23:48 +08:00
|
|
|
<div className="panel-container add-panel-widget-container">
|
|
|
|
|
<div className="add-panel-widget">
|
|
|
|
|
<div className="add-panel-widget__header grid-drag-handle">
|
2017-12-22 01:10:24 +08:00
|
|
|
<i className="gicon gicon-add-panel" />
|
2019-02-06 00:15:21 +08:00
|
|
|
<span className="add-panel-widget__title">New Panel</span>
|
2019-01-24 16:23:48 +08:00
|
|
|
<button className="add-panel-widget__close" onClick={this.handleCloseAddPanel}>
|
2018-01-09 22:27:53 +08:00
|
|
|
<i className="fa fa-close" />
|
|
|
|
|
</button>
|
2017-12-02 03:11:55 +08:00
|
|
|
</div>
|
2019-01-24 16:23:48 +08:00
|
|
|
<div className="add-panel-widget__btn-container">
|
2019-02-05 19:47:42 +08:00
|
|
|
<div className="add-panel-widget__create">
|
2019-02-06 00:15:21 +08:00
|
|
|
{this.renderOptionLink('queries', 'Add Query', this.onCreateNewPanel)}
|
|
|
|
|
{this.renderOptionLink('visualization', 'Choose Visualization', () =>
|
2019-02-05 19:47:42 +08:00
|
|
|
this.onCreateNewPanel('visualization')
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="add-panel-widget__actions">
|
2019-02-13 18:14:53 +08:00
|
|
|
<button className="btn btn-inverse add-panel-widget__action" onClick={this.onCreateNewRow}>
|
|
|
|
|
Convert to row
|
|
|
|
|
</button>
|
2019-02-05 19:47:42 +08:00
|
|
|
{copiedPanelPlugins.length === 1 && (
|
2019-02-06 00:15:21 +08:00
|
|
|
<button
|
|
|
|
|
className="btn btn-inverse add-panel-widget__action"
|
2019-02-05 19:47:42 +08:00
|
|
|
onClick={() => this.onPasteCopiedPanel(copiedPanelPlugins[0])}
|
|
|
|
|
>
|
|
|
|
|
Paste copied panel
|
2019-02-06 00:15:21 +08:00
|
|
|
</button>
|
2019-02-05 19:47:42 +08:00
|
|
|
)}
|
|
|
|
|
</div>
|
2018-12-14 21:33:49 +08:00
|
|
|
</div>
|
2017-12-02 03:11:55 +08:00
|
|
|
</div>
|
2017-10-16 15:55:55 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|