import React, { PureComponent } from 'react'; import { FieldConfigSource, GrafanaTheme, PanelPlugin } from '@grafana/data'; import { Button, HorizontalGroup, Icon, RadioButtonGroup, stylesFactory } from '@grafana/ui'; import { css, cx } from 'emotion'; import config from 'app/core/config'; import AutoSizer from 'react-virtualized-auto-sizer'; import { PanelModel } from '../../state/PanelModel'; import { DashboardModel } from '../../state/DashboardModel'; import { DashboardPanel } from '../../dashgrid/DashboardPanel'; import SplitPane from 'react-split-pane'; import { StoreState } from '../../../../types/store'; import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux'; import { updateLocation } from '../../../../core/reducers/location'; import { Unsubscribable } from 'rxjs'; import { DisplayMode, displayModes, PanelEditorTab } from './types'; import { PanelEditorTabs } from './PanelEditorTabs'; import { DashNavTimeControls } from '../DashNav/DashNavTimeControls'; import { CoreEvents, LocationState } from 'app/types'; import { calculatePanelSize } from './utils'; import { initPanelEditor, panelEditorCleanUp, updatePanelEditorUIState } from './state/actions'; import { PanelEditorUIState, setDiscardChanges } from './state/reducers'; import { getPanelEditorTabs } from './state/selectors'; import { getPanelStateById } from '../../state/selectors'; import { OptionsPaneContent } from './OptionsPaneContent'; import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; import { DashNavButton } from 'app/features/dashboard/components/DashNav/DashNavButton'; import { VariableModel } from 'app/features/variables/types'; import { getVariables } from 'app/features/variables/state/selectors'; import { SubMenuItems } from 'app/features/dashboard/components/SubMenu/SubMenuItems'; import { BackButton } from 'app/core/components/BackButton/BackButton'; import { appEvents } from 'app/core/core'; import { SaveDashboardModalProxy } from '../SaveDashboard/SaveDashboardModalProxy'; import { selectors } from '@grafana/e2e-selectors'; interface OwnProps { dashboard: DashboardModel; sourcePanel: PanelModel; } interface ConnectedProps { location: LocationState; plugin?: PanelPlugin; panel: PanelModel; initDone: boolean; tabs: PanelEditorTab[]; uiState: PanelEditorUIState; variables: VariableModel[]; } interface DispatchProps { updateLocation: typeof updateLocation; initPanelEditor: typeof initPanelEditor; panelEditorCleanUp: typeof panelEditorCleanUp; setDiscardChanges: typeof setDiscardChanges; updatePanelEditorUIState: typeof updatePanelEditorUIState; updateTimeZoneForSession: typeof updateTimeZoneForSession; } type Props = OwnProps & ConnectedProps & DispatchProps; export class PanelEditorUnconnected extends PureComponent { querySubscription: Unsubscribable; componentDidMount() { this.props.initPanelEditor(this.props.sourcePanel, this.props.dashboard); } componentWillUnmount() { this.props.panelEditorCleanUp(); } onPanelExit = () => { this.props.updateLocation({ query: { editPanel: null, tab: null }, partial: true, }); }; onDiscard = () => { this.props.setDiscardChanges(true); this.props.updateLocation({ query: { editPanel: null, tab: null }, partial: true, }); }; onOpenDashboardSettings = () => { this.props.updateLocation({ query: { editview: 'settings' }, partial: true }); }; onSaveDashboard = () => { appEvents.emit(CoreEvents.showModalReact, { component: SaveDashboardModalProxy, props: { dashboard: this.props.dashboard }, }); }; onChangeTab = (tab: PanelEditorTab) => { this.props.updateLocation({ query: { tab: tab.id }, partial: true }); }; onFieldConfigChange = (config: FieldConfigSource) => { const { panel } = this.props; panel.updateFieldConfig({ ...config, }); this.forceUpdate(); }; onPanelOptionsChanged = (options: any) => { this.props.panel.updateOptions(options); this.forceUpdate(); }; onPanelConfigChanged = (configKey: string, value: any) => { // @ts-ignore this.props.panel[configKey] = value; this.props.panel.render(); this.forceUpdate(); }; onDragFinished = (pane: Pane, size?: number) => { document.body.style.cursor = 'auto'; // When the drag handle is just clicked size is undefined if (!size) { return; } const targetPane = pane === Pane.Top ? 'topPaneSize' : 'rightPaneSize'; const { updatePanelEditorUIState } = this.props; updatePanelEditorUIState({ [targetPane]: size, }); }; onDragStarted = () => { document.body.style.cursor = 'row-resize'; }; onDisplayModeChange = (mode: DisplayMode) => { const { updatePanelEditorUIState } = this.props; updatePanelEditorUIState({ mode: mode, }); }; onTogglePanelOptions = () => { const { uiState, updatePanelEditorUIState } = this.props; updatePanelEditorUIState({ isPanelOptionsVisible: !uiState.isPanelOptionsVisible }); }; renderPanel = (styles: EditorStyles) => { const { dashboard, panel, tabs, uiState } = this.props; return (
{this.renderPanelToolbar(styles)}
{({ width, height }) => { if (width < 3 || height < 3) { return null; } return (
); }}
); }; renderHorizontalSplit(styles: EditorStyles) { const { dashboard, panel, tabs, uiState } = this.props; return tabs.length > 0 ? ( this.onDragFinished(Pane.Top, size)} > {this.renderPanel(styles)}
) : ( this.renderPanel(styles) ); } renderTemplateVariables(styles: EditorStyles) { const { variables } = this.props; if (!variables.length) { return null; } return (
); } renderPanelToolbar(styles: EditorStyles) { const { dashboard, location, uiState, variables, updateTimeZoneForSession } = this.props; return (
0 ? 'space-between' : 'flex-end'} align="flex-start"> {this.renderTemplateVariables(styles)} {!uiState.isPanelOptionsVisible && ( Show options )}
); } editorToolbar(styles: EditorStyles) { const { dashboard } = this.props; return (
{dashboard.title} / Edit Panel
); } renderOptionsPane() { const { plugin, dashboard, panel, uiState } = this.props; if (!plugin) { return
; } return ( ); } renderWithOptionsPane(styles: EditorStyles) { const { uiState } = this.props; return ( (document.body.style.cursor = 'col-resize')} onDragFinished={size => this.onDragFinished(Pane.Right, size)} > {this.renderHorizontalSplit(styles)} {this.renderOptionsPane()} ); } render() { const { initDone, uiState } = this.props; const styles = getStyles(config.theme, this.props); if (!initDone) { return null; } return (
{this.editorToolbar(styles)}
{uiState.isPanelOptionsVisible ? this.renderWithOptionsPane(styles) : this.renderHorizontalSplit(styles)}
); } } const mapStateToProps: MapStateToProps = state => { const panel = state.panelEditor.getPanel(); const { plugin } = getPanelStateById(state.dashboard, panel.id); return { location: state.location, plugin: plugin, panel, initDone: state.panelEditor.initDone, tabs: getPanelEditorTabs(state.location, plugin), uiState: state.panelEditor.ui, variables: getVariables(state), }; }; const mapDispatchToProps: MapDispatchToProps = { updateLocation, initPanelEditor, panelEditorCleanUp, setDiscardChanges, updatePanelEditorUIState, updateTimeZoneForSession, }; export const PanelEditor = connect(mapStateToProps, mapDispatchToProps)(PanelEditorUnconnected); enum Pane { Right, Top, } /* * Styles */ export const getStyles = stylesFactory((theme: GrafanaTheme, props: Props) => { const { uiState } = props; const handleColor = theme.palette.blue95; const paneSpacing = theme.spacing.md; const resizer = css` font-style: italic; background: transparent; border-top: 0; border-right: 0; border-bottom: 0; border-left: 0; border-color: transparent; border-style: solid; transition: 0.2s border-color ease-in-out; &:hover { border-color: ${handleColor}; } `; return { wrapper: css` width: 100%; height: 100%; position: fixed; z-index: ${theme.zIndex.sidemenu}; top: 0; left: 0; right: 0; bottom: 0; background: ${theme.colors.dashboardBg}; display: flex; flex-direction: column; `, verticalSplitPanesWrapper: css` display: flex; flex-direction: column; height: 100%; width: 100%; position: relative; `, mainPaneWrapper: css` display: flex; flex-direction: column; height: 100%; width: 100%; padding-right: ${uiState.isPanelOptionsVisible ? 0 : paneSpacing}; `, mainPaneWrapperNoTabs: css` padding-bottom: ${paneSpacing}; `, variablesWrapper: css` label: variablesWrapper; display: flex; flex-grow: 1; flex-wrap: wrap; `, panelWrapper: css` flex: 1 1 0; min-height: 0; width: 100%; padding-left: ${paneSpacing}; `, resizerV: cx( resizer, css` cursor: col-resize; width: ${paneSpacing}; border-right-width: 1px; margin-top: 18px; ` ), resizerH: cx( resizer, css` height: ${paneSpacing}; cursor: row-resize; position: relative; top: 0px; z-index: 1; border-top-width: 1px; margin-left: ${paneSpacing}; ` ), tabsWrapper: css` height: 100%; width: 100%; `, editorToolbar: css` display: flex; padding: ${theme.spacing.sm}; background: ${theme.colors.panelBg}; justify-content: space-between; border-bottom: 1px solid ${theme.colors.panelBorder}; `, panelToolbar: css` display: flex; padding: ${paneSpacing} 0 ${paneSpacing} ${paneSpacing}; justify-content: space-between; flex-wrap: wrap; `, toolbarLeft: css` padding-left: ${theme.spacing.sm}; `, centeringContainer: css` display: flex; justify-content: center; align-items: center; `, editorTitle: css` font-size: ${theme.typography.size.lg}; padding-left: ${theme.spacing.md}; `, }; }); type EditorStyles = ReturnType;