@darwino/darwino
Version:
A set of Javascript classes and utilities
156 lines (126 loc) • 3.61 kB
JavaScript
/*
* (c) Copyright Darwino Inc. 2014-2017.
*/
/*
* Data coming from a cursor.
*/
export default class CursorEntries {
constructor(rawData) {
this.rawData = rawData;
this.uncategorized = "(Not Categorized)";
}
getRows() {
if (!this.data) {
this._processRows();
}
return this.data;
}
_processRows() {
if (!this.rawData) {
this.data = [];
return;
}
if (!this.groupColumns && !this.processFct && !this.filter && !this.sort) {
this.data = this.rawData;
return;
}
this.data = null; // 1- We should filter out the rows
if (this.filter) {
this.data = (this.data || this.rawData).filter(this.filter);
} // 2- Sort for the groups and the sort order
if (this.groupColumns || this.sortColumns) {
if (this.inMemorySort) {
this.data = this._inMemorySort(this.data || this.rawData.slice(0));
}
} // 3- We should create the groups
if (this.groupColumns) {
this.data = this._groupRows(this.data || this.rawData, 0);
}
if (this.processFct) {
var fakegroup = {
__meta: {
indentLevel: -1,
children: this.data || this.rawData.slice(0)
}
};
this._processGroup(fakegroup, []);
this.data = fakegroup.__meta.children;
}
}
_inMemorySort(rows) {
return rows.sort((r1, r2) => {
if (this.groupColumns) {
for (var i = 0; i < this.groupColumns.length; i++) {
var desc = this.groupColumns[i].descending || false;
var v1 = (desc ? r2 : r1)[this.groupColumns[i].column];
var v2 = (desc ? r1 : r2)[this.groupColumns[i].column];
if (v1 < v2) return -1;
if (v1 > v2) return 1;
}
}
if (this.sortColumns) {
for (var _i = 0; _i < this.sortColumns.length; _i++) {
var _desc = this.sortColumns[_i].descending || false;
var _v = (_desc ? r2 : r1)[this.sortColumns[_i].column];
var _v2 = (_desc ? r1 : r2)[this.sortColumns[_i].column];
if (_v < _v2) return -1;
if (_v > _v2) return 1;
}
}
return 0;
});
}
_groupRows(rows, colIdx) {
var groups = {};
var list = [];
if (this.groupColumns[colIdx]) {
var column = this.groupColumns[colIdx].column;
rows.forEach(row => {
var key = row[column] || this.uncategorized;
row.__meta.indentLevel = colIdx + 1;
var g = groups[key];
if (!g) {
g = groups[key] = {
key,
__meta: {
category: true,
indentLevel: colIdx,
group: true,
children: []
}
};
list.push(g);
}
g.__meta.children.push(row);
});
}
if (colIdx + 1 < this.groupColumns.length) {
list.forEach(group => {
group.__meta.children = this._groupRows(group.__meta.children, colIdx + 1);
});
}
return list;
}
_processGroup(group, groups) {
// deeper groups calculated first
if (group.__meta.indentLevel < this.groupColumns.length - 1) {
var children = group.__meta.children;
children.forEach(row => {
if (row.__meta.category) {
this._processGroup(row, [...groups, group]);
}
});
}
this.processFct(group, groups);
}
groupBy(columns) {
this.groupColumns = columns;
}
sortBy(columns) {
this.sortColumns = columns;
}
processEntries(processFct) {
this.processFct = processFct;
}
}
//# sourceMappingURL=CursorEntries.js.map