import React, { PureComponent } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import AutoSizer from 'react-virtualized-auto-sizer'; import { css } from '@emotion/css'; import { Subscription } from 'rxjs'; import { FieldConfigSource, GrafanaTheme } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { HorizontalGroup, InlineSwitch, ModalsController, PageToolbar, RadioButtonGroup, stylesFactory, ToolbarButton, } from '@grafana/ui'; import config from 'app/core/config'; import { appEvents } from 'app/core/core'; import { calculatePanelSize } from './utils'; import { PanelEditorTabs } from './PanelEditorTabs'; import { DashNavTimeControls } from '../DashNav/DashNavTimeControls'; import { OptionsPane } from './OptionsPane'; import { SubMenuItems } from 'app/features/dashboard/components/SubMenu/SubMenuItems'; import { SplitPaneWrapper } from 'app/core/components/SplitPaneWrapper/SplitPaneWrapper'; import { SaveDashboardModalProxy } from '../SaveDashboard/SaveDashboardModalProxy'; import { DashboardPanel } from '../../dashgrid/DashboardPanel'; import { discardPanelChanges, initPanelEditor, updatePanelEditorUIState } from './state/actions'; import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; import { toggleTableView } from './state/reducers'; import { getPanelEditorTabs } from './state/selectors'; import { getPanelStateById } from '../../state/selectors'; import { getVariables } from 'app/features/variables/state/selectors'; import { StoreState } from 'app/types'; import { DisplayMode, displayModes, PanelEditorTab } from './types'; import { DashboardModel, PanelModel } from '../../state'; import { VisualizationButton } from './VisualizationButton'; import { PanelOptionsChangedEvent, ShowModalReactEvent } from 'app/types/events'; import { locationService } from '@grafana/runtime'; import { UnlinkModal } from '../../../library-panels/components/UnlinkModal/UnlinkModal'; import { SaveLibraryPanelModal } from 'app/features/library-panels/components/SaveLibraryPanelModal/SaveLibraryPanelModal'; import { isPanelModelLibraryPanel } from '../../../library-panels/guard'; import { getLibraryPanelConnectedDashboards } from '../../../library-panels/state/api'; import { createPanelLibraryErrorNotification, createPanelLibrarySuccessNotification, saveAndRefreshLibraryPanel, } from '../../../library-panels/utils'; import { notifyApp } from '../../../../core/actions'; import { PanelEditorTableView } from './PanelEditorTableView'; import { PanelModelWithLibraryPanel } from 'app/features/library-panels/types'; interface OwnProps { dashboard: DashboardModel; sourcePanel: PanelModel; tab?: string; } const mapStateToProps = (state: StoreState) => { const panel = state.panelEditor.getPanel(); const { plugin } = getPanelStateById(state.dashboard, panel.id); return { plugin: plugin, panel, initDone: state.panelEditor.initDone, uiState: state.panelEditor.ui, tableViewEnabled: state.panelEditor.tableViewEnabled, variables: getVariables(state), }; }; const mapDispatchToProps = { initPanelEditor, discardPanelChanges, updatePanelEditorUIState, updateTimeZoneForSession, toggleTableView, notifyApp, }; const connector = connect(mapStateToProps, mapDispatchToProps); type Props = OwnProps & ConnectedProps; interface State { showSaveLibraryPanelModal?: boolean; } export class PanelEditorUnconnected extends PureComponent { private eventSubs?: Subscription; state: State = { showSaveLibraryPanelModal: false, }; componentDidMount() { this.props.initPanelEditor(this.props.sourcePanel, this.props.dashboard); } componentDidUpdate() { const { panel, initDone } = this.props; if (initDone && !this.eventSubs) { this.eventSubs = new Subscription(); this.eventSubs.add(panel.events.subscribe(PanelOptionsChangedEvent, this.triggerForceUpdate)); } } componentWillUnmount() { // redux action exitPanelEditor is called on location change from DashboardPrompt this.eventSubs?.unsubscribe(); } triggerForceUpdate = () => { this.forceUpdate(); }; onBack = () => { locationService.partial({ editPanel: null, tab: null, }); }; onDiscard = () => { this.props.discardPanelChanges(); this.onBack(); }; onOpenDashboardSettings = () => { locationService.partial({ editview: 'settings', }); }; onSaveDashboard = () => { appEvents.publish( new ShowModalReactEvent({ component: SaveDashboardModalProxy, props: { dashboard: this.props.dashboard }, }) ); }; onSaveLibraryPanel = async () => { if (!isPanelModelLibraryPanel(this.props.panel)) { // New library panel, no need to display modal return; } const connectedDashboards = await getLibraryPanelConnectedDashboards(this.props.panel.libraryPanel.uid); if ( connectedDashboards.length === 0 || (connectedDashboards.length === 1 && connectedDashboards.includes(this.props.dashboard.id)) ) { try { await saveAndRefreshLibraryPanel(this.props.panel, this.props.dashboard.meta.folderId!); this.props.notifyApp(createPanelLibrarySuccessNotification('Library panel saved')); } catch (err) { this.props.notifyApp(createPanelLibraryErrorNotification(`Error saving library panel: "${err.statusText}"`)); } return; } this.setState({ showSaveLibraryPanelModal: true }); }; onChangeTab = (tab: PanelEditorTab) => { locationService.partial({ tab: tab.id, }); }; onFieldConfigChange = (config: FieldConfigSource) => { // we do not need to trigger force update here as the function call below // fires PanelOptionsChangedEvent which we subscribe to above this.props.panel.updateFieldConfig({ ...config, }); }; onPanelOptionsChanged = (options: any) => { // we do not need to trigger force update here as the function call below // fires PanelOptionsChangedEvent which we subscribe to above this.props.panel.updateOptions(options); }; onPanelConfigChanged = (configKey: keyof PanelModel, value: any) => { this.props.panel.setProperty(configKey, value); this.props.panel.render(); this.forceUpdate(); }; onDisplayModeChange = (mode?: DisplayMode) => { const { updatePanelEditorUIState } = this.props; if (this.props.tableViewEnabled) { this.props.toggleTableView(); } updatePanelEditorUIState({ mode: mode, }); }; onToggleTableView = () => { this.props.toggleTableView(); }; onTogglePanelOptions = () => { const { uiState, updatePanelEditorUIState } = this.props; updatePanelEditorUIState({ isPanelOptionsVisible: !uiState.isPanelOptionsVisible }); }; renderPanel(styles: EditorStyles, noTabsBelow: boolean) { const { dashboard, panel, uiState, tableViewEnabled } = this.props; return (
{this.renderPanelToolbar(styles)}
{({ width, height }) => { if (width < 3 || height < 3) { return null; } // If no tabs limit height so panel does not extend to edge if (noTabsBelow) { height -= config.theme2.spacing.gridSize * 2; } if (tableViewEnabled) { return ; } const panelSize = calculatePanelSize(uiState.mode, width, height, panel); return (
); }}
); } renderPanelAndEditor(styles: EditorStyles) { const { panel, dashboard, plugin, tab } = this.props; const tabs = getPanelEditorTabs(tab, plugin); if (tabs.length > 0) { return [ this.renderPanel(styles, false),
, ]; } return this.renderPanel(styles, true); } renderTemplateVariables(styles: EditorStyles) { const { variables } = this.props; if (!variables.length) { return null; } return (
); } renderPanelToolbar(styles: EditorStyles) { const { dashboard, uiState, variables, updateTimeZoneForSession, panel, tableViewEnabled } = this.props; return (
0 ? 'space-between' : 'flex-end'} align="flex-start"> {this.renderTemplateVariables(styles)} {!uiState.isPanelOptionsVisible && }
); } renderEditorActions() { let editorActions = [ , Discard , this.props.panel.libraryPanel ? ( Save library panel ) : ( Save ), Apply , ]; if (this.props.panel.libraryPanel) { editorActions.splice( 1, 0, {({ showModal, hideModal }) => { return ( { showModal(UnlinkModal, { onConfirm: () => { delete this.props.panel.libraryPanel; this.props.panel.render(); this.forceUpdate(); }, onDismiss: hideModal, isOpen: true, }); }} title="Disconnects this panel from the library panel so that you can edit it regularly." key="unlink" > Unlink ); }} ); // Remove "Apply" button editorActions.pop(); } return editorActions; } renderOptionsPane() { const { plugin, dashboard, panel, uiState } = this.props; const rightPaneSize = uiState.rightPaneSize <= 1 ? (uiState.rightPaneSize as number) * window.innerWidth : (uiState.rightPaneSize as number); if (!plugin) { return
; } return ( ); } onGoBackToDashboard = () => { locationService.partial({ editPanel: null, tab: null }); }; onConfirmAndDismissLibarayPanelModel = () => { this.setState({ showSaveLibraryPanelModal: false }); }; render() { const { dashboard, initDone, updatePanelEditorUIState, uiState } = this.props; const styles = getStyles(config.theme, this.props); if (!initDone) { return null; } return (
{this.renderEditorActions()}
{this.state.showSaveLibraryPanelModal && ( )}
); } } export const PanelEditor = connector(PanelEditorUnconnected); /* * Styles */ export const getStyles = stylesFactory((theme: GrafanaTheme, props: Props) => { const { uiState } = props; const paneSpacing = theme.spacing.md; 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}; `, 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}; padding-right: 2px; `, tabsWrapper: css` height: 100%; width: 100%; `, panelToolbar: css` display: flex; padding: 0 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; position: relative; `, }; }); type EditorStyles = ReturnType;