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

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-11-05 17:46:09 +01:00
import React, { PureComponent } from 'react';
import classNames from 'classnames';
2018-11-05 17:46:09 +01:00
import { QueriesTab } from './QueriesTab';
import { VizTypePicker } from './VizTypePicker';
2018-11-05 17:46:09 +01:00
import { store } from 'app/store/store';
2018-09-18 15:32:06 +02:00
import { updateLocation } from 'app/core/actions';
2018-06-19 14:51:57 +02:00
2018-11-05 17:46:09 +01:00
import { PanelModel } from '../panel_model';
import { DashboardModel } from '../dashboard_model';
import { PanelPlugin, PluginExports } from 'app/types/plugins';
2018-06-19 14:51:57 +02:00
interface PanelEditorProps {
panel: PanelModel;
dashboard: DashboardModel;
panelType: string;
pluginExports: PluginExports;
onTypeChanged: (newType: PanelPlugin) => void;
2018-06-19 14:51:57 +02:00
}
interface PanelEditorTab {
id: string;
text: string;
icon: string;
}
2018-06-19 21:25:57 +02:00
2018-11-05 17:46:09 +01:00
export class PanelEditor extends PureComponent<PanelEditorProps> {
2018-06-28 04:31:55 -07:00
tabs: PanelEditorTab[];
2018-06-19 21:25:57 +02:00
constructor(props) {
super(props);
2018-06-28 04:31:55 -07:00
this.tabs = [
{ id: 'queries', text: 'Queries', icon: 'fa fa-database' },
{ id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
2018-06-28 04:31:55 -07:00
];
2018-06-19 21:25:57 +02:00
}
renderQueriesTab() {
return <QueriesTab panel={this.props.panel} dashboard={this.props.dashboard} />;
}
2018-06-19 21:25:57 +02:00
renderPanelOptions() {
2018-11-05 17:46:09 +01:00
const { pluginExports, panel } = this.props;
2018-11-05 17:46:09 +01:00
if (pluginExports.PanelOptionsComponent) {
const OptionsComponent = pluginExports.PanelOptionsComponent;
return <OptionsComponent options={panel.getOptions()} onChange={this.onPanelOptionsChanged} />;
} else {
return <p>Visualization has no options</p>;
}
}
2018-11-05 17:46:09 +01:00
onPanelOptionsChanged = (options: any) => {
this.props.panel.updateOptions(options);
2018-11-05 10:31:39 -08:00
this.forceUpdate();
2018-11-05 17:46:09 +01:00
};
renderVizTab() {
2018-07-06 04:42:59 -07:00
return (
<div className="viz-editor">
2018-07-07 05:10:03 -07:00
<div className="viz-editor-col1">
<VizTypePicker currentType={this.props.panel.type} onTypeChanged={this.props.onTypeChanged} />
2018-07-06 04:42:59 -07:00
</div>
2018-07-07 05:10:03 -07:00
<div className="viz-editor-col2">
<h5 className="page-heading">Options</h5>
{this.renderPanelOptions()}
2018-07-06 04:42:59 -07:00
</div>
</div>
);
2018-06-19 21:25:57 +02:00
}
onChangeTab = (tab: PanelEditorTab) => {
2018-09-18 15:32:06 +02:00
store.dispatch(
updateLocation({
query: { tab: tab.id },
2018-10-10 09:26:17 +02:00
partial: true,
2018-09-18 15:32:06 +02:00
})
);
2018-11-05 17:46:09 +01:00
this.forceUpdate();
};
2018-06-26 16:45:42 +02:00
2018-06-19 14:51:57 +02:00
render() {
2018-10-04 09:14:47 +02:00
const { location } = store.getState();
const activeTab = location.query.tab || 'queries';
2018-06-19 14:51:57 +02:00
return (
<div className="tabbed-view tabbed-view--new">
2018-06-19 14:51:57 +02:00
<div className="tabbed-view-header">
<ul className="gf-tabs">
2018-06-28 04:31:55 -07:00
{this.tabs.map(tab => {
return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
})}
2018-06-19 14:51:57 +02:00
</ul>
<button className="tabbed-view-close-btn" ng-click="ctrl.exitFullscreen();">
<i className="fa fa-remove" />
</button>
</div>
2018-06-19 21:25:57 +02:00
<div className="tabbed-view-body">
{activeTab === 'queries' && this.renderQueriesTab()}
{activeTab === 'visualization' && this.renderVizTab()}
2018-06-19 21:25:57 +02:00
</div>
2018-06-19 14:51:57 +02:00
</div>
);
}
}
interface TabItemParams {
tab: PanelEditorTab;
activeTab: string;
onClick: (tab: PanelEditorTab) => void;
}
function TabItem({ tab, activeTab, onClick }: TabItemParams) {
const tabClasses = classNames({
'gf-tabs-link': true,
active: activeTab === tab.id,
});
return (
<li className="gf-tabs-item" key={tab.id}>
<a className={tabClasses} onClick={() => onClick(tab)}>
<i className={tab.icon} /> {tab.text}
</a>
</li>
);
}