mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
POC on how to save away settings from a viztype and restore when switching back to it #14274
This commit is contained in:
parent
9cbdd07315
commit
1ad4b44ec1
@ -15,6 +15,7 @@ const notPersistedProperties: { [str: string]: boolean } = {
|
|||||||
fullscreen: true,
|
fullscreen: true,
|
||||||
isEditing: true,
|
isEditing: true,
|
||||||
hasRefreshed: true,
|
hasRefreshed: true,
|
||||||
|
cachedPluginOptions: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
// For angular panels we need to clean up properties when changing type
|
// For angular panels we need to clean up properties when changing type
|
||||||
@ -51,12 +52,14 @@ const mustKeepProps: { [str: string]: boolean } = {
|
|||||||
events: true,
|
events: true,
|
||||||
cacheTimeout: true,
|
cacheTimeout: true,
|
||||||
nullPointMode: true,
|
nullPointMode: true,
|
||||||
|
cachedPluginOptions: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaults: any = {
|
const defaults: any = {
|
||||||
gridPos: { x: 0, y: 0, h: 3, w: 6 },
|
gridPos: { x: 0, y: 0, h: 3, w: 6 },
|
||||||
datasource: null,
|
datasource: null,
|
||||||
targets: [{}],
|
targets: [{}],
|
||||||
|
cachedPluginOptions: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export class PanelModel {
|
export class PanelModel {
|
||||||
@ -96,6 +99,9 @@ export class PanelModel {
|
|||||||
events: Emitter;
|
events: Emitter;
|
||||||
cacheTimeout?: any;
|
cacheTimeout?: any;
|
||||||
|
|
||||||
|
// cache props between plugins
|
||||||
|
cachedPluginOptions?: any;
|
||||||
|
|
||||||
constructor(model) {
|
constructor(model) {
|
||||||
this.events = new Emitter();
|
this.events = new Emitter();
|
||||||
|
|
||||||
@ -184,7 +190,40 @@ export class PanelModel {
|
|||||||
this.events.emit('panel-initialized');
|
this.events.emit('panel-initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPanelOptions() {
|
||||||
|
return Object.keys(this).reduce((acc, property) => {
|
||||||
|
if (notPersistedProperties[property]) {
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...acc,
|
||||||
|
[property]: this[property],
|
||||||
|
};
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
saveCurrentPanelOptions() {
|
||||||
|
const currentOptions = this.getPanelOptions();
|
||||||
|
this.cachedPluginOptions[this.type] = currentOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
restorePanelOptions(pluginId: string) {
|
||||||
|
const currentOptions = this.getPanelOptions();
|
||||||
|
const prevOptions = this.cachedPluginOptions[pluginId];
|
||||||
|
const newOptions = Object.keys(prevOptions).reduce((acc, currKey: string) => {
|
||||||
|
return {
|
||||||
|
...acc,
|
||||||
|
[currKey]: currentOptions[currKey] || prevOptions[currKey],
|
||||||
|
};
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
Object.keys(newOptions).map(property => {
|
||||||
|
this[property] = newOptions[property];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
changeType(pluginId: string, fromAngularPanel: boolean) {
|
changeType(pluginId: string, fromAngularPanel: boolean) {
|
||||||
|
this.saveCurrentPanelOptions();
|
||||||
this.type = pluginId;
|
this.type = pluginId;
|
||||||
|
|
||||||
// for now we need to remove alert rules when changing type
|
// for now we need to remove alert rules when changing type
|
||||||
@ -202,6 +241,8 @@ export class PanelModel {
|
|||||||
delete this[key];
|
delete this[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.restorePanelOptions(pluginId);
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
Loading…
Reference in New Issue
Block a user