mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
WIP server side.
This commit is contained in:
73
server/src/collection/NestedSet/index.js
Normal file
73
server/src/collection/NestedSet/index.js
Normal file
@@ -0,0 +1,73 @@
|
||||
|
||||
export default class NestedSet {
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {Object} options -
|
||||
*/
|
||||
constructor(items, options) {
|
||||
this.options = {
|
||||
parentId: 'parent_id',
|
||||
id: 'id',
|
||||
...options,
|
||||
};
|
||||
this.items = items;
|
||||
this.collection = {};
|
||||
this.toTree();
|
||||
|
||||
return this.collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Link nodes children.
|
||||
*/
|
||||
linkChildren() {
|
||||
if (this.items.length <= 0) return false;
|
||||
|
||||
const map = {};
|
||||
this.items.forEach((item) => {
|
||||
map[item.id] = item;
|
||||
map[item.id].children = [];
|
||||
});
|
||||
|
||||
this.items.forEach((item) => {
|
||||
const parentNodeId = item[this.options.parentId];
|
||||
if (parentNodeId) {
|
||||
map[parentNodeId].children.push(item);
|
||||
}
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
toTree() {
|
||||
const map = this.linkChildren();
|
||||
const tree = {};
|
||||
|
||||
this.items.forEach((item) => {
|
||||
const parentNodeId = item[this.options.parentId];
|
||||
if (!parentNodeId) {
|
||||
tree[item.id] = map[item.id];
|
||||
}
|
||||
});
|
||||
this.collection = Object.values(tree);
|
||||
}
|
||||
|
||||
walk() {
|
||||
|
||||
}
|
||||
|
||||
getParents() {
|
||||
|
||||
}
|
||||
|
||||
getChildren() {
|
||||
|
||||
}
|
||||
|
||||
toFlattenArray() {
|
||||
|
||||
}
|
||||
|
||||
toArray() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user