summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-selection.ts
blob: bbe1e5088c3cfc7b0717fa4eb994107d4b56fcfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
  }
}