diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts')
-rw-r--r-- | src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts | 45 |
1 files changed, 45 insertions, 0 deletions
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 new file mode 100644 index 000000000..bbe1e5088 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts @@ -0,0 +1,45 @@ +export class CdTableSelection { + private _selected: any[] = []; + hasMultiSelection: boolean; + hasSingleSelection: boolean; + hasSelection: boolean; + + constructor(rows?: any[]) { + if (rows) { + this._selected = rows; + } + this.update(); + } + + /** + * Recalculate the variables based on the current number + * of selected rows. + */ + 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(); + } + + /** + * Get the first selected row. + * @return {any | null} + */ + first() { + return this.hasSelection ? this._selected[0] : null; + } +} |