Просмотр исходного кода

dashboards: bulk edit delete

If both a parent folder and a child is chosen then
only sends the parent folder for deletion as the
child folder will be deleted anyway.
Daniel Lee 8 лет назад
Родитель
Сommit
e85abf6810

+ 21 - 1
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',

+ 20 - 2
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);
+    });
+  });
 });