pivot-editor/index.html
2021-05-08 07:03:41 -04:00

103 lines
2.6 KiB
HTML

<script>
var N = {
Create:(inMeta, ...inChildren) =>
{
var output = {
Meta:inMeta,
Children:[],
Parents:[],
WalkID:0,
RecieveAddition:[],
RecieveMultiplication:[],
GiveAddition:[],
GiveMultiplication:[]
};
inChildren.forEach( inChild => N.Connect(output, "Children", inChild, "Parents") );
return output;
},
Connect:(inParent, inParentRefs, inChild, inChildRefs) =>
{
inParent[inParentRefs].push(inChild);
inChild[inChildRefs].push(inParent);
},
Disconnect:(inParent, inParentRefs, inChild, inChildRefs) =>
{
//inParent[inParentRefs]
},
Walk:(inNode, inKey, inIterator, inWalkID) =>
{
let array = inNode[inKey];
for(let i=0; i<array.length; i++)
{
let next = array[i];
if(next.WalkID !== inWalkID)
{
next.WalkID = inWalkID;
let results = inIterator(next);
if(results !== false)
{
N.Walk(next, inKey, inIterator, inWalkID);
}
}
}
},
Modify(inNode, inAmount)
{
inNode.WalkID = "tweak-"+Math.random();
let leaves = [];
let gatherAddition = n => N.Connect(inNode, "GiveAddition", n, "RecieveAddition");
let gatherMultiply = n =>
{
N.Connect(inNode, "GiveMultiplication", n, "RecieveMultiplication");
n.Children.length == 0 ? leaves.push(n) : null;
};
N.Walk(inNode, "Parents", gatherAddition, inNode.WalkID);
N.Walk(inNode, "Children", gatherMultiply, inNode.WalkID);
leaves.forEach(leaf=>N.Walk(leaf, "Parents", gatherAddition, inNode.WalkID));
}
};
</script>
<script>
let tree1 = N.Create("root1",
N.Create("branch1",
N.Create("leaf1"),
N.Create("leaf2"),
N.Create("leaf3"),
),
N.Create("branch2",
N.Create("leaft3"),
N.Create("leaft4")
)
);
let leaves = [];
let leavesCollect = n =>
{
if(n.Children.length == 0)
{
leaves.push(n);
}
};
N.Walk(tree1, "Children", leavesCollect, 1);
let tree2 = N.Create("root2",
N.Create("branch3",
N.Create("leaf5"),
N.Create("leaf6")
),
N.Create("branch4", ...leaves)
);
let orchard = N.Create("orchard", tree1, tree2);
/*************************************************************/
N.Modify(tree1);
console.log(tree1);
</script>