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

148 lines
4.0 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';
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
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
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
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({
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
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 = store.getState().location.query.tab || 'queries';
const tabs: PanelEditorTab[] = [
2018-11-23 00:31:13 -06:00
{ id: 'queries', text: 'Queries' },
{ id: 'visualization', text: 'Visualization' },
{ id: 'advanced', text: 'Panel Options' },
2018-11-23 00:31:13 -06:00
];
// handle panels that do not have queries tab
if (plugin.exports.PanelCtrl) {
if (!plugin.exports.PanelCtrl.prototype.onDataReceived) {
// remove queries tab
tabs.shift();
// switch tab
if (activeTab === 'queries') {
activeTab = 'visualization';
}
}
}
if (config.alertingEnabled && plugin.id === 'graph') {
tabs.push({
2018-12-14 10:45:35 -06:00
id: 'alert',
text: 'Alert',
});
}
2018-06-19 07:51:57 -05:00
return (
2018-10-26 06:20:10 -05:00
<div className="panel-editor-container__editor">
2018-11-22 01:46:54 -06:00
{
2018-11-23 08:01:36 -06:00
// <div className="panel-editor__close">
// <i className="fa fa-arrow-left" />
// </div>
2018-11-22 01:46:54 -06: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">
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}>
<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>
);
}