2018-03-24 14:02:24 +00:00
|
|
|
export default class ColumnManager {
|
2019-01-12 03:33:27 +00:00
|
|
|
constructor(columns) {
|
|
|
|
this.columns = columns;
|
|
|
|
this._cached = {};
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
isAnyColumnsFixed() {
|
2020-03-07 11:45:13 +00:00
|
|
|
return this._cache('isAnyColumnsFixed', () => this.columns.some(column => !!column.fixed));
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
isAnyColumnsLeftFixed() {
|
2020-03-07 11:45:13 +00:00
|
|
|
return this._cache('isAnyColumnsLeftFixed', () =>
|
|
|
|
this.columns.some(column => column.fixed === 'left' || column.fixed === true),
|
|
|
|
);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
isAnyColumnsRightFixed() {
|
2020-03-07 11:45:13 +00:00
|
|
|
return this._cache('isAnyColumnsRightFixed', () =>
|
|
|
|
this.columns.some(column => column.fixed === 'right'),
|
|
|
|
);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
leftColumns() {
|
2020-03-07 11:45:13 +00:00
|
|
|
return this._cache('leftColumns', () =>
|
|
|
|
this.groupedColumns().filter(column => column.fixed === 'left' || column.fixed === true),
|
|
|
|
);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
rightColumns() {
|
2020-03-07 11:45:13 +00:00
|
|
|
return this._cache('rightColumns', () =>
|
|
|
|
this.groupedColumns().filter(column => column.fixed === 'right'),
|
|
|
|
);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
leafColumns() {
|
|
|
|
return this._cache('leafColumns', () => this._leafColumns(this.columns));
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
leftLeafColumns() {
|
|
|
|
return this._cache('leftLeafColumns', () => this._leafColumns(this.leftColumns()));
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
rightLeafColumns() {
|
|
|
|
return this._cache('rightLeafColumns', () => this._leafColumns(this.rightColumns()));
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add appropriate rowspan and colspan to column
|
2019-01-12 03:33:27 +00:00
|
|
|
groupedColumns() {
|
2018-03-24 14:02:24 +00:00
|
|
|
return this._cache('groupedColumns', () => {
|
|
|
|
const _groupColumns = (columns, currentRow = 0, parentColumn = {}, rows = []) => {
|
|
|
|
// track how many rows we got
|
2019-01-12 03:33:27 +00:00
|
|
|
rows[currentRow] = rows[currentRow] || [];
|
|
|
|
const grouped = [];
|
2018-03-24 14:02:24 +00:00
|
|
|
const setRowSpan = column => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const rowSpan = rows.length - currentRow;
|
|
|
|
if (
|
|
|
|
column &&
|
2018-03-24 14:02:24 +00:00
|
|
|
!column.children && // parent columns are supposed to be one row
|
|
|
|
rowSpan > 1 &&
|
|
|
|
(!column.rowSpan || column.rowSpan < rowSpan)
|
|
|
|
) {
|
2019-01-12 03:33:27 +00:00
|
|
|
column.rowSpan = rowSpan;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-24 14:02:24 +00:00
|
|
|
columns.forEach((column, index) => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const newColumn = { ...column };
|
|
|
|
rows[currentRow].push(newColumn);
|
|
|
|
parentColumn.colSpan = parentColumn.colSpan || 0;
|
2018-03-24 14:02:24 +00:00
|
|
|
if (newColumn.children && newColumn.children.length > 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
newColumn.children = _groupColumns(newColumn.children, currentRow + 1, newColumn, rows);
|
|
|
|
parentColumn.colSpan += newColumn.colSpan;
|
2018-03-24 14:02:24 +00:00
|
|
|
} else {
|
2020-03-07 11:45:13 +00:00
|
|
|
parentColumn.colSpan += 1;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
// update rowspan to all same row columns
|
2020-03-07 11:45:13 +00:00
|
|
|
for (let i = 0; i < rows[currentRow].length - 1; i += 1) {
|
2019-01-12 03:33:27 +00:00
|
|
|
setRowSpan(rows[currentRow][i]);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
// last column, update rowspan immediately
|
|
|
|
if (index + 1 === columns.length) {
|
2019-01-12 03:33:27 +00:00
|
|
|
setRowSpan(newColumn);
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
grouped.push(newColumn);
|
|
|
|
});
|
|
|
|
return grouped;
|
|
|
|
};
|
|
|
|
return _groupColumns(this.columns);
|
|
|
|
});
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
reset(columns) {
|
|
|
|
this.columns = columns;
|
|
|
|
this._cached = {};
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
_cache(name, fn) {
|
2018-03-24 14:02:24 +00:00
|
|
|
if (name in this._cached) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return this._cached[name];
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this._cached[name] = fn();
|
|
|
|
return this._cached[name];
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
_leafColumns(columns) {
|
|
|
|
const leafColumns = [];
|
2018-03-24 14:02:24 +00:00
|
|
|
columns.forEach(column => {
|
|
|
|
if (!column.children) {
|
2019-01-12 03:33:27 +00:00
|
|
|
leafColumns.push(column);
|
2018-03-24 14:02:24 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
leafColumns.push(...this._leafColumns(column.children));
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
return leafColumns;
|
2018-03-24 14:02:24 +00:00
|
|
|
}
|
|
|
|
}
|