diff --git a/public/app/features/dashboard/dashboard_list_ctrl.ts b/public/app/features/dashboard/dashboard_list_ctrl.ts index 232d8d7bb22..4522a53e81d 100644 --- a/public/app/features/dashboard/dashboard_list_ctrl.ts +++ b/public/app/features/dashboard/dashboard_list_ctrl.ts @@ -81,8 +81,28 @@ export class DashboardListCtrl { this.canMove = selectedDashboards > 0 && selectedFolders === 0; } + getDashboardsToDelete() { + const selectedFolderIds = this.getFolderIds(this.dashboards); + return _.filter(this.dashboards, o => { + return o.checked && ( + o.type !== 'dash-child' || + (o.type === 'dash-child' && !_.includes(selectedFolderIds, o.folderId)) + ); + }); + } + + getFolderIds(dashboards) { + const ids = []; + for (let dash of dashboards) { + if (dash.type === 'dash-folder') { + ids.push(dash.id); + } + } + return ids; + } + delete() { - const selectedDashboards = _.filter(this.dashboards, {checked: true}); + const selectedDashboards = this.getDashboardsToDelete(); appEvents.emit('confirm-modal', { title: 'Delete', diff --git a/public/app/features/dashboard/specs/dashboard_list_ctrl.jest.ts b/public/app/features/dashboard/specs/dashboard_list_ctrl.jest.ts index ad85c8d319c..9a0218a2a4b 100644 --- a/public/app/features/dashboard/specs/dashboard_list_ctrl.jest.ts +++ b/public/app/features/dashboard/specs/dashboard_list_ctrl.jest.ts @@ -2,9 +2,9 @@ import {DashboardListCtrl} from '../dashboard_list_ctrl'; import q from 'q'; describe('DashboardListCtrl', () => { - describe('when fetching dashboards', () => { - let ctrl; + let ctrl; + describe('when fetching dashboards', () => { describe('and dashboard has parent that is not in search result', () => { beforeEach(() => { const response = [ @@ -171,4 +171,22 @@ describe('DashboardListCtrl', () => { }); }); }); + + describe('when deleting dashboards', () => { + beforeEach(() => { + ctrl = new DashboardListCtrl({get: () => q.resolve([])}, {getNav: () => {}}, q); + ctrl.dashboards = [ + {id: 1, type: 'dash-folder', checked: true}, + {id: 2, type: 'dash-child', checked: true, folderId: 1}, + {id: 3, type: 'dash-db', checked: true} + ]; + }); + + it('should filter out children if parent is selected', () => { + const toBeDeleted = ctrl.getDashboardsToDelete(); + expect(toBeDeleted.length).toEqual(2); + expect(toBeDeleted[0].id).toEqual(1); + expect(toBeDeleted[1].id).toEqual(3); + }); + }); });