(array: T[], index: number): T[] {
/**
* Convert a flat array (with "parentId" references) into a hierarchical (tree) dataset structure (where children are array(s) inside their parent objects)
* @param flatArray input array (flat dataset)
- * @param options you can provide the following options:: "parentPropName" (defaults to "parent"), "childrenPropName" (defaults to "children") and "identifierPropName" (defaults to "id")
+ * @param options you can provide the following tree data options (which are all prop names, except 1 boolean flag, to use or else use their defaults):: collapsedPropName, childrenPropName, parentPropName, identifierPropName and levelPropName and initiallyCollapsed (boolean)
* @return roots - hierarchical (tree) data view array
*/
-export function unflattenParentChildArrayToTree(flatArray: P[], options?: { parentPropName?: string; childrenPropName?: string; identifierPropName?: string; levelPropName?: string; }): T[] {
- const childrenPropName = options?.childrenPropName ?? 'children';
- const parentPropName = options?.parentPropName ?? '__parentId';
+export function unflattenParentChildArrayToTree
(flatArray: P[], options?: { childrenPropName?: string; collapsedPropName?: string; identifierPropName?: string; levelPropName?: string; parentPropName?: string; initiallyCollapsed?: boolean; }): T[] {
const identifierPropName = options?.identifierPropName ?? 'id';
- const levelPropName = options?.levelPropName ?? '__treeLevel';
+ const childrenPropName = options?.childrenPropName ?? Constants.treeDataProperties.CHILDREN_PROP;
+ const parentPropName = options?.parentPropName ?? Constants.treeDataProperties.PARENT_PROP;
+ const levelPropName = options?.levelPropName ?? Constants.treeDataProperties.TREE_LEVEL_PROP;
+ const collapsedPropName = options?.collapsedPropName ?? Constants.treeDataProperties.COLLAPSED_PROP;
const inputArray: P[] = flatArray || [];
const roots: T[] = []; // items without parent which at the root
@@ -75,15 +77,16 @@ export function unflattenParentChildArrayToTree
{
const item = all[id];
if (!(parentPropName in item) || item[parentPropName] === null || item[parentPropName] === undefined || item[parentPropName] === '') {
- // delete item[parentPropName];
roots.push(item);
} else if (item[parentPropName] in all) {
const p = all[item[parentPropName]];
if (!(childrenPropName in p)) {
p[childrenPropName] = [];
}
- // delete item[parentPropName];
p[childrenPropName].push(item);
+ if (p[collapsedPropName] === undefined) {
+ p[collapsedPropName] = options?.initiallyCollapsed ?? false;
+ }
}
});
@@ -102,7 +105,7 @@ export function unflattenParentChildArrayToTree
(treeArray: T[], options: { childrenPropName: string; levelPropName: string; }, treeLevel = 0) {
- const childrenPropName = (options?.childrenPropName ?? 'children') as keyof T;
+ const childrenPropName = (options?.childrenPropName ?? Constants.treeDataProperties.CHILDREN_PROP) as keyof T;
if (Array.isArray(treeArray)) {
for (const item of treeArray) {
@@ -124,11 +127,12 @@ export function addTreeLevelByMutation(treeArray: T[], options: { childrenPro
* @param {Object} options - you can provide "childrenPropName" (defaults to "children")
* @return {Array