import { css } from '@emotion/css'; import React, { PureComponent } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import AutoSizer from 'react-virtualized-auto-sizer'; import { Subscription } from 'rxjs'; import { FieldConfigSource, GrafanaTheme2, NavModel, NavModelItem, PageLayoutType } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { config, locationService } from '@grafana/runtime'; import { Button, HorizontalGroup, InlineSwitch, ModalsController, PageToolbar, RadioButtonGroup, Stack, stylesFactory, Themeable2, ToolbarButton, ToolbarButtonRow, withTheme2, } from '@grafana/ui'; import { AppChromeUpdate } from 'app/core/components/AppChrome/AppChromeUpdate'; import { Page } from 'app/core/components/Page/Page'; import { SplitPaneWrapper } from 'app/core/components/SplitPaneWrapper/SplitPaneWrapper'; import { appEvents } from 'app/core/core'; import { SubMenuItems } from 'app/features/dashboard/components/SubMenu/SubMenuItems'; import { SaveLibraryPanelModal } from 'app/features/library-panels/components/SaveLibraryPanelModal/SaveLibraryPanelModal'; import { PanelModelWithLibraryPanel } from 'app/features/library-panels/types'; import { getPanelStateForModel } from 'app/features/panel/state/selectors'; import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; import { StoreState } from 'app/types'; import { PanelOptionsChangedEvent, ShowModalReactEvent } from 'app/types/events'; import { notifyApp } from '../../../../core/actions'; import { UnlinkModal } from '../../../library-panels/components/UnlinkModal/UnlinkModal'; import { isPanelModelLibraryPanel } from '../../../library-panels/guard'; import { getVariablesByKey } from '../../../variables/state/selectors'; import { DashboardPanel } from '../../dashgrid/DashboardPanel'; import { DashboardModel, PanelModel } from '../../state'; import { DashNavTimeControls } from '../DashNav/DashNavTimeControls'; import { SaveDashboardDrawer } from '../SaveDashboard/SaveDashboardDrawer'; import { OptionsPane } from './OptionsPane'; import { PanelEditorTableView } from './PanelEditorTableView'; import { PanelEditorTabs } from './PanelEditorTabs'; import { VisualizationButton } from './VisualizationButton'; import { discardPanelChanges, initPanelEditor, updatePanelEditorUIState } from './state/actions'; import { toggleTableView } from './state/reducers'; import { getPanelEditorTabs } from './state/selectors'; import { DisplayMode, displayModes, PanelEditorTab } from './types'; import { calculatePanelSize } from './utils'; interface OwnProps { dashboard: DashboardModel; sourcePanel: PanelModel; sectionNav: NavModel; pageNav: NavModelItem; className?: string; tab?: string; } const mapStateToProps = (state: StoreState, ownProps: OwnProps) => { const panel = state.panelEditor.getPanel(); const panelState = getPanelStateForModel(state, panel); return { panel, plugin: panelState?.plugin, instanceState: panelState?.instanceState, initDone: state.panelEditor.initDone, uiState: state.panelEditor.ui, tableViewEnabled: state.panelEditor.tableViewEnabled, variables: getVariablesByKey(ownProps.dashboard.uid, state), }; }; const mapDispatchToProps = { initPanelEditor, discardPanelChanges, updatePanelEditorUIState, updateTimeZoneForSession, toggleTableView, notifyApp, }; const connector = connect(mapStateToProps, mapDispatchToProps); type Props = OwnProps & ConnectedProps & Themeable2; 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, showCategory: null, }); }; onDiscard = () => { this.props.discardPanelChanges(); this.onBack(); }; onSaveDashboard = () => { appEvents.publish( new ShowModalReactEvent({ component: SaveDashboardDrawer, props: { dashboard: this.props.dashboard }, }) ); }; onSaveLibraryPanel = async () => { if (!isPanelModelLibraryPanel(this.props.panel)) { // New library panel, no need to display modal 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, isOnlyPanel: boolean) { const { dashboard, panel, uiState, tableViewEnabled, theme } = 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 (isOnlyPanel) { height -= theme.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); const isOnlyPanel = tabs.length === 0; const panelPane = this.renderPanel(styles, isOnlyPanel); if (tabs.length === 0) { return panelPane; } return [ panelPane,
, ]; } 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() { const size = config.featureToggles.topnav ? 'sm' : 'md'; let editorActions = [ , this.props.panel.libraryPanel ? ( ) : ( ), , ]; 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, instanceState } = this.props; if (!plugin) { return
; } return ( ); } onGoBackToDashboard = () => { locationService.partial({ editPanel: null, tab: null, showCategory: null }); }; onConfirmAndDismissLibarayPanelModel = () => { this.setState({ showSaveLibraryPanelModal: false }); }; renderToolbar() { if (config.featureToggles.topnav) { return ( {this.renderEditorActions()}} /> ); } return ( {this.renderEditorActions()} ); } render() { const { initDone, updatePanelEditorUIState, uiState, theme, sectionNav, pageNav, className } = this.props; const styles = getStyles(theme, this.props); if (!initDone) { return null; } return (
{this.state.showSaveLibraryPanelModal && ( )}
); } } export const PanelEditor = withTheme2(connector(PanelEditorUnconnected)); /* * Styles */ export const getStyles = stylesFactory((theme: GrafanaTheme2, props: Props) => { const { uiState } = props; const paneSpacing = theme.spacing(2); return { wrapper: css({ width: '100%', flexGrow: 1, minHeight: 0, display: 'flex', paddingTop: config.featureToggles.topnav ? theme.spacing(2) : 0, }), 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; gap: ${theme.spacing(1, 2)}; `, panelWrapper: css` flex: 1 1 0; min-height: 0; width: 100%; padding-left: ${paneSpacing}; `, 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(1)}; `, centeringContainer: css` display: flex; justify-content: center; align-items: center; position: relative; flex-direction: column; `, }; }); type EditorStyles = ReturnType;