Commit 4b8f4df1 by Torkel Ödegaard Committed by GitHub

Graph: Preseve existing data links in field config when loading dashboard (#27295)

* Graph: Preseve existing data links in field config

* ts fix
parent 06323b8e
...@@ -203,6 +203,7 @@ export class PanelModel implements DataConfigSource { ...@@ -203,6 +203,7 @@ export class PanelModel implements DataConfigSource {
getOptions() { getOptions() {
return this.options; return this.options;
} }
getFieldConfig() { getFieldConfig() {
return this.fieldConfig; return this.fieldConfig;
} }
...@@ -309,6 +310,7 @@ export class PanelModel implements DataConfigSource { ...@@ -309,6 +310,7 @@ export class PanelModel implements DataConfigSource {
if (plugin.angularConfigCtrl) { if (plugin.angularConfigCtrl) {
return; return;
} }
this.options = _.mergeWith({}, plugin.defaults, this.options || {}, (objValue: any, srcValue: any): any => { this.options = _.mergeWith({}, plugin.defaults, this.options || {}, (objValue: any, srcValue: any): any => {
if (_.isArray(srcValue)) { if (_.isArray(srcValue)) {
return srcValue; return srcValue;
......
...@@ -126,4 +126,25 @@ describe('Graph Panel Migrations', () => { ...@@ -126,4 +126,25 @@ describe('Graph Panel Migrations', () => {
const link = fieldSource.defaults.links![0]; const link = fieldSource.defaults.links![0];
expect(link.url).toEqual('THE DRILLDOWN URL'); expect(link.url).toEqual('THE DRILLDOWN URL');
}); });
it('from 7.1 it should preserve existing fieldConfig', () => {
const panel = ({
id: 1,
fieldConfig: {
defaults: {
links: [
{
title: 'Details',
url: '/link',
},
],
},
overrides: [],
},
} as unknown) as PanelModel;
graphPanelMigrationHandler(panel as PanelModel);
const fieldConfig = (panel as any).fieldConfig as FieldConfigSource;
expect(fieldConfig.defaults.links).toHaveLength(1);
});
}); });
...@@ -4,7 +4,7 @@ import { PanelModel, FieldConfigSource, DataLink } from '@grafana/data'; ...@@ -4,7 +4,7 @@ import { PanelModel, FieldConfigSource, DataLink } from '@grafana/data';
* Called when upgrading from a previously saved versoin * Called when upgrading from a previously saved versoin
*/ */
export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any> => { export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any> => {
const fieldOptions: FieldConfigSource = { const fieldConfig: FieldConfigSource = panel.fieldConfig ?? {
defaults: {}, defaults: {},
overrides: [], overrides: [],
}; };
...@@ -13,12 +13,12 @@ export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any> ...@@ -13,12 +13,12 @@ export const graphPanelMigrationHandler = (panel: PanelModel<any>): Partial<any>
// Move <7.1 dataLinks to the field section // Move <7.1 dataLinks to the field section
if (options.dataLinks) { if (options.dataLinks) {
fieldOptions.defaults.links = options.dataLinks as DataLink[]; fieldConfig.defaults.links = options.dataLinks as DataLink[];
delete options.dataLinks; delete options.dataLinks;
} }
// Mutate the original panel state (only necessary because it is angular) // Mutate the original panel state (only necessary because it is angular)
panel.options = options; panel.options = options;
panel.fieldConfig = fieldOptions; panel.fieldConfig = fieldConfig;
return options; return options;
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment