grafana/public/app/features/dashboard/dashgrid/PanelEditor.tsx

113 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-11-05 10:46:09 -06:00
import React, { PureComponent } from 'react';
import classNames from 'classnames';
2018-10-27 08:09:36 -05:00
import { QueriesTab } from './QueriesTab';
import { VisualizationTab } from './VisualizationTab';
2018-06-19 07:51:57 -05:00
2018-11-05 10:46:09 -06:00
import { store } from 'app/store/configureStore';
2018-09-18 08:32:06 -05:00
import { updateLocation } from 'app/core/actions';
2018-06-19 07:51:57 -05:00
2018-11-05 10:46:09 -06:00
import { PanelModel } from '../panel_model';
import { DashboardModel } from '../dashboard_model';
2018-11-06 11:14:29 -06:00
import { PanelPlugin } from 'app/types/plugins';
2018-11-05 10:46:09 -06:00
2018-06-19 07:51:57 -05:00
interface PanelEditorProps {
panel: PanelModel;
dashboard: DashboardModel;
2018-11-06 11:14:29 -06:00
plugin: PanelPlugin;
onTypeChanged: (newType: PanelPlugin) => void;
2018-06-19 07:51:57 -05:00
}
interface PanelEditorTab {
id: string;
text: string;
icon: string;
}
2018-06-19 14:25:57 -05:00
2018-11-05 10:46:09 -06:00
export class PanelEditor extends PureComponent<PanelEditorProps> {
2018-06-28 06:31:55 -05:00
tabs: PanelEditorTab[];
2018-06-19 14:25:57 -05:00
constructor(props) {
super(props);
2018-06-28 06:31:55 -05:00
this.tabs = [
{ id: 'queries', text: 'Queries', icon: 'fa fa-database' },
{ id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
2018-10-26 09:29:08 -05:00
{ id: 'alert', text: 'Alert', icon: 'gicon gicon-alert' },
2018-06-28 06:31:55 -05:00
];
2018-06-19 14:25:57 -05:00
}
onChangeTab = (tab: PanelEditorTab) => {
2018-09-18 08:32:06 -05:00
store.dispatch(
updateLocation({
query: { tab: tab.id },
2018-10-10 02:26:17 -05:00
partial: true,
2018-09-18 08:32:06 -05:00
})
);
2018-11-05 10:46:09 -06:00
this.forceUpdate();
};
2018-06-26 09:45:42 -05:00
2018-10-27 03:14:38 -05:00
onClose = () => {
store.dispatch(
updateLocation({
2018-10-27 08:09:36 -05:00
query: { tab: null, fullscreen: null, edit: null },
2018-10-27 03:14:38 -05:00
partial: true,
})
);
};
2018-06-19 07:51:57 -05:00
render() {
const { panel, dashboard, onTypeChanged, plugin } = this.props;
2018-10-04 02:14:47 -05:00
const { location } = store.getState();
const activeTab = location.query.tab || 'queries';
2018-06-19 07:51:57 -05:00
return (
2018-10-26 06:20:10 -05:00
<div className="panel-editor-container__editor">
2018-10-27 08:09:36 -05:00
<div className="panel-editor-resizer">
<div className="panel-editor-resizer__handle">
<div className="panel-editor-resizer__handle-dots" />
</div>
</div>
2018-11-08 05:25:22 -06:00
<div className="panel-editor-tabs">
<ul className="gf-tabs">
{this.tabs.map(tab => {
return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
})}
</ul>
2018-11-08 05:25:22 -06:00
<button className="panel-editor-tabs__close" onClick={this.onClose}>
<i className="fa fa-remove" />
</button>
2018-06-19 14:25:57 -05:00
</div>
2018-11-08 05:25:22 -06:00
{activeTab === 'queries' && <QueriesTab panel={panel} dashboard={dashboard} />}
{activeTab === 'visualization' && (
<VisualizationTab panel={panel} dashboard={dashboard} plugin={plugin} onTypeChanged={onTypeChanged} />
)}
2018-06-19 07:51:57 -05:00
</div>
);
}
}
interface TabItemParams {
tab: PanelEditorTab;
activeTab: string;
onClick: (tab: PanelEditorTab) => void;
}
function TabItem({ tab, activeTab, onClick }: TabItemParams) {
2018-11-08 05:25:22 -06:00
const tabClasses = classNames({
'gf-tabs-link': true,
active: activeTab === tab.id,
});
return (
<li className="gf-tabs-item" onClick={() => onClick(tab)}>
<a className={tabClasses}>
<i className={tab.icon} /> {tab.text}
</a>
2018-11-08 05:25:22 -06:00
</li>
);
}