Skip to content

Commit f6cfcbb

Browse files
committed
refactored organizeAxTree function to not use the built in includes method
1 parent 3dceab2 commit f6cfcbb

File tree

1 file changed

+65
-12
lines changed
  • src/app/components/StateRoute/AxMap

1 file changed

+65
-12
lines changed

src/app/components/StateRoute/AxMap/Ax.tsx

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,45 +195,99 @@ export default function AxTree(props) {
195195
const LinkComponent = getLinkComponent({ layout, linkType, orientation });
196196

197197
const currAxSnapshot = JSON.parse(JSON.stringify(axSnapshots[currLocation.index]));
198+
console.log('currAxSnapshot: ', currAxSnapshot);
198199

199200
// root node of currAxSnapshot
200201
const rootAxNode = JSON.parse(JSON.stringify(currAxSnapshot[0]));
201202

202203
// // array that holds the ax tree as a nested object and the root node initially
203204
const nodeAxArr = [];
204205

206+
// code handling the initial root case / if there is only 1 child
207+
// condition checking if the last node added has childIds length > 0
208+
209+
// function to handle nesting past the root node
205210
// const organizeAxTree = (currNode, currAxSnapshot) => {
206211
// for (let i = 0; i < currNode.length; i++) {
207-
// if (currNode.childIds.length > 0) {
208-
// currNode.children = [];
212+
// if (currNode[i].childIds.length > 0) {
213+
// currNode[i].children = [];
209214

210215
// for (let j = 0; j < currAxSnapshot.length; j++) {
211216

212-
// if (currNode.childIds.includes(currAxSnapshot[j].nodeId)) {
213-
// currNode.children.push(currAxSnapshot[j]);
217+
// if (currNode[i].childIds.includes(currAxSnapshot[j].nodeId)) {
218+
// currNode[i].children.push(currAxSnapshot[j]);
214219

215-
// organizeAxTree(currNode.children, currAxSnapshot);
220+
// organizeAxTree(currNode[i].children, currAxSnapshot);
216221
// }
217222
// }
218223
// }
219224
// }
220225
// }
221-
222226
// organizeAxTree(nodeAxArr, currAxSnapshot);
223227

228+
// const organizeAxTree = (currNodeArr, currAxSnapshot, i = 0) => {
229+
// console.log('currNode: ', currNodeArr);
230+
// // handles each child node within the children property
231+
// while (currNodeArr[i]) {
232+
// if (currNodeArr[i].childIds) {
233+
// currNodeArr[i].children = [];
234+
// // iterate through every snapshot in currAxSnapshot
235+
// for (let j = 0; j < currAxSnapshot.length; j++) {
236+
// // includes does not have good time complexity, can convert to nested for or for of loop
237+
// for (const childEle of currNodeArr[i].childIds) {
238+
// if (childEle === currAxSnapshot[j].nodeId) {
239+
// currNodeArr[i].children.push(currAxSnapshot[j]);
240+
// }
241+
// }
242+
// }
243+
// }
244+
// i++;
245+
// }
246+
// i = 0;
247+
// organizeAxTree(currNodeArr[i].children, currAxSnapshot);
248+
// }
249+
250+
// if (currNode.childIds) {
251+
// currNode.children = [];
252+
// // checks if there is more than 1 child
253+
// if (currNode.childIds.length > 1) {
254+
// for (let m = 0; m < currNode.childIds.length; m++) {
255+
// for (let j = 0; j < currAxSnapshot.length; j++) {
256+
// if (currNode.childIds.includes(currAxSnapshot[j].nodeId)) {
257+
// currNode.children.push(currAxSnapshot[j]);
258+
// }
259+
// }
260+
// }
261+
// } else if (currNode.childIds.length === 1) {
262+
// for (let j = 0; j < currAxSnapshot.length; j++) {
263+
// if (currNode.childIds.includes(currAxSnapshot[j].nodeId)) {
264+
// currNode.children.push(currAxSnapshot[j]);
265+
// }
266+
// }
267+
// organizeAxTree(currNode.children[0], currAxSnapshot);
268+
// }
269+
// organizeAxTree(currNode.children, currAxSnapshot);
270+
// }
271+
272+
// organizeAxTree([rootAxNode], currAxSnapshot);
224273
const organizeAxTree = (currNode, currAxSnapshot) => {
225274
if (currNode.childIds && currNode.childIds.length > 0) {
226275
currNode.children = [];
227276
for (let j = 0; j < currAxSnapshot.length; j++) {
228-
if (currNode.childIds.includes(currAxSnapshot[j].nodeId)) {
229-
currNode.children.push(currAxSnapshot[j]);
230-
organizeAxTree(currAxSnapshot[j], currAxSnapshot);
277+
for (const childEle of currNode.childIds) {
278+
if (childEle === currAxSnapshot[j].nodeId) {
279+
currNode.children.push(currAxSnapshot[j]);
280+
organizeAxTree(currAxSnapshot[j], currAxSnapshot);
281+
}
231282
}
283+
232284
}
233285
}
234286
}
235287

236288
organizeAxTree(rootAxNode, currAxSnapshot);
289+
290+
console.log('rootAxNode: ', rootAxNode);
237291

238292
// store each individual node, now with children property in nodeAxArr
239293
// need to consider order, iterate through the children property first?
@@ -255,7 +309,6 @@ export default function AxTree(props) {
255309

256310
populateNodeAxArr(rootAxNode);
257311
console.log('nodeAxArr: ', nodeAxArr);
258-
console.log('rootAxNode: ', rootAxNode);
259312

260313
return totalWidth < 10 ? null : (
261314
<div>
@@ -282,7 +335,7 @@ export default function AxTree(props) {
282335
/>
283336
<Group transform={`scale(${aspect})`} top={margin.top} left={margin.left}>
284337
<Tree
285-
root={hierarchy(nodeAxArr[1], (d) => (d.isExpanded ? null : d.children))}
338+
root={hierarchy(nodeAxArr[0], (d) => (d.isExpanded ? null : d.children))}
286339
size={[sizeWidth / aspect, sizeHeight / aspect]}
287340
separation={(a, b) => (a.parent === b.parent ? 0.5 : 0.5) / a.depth}
288341
>
@@ -306,7 +359,7 @@ export default function AxTree(props) {
306359
//return nodeLength * 7 + 20; //uncomment this line if we want each node to be directly proportional to the name.length (instead of nodes of similar sizes to snap to the same width)
307360
if (nodeLength <= 5) return nodeLength + 50;
308361
if (nodeLength <= 10) return nodeLength + 120;
309-
return nodeLength + 140;
362+
return nodeLength + 180;
310363
};
311364

312365
const width: number = widthFunc(node.data.name.value); // the width is determined by the length of the node.name

0 commit comments

Comments
 (0)