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

158 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-11-05 10:46:09 -06:00
import React, { PureComponent } from 'react';
import classNames from 'classnames';
2018-11-05 10:46:09 -06:00
import { QueriesTab } from './QueriesTab';
2019-02-05 10:15:21 -06:00
import VisualizationTab from './VisualizationTab';
import { GeneralTab } from './GeneralTab';
import { AlertTab } from '../../alerting/AlertTab';
2018-11-05 10:46:09 -06:00
import config from 'app/core/config';
import { store } from 'app/store/store';
2018-09-18 08:32:06 -05:00
import { updateLocation } from 'app/core/actions';
import { AngularComponent } from 'app/core/services/AngularLoader';
2018-06-19 07:51:57 -05:00
import { PanelModel } from '../state/PanelModel';
import { DashboardModel } from '../state/DashboardModel';
2018-11-06 11:14:29 -06:00
import { PanelPlugin } from 'app/types/plugins';
2018-11-05 10:46:09 -06:00
2019-01-08 13:51:00 -06:00
import { Tooltip } from '@grafana/ui';
2018-06-19 07:51:57 -05:00
interface PanelEditorProps {
panel: PanelModel;
dashboard: DashboardModel;
2018-11-06 11:14:29 -06:00
plugin: PanelPlugin;
angularPanel?: AngularComponent;
onTypeChanged: (newType: PanelPlugin) => void;
2018-06-19 07:51:57 -05:00
}
interface PanelEditorTab {
id: string;
text: string;
}
2018-06-19 14:25:57 -05:00
enum PanelEditorTabIds {
Queries = 'queries',
Visualization = 'visualization',
Advanced = 'advanced',
Alert = 'alert',
}
interface PanelEditorTab {
id: string;
text: string;
}
const panelEditorTabTexts = {
[PanelEditorTabIds.Queries]: 'Queries',
[PanelEditorTabIds.Visualization]: 'Visualization',
[PanelEditorTabIds.Advanced]: 'General',
[PanelEditorTabIds.Alert]: 'Alert',
};
const getPanelEditorTab = (tabId: PanelEditorTabIds): PanelEditorTab => {
return {
id: tabId,
text: panelEditorTabTexts[tabId],
};
};
2018-11-05 10:46:09 -06:00
export class PanelEditor extends PureComponent<PanelEditorProps> {
2018-06-19 14:25:57 -05:00
constructor(props) {
super(props);
}
onChangeTab = (tab: PanelEditorTab) => {
2018-09-18 08:32:06 -05:00
store.dispatch(
updateLocation({
2019-02-05 10:15:21 -06:00
query: { tab: tab.id, openVizPicker: null },
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
renderCurrentTab(activeTab: string) {
const { panel, dashboard, onTypeChanged, plugin, angularPanel } = this.props;
switch (activeTab) {
case 'advanced':
return <GeneralTab panel={panel} />;
case 'queries':
return <QueriesTab panel={panel} dashboard={dashboard} />;
2018-12-14 12:34:51 -06:00
case 'alert':
2019-01-02 08:22:22 -06:00
return <AlertTab angularPanel={angularPanel} dashboard={dashboard} panel={panel} />;
case 'visualization':
return (
<VisualizationTab
panel={panel}
dashboard={dashboard}
plugin={plugin}
onTypeChanged={onTypeChanged}
angularPanel={angularPanel}
/>
);
default:
return null;
}
}
render() {
const { plugin } = this.props;
let activeTab: PanelEditorTabIds = store.getState().location.query.tab || PanelEditorTabIds.Queries;
const tabs: PanelEditorTab[] = [
getPanelEditorTab(PanelEditorTabIds.Queries),
getPanelEditorTab(PanelEditorTabIds.Visualization),
getPanelEditorTab(PanelEditorTabIds.Advanced),
2018-11-23 00:31:13 -06:00
];
// handle panels that do not have queries tab
if (plugin.dataFormats.length === 0) {
// remove queries tab
tabs.shift();
// switch tab
if (activeTab === PanelEditorTabIds.Queries) {
activeTab = PanelEditorTabIds.Visualization;
}
}
if (config.alertingEnabled && plugin.id === 'graph') {
tabs.push(getPanelEditorTab(PanelEditorTabIds.Alert));
}
2018-06-19 07:51:57 -05:00
return (
2018-10-26 06:20:10 -05:00
<div className="panel-editor-container__editor">
<div className="panel-editor-tabs">
2018-11-22 01:46:54 -06:00
{tabs.map(tab => {
return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
})}
2018-06-19 07:51:57 -05:00
</div>
2018-11-22 04:41:33 -06:00
<div className="panel-editor__right">{this.renderCurrentTab(activeTab)}</div>
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) {
const tabClasses = classNames({
2018-11-21 13:32:04 -06:00
'panel-editor-tabs__link': true,
active: activeTab === tab.id,
});
return (
2018-11-21 13:32:04 -06:00
<div className="panel-editor-tabs__item" onClick={() => onClick(tab)}>
<a className={tabClasses}>
2019-01-29 06:42:29 -06:00
<Tooltip content={`${tab.text}`} placement="auto">
<i className={`gicon gicon-${tab.id}${activeTab === tab.id ? '-active' : ''}`} />
</Tooltip>
</a>
2018-11-21 13:32:04 -06:00
</div>
);
}