From: Bill Erickson Date: Thu, 13 Aug 2020 15:44:34 +0000 (-0400) Subject: LP1904036 Grid rowSelectionChange Output X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=108555f1adfce9ec0b20043ee1da9b406d4899c4;p=working%2FEvergreen.git LP1904036 Grid rowSelectionChange Output Signed-off-by: Bill Erickson Signed-off-by: Jane Sandberg Signed-off-by: Galen Charlton --- diff --git a/Open-ILS/src/eg2/src/app/share/grid/grid-body.component.html b/Open-ILS/src/eg2/src/app/share/grid/grid-body.component.html index 8cf795fc68..55c7fefc88 100644 --- a/Open-ILS/src/eg2/src/app/share/grid/grid-body.component.html +++ b/Open-ILS/src/eg2/src/app/share/grid/grid-body.component.html @@ -14,7 +14,9 @@
- ; @Output() onRowClick: EventEmitter; + // Emits an array of grid row indexes on any row selection change. + @Output() rowSelectionChange: EventEmitter; + @ViewChild('toolbar', { static: true }) toolbar: GridToolbarComponent; constructor( @@ -139,6 +142,7 @@ export class GridComponent implements OnInit, AfterViewInit, OnDestroy { new GridContext(this.idl, this.org, this.store, this.format); this.onRowActivate = new EventEmitter(); this.onRowClick = new EventEmitter(); + this.rowSelectionChange = new EventEmitter(); } ngOnInit() { @@ -204,6 +208,10 @@ export class GridComponent implements OnInit, AfterViewInit, OnDestroy { return ''; }; + this.context.rowSelector.selectionChange.subscribe( + keys => this.rowSelectionChange.emit(keys) + ); + if (this.showLinkSelectors) { console.debug( 'showLinkSelectors is deprecated and no longer has any effect'); @@ -217,6 +225,7 @@ export class GridComponent implements OnInit, AfterViewInit, OnDestroy { } ngOnDestroy() { + this.context.rowSelector.selectionChange.unsubscribe(); this.context.destroy(); } diff --git a/Open-ILS/src/eg2/src/app/share/grid/grid.ts b/Open-ILS/src/eg2/src/app/share/grid/grid.ts index 6f7191ea25..3c46df8304 100644 --- a/Open-ILS/src/eg2/src/app/share/grid/grid.ts +++ b/Open-ILS/src/eg2/src/app/share/grid/grid.ts @@ -430,6 +430,13 @@ export interface GridCellTextGenerator { export class GridRowSelector { indexes: {[string: string]: boolean}; + // Track these so we can emit the selectionChange event + // only when the selection actually changes. + previousSelection: string[] = []; + + // Emits the selected indexes on selection change + selectionChange: EventEmitter = new EventEmitter(); + constructor() { this.clear(); } @@ -445,25 +452,40 @@ export class GridRowSelector { return true; } + emitChange() { + const keys = this.selected(); + + if (keys.length === this.previousSelection.length && + this.contains(this.previousSelection)) { + return; // No change has occurred + } + + this.previousSelection = keys; + this.selectionChange.emit(keys); + } + select(index: string | string[]) { const indexes = [].concat(index); indexes.forEach(i => this.indexes[i] = true); + this.emitChange(); } deselect(index: string | string[]) { const indexes = [].concat(index); indexes.forEach(i => delete this.indexes[i]); + this.emitChange(); + } + + toggle(index: string) { + if (this.indexes[index]) { + this.deselect(index); + } else { + this.select(index); + } } - // Returns the list of selected index values. - // In some contexts (template checkboxes) the value for an index is - // set to false to deselect instead of having it removed (via deselect()). - // NOTE GridRowSelector has no knowledge of when a row is no longer - // present in the grid. Use GridContext.getSelectedRows() to get - // list of selected rows that are still present in the grid. - selected() { - return Object.keys(this.indexes).filter( - ind => Boolean(this.indexes[ind])); + selected(): string[] { + return Object.keys(this.indexes); } isEmpty(): boolean { @@ -472,6 +494,7 @@ export class GridRowSelector { clear() { this.indexes = {}; + this.emitChange(); } }