grafana/public/app/features/dashboard/components/AddPanelWidget/AddPanelWidget.tsx

184 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-10-16 15:55:55 +08:00
import React from 'react';
2017-10-16 16:39:50 +08:00
import _ from 'lodash';
2017-10-16 15:55:55 +08:00
import config from 'app/core/config';
import { PanelModel } from '../../state/PanelModel';
import { DashboardModel } from '../../state/DashboardModel';
import store from 'app/core/store';
import { LS_PANEL_COPY_KEY } from 'app/core/constants';
import { updateLocation } from 'app/core/actions';
import { store as reduxStore } from 'app/store/store';
2017-10-16 15:55:55 +08:00
export interface Props {
2017-10-16 15:55:55 +08:00
panel: PanelModel;
dashboard: DashboardModel;
2017-10-16 15:55:55 +08:00
}
export interface State {
copiedPanelPlugins: any[];
2017-10-16 15:55:55 +08:00
}
type Location = {
query: {
panelId: number;
edit: boolean;
fullscreen: boolean;
tab?: string;
isVizPickerOpen?: boolean;
};
partial: boolean;
};
export class AddPanelWidget extends React.Component<Props, State> {
2017-10-16 15:55:55 +08:00
constructor(props) {
super(props);
this.handleCloseAddPanel = this.handleCloseAddPanel.bind(this);
2017-10-16 15:55:55 +08:00
this.state = {
copiedPanelPlugins: this.getCopiedPanelPlugins(),
2017-10-16 15:55:55 +08:00
};
}
getCopiedPanelPlugins() {
const panels = _.chain(config.panels)
.filter({ hideFromList: false })
.map(item => item)
.value();
const copiedPanels = [];
const copiedPanelJson = store.get(LS_PANEL_COPY_KEY);
if (copiedPanelJson) {
const copiedPanel = JSON.parse(copiedPanelJson);
const pluginInfo = _.find(panels, { id: copiedPanel.type });
if (pluginInfo) {
const pluginCopy = _.cloneDeep(pluginInfo);
pluginCopy.name = copiedPanel.title;
pluginCopy.sort = -1;
pluginCopy.defaults = copiedPanel;
copiedPanels.push(pluginCopy);
}
}
return _.sortBy(copiedPanels, 'sort');
}
handleCloseAddPanel(evt) {
evt.preventDefault();
this.props.dashboard.removePanel(this.props.dashboard.panels[0]);
}
copyButton(panel) {
return (
<button className="btn-inverse btn" onClick={() => this.onPasteCopiedPanel(panel)} title={panel.name}>
Paste copied Panel
</button>
);
}
moveToEdit(location) {
reduxStore.dispatch(updateLocation(location));
2017-10-16 15:55:55 +08:00
}
2019-02-05 00:18:46 +08:00
onCreateNewPanel = (tab = 'queries') => {
const dashboard = this.props.dashboard;
const { gridPos } = this.props.panel;
2017-10-16 15:55:55 +08:00
2018-08-27 02:19:23 +08:00
const newPanel: any = {
type: 'graph',
2017-10-16 15:55:55 +08:00
title: 'Panel Title',
gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
2017-10-16 16:39:50 +08:00
};
dashboard.addPanel(newPanel);
dashboard.removePanel(this.props.panel);
let location: Location = {
query: {
panelId: newPanel.id,
edit: true,
fullscreen: true,
},
partial: true,
};
if (tab === 'visualization') {
location = {
...location,
query: {
...location.query,
tab: 'visualization',
isVizPickerOpen: true,
},
};
this.moveToEdit(location);
} else {
this.moveToEdit(location);
}
};
onPasteCopiedPanel = panelPluginInfo => {
const dashboard = this.props.dashboard;
const { gridPos } = this.props.panel;
const newPanel: any = {
type: panelPluginInfo.id,
title: 'Panel Title',
gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
};
2017-10-16 15:55:55 +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
dashboard.addPanel(newPanel);
dashboard.removePanel(this.props.panel);
};
2018-06-07 09:08:39 +08:00
onCreateNewRow = () => {
const dashboard = this.props.dashboard;
const newRow: any = {
type: 'row',
title: 'Row title',
gridPos: { x: 0, y: 0 },
};
dashboard.addPanel(newRow);
dashboard.removePanel(this.props.panel);
};
2019-02-04 23:48:27 +08:00
renderOptionLink = (icon, text, onClick) => {
return (
<div>
<a href="#" onClick={onClick} className="add-panel-widget__link btn-inverse">
<div className="add-panel-widget__icon">
<i className={`gicon gicon-${icon}`} />
</div>
<span>{text}</span>
</a>
</div>
);
};
2019-02-04 23:48:27 +08:00
render() {
2017-10-16 15:55:55 +08:00
return (
<div className="panel-container add-panel-widget-container">
<div className="add-panel-widget">
<div className="add-panel-widget__header grid-drag-handle">
<i className="gicon gicon-add-panel" />
<button className="add-panel-widget__close" onClick={this.handleCloseAddPanel}>
<i className="fa fa-close" />
</button>
</div>
<div className="add-panel-widget__btn-container">
2019-02-04 23:48:27 +08:00
{this.renderOptionLink('queries', 'Add query', this.onCreateNewPanel)}
2019-02-05 00:18:46 +08:00
{this.renderOptionLink('visualization', 'Choose Panel type', () => this.onCreateNewPanel('visualization'))}
2019-02-04 23:48:27 +08:00
{this.renderOptionLink('queries', 'Convert to row', this.onCreateNewRow)}
</div>
</div>
2017-10-16 15:55:55 +08:00
</div>
);
}
}