pivot-editor/index.html

235 lines
6.8 KiB
HTML
Raw Normal View History

2021-05-08 13:51:19 -04:00
<div id="app"></div>
<script type="module">
import { h, Component, render } from 'https://unpkg.com/preact?module';
2021-05-05 21:53:04 -04:00
var N = {
2021-05-08 07:03:41 -04:00
Create:(inMeta, ...inChildren) =>
2021-05-05 21:53:04 -04:00
{
var output = {
Meta:inMeta,
Children:[],
Parents:[],
2021-05-08 13:21:43 -04:00
/*
2021-05-05 21:53:04 -04:00
WalkID:0,
2021-05-08 08:46:37 -04:00
GetUpward:[],
GetDownward:[],
GetOutside:[],
SetUpward:[],
SetDownward:[],
SetOutside:[]
2021-05-08 13:21:43 -04:00
*/
2021-05-05 21:53:04 -04:00
};
2021-05-08 07:03:41 -04:00
inChildren.forEach( inChild => N.Connect(output, "Children", inChild, "Parents") );
2021-05-05 21:53:04 -04:00
return output;
},
2021-05-08 07:03:41 -04:00
Connect:(inParent, inParentRefs, inChild, inChildRefs) =>
2021-05-05 21:53:04 -04:00
{
2021-05-08 07:03:41 -04:00
inParent[inParentRefs].push(inChild);
inChild[inChildRefs].push(inParent);
},
Disconnect:(inParent, inParentRefs, inChild, inChildRefs) =>
{
2021-05-08 08:46:37 -04:00
let checkRemove = (inArray, inMember) =>
{
inArray.findIndex( (inMember, inIndex, inArray) => (inMember === match) ? inArray.splice(inIndex, 1) : false );
};
checkRemove(inParent[inParentRefs], inChild);
checkRemove(inChild[inChildRefs], inParent);
2021-05-05 21:53:04 -04:00
},
Walk:(inNode, inKey, inIterator, inWalkID) =>
{
let array = inNode[inKey];
for(let i=0; i<array.length; i++)
{
let next = array[i];
2021-05-05 22:50:54 -04:00
if(next.WalkID !== inWalkID)
2021-05-05 21:53:04 -04:00
{
next.WalkID = inWalkID;
let results = inIterator(next);
if(results !== false)
{
N.Walk(next, inKey, inIterator, inWalkID);
}
}
}
},
2021-05-08 08:46:37 -04:00
Modify(inNode)
2021-05-08 07:03:41 -04:00
{
inNode.WalkID = "tweak-"+Math.random();
let leaves = [];
2021-05-08 08:46:37 -04:00
let gatherUp = n => N.Connect(inNode, "SetUpward", n, "GetUpward");
let gatherDown = n =>
2021-05-08 07:03:41 -04:00
{
2021-05-08 08:46:37 -04:00
N.Connect(inNode, "SetDownward", n, "GetDownward");
2021-05-08 07:03:41 -04:00
n.Children.length == 0 ? leaves.push(n) : null;
};
2021-05-08 08:46:37 -04:00
let gatherOut = n => N.Connect(inNode, "SetOutside", n, "GetOutside");
2021-05-08 07:03:41 -04:00
2021-05-08 08:46:37 -04:00
N.Walk(inNode, "Parents", gatherUp, inNode.WalkID);
N.Connect(inNode, "SetDownward", inNode, "GetDownward");
N.Walk(inNode, "Children", gatherDown, inNode.WalkID);
leaves.forEach(leaf=>N.Walk(leaf, "Parents", gatherOut, inNode.WalkID));
2021-05-08 07:03:41 -04:00
}
2021-05-08 13:21:43 -04:00
// delete modify
// create pivot
// delete pivot
2021-05-05 21:53:04 -04:00
};
2021-05-08 09:56:44 -04:00
2021-05-08 13:21:43 -04:00
let Leafify = inRows => inRows.map(r => N.Create({Row:r}));
let Pivot = (inParent, inColumnIndicies, inSumIndicies, inDepth) =>
{
2021-05-08 15:32:40 -04:00
/*
arguments:
- a Node with leaf Nodes temporarily stored in its Meta.Leaves
- where each leaf Node has a row of table data in it's Meta.Row
- a list of columns to pivot on
- a list of columns to sum
- optional traversal depth, defaults to 0
*/
2021-05-08 13:21:43 -04:00
let depth = inDepth||0;
let uniques = {};
inParent.Meta.Leaves.forEach((inLeaf)=>
{
2021-05-08 15:32:40 -04:00
let row = inLeaf.Meta.Row; // get the raw "CSV" row out of the leaf Node's Meta
let value = row[inColumnIndicies[depth]]; // get the pivot column
let match = uniques[value]; // check in the uniques list if this pivot column exists
2021-05-08 13:51:19 -04:00
if(!match)
{
2021-05-08 15:32:40 -04:00
// if not, store a value under that key that will be the meta object for a new child
match = uniques[value] = {
Label:value,
Row:inSumIndicies.map((inColumnIndex, inIndex, inArray) => row[inColumnIndex]), // create a Meta.Row also on the new child nodes to store summed totals
Leaves:[]
};
// grow a child off of the parent using the meta object
2021-05-08 13:51:19 -04:00
N.Connect(inParent, "Children", N.Create(match), "Parents");
2021-05-08 13:21:43 -04:00
}
2021-05-08 13:51:19 -04:00
else
2021-05-08 13:21:43 -04:00
{
2021-05-08 15:32:40 -04:00
// if a match does exist, sum the appropriate columns
inSumIndicies.forEach((inColumnIndex, inIndex, inArray) => match.Row[inIndex] += row[inColumnIndex]);
2021-05-08 13:21:43 -04:00
}
2021-05-08 15:32:40 -04:00
// move the leaves into the child
2021-05-08 13:51:19 -04:00
match.Leaves.push(inLeaf);
2021-05-08 13:21:43 -04:00
});
2021-05-08 15:32:40 -04:00
2021-05-08 13:51:19 -04:00
delete inParent.Meta.Leaves;
2021-05-08 13:21:43 -04:00
if(depth == inColumnIndicies.length-1)
{
// cant go any deeper
inParent.Children.forEach( inChild =>
{
2021-05-08 13:51:19 -04:00
inChild.Meta.Leaves.forEach( inLeaf => N.Connect(inChild, "Children", inLeaf, "Parents") );
delete inChild.Meta.Leaves;
2021-05-08 13:21:43 -04:00
});
}
else
{
2021-05-08 13:51:19 -04:00
inParent.Children.forEach( child => Pivot(child, inColumnIndicies, inSumIndicies, depth+1) );
2021-05-08 13:21:43 -04:00
}
};
let csv = Leafify([
2021-05-08 13:51:19 -04:00
["#1", "a", "long", 1],
["#2", "b", "long", 2],
["#3", "b", "short", 2],
["#4", "a", "long", 3],
["#5", "b", "short", 1],
["#6", "a", "short", 0],
["#7", "b", "short", 7],
2021-05-08 13:21:43 -04:00
]);
2021-05-08 15:32:40 -04:00
let pivotRoot1 = N.Create({Leaves:csv});
Pivot(pivotRoot1, [1, 2], [3]);
let pivotRoot2 = N.Create({Leaves:csv});
Pivot(pivotRoot2, [2, 1], [3]);
let pivots = [pivotRoot1, pivotRoot2];
2021-05-08 13:21:43 -04:00
2021-05-08 13:51:19 -04:00
let ElNode = ({node}) =>
{
var children = [];
var table = [];
if(node.Children.length)
{
if(node.Meta.Row)
{
table = node.Meta.Row.map( cell => h("span", {style:{padding:"10px"}}, cell));
}
children = [
h("strong", null, node.Meta.Label||"a node"),
...table,
...node.Children.map( inChild => h(ElNode, {node:inChild}))
];
}
else
{
children = [
h("strong", null, node.Meta.Label||"a node"),
...node.Meta.Row.map( cell => h("span", {style:{padding:"10px"}}, cell))
];
}
return h("div", {style:{padding:"10px"}}, children);
}
2021-05-08 15:32:40 -04:00
let ElPivot = ({pivot}) =>
{
2021-05-08 23:34:54 -04:00
return h("div", {style:{display:"inline-block"}}, [
h(ElNode, {node:pivot})
2021-05-08 15:32:40 -04:00
]);
}
2021-05-08 23:34:54 -04:00
let ElRoot = ({pivots}) =>
2021-05-08 13:51:19 -04:00
{
return h("div", null, [
h("h3", null, "tree view"),
2021-05-08 23:34:54 -04:00
...pivots.map(pivot=>h(ElPivot, {pivot}))
2021-05-08 13:51:19 -04:00
])
};
2021-05-08 23:34:54 -04:00
render(h(ElRoot, {pivots}, null), document.querySelector("#app"));
2021-05-08 13:51:19 -04:00
2021-05-08 13:21:43 -04:00
</script>
<!--=
2021-05-05 21:53:04 -04:00
<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 = [];
2021-05-08 07:03:41 -04:00
let leavesCollect = n =>
2021-05-05 21:53:04 -04:00
{
if(n.Children.length == 0)
{
leaves.push(n);
}
};
2021-05-08 07:03:41 -04:00
N.Walk(tree1, "Children", leavesCollect, 1);
2021-05-05 21:53:04 -04:00
let tree2 = N.Create("root2",
N.Create("branch3",
N.Create("leaf5"),
N.Create("leaf6")
),
N.Create("branch4", ...leaves)
);
2021-05-05 22:50:54 -04:00
let orchard = N.Create("orchard", tree1, tree2);
2021-05-05 21:53:04 -04:00
2021-05-05 22:50:54 -04:00
/*************************************************************/
2021-05-05 21:53:04 -04:00
2021-05-08 07:03:41 -04:00
N.Modify(tree1);
console.log(tree1);
2021-05-05 21:53:04 -04:00
2021-05-08 13:21:43 -04:00
</script>
-->