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

125 lines
3.3 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 './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
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 'general':
return <GeneralTab panel={panel} />;
case 'queries':
return <QueriesTab panel={panel} dashboard={dashboard} />;
case 'alert':
return <AlertTab angularPanel={angularPanel} />;
case 'visualization':
return (
<VisualizationTab
panel={panel}
dashboard={dashboard}
plugin={plugin}
onTypeChanged={onTypeChanged}
angularPanel={angularPanel}
/>
);
default:
return null;
}
}
render() {
const { plugin } = this.props;
const activeTab = store.getState().location.query.tab || 'queries';
2018-11-22 01:46:54 -06:00
const tabs = [{ id: 'queries', text: 'Queries' }, { id: 'visualization', text: 'Visualization' }];
if (config.alertingEnabled && plugin.id === 'graph') {
tabs.push({
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
{
// <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>
{this.renderCurrentTab(activeTab)}
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}>
2018-11-21 13:32:04 -06:00
<img src={`public/img/panel-tabs/${tab.id}${activeTab === tab.id ? '-selected' : ''}.svg`} />
</a>
2018-11-21 13:32:04 -06:00
</div>
);
}