@hashicorp/design-system-components
Version:
Helios Design System Components
84 lines (80 loc) • 1.86 kB
JavaScript
import HdsAdvancedTableRow from './row.js';
import { action } from '@ember/object';
import { n } from 'decorator-transforms/runtime';
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
function getVisibleRows(rows) {
return rows.reduce((acc, row) => {
acc.push(row);
if (row.isOpen && row.children) {
acc.push(...getVisibleRows(row.children));
}
return acc;
}, []);
}
function getChildrenCount(rows) {
return rows.reduce((acc, row) => acc + 1 + getChildrenCount(row.children), 0);
}
class HdsAdvancedTableTableModel {
rows = [];
constructor(args) {
const {
model,
childrenKey
} = args;
this.rows = model.map(row => {
return new HdsAdvancedTableRow({
...row,
childrenKey
});
});
}
get totalRowCount() {
return getChildrenCount(this.rows);
}
get flattenedVisibleRows() {
return getVisibleRows(this.rows);
}
get lastVisibleRow() {
return this.flattenedVisibleRows[this.flattenedVisibleRows.length - 1];
}
get hasRowsWithChildren() {
return this.rows.some(row => row.hasChildren);
}
get allRowsAreOpen() {
return this.flattenedVisibleRows.length === this.totalRowCount;
}
get expandState() {
if (this.allRowsAreOpen) {
return true;
} else {
return false;
}
}
openAll() {
this.rows.forEach(row => row.openAll());
}
static {
n(this.prototype, "openAll", [action]);
}
collapseAll() {
this.rows.forEach(row => row.collapseAll());
}
static {
n(this.prototype, "collapseAll", [action]);
}
toggleAll() {
if (this.allRowsAreOpen) {
this.collapseAll();
} else {
this.openAll();
}
}
static {
n(this.prototype, "toggleAll", [action]);
}
}
export { HdsAdvancedTableTableModel as default };
//# sourceMappingURL=table.js.map