mirror of
https://github.com/grafana/grafana.git
synced 2025-09-26 02:34:01 +08:00
Began work on handling panel type switching and keep setting
This commit is contained in:
@ -25,10 +25,13 @@ export interface PanelEditorProps<T = any> {
|
|||||||
onChange: (options: T) => void;
|
onChange: (options: T) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type PreservePanelOptionsHandler<TOptions> = (pluginId: string, prevOptions: any) => Partial<TOptions>;
|
||||||
|
|
||||||
export class ReactPanelPlugin<TOptions = any> {
|
export class ReactPanelPlugin<TOptions = any> {
|
||||||
panel: ComponentClass<PanelProps<TOptions>>;
|
panel: ComponentClass<PanelProps<TOptions>>;
|
||||||
editor?: ComponentClass<PanelEditorProps<TOptions>>;
|
editor?: ComponentClass<PanelEditorProps<TOptions>>;
|
||||||
defaults?: TOptions;
|
defaults?: TOptions;
|
||||||
|
preserveOptions?: PreservePanelOptionsHandler<TOptions>;
|
||||||
|
|
||||||
constructor(panel: ComponentClass<PanelProps<TOptions>>) {
|
constructor(panel: ComponentClass<PanelProps<TOptions>>) {
|
||||||
this.panel = panel;
|
this.panel = panel;
|
||||||
@ -41,6 +44,10 @@ export class ReactPanelPlugin<TOptions = any> {
|
|||||||
setDefaults(defaults: TOptions) {
|
setDefaults(defaults: TOptions) {
|
||||||
this.defaults = defaults;
|
this.defaults = defaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setPreserveOptionsHandler(handler: PreservePanelOptionsHandler<TOptions>) {
|
||||||
|
this.preserveOptions = handler;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PanelSize {
|
export interface PanelSize {
|
||||||
|
@ -76,23 +76,21 @@ export class DashboardPanel extends PureComponent<Props, State> {
|
|||||||
// unmount angular panel
|
// unmount angular panel
|
||||||
this.cleanUpAngularPanel();
|
this.cleanUpAngularPanel();
|
||||||
|
|
||||||
if (panel.type !== pluginId) {
|
if (!plugin.exports) {
|
||||||
this.props.panel.changeType(pluginId, fromAngularPanel);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (plugin.exports) {
|
|
||||||
this.setState({ plugin, angularPanel: null });
|
|
||||||
} else {
|
|
||||||
try {
|
try {
|
||||||
plugin.exports = await importPluginModule(plugin.module);
|
plugin.exports = await importPluginModule(plugin.module);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
plugin = getPanelPluginNotFound(pluginId);
|
plugin = getPanelPluginNotFound(pluginId);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (panel.type !== pluginId) {
|
||||||
|
this.props.panel.changeType(pluginId, fromAngularPanel);
|
||||||
|
}
|
||||||
|
|
||||||
this.setState({ plugin, angularPanel: null });
|
this.setState({ plugin, angularPanel: null });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.loadPlugin(this.props.panel.type);
|
this.loadPlugin(this.props.panel.type);
|
||||||
|
@ -19,6 +19,6 @@ export const defaults: BarGaugeOptions = {
|
|||||||
suffix: '',
|
suffix: '',
|
||||||
decimals: null,
|
decimals: null,
|
||||||
},
|
},
|
||||||
thresholds: [{ index: 2, value: 80, color: 'red' }, { index: 0, value: -Infinity, color: 'green' }],
|
thresholds: [{ index: 1, value: 80, color: 'red' }, { index: 0, value: -Infinity, color: 'green' }],
|
||||||
valueMappings: [],
|
valueMappings: [],
|
||||||
};
|
};
|
||||||
|
@ -8,3 +8,12 @@ export const reactPanel = new ReactPanelPlugin<GaugeOptions>(GaugePanel);
|
|||||||
|
|
||||||
reactPanel.setEditor(GaugePanelEditor);
|
reactPanel.setEditor(GaugePanelEditor);
|
||||||
reactPanel.setDefaults(defaults);
|
reactPanel.setDefaults(defaults);
|
||||||
|
reactPanel.setPreserveOptionsHandler((pluginId: string, prevOptions: any) => {
|
||||||
|
const options: Partial<GaugeOptions> = {};
|
||||||
|
|
||||||
|
if (prevOptions.valueOptions.unit) {
|
||||||
|
options.valueOptions = prevOptions.valueOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
return options;
|
||||||
|
});
|
||||||
|
@ -31,5 +31,5 @@ export const defaults: GaugeOptions = {
|
|||||||
unit: 'none',
|
unit: 'none',
|
||||||
},
|
},
|
||||||
valueMappings: [],
|
valueMappings: [],
|
||||||
thresholds: [],
|
thresholds: [{ index: 1, value: 80, color: 'red' }, { index: 0, value: -Infinity, color: 'green' }],
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user