From 512a42d2c61d22e24840a39083f6ce50d940562f Mon Sep 17 00:00:00 2001 From: okhowang <3352585+okhowang@users.noreply.github.com> Date: Fri, 3 Jan 2020 19:18:39 +0800 Subject: [PATCH] Panel: disable edit/duplicate/delete entry for repeat panel (#21257) --- public/app/core/services/keybindingSrv.ts | 9 +++++---- public/app/features/dashboard/state/DashboardModel.ts | 8 ++++++++ public/app/features/dashboard/utils/getPanelMenu.ts | 6 +++--- public/app/features/panel/panel_ctrl.ts | 6 +++--- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/public/app/core/services/keybindingSrv.ts b/public/app/core/services/keybindingSrv.ts index 89a1dac5b16..237d34441e5 100644 --- a/public/app/core/services/keybindingSrv.ts +++ b/public/app/core/services/keybindingSrv.ts @@ -14,6 +14,7 @@ import { ContextSrv } from './context_srv'; import { ILocationService, IRootScopeService, ITimeoutService } from 'angular'; import { GrafanaRootScope } from 'app/routes/GrafanaCtrl'; import { getLocationSrv } from '@grafana/runtime'; +import { DashboardModel } from '../../features/dashboard/state'; export class KeybindingSrv { helpModal: boolean; @@ -174,7 +175,7 @@ export class KeybindingSrv { this.$location.search(search); } - setupDashboardBindings(scope: IRootScopeService & AppEventEmitter, dashboard: any) { + setupDashboardBindings(scope: IRootScopeService & AppEventEmitter, dashboard: DashboardModel) { this.bind('mod+o', () => { dashboard.graphTooltip = (dashboard.graphTooltip + 1) % 3; appEvents.emit(CoreEvents.graphHoverClear); @@ -203,7 +204,7 @@ export class KeybindingSrv { // edit panel this.bind('e', () => { - if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) { + if (dashboard.canEditPanelById(dashboard.meta.focusPanelId)) { appEvents.emit(PanelEvents.panelChangeView, { fullscreen: true, edit: true, @@ -248,7 +249,7 @@ export class KeybindingSrv { // delete panel this.bind('p r', () => { - if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) { + if (dashboard.canEditPanelById(dashboard.meta.focusPanelId)) { appEvents.emit(CoreEvents.removePanel, dashboard.meta.focusPanelId); dashboard.meta.focusPanelId = 0; } @@ -256,7 +257,7 @@ export class KeybindingSrv { // duplicate panel this.bind('p d', () => { - if (dashboard.meta.focusPanelId && dashboard.meta.canEdit) { + if (dashboard.canEditPanelById(dashboard.meta.focusPanelId)) { const panelIndex = dashboard.getPanelInfoById(dashboard.meta.focusPanelId).index; dashboard.duplicatePanel(dashboard.panels[panelIndex]); } diff --git a/public/app/features/dashboard/state/DashboardModel.ts b/public/app/features/dashboard/state/DashboardModel.ts index d6b7211a85a..c1b26ee774a 100644 --- a/public/app/features/dashboard/state/DashboardModel.ts +++ b/public/app/features/dashboard/state/DashboardModel.ts @@ -293,6 +293,14 @@ export class DashboardModel { return null; } + canEditPanel(panel?: PanelModel): boolean { + return this.meta.canEdit && panel && !panel.repeatPanelId; + } + + canEditPanelById(id: number): boolean { + return this.canEditPanel(this.getPanelById(id)); + } + addPanel(panelData: any) { panelData.id = this.getNextPanelId(); diff --git a/public/app/features/dashboard/utils/getPanelMenu.ts b/public/app/features/dashboard/utils/getPanelMenu.ts index 224d12b9ce0..521046740bd 100644 --- a/public/app/features/dashboard/utils/getPanelMenu.ts +++ b/public/app/features/dashboard/utils/getPanelMenu.ts @@ -107,7 +107,7 @@ export const getPanelMenu = (dashboard: DashboardModel, panel: PanelModel) => { shortcut: 'v', }); - if (dashboard.meta.canEdit) { + if (dashboard.canEditPanel(panel)) { menu.push({ text: 'Edit', iconClassName: 'gicon gicon-editor', @@ -152,7 +152,7 @@ export const getPanelMenu = (dashboard: DashboardModel, panel: PanelModel) => { const subMenu: PanelMenuItem[] = []; - if (!panel.fullscreen && dashboard.meta.canEdit) { + if (!panel.fullscreen && dashboard.canEditPanel(panel)) { subMenu.push({ text: 'Duplicate', onClick: onDuplicatePanel, @@ -178,7 +178,7 @@ export const getPanelMenu = (dashboard: DashboardModel, panel: PanelModel) => { onClick: onMore, }); - if (dashboard.meta.canEdit) { + if (dashboard.canEditPanel(panel)) { menu.push({ type: 'divider' }); menu.push({ diff --git a/public/app/features/panel/panel_ctrl.ts b/public/app/features/panel/panel_ctrl.ts index 067cb458926..60559e1c2c3 100644 --- a/public/app/features/panel/panel_ctrl.ts +++ b/public/app/features/panel/panel_ctrl.ts @@ -127,7 +127,7 @@ export class PanelCtrl { shortcut: 'v', }); - if (this.dashboard.meta.canEdit) { + if (this.dashboard.canEditPanel(this.panel)) { menu.push({ text: 'Edit', click: 'ctrl.editPanel();', @@ -164,7 +164,7 @@ export class PanelCtrl { submenu: extendedMenu, }); - if (this.dashboard.meta.canEdit) { + if (this.dashboard.canEditPanel(this.panel)) { menu.push({ divider: true, role: 'Editor' }); menu.push({ text: 'Remove', @@ -180,7 +180,7 @@ export class PanelCtrl { getExtendedMenu() { const menu = []; - if (!this.panel.fullscreen && this.dashboard.meta.canEdit) { + if (!this.panel.fullscreen && this.dashboard.canEditPanel(this.panel)) { menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()',