From ff009bee9f361b70b87fd67ca720d103508c54f6 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Wed, 29 Sep 2021 16:33:11 +0100 Subject: [PATCH] DashboardRow: Prevent unintended editing of DashboardRow with keyboard shortcut 'e' (#39792) --- .../dashboard/state/DashboardModel.test.ts | 58 +++++++++++++++++++ .../dashboard/state/DashboardModel.ts | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/public/app/features/dashboard/state/DashboardModel.test.ts b/public/app/features/dashboard/state/DashboardModel.test.ts index 8900500fcf8..f86e3dd0317 100644 --- a/public/app/features/dashboard/state/DashboardModel.test.ts +++ b/public/app/features/dashboard/state/DashboardModel.test.ts @@ -766,6 +766,64 @@ describe('DashboardModel', () => { } ); }); + + describe('canEditPanel', () => { + it('returns false if the dashboard cannot be edited', () => { + const dashboard = new DashboardModel({ + panels: [ + { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } }, + { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } }, + ], + }); + dashboard.meta.canEdit = false; + const panel = dashboard.getPanelById(2); + expect(dashboard.canEditPanel(panel)).toBe(false); + }); + + it('returns false if no panel is passed in', () => { + const dashboard = new DashboardModel({ + panels: [ + { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } }, + { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } }, + ], + }); + expect(dashboard.canEditPanel()).toBe(false); + }); + + it('returns false if the panel is a repeat', () => { + const dashboard = new DashboardModel({ + panels: [ + { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } }, + { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } }, + { id: 3, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 }, repeatPanelId: 2 }, + ], + }); + const panel = dashboard.getPanelById(3); + expect(dashboard.canEditPanel(panel)).toBe(false); + }); + + it('returns false if the panel is a row', () => { + const dashboard = new DashboardModel({ + panels: [ + { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } }, + { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } }, + ], + }); + const panel = dashboard.getPanelById(1); + expect(dashboard.canEditPanel(panel)).toBe(false); + }); + + it('returns true otherwise', () => { + const dashboard = new DashboardModel({ + panels: [ + { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } }, + { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } }, + ], + }); + const panel = dashboard.getPanelById(2); + expect(dashboard.canEditPanel(panel)).toBe(true); + }); + }); }); describe('exitViewPanel', () => { diff --git a/public/app/features/dashboard/state/DashboardModel.ts b/public/app/features/dashboard/state/DashboardModel.ts index 14b6d4d6878..6c23d56e08a 100644 --- a/public/app/features/dashboard/state/DashboardModel.ts +++ b/public/app/features/dashboard/state/DashboardModel.ts @@ -470,7 +470,7 @@ export class DashboardModel { } canEditPanel(panel?: PanelModel | null): boolean | undefined | null { - return this.meta.canEdit && panel && !panel.repeatPanelId; + return Boolean(this.meta.canEdit && panel && !panel.repeatPanelId && panel.type !== 'row'); } canEditPanelById(id: number): boolean | undefined | null {