Files
Superalgos/Utilities/HiriarchyMap.js
T

48 lines
1.8 KiB
JavaScript

/*
This function scans a node hiriatchy recursively and creates a Map by Id of all the nodes of it.
*/
function getHiriarchyMap(rootNode) {
let hiriatchyMap = new Map()
evalNode(rootNode)
return hiriatchyMap
function evalNode(node) {
if (node === undefined) { return }
hiriatchyMap.set(node.id, node)
/* Now we go down through all this node children */
let nodeDefinition = APP_SCHEMA_MAP.get(node.type)
if (nodeDefinition === undefined) { return }
if (nodeDefinition.properties !== undefined) {
let previousPropertyName // Since there are cases where there are many properties with the same name,because they can hold nodes of different types but only one at the time, we have to avoind counting each property of those as individual children.
for (let i = 0; i < nodeDefinition.properties.length; i++) {
let property = nodeDefinition.properties[i]
switch (property.type) {
case 'node': {
if (property.name !== previousPropertyName) {
if (node[property.name] !== undefined) {
evalNode(node[property.name])
}
previousPropertyName = property.name
}
break
}
case 'array': {
if (node[property.name] !== undefined) {
let nodePropertyArray = node[property.name]
for (let m = 0; m < nodePropertyArray.length; m++) {
evalNode(nodePropertyArray[m])
}
}
break
}
}
}
}
}
}