2020-09-28 02:06:46 -05:00
|
|
|
import React, { PureComponent } from 'react';
|
2020-02-12 03:42:57 -06:00
|
|
|
import { config } from 'app/core/config';
|
2020-02-09 08:39:46 -06:00
|
|
|
import { css } from 'emotion';
|
2020-04-16 14:28:19 -05:00
|
|
|
import { IconName, stylesFactory, Tab, TabContent, TabsBar } from '@grafana/ui';
|
2020-02-09 08:39:46 -06:00
|
|
|
import { QueriesTab } from '../../panel_editor/QueriesTab';
|
2020-02-09 10:39:26 -06:00
|
|
|
import { AlertTab } from 'app/features/alerting/AlertTab';
|
2020-04-06 23:54:09 -05:00
|
|
|
import { TransformationsEditor } from '../TransformationsEditor/TransformationsEditor';
|
2020-09-28 02:06:46 -05:00
|
|
|
import { DashboardModel, PanelModel } from '../../state';
|
|
|
|
import { PanelEditorTab, PanelEditorTabId } from './types';
|
2020-11-26 08:14:57 -06:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { PanelQueriesChangedEvent, PanelTransformationsChangedEvent } from 'app/types/events';
|
2020-02-09 08:39:46 -06:00
|
|
|
|
|
|
|
interface PanelEditorTabsProps {
|
|
|
|
panel: PanelModel;
|
|
|
|
dashboard: DashboardModel;
|
2020-02-12 03:42:57 -06:00
|
|
|
tabs: PanelEditorTab[];
|
|
|
|
onChangeTab: (tab: PanelEditorTab) => void;
|
2020-02-09 08:39:46 -06:00
|
|
|
}
|
|
|
|
|
2020-09-28 02:06:46 -05:00
|
|
|
export class PanelEditorTabs extends PureComponent<PanelEditorTabsProps> {
|
2020-11-26 08:14:57 -06:00
|
|
|
private eventSubs = new Subscription();
|
|
|
|
|
2020-09-28 02:06:46 -05:00
|
|
|
componentDidMount() {
|
2020-11-26 08:14:57 -06:00
|
|
|
const { events } = this.props.panel;
|
|
|
|
this.eventSubs.add(events.subscribe(PanelQueriesChangedEvent, this.triggerForceUpdate));
|
|
|
|
this.eventSubs.add(events.subscribe(PanelTransformationsChangedEvent, this.triggerForceUpdate));
|
2020-09-28 02:06:46 -05:00
|
|
|
}
|
2020-05-25 07:05:43 -05:00
|
|
|
|
2020-09-28 02:06:46 -05:00
|
|
|
componentWillUnmount() {
|
2020-11-26 08:14:57 -06:00
|
|
|
this.eventSubs.unsubscribe();
|
2020-09-28 02:06:46 -05:00
|
|
|
}
|
2020-04-17 07:46:32 -05:00
|
|
|
|
2020-09-28 02:06:46 -05:00
|
|
|
triggerForceUpdate = () => {
|
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
getCounter = (tab: PanelEditorTab) => {
|
|
|
|
const { panel } = this.props;
|
|
|
|
|
|
|
|
switch (tab.id) {
|
|
|
|
case PanelEditorTabId.Query:
|
|
|
|
return panel.targets.length;
|
|
|
|
case PanelEditorTabId.Alert:
|
|
|
|
return panel.alert ? 1 : 0;
|
|
|
|
case PanelEditorTabId.Transform:
|
|
|
|
const transformations = panel.getTransformations() ?? [];
|
|
|
|
return transformations.length;
|
|
|
|
}
|
2020-02-12 03:42:57 -06:00
|
|
|
|
|
|
|
return null;
|
2020-09-28 02:06:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { dashboard, onChangeTab, tabs, panel } = this.props;
|
|
|
|
const styles = getPanelEditorTabsStyles();
|
|
|
|
const activeTab = tabs.find(item => item.active)!;
|
2020-02-12 03:42:57 -06:00
|
|
|
|
2020-09-28 02:06:46 -05:00
|
|
|
if (tabs.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<TabsBar className={styles.tabBar}>
|
|
|
|
{tabs.map(tab => {
|
|
|
|
return (
|
|
|
|
<Tab
|
|
|
|
key={tab.id}
|
|
|
|
label={tab.text}
|
|
|
|
active={tab.active}
|
|
|
|
onChangeTab={() => onChangeTab(tab)}
|
|
|
|
icon={tab.icon as IconName}
|
|
|
|
counter={this.getCounter(tab)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</TabsBar>
|
|
|
|
<TabContent className={styles.tabContent}>
|
|
|
|
{activeTab.id === PanelEditorTabId.Query && <QueriesTab panel={panel} dashboard={dashboard} />}
|
|
|
|
{activeTab.id === PanelEditorTabId.Alert && <AlertTab panel={panel} dashboard={dashboard} />}
|
|
|
|
{activeTab.id === PanelEditorTabId.Transform && <TransformationsEditor panel={panel} />}
|
|
|
|
</TabContent>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 03:42:57 -06:00
|
|
|
|
2020-02-09 08:39:46 -06:00
|
|
|
const getPanelEditorTabsStyles = stylesFactory(() => {
|
2020-02-12 03:42:57 -06:00
|
|
|
const { theme } = config;
|
|
|
|
|
2020-02-09 08:39:46 -06:00
|
|
|
return {
|
|
|
|
wrapper: css`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
height: 100%;
|
|
|
|
`,
|
2020-02-25 10:58:20 -06:00
|
|
|
tabBar: css`
|
2020-03-30 07:39:18 -05:00
|
|
|
padding-left: ${theme.spacing.md};
|
2020-02-25 10:58:20 -06:00
|
|
|
`,
|
2020-02-12 03:42:57 -06:00
|
|
|
tabContent: css`
|
|
|
|
padding: 0;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2020-02-09 08:39:46 -06:00
|
|
|
flex-grow: 1;
|
2020-02-12 03:42:57 -06:00
|
|
|
min-height: 0;
|
2020-03-24 04:30:53 -05:00
|
|
|
background: ${theme.colors.panelBg};
|
2020-03-30 07:39:18 -05:00
|
|
|
border-right: 1px solid ${theme.colors.pageHeaderBorder};
|
2020-02-12 03:42:57 -06:00
|
|
|
|
|
|
|
.toolbar {
|
|
|
|
background: transparent;
|
|
|
|
}
|
2020-02-09 08:39:46 -06:00
|
|
|
`,
|
|
|
|
};
|
|
|
|
});
|