@elliemae/ds-tree-model 3.14.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var DSTree_exports = {};
26
+ __export(DSTree_exports, {
27
+ DSTree: () => DSTree
28
+ });
29
+ module.exports = __toCommonJS(DSTree_exports);
30
+ var React = __toESM(require("react"));
31
+ var import_Node = require("./Node");
32
+ class DSTree {
33
+ constructor(item, options) {
34
+ this.getRoot = () => this.root;
35
+ this.getNode = (id) => this.nodes[id];
36
+ this.getNodes = () => this.nodes;
37
+ this.walk = (callback) => {
38
+ this.root.walk(callback);
39
+ };
40
+ this.walkParents = (callback) => {
41
+ this.root.walkParents(callback);
42
+ };
43
+ this.findNode = (callback) => this.root.findNode(callback);
44
+ this.findAllNodes = (callback) => this.root.findAllNodes(callback);
45
+ this.flatten = () => this.root.flatten();
46
+ this.addNode = (item, options = {}) => {
47
+ const { position, parent, callback } = options;
48
+ const parentDefault = parent || this.root;
49
+ const positionDefault = position ?? parentDefault.children.length;
50
+ const node = new import_Node.Node(item, {
51
+ childIndex: positionDefault,
52
+ depth: parentDefault.depth + 1,
53
+ parent: parentDefault,
54
+ tree: this
55
+ });
56
+ this.getNode(parentDefault.dsId).addNode(node);
57
+ this._hash += 1;
58
+ callback?.(node, this);
59
+ return node;
60
+ };
61
+ this.removeNode = (id, options = {}) => {
62
+ const node = this.getNode(id);
63
+ if (!node)
64
+ throw new Error(`Node with id ${id} not found`);
65
+ node.removeNode();
66
+ this._hash += 1;
67
+ options.callback?.(node, this);
68
+ return node;
69
+ };
70
+ this.moveNode = (id, options = {}) => {
71
+ const removedNode = this.removeNode(id);
72
+ const node = this.addNode(removedNode.getJson(), options);
73
+ this._hash += 1;
74
+ return node;
75
+ };
76
+ this.replaceNode = (id, item, options = {}) => {
77
+ const removedNode = this.removeNode(id);
78
+ const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });
79
+ this._hash += 1;
80
+ options.callback?.(node, this);
81
+ return node;
82
+ };
83
+ this.getPath = (id) => this.getRoot().getPath(id);
84
+ this.getPathIds = (id) => this.getRoot().getPathIds(id);
85
+ this.getUniqueId = options.getUniqueId;
86
+ this.root = new import_Node.Node(item, { childIndex: 0, depth: 0, tree: this });
87
+ this.nodes = {};
88
+ this._hash = 0;
89
+ this.root.walk((node) => {
90
+ this.nodes[node.dsId] = node;
91
+ return true;
92
+ });
93
+ }
94
+ addNodeToNodesDictionary(node) {
95
+ if (this.nodes[node.dsId]) {
96
+ throw new Error(`DSTree: repeated dsId ${node.dsId}`);
97
+ }
98
+ this.nodes[node.dsId] = node;
99
+ }
100
+ removeNodeFromNodesDictionary(node) {
101
+ delete this.nodes[node.dsId];
102
+ }
103
+ get hash() {
104
+ return this._hash;
105
+ }
106
+ }
107
+ //# sourceMappingURL=DSTree.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/DSTree.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';\nimport { Node } from './Node';\n\nexport class DSTree<T extends Item> {\n private root: Node<T>;\n\n private _hash: number;\n\n private nodes: Record<string, Node<T>>;\n\n getUniqueId: (item: T) => string;\n\n constructor(item: T, options: { getUniqueId: (item: T) => string }) {\n this.getUniqueId = options.getUniqueId;\n this.root = new Node(item, { childIndex: 0, depth: 0, tree: this });\n this.nodes = {};\n this._hash = 0;\n\n // autocalculate a dictionary where dsId are keys and nodes are values\n // and add the tree link to each node\n this.root.walk((node) => {\n this.nodes[node.dsId] = node;\n return true;\n });\n }\n\n addNodeToNodesDictionary(node: Node<T>): void {\n // check that we have no repeated ids\n if (this.nodes[node.dsId]) {\n throw new Error(`DSTree: repeated dsId ${node.dsId}`);\n }\n\n this.nodes[node.dsId] = node;\n }\n\n removeNodeFromNodesDictionary(node: Node<T>): void {\n delete this.nodes[node.dsId];\n }\n\n get hash(): number {\n return this._hash;\n }\n\n getRoot = (): Node<T> => this.root;\n\n getNode = (id: string): Node<T> => this.nodes[id];\n\n getNodes = (): Record<string, Node<T>> => this.nodes;\n\n walk = (callback: (node: Node<T>) => boolean): void => {\n this.root.walk(callback);\n };\n\n walkParents = (callback: (node: Node<T>) => boolean): void => {\n this.root.walkParents(callback);\n };\n\n findNode = (callback: (node: Node<T>) => boolean): Node<T> | null => this.root.findNode(callback);\n\n findAllNodes = (callback: (node: Node<T>) => boolean): Node<T>[] => this.root.findAllNodes(callback);\n\n flatten = (): Node<T>[] => this.root.flatten();\n\n addNode = (item: T, options: AddOptions<T> = {}): Node<T> => {\n const { position, parent, callback } = options;\n const parentDefault = parent || this.root;\n const positionDefault = position ?? parentDefault.children.length;\n\n // create node and add it to the parent\n const node = new Node(item, {\n childIndex: positionDefault,\n depth: parentDefault.depth + 1,\n parent: parentDefault,\n tree: this,\n });\n this.getNode(parentDefault.dsId).addNode(node);\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n callback?.(node, this);\n\n return node;\n };\n\n removeNode = (id: string, options: RemoveOptions<T> = {}): Node<T> => {\n const node = this.getNode(id);\n if (!node) throw new Error(`Node with id ${id} not found`);\n\n node.removeNode();\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n moveNode = (id: string, options: MoveOptions<T> = {}): Node<T> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(removedNode.getJson(), options);\n\n this._hash += 1;\n\n return node;\n };\n\n replaceNode = (id: string, item: T, options: MutateOptions<T> = {}): Node<T> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });\n\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n getPath = (id: string): Node<T>[] => this.getRoot().getPath(id);\n\n getPathIds = (id: string): string[] => this.getRoot().getPathIds(id);\n}\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADCvB,kBAAqB;AAEd,MAAM,OAAuB;AAAA,EASlC,YAAY,MAAS,SAA+C;AA+BpE,mBAAU,MAAe,KAAK;AAE9B,mBAAU,CAAC,OAAwB,KAAK,MAAM;AAE9C,oBAAW,MAA+B,KAAK;AAE/C,gBAAO,CAAC,aAA+C;AACrD,WAAK,KAAK,KAAK,QAAQ;AAAA,IACzB;AAEA,uBAAc,CAAC,aAA+C;AAC5D,WAAK,KAAK,YAAY,QAAQ;AAAA,IAChC;AAEA,oBAAW,CAAC,aAAyD,KAAK,KAAK,SAAS,QAAQ;AAEhG,wBAAe,CAAC,aAAoD,KAAK,KAAK,aAAa,QAAQ;AAEnG,mBAAU,MAAiB,KAAK,KAAK,QAAQ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AC3B,SAAK,OAAO,IAAI,iBAAK,MAAM,EAAE,YAAY,GAAG,OAAO,GAAG,MAAM,KAAK,CAAC;AAClE,SAAK,QAAQ,CAAC;AACd,SAAK,QAAQ;AAIb,SAAK,KAAK,KAAK,CAAC,SAAS;AACvB,WAAK,MAAM,KAAK,QAAQ;AACxB,aAAO;AAAA,IACT,CAAC;AAAA,EACH;AAAA,EAEA,yBAAyB,MAAqB;AAE5C,QAAI,KAAK,MAAM,KAAK,OAAO;AACzB,YAAM,IAAI,MAAM,yBAAyB,KAAK,MAAM;AAAA,IACtD;AAEA,SAAK,MAAM,KAAK,QAAQ;AAAA,EAC1B;AAAA,EAEA,8BAA8B,MAAqB;AACjD,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAoFF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var Node_exports = {};
26
+ __export(Node_exports, {
27
+ Node: () => Node
28
+ });
29
+ module.exports = __toCommonJS(Node_exports);
30
+ var React = __toESM(require("react"));
31
+ var import_lodash = require("lodash");
32
+ class Node {
33
+ constructor(item, { childIndex, depth, parent, tree }) {
34
+ this.fixChildIndexes = () => {
35
+ for (let i = 0; i < this.children.length; i++) {
36
+ this.children[i].childIndex = i;
37
+ }
38
+ this._hash += 1;
39
+ };
40
+ this.getCleanItem = (item) => {
41
+ const plainItem = (0, import_lodash.omit)(item, ["subitems"]);
42
+ return (0, import_lodash.cloneDeep)(plainItem);
43
+ };
44
+ this.walk = (callback) => {
45
+ const shouldContinueWalking = callback(this);
46
+ if (shouldContinueWalking) {
47
+ for (const child of this.children) {
48
+ child.walk(callback);
49
+ }
50
+ }
51
+ };
52
+ this.walkParents = (callback) => {
53
+ const shouldContinueWalking = callback(this);
54
+ if (shouldContinueWalking && this.parent) {
55
+ this.parent.walkParents(callback);
56
+ }
57
+ };
58
+ this.findNode = (callback) => {
59
+ let found = null;
60
+ this.walk((node) => {
61
+ if (found)
62
+ return false;
63
+ if (callback(node))
64
+ found = node;
65
+ return !found;
66
+ });
67
+ return found;
68
+ };
69
+ this.findAllNodes = (callback) => {
70
+ const found = [];
71
+ this.walk((node) => {
72
+ if (callback(node))
73
+ found.push(node);
74
+ return true;
75
+ });
76
+ return found;
77
+ };
78
+ this.flatten = () => {
79
+ const flattened = [];
80
+ this.walk((node) => {
81
+ flattened.push(node);
82
+ return true;
83
+ });
84
+ return flattened;
85
+ };
86
+ this.getPath = (id) => {
87
+ const path = [];
88
+ const node = this.tree.getNode(id);
89
+ if (!node)
90
+ return path;
91
+ node.walkParents((parent) => {
92
+ path.push(parent);
93
+ return parent.dsId !== this.dsId;
94
+ });
95
+ return path.reverse();
96
+ };
97
+ this.getPathIds = (id) => this.getPath(id).map((node) => node.dsId);
98
+ this.getJson = () => ({
99
+ ...this.plainItem,
100
+ subitems: this.children.map((child) => child.getJson())
101
+ });
102
+ this.addNode = (node) => {
103
+ const position = node.childIndex;
104
+ if (position < 0 || position > this.children.length) {
105
+ throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);
106
+ }
107
+ this.children.splice(position, 0, node);
108
+ this.fixChildIndexes();
109
+ node.walk((child) => {
110
+ this.tree.addNodeToNodesDictionary(child);
111
+ return true;
112
+ });
113
+ this._hash += 1;
114
+ };
115
+ this.removeNode = () => {
116
+ if (!this.parent)
117
+ throw new Error(`Cannot remove root node with id ${this.dsId}`);
118
+ this.parent.children.splice(this.childIndex, 1);
119
+ this.parent.fixChildIndexes();
120
+ this.walk((child) => {
121
+ this.tree.removeNodeFromNodesDictionary(child);
122
+ return true;
123
+ });
124
+ this._hash += 1;
125
+ };
126
+ this.dsId = tree.getUniqueId(item);
127
+ this.childIndex = childIndex;
128
+ this.depth = depth;
129
+ this.parent = parent ?? null;
130
+ this.tree = tree;
131
+ this._hash = 0;
132
+ this.children = item.subitems?.map(
133
+ (subitem, index) => new Node(subitem, { childIndex: index, depth: depth + 1, parent: this, tree })
134
+ ) ?? [];
135
+ this.plainItem = this.getCleanItem(item);
136
+ }
137
+ get hash() {
138
+ return this._hash;
139
+ }
140
+ }
141
+ //# sourceMappingURL=Node.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/Node.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["/* eslint-disable no-use-before-define */\n/* eslint-disable max-params */\nimport { cloneDeep, omit } from 'lodash';\nimport type { DSTree } from './DSTree';\nimport type { Item, NodeConstructorOptions } from './types';\n\nexport class Node<T extends Item> {\n dsId: string;\n\n plainItem: T;\n\n childIndex: number;\n\n depth: number;\n\n parent: Node<T> | null;\n\n children: Node<T>[];\n\n private tree!: DSTree<T>;\n\n private _hash: number;\n\n constructor(item: T, { childIndex, depth, parent, tree }: NodeConstructorOptions<T>) {\n this.dsId = tree.getUniqueId(item);\n this.childIndex = childIndex;\n this.depth = depth;\n this.parent = parent ?? null;\n this.tree = tree;\n this._hash = 0;\n\n this.children =\n item.subitems?.map(\n (subitem, index) => new Node(subitem as T, { childIndex: index, depth: depth + 1, parent: this, tree }),\n ) ?? [];\n\n // Save the item without the subitems\n this.plainItem = this.getCleanItem(item);\n }\n\n // ===========================================================================\n // INTERNAL METHODS\n // ===========================================================================\n\n fixChildIndexes = (): void => {\n for (let i = 0; i < this.children.length; i++) {\n this.children[i].childIndex = i;\n }\n this._hash += 1;\n };\n\n getCleanItem = (item: T): T => {\n const plainItem = omit(item, ['subitems']);\n return cloneDeep(plainItem as T);\n };\n\n // ===========================================================================\n // READ METHODS\n // ===========================================================================\n\n get hash(): number {\n return this._hash;\n }\n\n walk = (callback: (node: Node<T>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking) {\n for (const child of this.children) {\n child.walk(callback);\n }\n }\n };\n\n walkParents = (callback: (node: Node<T>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking && this.parent) {\n this.parent.walkParents(callback);\n }\n };\n\n findNode = (callback: (node: Node<T>) => boolean): Node<T> | null => {\n // find first node using walk\n let found: Node<T> | null = null;\n this.walk((node) => {\n // If we already found it, don't look anymore\n if (found) return false;\n\n // Otherwise keep looking\n if (callback(node)) found = node;\n return !found;\n });\n\n return found;\n };\n\n findAllNodes = (callback: (node: Node<T>) => boolean): Node<T>[] => {\n const found: Node<T>[] = [];\n this.walk((node) => {\n if (callback(node)) found.push(node);\n return true;\n });\n return found;\n };\n\n flatten = (): Node<T>[] => {\n const flattened: Node<T>[] = [];\n this.walk((node) => {\n flattened.push(node);\n return true;\n });\n return flattened;\n };\n\n getPath = (id: string): Node<T>[] => {\n const path: Node<T>[] = [];\n const node = this.tree.getNode(id);\n if (!node) return path;\n node.walkParents((parent) => {\n path.push(parent);\n return parent.dsId !== this.dsId;\n });\n return path.reverse();\n };\n\n getPathIds = (id: string): string[] => this.getPath(id).map((node) => node.dsId);\n\n getJson = (): T => ({\n ...this.plainItem,\n subitems: this.children.map((child) => child.getJson()),\n });\n\n // ===========================================================================\n // WRITE METHODS\n // ===========================================================================\n\n addNode = (node: Node<T>) => {\n // check that the position is valid for that parent\n const position = node.childIndex;\n if (position < 0 || position > this.children.length) {\n throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);\n }\n\n // add the new node\n this.children.splice(position, 0, node);\n\n // fix the childIndex of the nodes after the new one\n this.fixChildIndexes();\n\n // add subtree of the new node to the node dictionary\n node.walk((child) => {\n this.tree.addNodeToNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n\n removeNode = () => {\n // if no parent throw error\n if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);\n\n // remove from parent\n this.parent.children.splice(this.childIndex, 1);\n\n // fix the childIndex of the nodes of the parent\n this.parent.fixChildIndexes();\n\n // remove subtree from the node dictionary\n this.walk((child) => {\n this.tree.removeNodeFromNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n}\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADEvB,oBAAgC;AAIzB,MAAM,KAAqB;AAAA,EAiBhC,YAAY,MAAS,EAAE,YAAY,OAAO,QAAQ,KAAK,GAA8B;AAqBrF,2BAAkB,MAAY;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK;AAC7C,aAAK,SAAS,GAAG,aAAa;AAAA,MAChC;AACA,WAAK,SAAS;AAAA,IAChB;AAEA,wBAAe,CAAC,SAAe;AAC7B,YAAM,gBAAY,oBAAK,MAAM,CAAC,UAAU,CAAC;AACzC,iBAAO,yBAAU,SAAc;AAAA,IACjC;AAUA,gBAAO,CAAC,aAA+C;AACrD,YAAM,wBAAwB,SAAS,IAAI;AAC3C,UAAI,uBAA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,CAAC;AAAA,IACxD;AAMA,mBAAU,CAAC,SAAkB;AAE3B,YAAM,WAAW,KAAK;AACtB,UAAI,WAAW,KAAK,WAAW,KAAK,SAAS,QAAQ;AACnD,cAAM,IAAI,MAAM,oBAAoB,uBAAuB,KAAK,kBAAkB,KAAK,MAAM;AAAA,MAC/F;AAGA,WAAK,SAAS,OAAO,UAAU,GAAG,IAAI;AAGtC,WAAK,gBAAgB;AAGrB,WAAK,KAAK,CAAC,UAAU;AACnB,aAAK,KAAK,yBAAyB,KAAK;AACxC,eAAO;AAAA,MACT,CAAC;AAED,WAAK,SAAS;AAAA,IAChB;AAEA,sBAAa,MAAM;AAEjB,UAAI,CAAC,KAAK;AAAQ,cAAM,IAAI,MAAM,mCAAmC,KAAK,MAAM;AAGhF,WAAK,OAAO,SAAS,OAAO,KAAK,YAAY,CAAC;AAG9C,WAAK,OAAO,gBAAgB;AAG5B,WAAK,KAAK,CAAC,UAAU;AACnB,aAAK,KAAK,8BAA8B,KAAK;AAC7C,eAAO;AAAA,MACT,CAAC;AAED,WAAK,SAAS;AAAA,IAChB;AAtJE,SAAK,OAAO,KAAK,YAAY,IAAI;AACjC,SAAK,aAAa;AAClB,SAAK,QAAQ;AACb,SAAK,SAAS,UAAU;AACxB,SAAK,OAAO;AACZ,SAAK,QAAQ;AAEb,SAAK,WACH,KAAK,UAAU;AAAA,MACb,CAAC,SAAS,UAAU,IAAI,KAAK,SAAc,EAAE,YAAY,OAAO,OAAO,QAAQ,GAAG,QAAQ,MAAM,KAAK,CAAC;AAAA,IACxG,KAAK,CAAC;AAGR,SAAK,YAAY,KAAK,aAAa,IAAI;AAAA,EACzC;AAAA,EAsBA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAiHF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var src_exports = {};
26
+ __export(src_exports, {
27
+ DSTree: () => import_DSTree.DSTree,
28
+ useDSTree: () => import_useDSTree.useDSTree
29
+ });
30
+ module.exports = __toCommonJS(src_exports);
31
+ var React = __toESM(require("react"));
32
+ var import_DSTree = require("./DSTree");
33
+ var import_useDSTree = require("./useDSTree");
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["export type { Item } from './types';\nexport { DSTree } from './DSTree';\nexport { useDSTree } from './useDSTree';\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADCvB,oBAAuB;AACvB,uBAA0B;",
6
+ "names": []
7
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
18
+ mod
19
+ ));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+ var types_exports = {};
22
+ module.exports = __toCommonJS(types_exports);
23
+ var React = __toESM(require("react"));
24
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/types.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import type { DSTree } from './DSTree';\nimport type { Node } from './Node';\n\nexport interface Item {\n subitems?: Item[];\n}\n\nexport interface NodeConstructorOptions<T extends Item> {\n childIndex: number;\n depth: number;\n tree: DSTree<T>;\n parent?: Node<T> | null;\n}\n\nexport interface AddOptions<T extends Item> {\n position?: number;\n parent?: Node<T> | null;\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\n\nexport interface RemoveOptions<T extends Item> {\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\n\nexport interface MoveOptions<T extends Item> {\n position?: number;\n parent?: Node<T>;\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\n\nexport interface MutateOptions<T extends Item> {\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;ACAA,YAAuB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var useDSTree_exports = {};
26
+ __export(useDSTree_exports, {
27
+ useDSTree: () => useDSTree
28
+ });
29
+ module.exports = __toCommonJS(useDSTree_exports);
30
+ var React = __toESM(require("react"));
31
+ var import_react = require("react");
32
+ var import_DSTree = require("./DSTree");
33
+ const useDSTree = (rootItem, opts) => {
34
+ const tree = (0, import_react.useMemo)(() => new import_DSTree.DSTree(rootItem, opts), [opts, rootItem]);
35
+ const [hash, setHash] = (0, import_react.useState)(tree.hash);
36
+ const addNode = (0, import_react.useCallback)(
37
+ (item, options) => {
38
+ const cb = (node, treecb) => {
39
+ options?.callback?.(node, treecb);
40
+ setHash(treecb.hash);
41
+ };
42
+ return tree.addNode(item, { ...options, callback: cb });
43
+ },
44
+ [tree]
45
+ );
46
+ const removeNode = (0, import_react.useCallback)(
47
+ (id, options) => {
48
+ const cb = (node, treecb) => {
49
+ options?.callback?.(node, treecb);
50
+ setHash(treecb.hash);
51
+ };
52
+ return tree.removeNode(id, { ...options, callback: cb });
53
+ },
54
+ [tree]
55
+ );
56
+ const moveNode = (0, import_react.useCallback)(
57
+ (id, options) => {
58
+ const cb = (node, treecb) => {
59
+ options?.callback?.(node, treecb);
60
+ setHash(treecb.hash);
61
+ };
62
+ return tree.moveNode(id, { ...options, callback: cb });
63
+ },
64
+ [tree]
65
+ );
66
+ const replaceNode = (0, import_react.useCallback)(
67
+ (id, item, options) => {
68
+ const cb = (node, treecb) => {
69
+ options?.callback?.(node, treecb);
70
+ setHash(treecb.hash);
71
+ };
72
+ return tree.replaceNode(id, item, { ...options, callback: cb });
73
+ },
74
+ [tree]
75
+ );
76
+ return {
77
+ hash,
78
+ getRoot: tree.getRoot,
79
+ getNode: tree.getNode,
80
+ walk: tree.walk,
81
+ walkParents: tree.walkParents,
82
+ findNode: tree.findNode,
83
+ findAllNodes: tree.findAllNodes,
84
+ flatten: tree.flatten,
85
+ addNode,
86
+ removeNode,
87
+ moveNode,
88
+ replaceNode,
89
+ getPath: tree.getPath,
90
+ getPathIds: tree.getPathIds
91
+ };
92
+ };
93
+ //# sourceMappingURL=useDSTree.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/useDSTree.ts", "../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import { useCallback, useMemo, useState } from 'react';\nimport { DSTree } from './DSTree';\nimport type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';\n\nexport const useDSTree = <T extends Item>(rootItem: T, opts: { getUniqueId: (item: T) => string }) => {\n const tree = useMemo(() => new DSTree(rootItem, opts), [opts, rootItem]);\n const [hash, setHash] = useState(tree.hash);\n\n const addNode = useCallback(\n (item: T, options?: AddOptions<T>) => {\n const cb: AddOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.addNode(item, { ...options, callback: cb });\n },\n [tree],\n );\n\n const removeNode = useCallback(\n (id: string, options?: RemoveOptions<T>) => {\n const cb: RemoveOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.removeNode(id, { ...options, callback: cb });\n },\n [tree],\n );\n\n const moveNode = useCallback(\n (id: string, options?: MoveOptions<T>) => {\n const cb: MoveOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.moveNode(id, { ...options, callback: cb });\n },\n [tree],\n );\n\n const replaceNode = useCallback(\n (id: string, item: T, options?: MutateOptions<T>) => {\n const cb: MutateOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.replaceNode(id, item, { ...options, callback: cb });\n },\n [tree],\n );\n\n return {\n hash,\n getRoot: tree.getRoot,\n getNode: tree.getNode,\n walk: tree.walk,\n walkParents: tree.walkParents,\n findNode: tree.findNode,\n findAllNodes: tree.findAllNodes,\n flatten: tree.flatten,\n addNode,\n removeNode,\n moveNode,\n replaceNode,\n getPath: tree.getPath,\n getPathIds: tree.getPathIds,\n };\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,mBAA+C;AAC/C,oBAAuB;AAGhB,MAAM,YAAY,CAAiB,UAAa,SAA+C;AACpG,QAAM,WAAO,sBAAQ,MAAM,IAAI,qBAAO,UAAU,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC;AACvE,QAAM,CAAC,MAAM,OAAO,QAAI,uBAAS,KAAK,IAAI;AAE1C,QAAM,cAAU;AAAA,IACd,CAAC,MAAS,YAA4B;AACpC,YA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;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS,KAAK;AAAA,IACd,YAAY,KAAK;AAAA,EACnB;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,81 @@
1
+ import * as React from "react";
2
+ import { Node } from "./Node";
3
+ class DSTree {
4
+ constructor(item, options) {
5
+ this.getRoot = () => this.root;
6
+ this.getNode = (id) => this.nodes[id];
7
+ this.getNodes = () => this.nodes;
8
+ this.walk = (callback) => {
9
+ this.root.walk(callback);
10
+ };
11
+ this.walkParents = (callback) => {
12
+ this.root.walkParents(callback);
13
+ };
14
+ this.findNode = (callback) => this.root.findNode(callback);
15
+ this.findAllNodes = (callback) => this.root.findAllNodes(callback);
16
+ this.flatten = () => this.root.flatten();
17
+ this.addNode = (item, options = {}) => {
18
+ const { position, parent, callback } = options;
19
+ const parentDefault = parent || this.root;
20
+ const positionDefault = position ?? parentDefault.children.length;
21
+ const node = new Node(item, {
22
+ childIndex: positionDefault,
23
+ depth: parentDefault.depth + 1,
24
+ parent: parentDefault,
25
+ tree: this
26
+ });
27
+ this.getNode(parentDefault.dsId).addNode(node);
28
+ this._hash += 1;
29
+ callback?.(node, this);
30
+ return node;
31
+ };
32
+ this.removeNode = (id, options = {}) => {
33
+ const node = this.getNode(id);
34
+ if (!node)
35
+ throw new Error(`Node with id ${id} not found`);
36
+ node.removeNode();
37
+ this._hash += 1;
38
+ options.callback?.(node, this);
39
+ return node;
40
+ };
41
+ this.moveNode = (id, options = {}) => {
42
+ const removedNode = this.removeNode(id);
43
+ const node = this.addNode(removedNode.getJson(), options);
44
+ this._hash += 1;
45
+ return node;
46
+ };
47
+ this.replaceNode = (id, item, options = {}) => {
48
+ const removedNode = this.removeNode(id);
49
+ const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });
50
+ this._hash += 1;
51
+ options.callback?.(node, this);
52
+ return node;
53
+ };
54
+ this.getPath = (id) => this.getRoot().getPath(id);
55
+ this.getPathIds = (id) => this.getRoot().getPathIds(id);
56
+ this.getUniqueId = options.getUniqueId;
57
+ this.root = new Node(item, { childIndex: 0, depth: 0, tree: this });
58
+ this.nodes = {};
59
+ this._hash = 0;
60
+ this.root.walk((node) => {
61
+ this.nodes[node.dsId] = node;
62
+ return true;
63
+ });
64
+ }
65
+ addNodeToNodesDictionary(node) {
66
+ if (this.nodes[node.dsId]) {
67
+ throw new Error(`DSTree: repeated dsId ${node.dsId}`);
68
+ }
69
+ this.nodes[node.dsId] = node;
70
+ }
71
+ removeNodeFromNodesDictionary(node) {
72
+ delete this.nodes[node.dsId];
73
+ }
74
+ get hash() {
75
+ return this._hash;
76
+ }
77
+ }
78
+ export {
79
+ DSTree
80
+ };
81
+ //# sourceMappingURL=DSTree.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/DSTree.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';\nimport { Node } from './Node';\n\nexport class DSTree<T extends Item> {\n private root: Node<T>;\n\n private _hash: number;\n\n private nodes: Record<string, Node<T>>;\n\n getUniqueId: (item: T) => string;\n\n constructor(item: T, options: { getUniqueId: (item: T) => string }) {\n this.getUniqueId = options.getUniqueId;\n this.root = new Node(item, { childIndex: 0, depth: 0, tree: this });\n this.nodes = {};\n this._hash = 0;\n\n // autocalculate a dictionary where dsId are keys and nodes are values\n // and add the tree link to each node\n this.root.walk((node) => {\n this.nodes[node.dsId] = node;\n return true;\n });\n }\n\n addNodeToNodesDictionary(node: Node<T>): void {\n // check that we have no repeated ids\n if (this.nodes[node.dsId]) {\n throw new Error(`DSTree: repeated dsId ${node.dsId}`);\n }\n\n this.nodes[node.dsId] = node;\n }\n\n removeNodeFromNodesDictionary(node: Node<T>): void {\n delete this.nodes[node.dsId];\n }\n\n get hash(): number {\n return this._hash;\n }\n\n getRoot = (): Node<T> => this.root;\n\n getNode = (id: string): Node<T> => this.nodes[id];\n\n getNodes = (): Record<string, Node<T>> => this.nodes;\n\n walk = (callback: (node: Node<T>) => boolean): void => {\n this.root.walk(callback);\n };\n\n walkParents = (callback: (node: Node<T>) => boolean): void => {\n this.root.walkParents(callback);\n };\n\n findNode = (callback: (node: Node<T>) => boolean): Node<T> | null => this.root.findNode(callback);\n\n findAllNodes = (callback: (node: Node<T>) => boolean): Node<T>[] => this.root.findAllNodes(callback);\n\n flatten = (): Node<T>[] => this.root.flatten();\n\n addNode = (item: T, options: AddOptions<T> = {}): Node<T> => {\n const { position, parent, callback } = options;\n const parentDefault = parent || this.root;\n const positionDefault = position ?? parentDefault.children.length;\n\n // create node and add it to the parent\n const node = new Node(item, {\n childIndex: positionDefault,\n depth: parentDefault.depth + 1,\n parent: parentDefault,\n tree: this,\n });\n this.getNode(parentDefault.dsId).addNode(node);\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n callback?.(node, this);\n\n return node;\n };\n\n removeNode = (id: string, options: RemoveOptions<T> = {}): Node<T> => {\n const node = this.getNode(id);\n if (!node) throw new Error(`Node with id ${id} not found`);\n\n node.removeNode();\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n moveNode = (id: string, options: MoveOptions<T> = {}): Node<T> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(removedNode.getJson(), options);\n\n this._hash += 1;\n\n return node;\n };\n\n replaceNode = (id: string, item: T, options: MutateOptions<T> = {}): Node<T> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });\n\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n getPath = (id: string): Node<T>[] => this.getRoot().getPath(id);\n\n getPathIds = (id: string): string[] => this.getRoot().getPathIds(id);\n}\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACCvB,SAAS,YAAY;AAEd,MAAM,OAAuB;AAAA,EASlC,YAAY,MAAS,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c,CAAC,IAAY,MAAS,UAA4B,CAAC,MAAe;AAC9E,YAAM,cAAc,KAAK,WAAW,EAAE;AACtC,YAAM,OAAO,KAAK,QAAQ,MAAM,EAAE,QAAQ,YAAY,QAAQ,UAAU,YAAY,WAAW,CAAC;AAEhG,WAAK,SAAS;AAGd,cAAQ,WAAW,MAAM,IAAI;AAE7B,aAAO;AAAA,IACT;AAEA,mBAAU,CAAC,OAA0B,KAAK,QAAQ,EAAE,QAAQ,EAAE;AAE9D,sBAAa,CAAC,OAAyB,KAAK,QAAQ,EAAE,WAAW,EAAE;AA/GjE,SAAK,cAAc,QAAQ;AAC3B,SAAK,OAAO,IAAI,KAAK,MAAM,EAAE,YAAY,GAAG,OAAO,GAAG,MAAM,KAAK,CAAC;AAClE,SAAK,QAAQ,CAAC;AACd,SAAK,QAAQ;AAIb,SAAK,KAAK,KAAK,CAAC,SAAS;AACvB,WAAK,MAAM,KAAK,QAAQ;AACxB,aAAO;AAAA,IACT,CAAC;AAAA,EACH;AAAA,EAEA,yBAAyB,MAAqB;AAE5C,QAAI,KAAK,MAAM,KAAK,OAAO;AACzB,YAAM,IAAI,MAAM,yBAAyB,KAAK,MAAM;AAAA,IACtD;AAEA,SAAK,MAAM,KAAK,QAAQ;AAAA,EAC1B;AAAA,EAEA,8BAA8B,MAAqB;AACjD,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAoFF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,115 @@
1
+ import * as React from "react";
2
+ import { cloneDeep, omit } from "lodash";
3
+ class Node {
4
+ constructor(item, { childIndex, depth, parent, tree }) {
5
+ this.fixChildIndexes = () => {
6
+ for (let i = 0; i < this.children.length; i++) {
7
+ this.children[i].childIndex = i;
8
+ }
9
+ this._hash += 1;
10
+ };
11
+ this.getCleanItem = (item) => {
12
+ const plainItem = omit(item, ["subitems"]);
13
+ return cloneDeep(plainItem);
14
+ };
15
+ this.walk = (callback) => {
16
+ const shouldContinueWalking = callback(this);
17
+ if (shouldContinueWalking) {
18
+ for (const child of this.children) {
19
+ child.walk(callback);
20
+ }
21
+ }
22
+ };
23
+ this.walkParents = (callback) => {
24
+ const shouldContinueWalking = callback(this);
25
+ if (shouldContinueWalking && this.parent) {
26
+ this.parent.walkParents(callback);
27
+ }
28
+ };
29
+ this.findNode = (callback) => {
30
+ let found = null;
31
+ this.walk((node) => {
32
+ if (found)
33
+ return false;
34
+ if (callback(node))
35
+ found = node;
36
+ return !found;
37
+ });
38
+ return found;
39
+ };
40
+ this.findAllNodes = (callback) => {
41
+ const found = [];
42
+ this.walk((node) => {
43
+ if (callback(node))
44
+ found.push(node);
45
+ return true;
46
+ });
47
+ return found;
48
+ };
49
+ this.flatten = () => {
50
+ const flattened = [];
51
+ this.walk((node) => {
52
+ flattened.push(node);
53
+ return true;
54
+ });
55
+ return flattened;
56
+ };
57
+ this.getPath = (id) => {
58
+ const path = [];
59
+ const node = this.tree.getNode(id);
60
+ if (!node)
61
+ return path;
62
+ node.walkParents((parent) => {
63
+ path.push(parent);
64
+ return parent.dsId !== this.dsId;
65
+ });
66
+ return path.reverse();
67
+ };
68
+ this.getPathIds = (id) => this.getPath(id).map((node) => node.dsId);
69
+ this.getJson = () => ({
70
+ ...this.plainItem,
71
+ subitems: this.children.map((child) => child.getJson())
72
+ });
73
+ this.addNode = (node) => {
74
+ const position = node.childIndex;
75
+ if (position < 0 || position > this.children.length) {
76
+ throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);
77
+ }
78
+ this.children.splice(position, 0, node);
79
+ this.fixChildIndexes();
80
+ node.walk((child) => {
81
+ this.tree.addNodeToNodesDictionary(child);
82
+ return true;
83
+ });
84
+ this._hash += 1;
85
+ };
86
+ this.removeNode = () => {
87
+ if (!this.parent)
88
+ throw new Error(`Cannot remove root node with id ${this.dsId}`);
89
+ this.parent.children.splice(this.childIndex, 1);
90
+ this.parent.fixChildIndexes();
91
+ this.walk((child) => {
92
+ this.tree.removeNodeFromNodesDictionary(child);
93
+ return true;
94
+ });
95
+ this._hash += 1;
96
+ };
97
+ this.dsId = tree.getUniqueId(item);
98
+ this.childIndex = childIndex;
99
+ this.depth = depth;
100
+ this.parent = parent ?? null;
101
+ this.tree = tree;
102
+ this._hash = 0;
103
+ this.children = item.subitems?.map(
104
+ (subitem, index) => new Node(subitem, { childIndex: index, depth: depth + 1, parent: this, tree })
105
+ ) ?? [];
106
+ this.plainItem = this.getCleanItem(item);
107
+ }
108
+ get hash() {
109
+ return this._hash;
110
+ }
111
+ }
112
+ export {
113
+ Node
114
+ };
115
+ //# sourceMappingURL=Node.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/Node.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable no-use-before-define */\n/* eslint-disable max-params */\nimport { cloneDeep, omit } from 'lodash';\nimport type { DSTree } from './DSTree';\nimport type { Item, NodeConstructorOptions } from './types';\n\nexport class Node<T extends Item> {\n dsId: string;\n\n plainItem: T;\n\n childIndex: number;\n\n depth: number;\n\n parent: Node<T> | null;\n\n children: Node<T>[];\n\n private tree!: DSTree<T>;\n\n private _hash: number;\n\n constructor(item: T, { childIndex, depth, parent, tree }: NodeConstructorOptions<T>) {\n this.dsId = tree.getUniqueId(item);\n this.childIndex = childIndex;\n this.depth = depth;\n this.parent = parent ?? null;\n this.tree = tree;\n this._hash = 0;\n\n this.children =\n item.subitems?.map(\n (subitem, index) => new Node(subitem as T, { childIndex: index, depth: depth + 1, parent: this, tree }),\n ) ?? [];\n\n // Save the item without the subitems\n this.plainItem = this.getCleanItem(item);\n }\n\n // ===========================================================================\n // INTERNAL METHODS\n // ===========================================================================\n\n fixChildIndexes = (): void => {\n for (let i = 0; i < this.children.length; i++) {\n this.children[i].childIndex = i;\n }\n this._hash += 1;\n };\n\n getCleanItem = (item: T): T => {\n const plainItem = omit(item, ['subitems']);\n return cloneDeep(plainItem as T);\n };\n\n // ===========================================================================\n // READ METHODS\n // ===========================================================================\n\n get hash(): number {\n return this._hash;\n }\n\n walk = (callback: (node: Node<T>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking) {\n for (const child of this.children) {\n child.walk(callback);\n }\n }\n };\n\n walkParents = (callback: (node: Node<T>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking && this.parent) {\n this.parent.walkParents(callback);\n }\n };\n\n findNode = (callback: (node: Node<T>) => boolean): Node<T> | null => {\n // find first node using walk\n let found: Node<T> | null = null;\n this.walk((node) => {\n // If we already found it, don't look anymore\n if (found) return false;\n\n // Otherwise keep looking\n if (callback(node)) found = node;\n return !found;\n });\n\n return found;\n };\n\n findAllNodes = (callback: (node: Node<T>) => boolean): Node<T>[] => {\n const found: Node<T>[] = [];\n this.walk((node) => {\n if (callback(node)) found.push(node);\n return true;\n });\n return found;\n };\n\n flatten = (): Node<T>[] => {\n const flattened: Node<T>[] = [];\n this.walk((node) => {\n flattened.push(node);\n return true;\n });\n return flattened;\n };\n\n getPath = (id: string): Node<T>[] => {\n const path: Node<T>[] = [];\n const node = this.tree.getNode(id);\n if (!node) return path;\n node.walkParents((parent) => {\n path.push(parent);\n return parent.dsId !== this.dsId;\n });\n return path.reverse();\n };\n\n getPathIds = (id: string): string[] => this.getPath(id).map((node) => node.dsId);\n\n getJson = (): T => ({\n ...this.plainItem,\n subitems: this.children.map((child) => child.getJson()),\n });\n\n // ===========================================================================\n // WRITE METHODS\n // ===========================================================================\n\n addNode = (node: Node<T>) => {\n // check that the position is valid for that parent\n const position = node.childIndex;\n if (position < 0 || position > this.children.length) {\n throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);\n }\n\n // add the new node\n this.children.splice(position, 0, node);\n\n // fix the childIndex of the nodes after the new one\n this.fixChildIndexes();\n\n // add subtree of the new node to the node dictionary\n node.walk((child) => {\n this.tree.addNodeToNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n\n removeNode = () => {\n // if no parent throw error\n if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);\n\n // remove from parent\n this.parent.children.splice(this.childIndex, 1);\n\n // fix the childIndex of the nodes of the parent\n this.parent.fixChildIndexes();\n\n // remove subtree from the node dictionary\n this.walk((child) => {\n this.tree.removeNodeFromNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n}\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACEvB,SAAS,WAAW,YAAY;AAIzB,MAAM,KAAqB;AAAA,EAiBhC,YAAY,MAAS,EAAE,YAAY,OAAO,QAAQ,KAAK,GAA8B;AAqBrF,2BAAkB,MAAY;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK;AAC7C,aAAK,SAAS,GAAG,aAAa;AAAA,MAChC;AACA,WAAK,SAAS;AAAA,IAChB;AAEA,wBAAe,CAAC,SAAe;AAC7B,YAAM,YAAY,KAAK,MAAM,CAAC,UAAU,CAAC;AACzC,aAAO,UAAU,SAAc;AAAA,IACjC;AAUA,gBAAO,CAAC,aAA+C;AACrD,YAAM,wBAAwB,SAAS,IAAI;AAC3C,UAAI,uBAAuB;AACzB,mBAAW,SAAS,KAAK,UAAU;AACjC,gBAAM,KAAK,QAAQ;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AAEA,uBAAc,CAAC,aAA+C;AAC5D,YAAM,wBAAwB,SAAS,IAAI;AAC3C,UAAI,yBAAyB,KAAK,QAAQ;AACxC,aAAK,OAAO,YAAY,QAAQ;AAAA,MAClC;AAAA,IACF;AAEA,oBAAW,CAAC,aAAyD;AAEnE,UAAI,QAAwB;AAC5B,WAAK,KAAK,CAAC,SAAS;AAElB,YAAI;AAAO,iBAAO;AAGlB,YAAI,SAAS,IAAI;AAAG,kBAAQ;AAC5B,eAAO,CAAC;AAAA,MACV,CAAC;AAED,aAAO;AAAA,IACT;AA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I,MAAM,mCAAmC,KAAK,MAAM;AAGhF,WAAK,OAAO,SAAS,OAAO,KAAK,YAAY,CAAC;AAG9C,WAAK,OAAO,gBAAgB;AAG5B,WAAK,KAAK,CAAC,UAAU;AACnB,aAAK,KAAK,8BAA8B,KAAK;AAC7C,eAAO;AAAA,MACT,CAAC;AAED,WAAK,SAAS;AAAA,IAChB;AAtJE,SAAK,OAAO,KAAK,YAAY,IAAI;AACjC,SAAK,aAAa;AAClB,SAAK,QAAQ;AACb,SAAK,SAAS,UAAU;AACxB,SAAK,OAAO;AACZ,SAAK,QAAQ;AAEb,SAAK,WACH,KAAK,UAAU;AAAA,MACb,CAAC,SAAS,UAAU,IAAI,KAAK,SAAc,EAAE,YAAY,OAAO,OAAO,QAAQ,GAAG,QAAQ,MAAM,KAAK,CAAC;AAAA,IACxG,KAAK,CAAC;AAGR,SAAK,YAAY,KAAK,aAAa,IAAI;AAAA,EACzC;AAAA,EAsBA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAiHF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,8 @@
1
+ import * as React from "react";
2
+ import { DSTree } from "./DSTree";
3
+ import { useDSTree } from "./useDSTree";
4
+ export {
5
+ DSTree,
6
+ useDSTree
7
+ };
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/index.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "export type { Item } from './types';\nexport { DSTree } from './DSTree';\nexport { useDSTree } from './useDSTree';\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACCvB,SAAS,cAAc;AACvB,SAAS,iBAAiB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,2 @@
1
+ import * as React from "react";
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;",
6
+ "names": []
7
+ }
@@ -0,0 +1,67 @@
1
+ import * as React from "react";
2
+ import { useCallback, useMemo, useState } from "react";
3
+ import { DSTree } from "./DSTree";
4
+ const useDSTree = (rootItem, opts) => {
5
+ const tree = useMemo(() => new DSTree(rootItem, opts), [opts, rootItem]);
6
+ const [hash, setHash] = useState(tree.hash);
7
+ const addNode = useCallback(
8
+ (item, options) => {
9
+ const cb = (node, treecb) => {
10
+ options?.callback?.(node, treecb);
11
+ setHash(treecb.hash);
12
+ };
13
+ return tree.addNode(item, { ...options, callback: cb });
14
+ },
15
+ [tree]
16
+ );
17
+ const removeNode = useCallback(
18
+ (id, options) => {
19
+ const cb = (node, treecb) => {
20
+ options?.callback?.(node, treecb);
21
+ setHash(treecb.hash);
22
+ };
23
+ return tree.removeNode(id, { ...options, callback: cb });
24
+ },
25
+ [tree]
26
+ );
27
+ const moveNode = useCallback(
28
+ (id, options) => {
29
+ const cb = (node, treecb) => {
30
+ options?.callback?.(node, treecb);
31
+ setHash(treecb.hash);
32
+ };
33
+ return tree.moveNode(id, { ...options, callback: cb });
34
+ },
35
+ [tree]
36
+ );
37
+ const replaceNode = useCallback(
38
+ (id, item, options) => {
39
+ const cb = (node, treecb) => {
40
+ options?.callback?.(node, treecb);
41
+ setHash(treecb.hash);
42
+ };
43
+ return tree.replaceNode(id, item, { ...options, callback: cb });
44
+ },
45
+ [tree]
46
+ );
47
+ return {
48
+ hash,
49
+ getRoot: tree.getRoot,
50
+ getNode: tree.getNode,
51
+ walk: tree.walk,
52
+ walkParents: tree.walkParents,
53
+ findNode: tree.findNode,
54
+ findAllNodes: tree.findAllNodes,
55
+ flatten: tree.flatten,
56
+ addNode,
57
+ removeNode,
58
+ moveNode,
59
+ replaceNode,
60
+ getPath: tree.getPath,
61
+ getPathIds: tree.getPathIds
62
+ };
63
+ };
64
+ export {
65
+ useDSTree
66
+ };
67
+ //# sourceMappingURL=useDSTree.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/useDSTree.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { useCallback, useMemo, useState } from 'react';\nimport { DSTree } from './DSTree';\nimport type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';\n\nexport const useDSTree = <T extends Item>(rootItem: T, opts: { getUniqueId: (item: T) => string }) => {\n const tree = useMemo(() => new DSTree(rootItem, opts), [opts, rootItem]);\n const [hash, setHash] = useState(tree.hash);\n\n const addNode = useCallback(\n (item: T, options?: AddOptions<T>) => {\n const cb: AddOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.addNode(item, { ...options, callback: cb });\n },\n [tree],\n );\n\n const removeNode = useCallback(\n (id: string, options?: RemoveOptions<T>) => {\n const cb: RemoveOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.removeNode(id, { ...options, callback: cb });\n },\n [tree],\n );\n\n const moveNode = useCallback(\n (id: string, options?: MoveOptions<T>) => {\n const cb: MoveOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.moveNode(id, { ...options, callback: cb });\n },\n [tree],\n );\n\n const replaceNode = useCallback(\n (id: string, item: T, options?: MutateOptions<T>) => {\n const cb: MutateOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.replaceNode(id, item, { ...options, callback: cb });\n },\n [tree],\n );\n\n return {\n hash,\n getRoot: tree.getRoot,\n getNode: tree.getNode,\n walk: tree.walk,\n walkParents: tree.walkParents,\n findNode: tree.findNode,\n findAllNodes: tree.findAllNodes,\n flatten: tree.flatten,\n addNode,\n removeNode,\n moveNode,\n replaceNode,\n getPath: tree.getPath,\n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Q,OAAO,IAAI;AAAA,MACrB;AACA,aAAO,KAAK,YAAY,IAAI,MAAM,EAAE,GAAG,SAAS,UAAU,GAAG,CAAC;AAAA,IAChE;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO;AAAA,IACL;AAAA,IACA,SAAS,KAAK;AAAA,IACd,SAAS,KAAK;AAAA,IACd,MAAM,KAAK;AAAA,IACX,aAAa,KAAK;AAAA,IAClB,UAAU,KAAK;AAAA,IACf,cAAc,KAAK;AAAA,IACnB,SAAS,KAAK;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS,KAAK;AAAA,IACd,YAAY,KAAK;AAAA,EACnB;AACF;",
6
+ "names": []
7
+ }
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@elliemae/ds-tree-model",
3
+ "version": "3.14.0-next.1",
4
+ "license": "MIT",
5
+ "description": "ICE MT - Dimsum - Tree Model",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "module": "./dist/esm/index.js",
10
+ "main": "./dist/cjs/index.js",
11
+ "types": "./dist/types/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/esm/index.js",
15
+ "require": "./dist/cjs/index.js"
16
+ }
17
+ },
18
+ "sideEffects": [
19
+ "*.css",
20
+ "*.scss"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://git.elliemae.io/platform-ui/dimsum.git"
25
+ },
26
+ "engines": {
27
+ "pnpm": ">=6",
28
+ "node": ">=16"
29
+ },
30
+ "author": "ICE MT",
31
+ "jestSonar": {
32
+ "sonar56x": true,
33
+ "reportPath": "reports",
34
+ "reportFile": "tests.xml",
35
+ "indent": 4
36
+ },
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "typeSafety": false
40
+ },
41
+ "dependencies": {},
42
+ "devDependencies": {
43
+ "@testing-library/jest-dom": "~5.16.5"
44
+ },
45
+ "peerDependencies": {
46
+ "lodash": "^4.17.21",
47
+ "react": "~17.0.2",
48
+ "react-dom": "^17.0.2"
49
+ },
50
+ "scripts": {
51
+ "test": "node ../../scripts/testing/test.mjs",
52
+ "lint": "node ../../scripts/lint.mjs",
53
+ "eslint:fix": "eslint --ext='.js,.jsx,.test.js,.ts,.tsx' --fix --config='../../.eslintrc.js' src/",
54
+ "dts": "node ../../scripts/dts.mjs",
55
+ "build": "cross-env NODE_ENV=production node ../../scripts/build/build.mjs",
56
+ "dev:build": "pnpm --filter {.}... build && pnpm --filter {.}... dts",
57
+ "dev:install": "pnpm --filter {.}... i --no-lockfile && pnpm run dev:build",
58
+ "checkDeps": "npx -yes ../ds-codemods check-missing-packages --projectFolderPath=\"./\" --ignorePackagesGlobPattern=\"\" --ignoreFilesGlobPattern=\"**/test-ables/*,**/tests/*\"",
59
+ "dev": "cross-env NODE_ENV=development node ../../scripts/build/build.mjs --watch"
60
+ }
61
+ }