From 75ac96df6f4e547498a1f21a50419ed3316d3d8b Mon Sep 17 00:00:00 2001 From: Patrick Seidensal Date: Mon, 23 Sep 2019 21:42:00 +0200 Subject: [PATCH] mgr/dashboard: remove the need to call `update()` on CdTableSelection Signed-off-by: Patrick Seidensal --- .../iscsi-target-details.component.spec.ts | 1 - .../rbd-snapshot-list.component.spec.ts | 1 - .../rbd-trash-list.component.spec.ts | 6 +--- .../cephfs-tabs/cephfs-tabs.component.spec.ts | 1 - .../host-details.component.spec.ts | 1 - .../mgr-module-list.component.spec.ts | 8 ++--- .../osd/osd-list/osd-list.component.spec.ts | 1 - .../silence-list.component.spec.ts | 6 ++-- .../nfs-details/nfs-details.component.spec.ts | 2 -- .../pool-details.component.spec.ts | 2 -- .../pool-list/pool-list.component.spec.ts | 13 ++------ .../role-details.component.spec.ts | 12 +++---- .../datatable/table/table.component.html | 2 +- .../shared/datatable/table/table.component.ts | 6 ++-- .../app/shared/models/cd-table-selection.ts | 31 ++++++++++++++----- .../frontend/src/testing/unit-test-helper.ts | 1 - 16 files changed, 40 insertions(+), 54 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-details/iscsi-target-details.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-details/iscsi-target-details.component.spec.ts index 852c7619926..95fb5b95895 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-details/iscsi-target-details.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-details/iscsi-target-details.component.spec.ts @@ -74,7 +74,6 @@ describe('IscsiTargetDetailsComponent', () => { target_controls: { dataout_timeout: 2 } } ]; - component.selection.update(); fixture.detectChanges(); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts index d0e395b3f50..37b09c63780 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-snapshot-list/rbd-snapshot-list.component.spec.ts @@ -202,7 +202,6 @@ describe('RbdSnapshotListComponent', () => { it('should display old snapshot name', () => { component.selection.selected = [{ name: 'oldname' }]; - component.selection.update(); component.openEditSnapshotModal(); expect(component.modalRef.content.snapName).toBe('oldname'); expect(component.modalRef.content.editing).toBeTruthy(); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-trash-list/rbd-trash-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-trash-list/rbd-trash-list.component.spec.ts index 925d5fcfa71..136b3aa4443 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-trash-list/rbd-trash-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-trash-list/rbd-trash-list.component.spec.ts @@ -51,12 +51,8 @@ describe('RbdTrashListComponent', () => { }); it('should call updateSelection', () => { - const selection = new CdTableSelection(); - selection.selected = ['foo']; - selection.update(); - expect(component.selection.hasSelection).toBeFalsy(); - component.updateSelection(selection); + component.updateSelection(new CdTableSelection(['foo'])); expect(component.selection.hasSelection).toBeTruthy(); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-tabs/cephfs-tabs.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-tabs/cephfs-tabs.component.spec.ts index 64170da229a..bb394e24241 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-tabs/cephfs-tabs.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-tabs/cephfs-tabs.component.spec.ts @@ -51,7 +51,6 @@ describe('CephfsTabsComponent', () => { const setSelection = (selection: object[]) => { component.selection.selected = selection; - component.selection.update(); component.ngOnChanges(); }; diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/host-details/host-details.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/host-details/host-details.component.spec.ts index 354b67bc331..ebe9f097e72 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/host-details/host-details.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/host-details/host-details.component.spec.ts @@ -58,7 +58,6 @@ describe('HostDetailsComponent', () => { hostname: 'localhost' } ]; - component.selection.update(); }); it('should recognize a tabset child', () => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts index 15cc3034909..931ef28e921 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/mgr-modules/mgr-module-list/mgr-module-list.component.spec.ts @@ -97,12 +97,11 @@ describe('MgrModuleListComponent', () => { it('should enable module', fakeAsync(() => { spyOn(mgrModuleService, 'enable').and.returnValue(observableThrowError('y')); spyOn(mgrModuleService, 'list').and.returnValues(observableThrowError('z'), observableOf([])); - component.selection.selected.push({ + component.selection.add({ name: 'foo', enabled: false, always_on: false }); - component.selection.update(); component.updateModuleState(); tick(2000); tick(2000); @@ -117,12 +116,11 @@ describe('MgrModuleListComponent', () => { it('should disable module', fakeAsync(() => { spyOn(mgrModuleService, 'disable').and.returnValue(observableThrowError('x')); spyOn(mgrModuleService, 'list').and.returnValue(observableOf([])); - component.selection.selected.push({ + component.selection.add({ name: 'bar', enabled: true, always_on: false }); - component.selection.update(); component.updateModuleState(); tick(2000); expect(mgrModuleService.disable).toHaveBeenCalledWith('bar'); @@ -139,7 +137,6 @@ describe('MgrModuleListComponent', () => { name: 'dashboard' } ]; - component.selection.update(); expect(component.isTableActionDisabled('enabled')).toBeTruthy(); }); @@ -150,7 +147,6 @@ describe('MgrModuleListComponent', () => { always_on: true } ]; - component.selection.update(); expect(component.isTableActionDisabled('enabled')).toBeTruthy(); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts index ac6ce511b90..d3fb7285c67 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-list/osd-list.component.spec.ts @@ -53,7 +53,6 @@ describe('OsdListComponent', () => { // Table data and selection component.selection = new CdTableSelection(); component.selection.selected = selection; - component.selection.update(); component.osds = data; component.permissions = fakeAuthStorageService.getPermissions(); }; diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts index ccacfd0fa78..d063b7309c1 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/prometheus/silence-list/silence-list.component.spec.ts @@ -94,10 +94,8 @@ describe('SilenceListComponent', () => { }); describe('expire silence', () => { - const setSelectedSilence = (silenceName: string) => { - component.selection.selected = [{ id: silenceName }]; - component.selection.update(); - }; + const setSelectedSilence = (silenceName: string) => + (component.selection.selected = [{ id: silenceName }]); const expireSilence = () => { component.expireSilence(); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-details/nfs-details.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-details/nfs-details.component.spec.ts index 2e2cb52c02e..f3f28294148 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-details/nfs-details.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-details/nfs-details.component.spec.ts @@ -51,7 +51,6 @@ describe('NfsDetailsComponent', () => { state: 'LOADING' } ]; - component.selection.update(); component.ngOnChanges(); fixture.detectChanges(); }); @@ -85,7 +84,6 @@ describe('NfsDetailsComponent', () => { } }); component.selection.selected = [newData]; - component.selection.update(); component.ngOnChanges(); expect(component.data).toEqual({ 'Access Type': 'RW', diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-details/pool-details.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-details/pool-details.component.spec.ts index 4bf43944a14..f649c0d2352 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-details/pool-details.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-details/pool-details.component.spec.ts @@ -41,7 +41,6 @@ describe('PoolDetailsComponent', () => { pool: 0 } ]; - poolDetailsComponent.selection.update(); }); it('should recognize a tabset child', () => { @@ -64,7 +63,6 @@ describe('PoolDetailsComponent', () => { tiers: [] } ]; - poolDetailsComponent.selection.update(); fixture.detectChanges(); const tabs = poolDetailsComponent.tabsetChild.tabs; expect(tabs.length).toEqual(2); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-list/pool-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-list/pool-list.component.spec.ts index 3a365223ffd..e38ca50e881 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-list/pool-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-list/pool-list.component.spec.ts @@ -140,10 +140,8 @@ describe('PoolListComponent', () => { describe('pool deletion', () => { let taskWrapper: TaskWrapperService; - const setSelectedPool = (poolName: string) => { - component.selection.selected = [{ pool_name: poolName }]; - component.selection.update(); - }; + const setSelectedPool = (poolName: string) => + (component.selection.selected = [{ pool_name: poolName }]); const callDeletion = () => { component.deletePoolModal(); @@ -422,12 +420,7 @@ describe('PoolListComponent', () => { describe('getSelectionTiers', () => { const setSelectionTiers = (tiers: number[]) => { - component.selection.selected = [ - { - tiers - } - ]; - component.selection.update(); + component.selection.selected = [{ tiers }]; component.getSelectionTiers(); }; diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-details/role-details.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-details/role-details.component.spec.ts index e14cbb63e66..74f0ff40089 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-details/role-details.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-details/role-details.component.spec.ts @@ -31,8 +31,7 @@ describe('RoleDetailsComponent', () => { it('should create scopes permissions [1/2]', () => { component.scopes = ['log', 'rgw']; - component.selection = new CdTableSelection(); - component.selection.selected = [ + component.selection = new CdTableSelection([ { description: 'RGW Manager', name: 'rgw-manager', @@ -41,8 +40,7 @@ describe('RoleDetailsComponent', () => { }, system: true } - ]; - component.selection.update(); + ]); expect(component.scopes_permissions.length).toBe(0); component.ngOnChanges(); expect(component.scopes_permissions).toEqual([ @@ -53,8 +51,7 @@ describe('RoleDetailsComponent', () => { it('should create scopes permissions [2/2]', () => { component.scopes = ['cephfs', 'log', 'rgw']; - component.selection = new CdTableSelection(); - component.selection.selected = [ + component.selection = new CdTableSelection([ { description: 'Test', name: 'test', @@ -64,8 +61,7 @@ describe('RoleDetailsComponent', () => { }, system: false } - ]; - component.selection.update(); + ]); expect(component.scopes_permissions.length).toBe(0); component.ngOnChanges(); expect(component.scopes_permissions).toEqual([ diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.html b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.html index 7b3fb44a183..a596468169a 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.html @@ -98,7 +98,7 @@ [cssClasses]="paginationClasses" [selectionType]="selectionType" [selected]="selection.selected" - (select)="onSelect()" + (select)="onSelect($event)" [sorts]="userConfig.sorts" (sort)="changeSorting($event)" [columns]="tableColumns" diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.ts index e0053fc4a60..36d32cf81fd 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.ts @@ -479,11 +479,11 @@ export class TableComponent implements AfterContentChecked, OnInit, OnChanges, O return; } this.selection.selected = newSelected; - this.onSelect(); + this.onSelect(this.selection); } - onSelect() { - this.selection.update(); + onSelect($event) { + this.selection.selected = $event['selected']; this.updateSelection.emit(_.clone(this.selection)); } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts index 9732abc5a03..bbe1e5088c3 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts @@ -1,10 +1,13 @@ export class CdTableSelection { - selected: any[] = []; + private _selected: any[] = []; hasMultiSelection: boolean; hasSingleSelection: boolean; hasSelection: boolean; - constructor() { + constructor(rows?: any[]) { + if (rows) { + this._selected = rows; + } this.update(); } @@ -12,10 +15,24 @@ export class CdTableSelection { * Recalculate the variables based on the current number * of selected rows. */ - update() { - this.hasSelection = this.selected.length > 0; - this.hasSingleSelection = this.selected.length === 1; - this.hasMultiSelection = this.selected.length > 1; + private update() { + this.hasSelection = this._selected.length > 0; + this.hasSingleSelection = this._selected.length === 1; + this.hasMultiSelection = this._selected.length > 1; + } + + set selected(selection: any[]) { + this._selected = selection; + this.update(); + } + + get selected() { + return this._selected; + } + + add(row: any) { + this._selected.push(row); + this.update(); } /** @@ -23,6 +40,6 @@ export class CdTableSelection { * @return {any | null} */ first() { - return this.hasSelection ? this.selected[0] : null; + return this.hasSelection ? this._selected[0] : null; } } diff --git a/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts b/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts index 3d5562d260e..b21c5a2a8dc 100644 --- a/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts +++ b/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts @@ -111,7 +111,6 @@ export class PermissionHelper { setSelection(selection: object[]) { this.tac.selection.selected = selection; - this.tac.selection.update(); } } -- 2.39.5