2020-02-11 06:48:36 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2020-02-11 07:57:16 -06:00
|
|
|
import { GrafanaTheme, FieldConfigSource, PanelData, PanelPlugin, SelectableValue } from '@grafana/data';
|
2020-02-11 06:48:36 -06:00
|
|
|
import { stylesFactory, Forms, CustomScrollbar, selectThemeVariant, ControlledCollapse } from '@grafana/ui';
|
2020-02-08 05:01:10 -06:00
|
|
|
import { css, cx } from 'emotion';
|
2019-12-16 02:18:48 -06:00
|
|
|
import config from 'app/core/config';
|
2020-02-09 08:39:46 -06:00
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
2019-12-16 02:18:48 -06:00
|
|
|
|
|
|
|
import { PanelModel } from '../../state/PanelModel';
|
|
|
|
import { DashboardModel } from '../../state/DashboardModel';
|
|
|
|
import { DashboardPanel } from '../../dashgrid/DashboardPanel';
|
2020-02-09 08:39:46 -06:00
|
|
|
|
2020-02-08 05:01:10 -06:00
|
|
|
import SplitPane from 'react-split-pane';
|
2020-02-07 07:59:04 -06:00
|
|
|
import { StoreState } from '../../../../types/store';
|
2020-02-11 07:57:16 -06:00
|
|
|
import { connect, MapStateToProps, MapDispatchToProps } from 'react-redux';
|
2020-02-07 07:59:04 -06:00
|
|
|
import { updateLocation } from '../../../../core/reducers/location';
|
2020-02-08 06:23:16 -06:00
|
|
|
import { Unsubscribable } from 'rxjs';
|
2020-02-08 12:31:17 -06:00
|
|
|
import { PanelTitle } from './PanelTitle';
|
2020-02-09 08:39:46 -06:00
|
|
|
import { DisplayMode, displayModes } from './types';
|
|
|
|
import { PanelEditorTabs } from './PanelEditorTabs';
|
2020-02-09 11:48:14 -06:00
|
|
|
import { DashNavTimeControls } from '../DashNav/DashNavTimeControls';
|
2020-02-11 07:57:16 -06:00
|
|
|
import { LocationState } from 'app/types';
|
2020-02-11 06:48:36 -06:00
|
|
|
import { calculatePanelSize } from './utils';
|
2020-02-11 07:57:16 -06:00
|
|
|
import { initPanelEditor, panelEditorCleanUp } from './state/actions';
|
|
|
|
import { setDisplayMode, toggleOptionsView, setDiscardChanges } from './state/reducers';
|
2020-02-11 06:48:36 -06:00
|
|
|
import { FieldConfigEditor } from './FieldConfigEditor';
|
2019-12-16 02:18:48 -06:00
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
interface OwnProps {
|
2019-12-16 02:18:48 -06:00
|
|
|
dashboard: DashboardModel;
|
2020-02-08 11:29:09 -06:00
|
|
|
sourcePanel: PanelModel;
|
2019-12-16 02:18:48 -06:00
|
|
|
}
|
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
interface ConnectedProps {
|
|
|
|
location: LocationState;
|
|
|
|
plugin?: PanelPlugin;
|
2020-02-08 11:29:09 -06:00
|
|
|
panel: PanelModel;
|
2020-02-08 06:23:16 -06:00
|
|
|
data: PanelData;
|
2020-02-09 08:39:46 -06:00
|
|
|
mode: DisplayMode;
|
2020-02-11 07:57:16 -06:00
|
|
|
isPanelOptionsVisible: boolean;
|
|
|
|
initDone: boolean;
|
2019-12-16 02:18:48 -06:00
|
|
|
}
|
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
interface DispatchProps {
|
|
|
|
updateLocation: typeof updateLocation;
|
|
|
|
initPanelEditor: typeof initPanelEditor;
|
|
|
|
panelEditorCleanUp: typeof panelEditorCleanUp;
|
|
|
|
setDisplayMode: typeof setDisplayMode;
|
|
|
|
toggleOptionsView: typeof toggleOptionsView;
|
|
|
|
setDiscardChanges: typeof setDiscardChanges;
|
|
|
|
}
|
2020-02-08 11:29:09 -06:00
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
type Props = OwnProps & ConnectedProps & DispatchProps;
|
2020-02-09 11:48:14 -06:00
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
export class PanelEditorUnconnected extends PureComponent<Props> {
|
|
|
|
querySubscription: Unsubscribable;
|
2020-02-09 11:48:14 -06:00
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
componentDidMount() {
|
|
|
|
this.props.initPanelEditor(this.props.sourcePanel, this.props.dashboard);
|
2020-02-08 06:23:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-02-11 07:57:16 -06:00
|
|
|
this.props.panelEditorCleanUp();
|
2020-02-07 07:59:04 -06:00
|
|
|
}
|
2019-12-16 02:18:48 -06:00
|
|
|
|
2020-02-07 07:59:04 -06:00
|
|
|
onPanelExit = () => {
|
2020-02-11 07:57:16 -06:00
|
|
|
this.props.updateLocation({
|
2020-02-07 07:59:04 -06:00
|
|
|
query: { editPanel: null },
|
|
|
|
partial: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onDiscard = () => {
|
2020-02-11 07:57:16 -06:00
|
|
|
this.props.setDiscardChanges(true);
|
2020-02-07 07:59:04 -06:00
|
|
|
this.props.updateLocation({
|
|
|
|
query: { editPanel: null },
|
|
|
|
partial: true,
|
|
|
|
});
|
|
|
|
};
|
2019-12-16 02:18:48 -06:00
|
|
|
|
2020-02-08 11:29:09 -06:00
|
|
|
onFieldConfigsChange = (fieldOptions: FieldConfigSource) => {
|
|
|
|
// NOTE: for now, assume this is from 'fieldOptions' -- TODO? put on panel model directly?
|
2020-02-11 07:57:16 -06:00
|
|
|
const { panel } = this.props;
|
2020-02-08 11:29:09 -06:00
|
|
|
const options = panel.getOptions();
|
|
|
|
panel.updateOptions({
|
|
|
|
...options,
|
|
|
|
fieldOptions, // Assume it is from shared singlestat -- TODO own property?
|
|
|
|
});
|
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
renderFieldOptions() {
|
2020-02-11 07:57:16 -06:00
|
|
|
const { plugin, panel, data } = this.props;
|
|
|
|
|
2020-02-08 11:29:09 -06:00
|
|
|
const fieldOptions = panel.options['fieldOptions'] as FieldConfigSource;
|
2020-02-11 07:57:16 -06:00
|
|
|
|
2020-02-08 11:29:09 -06:00
|
|
|
if (!fieldOptions || !plugin) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-02-09 10:39:26 -06:00
|
|
|
<FieldConfigEditor
|
|
|
|
config={fieldOptions}
|
|
|
|
custom={plugin.customFieldConfigs}
|
|
|
|
onChange={this.onFieldConfigsChange}
|
|
|
|
data={data.series}
|
|
|
|
/>
|
2020-02-08 11:29:09 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onPanelOptionsChanged = (options: any) => {
|
2020-02-11 07:57:16 -06:00
|
|
|
this.props.panel.updateOptions(options);
|
2020-02-08 11:29:09 -06:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The existing visualization tab
|
|
|
|
*/
|
|
|
|
renderVisSettings() {
|
2020-02-11 07:57:16 -06:00
|
|
|
const { data, panel } = this.props;
|
|
|
|
const { plugin } = this.props;
|
|
|
|
|
2020-02-08 11:29:09 -06:00
|
|
|
if (!plugin) {
|
2020-02-11 07:57:16 -06:00
|
|
|
return null;
|
2020-02-08 11:29:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (plugin.editor && panel) {
|
2020-02-09 07:34:42 -06:00
|
|
|
return (
|
2020-02-09 10:39:26 -06:00
|
|
|
<div style={{ marginTop: '10px' }}>
|
2020-02-09 07:34:42 -06:00
|
|
|
<plugin.editor data={data} options={panel.getOptions()} onOptionsChange={this.onPanelOptionsChanged} />
|
|
|
|
</div>
|
|
|
|
);
|
2020-02-08 11:29:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return <div>No editor (angular?)</div>;
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:01:10 -06:00
|
|
|
onDragFinished = () => {
|
|
|
|
document.body.style.cursor = 'auto';
|
|
|
|
console.log('TODO, save splitter settings');
|
|
|
|
};
|
|
|
|
|
2020-02-08 12:31:17 -06:00
|
|
|
onPanelTitleChange = (title: string) => {
|
2020-02-11 07:57:16 -06:00
|
|
|
this.props.panel.title = title;
|
2020-02-08 12:31:17 -06:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2020-02-09 08:39:46 -06:00
|
|
|
onDiplayModeChange = (mode: SelectableValue<DisplayMode>) => {
|
2020-02-11 07:57:16 -06:00
|
|
|
this.props.setDisplayMode(mode.value);
|
2020-02-09 08:39:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
onTogglePanelOptions = () => {
|
2020-02-11 07:57:16 -06:00
|
|
|
this.props.toggleOptionsView();
|
2020-02-09 08:39:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
renderHorizontalSplit(styles: any) {
|
2020-02-11 07:57:16 -06:00
|
|
|
const { dashboard, panel, mode } = this.props;
|
2020-02-09 03:50:58 -06:00
|
|
|
|
|
|
|
return (
|
2020-02-09 08:39:46 -06:00
|
|
|
<SplitPane
|
|
|
|
split="horizontal"
|
|
|
|
minSize={50}
|
|
|
|
primary="second"
|
|
|
|
defaultSize="40%"
|
|
|
|
resizerClassName={styles.resizerH}
|
|
|
|
onDragStarted={() => (document.body.style.cursor = 'row-resize')}
|
|
|
|
onDragFinished={this.onDragFinished}
|
|
|
|
>
|
|
|
|
<div className={styles.panelWrapper}>
|
|
|
|
<AutoSizer>
|
|
|
|
{({ width, height }) => {
|
|
|
|
if (width < 3 || height < 3) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-09 03:50:58 -06:00
|
|
|
return (
|
2020-02-09 08:39:46 -06:00
|
|
|
<div className={styles.centeringContainer} style={{ width, height }}>
|
|
|
|
<div style={calculatePanelSize(mode, width, height, panel)}>
|
|
|
|
<DashboardPanel
|
|
|
|
dashboard={dashboard}
|
|
|
|
panel={panel}
|
|
|
|
isEditing={false}
|
|
|
|
isInEditMode
|
|
|
|
isFullscreen={false}
|
|
|
|
isInView={true}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-02-09 03:50:58 -06:00
|
|
|
);
|
2020-02-09 08:39:46 -06:00
|
|
|
}}
|
|
|
|
</AutoSizer>
|
|
|
|
</div>
|
|
|
|
<div className={styles.noScrollPaneContent}>
|
|
|
|
<PanelEditorTabs panel={panel} dashboard={dashboard} />
|
|
|
|
</div>
|
|
|
|
</SplitPane>
|
2020-02-09 03:50:58 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-16 02:18:48 -06:00
|
|
|
render() {
|
2020-02-11 07:57:16 -06:00
|
|
|
const { dashboard, location, panel, mode, isPanelOptionsVisible, initDone } = this.props;
|
2019-12-16 02:18:48 -06:00
|
|
|
const styles = getStyles(config.theme);
|
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
if (!initDone) {
|
2019-12-16 02:18:48 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-02-08 05:01:10 -06:00
|
|
|
<div className={styles.wrapper}>
|
2020-02-08 12:31:17 -06:00
|
|
|
<div className={styles.toolbar}>
|
|
|
|
<div className={styles.toolbarLeft}>
|
|
|
|
<button className="navbar-edit__back-btn" onClick={this.onPanelExit}>
|
|
|
|
<i className="fa fa-arrow-left"></i>
|
|
|
|
</button>
|
|
|
|
<PanelTitle value={panel.title} onChange={this.onPanelTitleChange} />
|
|
|
|
</div>
|
2020-02-09 08:39:46 -06:00
|
|
|
<div className={styles.toolbarLeft}>
|
|
|
|
<Forms.Select
|
|
|
|
value={displayModes.find(v => v.value === mode)}
|
|
|
|
options={displayModes}
|
|
|
|
onChange={this.onDiplayModeChange}
|
|
|
|
/>
|
|
|
|
<Forms.Button icon="fa fa-cog" variant="secondary" onClick={this.onTogglePanelOptions} />
|
2020-02-08 12:31:17 -06:00
|
|
|
<Forms.Button variant="destructive" onClick={this.onDiscard}>
|
|
|
|
Discard
|
|
|
|
</Forms.Button>
|
2020-02-09 11:48:14 -06:00
|
|
|
|
|
|
|
<div>
|
|
|
|
<DashNavTimeControls dashboard={dashboard} location={location} updateLocation={updateLocation} />
|
|
|
|
</div>
|
2020-02-08 12:31:17 -06:00
|
|
|
</div>
|
2020-02-08 05:01:10 -06:00
|
|
|
</div>
|
2020-02-08 12:31:17 -06:00
|
|
|
<div className={styles.panes}>
|
2020-02-11 07:57:16 -06:00
|
|
|
{isPanelOptionsVisible ? (
|
2020-02-08 12:31:17 -06:00
|
|
|
<SplitPane
|
2020-02-09 08:39:46 -06:00
|
|
|
split="vertical"
|
2020-02-09 03:50:58 -06:00
|
|
|
minSize={100}
|
2020-02-08 12:31:17 -06:00
|
|
|
primary="second"
|
2020-02-09 08:39:46 -06:00
|
|
|
defaultSize={350}
|
|
|
|
resizerClassName={styles.resizerV}
|
|
|
|
onDragStarted={() => (document.body.style.cursor = 'col-resize')}
|
2020-02-08 12:31:17 -06:00
|
|
|
onDragFinished={this.onDragFinished}
|
|
|
|
>
|
2020-02-09 08:39:46 -06:00
|
|
|
{this.renderHorizontalSplit(styles)}
|
|
|
|
<div className={styles.noScrollPaneContent}>
|
|
|
|
<CustomScrollbar>
|
|
|
|
<div style={{ padding: '10px' }}>
|
|
|
|
{this.renderFieldOptions()}
|
2020-02-09 10:39:26 -06:00
|
|
|
<ControlledCollapse label="Visualization Settings" collapsible>
|
|
|
|
{this.renderVisSettings()}
|
|
|
|
</ControlledCollapse>
|
2020-02-09 08:39:46 -06:00
|
|
|
</div>
|
|
|
|
</CustomScrollbar>
|
2020-02-08 12:31:17 -06:00
|
|
|
</div>
|
|
|
|
</SplitPane>
|
2020-02-09 08:39:46 -06:00
|
|
|
) : (
|
|
|
|
this.renderHorizontalSplit(styles)
|
|
|
|
)}
|
2020-02-08 12:31:17 -06:00
|
|
|
</div>
|
2020-02-08 05:01:10 -06:00
|
|
|
</div>
|
2019-12-16 02:18:48 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 07:59:04 -06:00
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, props) => ({
|
2020-02-07 07:59:04 -06:00
|
|
|
location: state.location,
|
2020-02-11 07:57:16 -06:00
|
|
|
plugin: state.plugins.panels[props.sourcePanel.type],
|
|
|
|
panel: state.panelEditorNew.getPanel(),
|
|
|
|
mode: state.panelEditorNew.mode,
|
|
|
|
isPanelOptionsVisible: state.panelEditorNew.isPanelOptionsVisible,
|
|
|
|
data: state.panelEditorNew.getData(),
|
|
|
|
initDone: state.panelEditorNew.initDone,
|
2020-02-07 07:59:04 -06:00
|
|
|
});
|
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
|
2020-02-07 07:59:04 -06:00
|
|
|
updateLocation,
|
2020-02-11 07:57:16 -06:00
|
|
|
initPanelEditor,
|
|
|
|
panelEditorCleanUp,
|
|
|
|
setDisplayMode,
|
|
|
|
toggleOptionsView,
|
|
|
|
setDiscardChanges,
|
2020-02-07 07:59:04 -06:00
|
|
|
};
|
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
export const PanelEditor = connect(mapStateToProps, mapDispatchToProps)(PanelEditorUnconnected);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Styles
|
|
|
|
*/
|
|
|
|
const getStyles = stylesFactory((theme: GrafanaTheme) => {
|
|
|
|
const handleColor = selectThemeVariant(
|
|
|
|
{
|
|
|
|
dark: theme.colors.dark9,
|
|
|
|
light: theme.colors.gray6,
|
|
|
|
},
|
|
|
|
theme.type
|
|
|
|
);
|
|
|
|
|
|
|
|
const resizer = css`
|
|
|
|
padding: 3px;
|
|
|
|
font-style: italic;
|
|
|
|
background: ${theme.colors.panelBg};
|
|
|
|
&:hover {
|
|
|
|
background: ${handleColor};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
wrapper: css`
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
position: fixed;
|
|
|
|
z-index: ${theme.zIndex.modal};
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
bottom: 0;
|
|
|
|
background: ${theme.colors.pageBg};
|
|
|
|
`,
|
|
|
|
panelWrapper: css`
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
`,
|
|
|
|
resizerV: cx(
|
|
|
|
resizer,
|
|
|
|
css`
|
|
|
|
cursor: col-resize;
|
|
|
|
`
|
|
|
|
),
|
|
|
|
resizerH: cx(
|
|
|
|
resizer,
|
|
|
|
css`
|
|
|
|
cursor: row-resize;
|
|
|
|
`
|
|
|
|
),
|
|
|
|
noScrollPaneContent: css`
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
overflow: hidden;
|
|
|
|
`,
|
|
|
|
toolbar: css`
|
|
|
|
padding: ${theme.spacing.sm};
|
|
|
|
height: 48px;
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
`,
|
|
|
|
panes: css`
|
|
|
|
height: calc(100% - 48px);
|
|
|
|
position: relative;
|
|
|
|
`,
|
|
|
|
toolbarLeft: css`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
`,
|
|
|
|
centeringContainer: css`
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
});
|