48 lines
909 B
JavaScript
48 lines
909 B
JavaScript
/**
|
|
* @module
|
|
*/
|
|
|
|
/**
|
|
*
|
|
* @param {Object} node
|
|
* @param {Object[]} flatList
|
|
* @param {Number} depth
|
|
*/
|
|
function traverse(node, flatList, depth = 1) {
|
|
const normNode = {
|
|
id: node.label,
|
|
label: node.label,
|
|
opacity: node.opacity ?? null,
|
|
isMain: node.isMain ?? false,
|
|
active: true,
|
|
};
|
|
if (node.model) {
|
|
normNode.model = node.model;
|
|
}
|
|
flatList.push({
|
|
...normNode,
|
|
depth
|
|
});
|
|
if (node.children) {
|
|
for(let child of node.children) {
|
|
traverse(child, flatList, depth + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a flat list of nodes from
|
|
* the nested structure in config
|
|
* @param {Array} nodes
|
|
* @returns {Object[]} A flat list of nodes
|
|
**/
|
|
export function normalizeNodes (nodes) {
|
|
let flatList = [];
|
|
|
|
for (let node of nodes) {
|
|
traverse(node, flatList);
|
|
}
|
|
|
|
return flatList;
|
|
}
|