2021-05-08 13:51:19 -04:00
|
|
|
<div id="app"></div>
|
2021-05-11 06:26:18 -04:00
|
|
|
|
2021-05-09 20:26:40 -04:00
|
|
|
<script>
|
2021-05-08 13:51:19 -04:00
|
|
|
|
2021-05-09 20:26:40 -04:00
|
|
|
var N =
|
|
|
|
{
|
2021-05-09 14:19:13 -04:00
|
|
|
ID:{
|
|
|
|
Walk:0,
|
|
|
|
Instance:0
|
|
|
|
},
|
2021-05-11 23:18:08 -04:00
|
|
|
|
2021-05-11 06:26:18 -04:00
|
|
|
Create(inMeta)
|
2021-05-05 21:53:04 -04:00
|
|
|
{
|
2021-05-11 06:26:18 -04:00
|
|
|
return {
|
2021-05-09 14:19:13 -04:00
|
|
|
ID:{
|
|
|
|
Walk:0,
|
|
|
|
Instance:N.ID.Instance++
|
2021-05-11 06:26:18 -04:00
|
|
|
},
|
|
|
|
Meta:inMeta||{},
|
|
|
|
Link:{}
|
2021-05-05 21:53:04 -04:00
|
|
|
};
|
|
|
|
},
|
2021-05-09 20:26:40 -04:00
|
|
|
Connect(inNodeMajor, inNodeMinor, inKey)
|
2021-05-05 21:53:04 -04:00
|
|
|
{
|
2021-05-11 23:18:08 -04:00
|
|
|
N.Step(inNodeMajor, inKey, true, true).push(inNodeMinor);
|
|
|
|
N.Step(inNodeMinor, inKey, false, true).push(inNodeMajor);
|
2021-05-08 07:03:41 -04:00
|
|
|
},
|
2021-05-09 20:26:40 -04:00
|
|
|
Disconnect(inNodeMajor, inNodeMinor, inKey)
|
2021-05-08 07:03:41 -04:00
|
|
|
{
|
2021-05-09 20:26:40 -04:00
|
|
|
let remove = (inArray, inMatch) => inArray.findIndex( (inMember, inIndex, inArray) => (inMember === inMatch) ? inArray.splice(inIndex, 1) : false );
|
|
|
|
|
|
|
|
if(inNodeMinor === null)
|
|
|
|
{
|
|
|
|
inNodeMajor.Link[inKey].Set.forEach( inLoopNode =>
|
2021-05-09 14:19:13 -04:00
|
|
|
{
|
2021-05-09 20:26:40 -04:00
|
|
|
if(inLoopNode.Link[inKey].Get.length == 1)
|
|
|
|
{
|
|
|
|
// we're about to delete the last get reference on a minor node, just purge the key entirely
|
|
|
|
delete inLoopNode.Link[inKey];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remove( inLoopNode.Link[inKey].Get, inNodeMajor);
|
|
|
|
}
|
|
|
|
|
2021-05-09 14:19:13 -04:00
|
|
|
});
|
2021-05-09 20:26:40 -04:00
|
|
|
// we just wiped out all set, if get is empty we can purge the key from major
|
|
|
|
if(inNodeMajor.Link[inKey].Get.length == 0)
|
|
|
|
{
|
|
|
|
delete inNodeMajor.Link[inKey];
|
|
|
|
}
|
2021-05-09 14:19:13 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:26:40 -04:00
|
|
|
if(inNodeMajor === null)
|
|
|
|
{
|
|
|
|
inNodeMinor.Link[inKey].Get.forEach( inLoopNode =>
|
2021-05-09 14:19:13 -04:00
|
|
|
{
|
2021-05-09 20:26:40 -04:00
|
|
|
if(inLoopNode.Link[inKey].Set.length == 1)
|
|
|
|
{
|
|
|
|
delete inLoopNode.Link[inKey];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remove( inLoopNode.Link[inKey].Set, inNodeMinor);
|
|
|
|
}
|
2021-05-09 14:19:13 -04:00
|
|
|
});
|
2021-05-09 20:26:40 -04:00
|
|
|
// we just wiped out all get, if set is empty we can purge the key from minor
|
|
|
|
if(inNodeMinor.Link[inKey].Set.length == 0)
|
|
|
|
{
|
|
|
|
delete inNodeMinor.Link[inKey];
|
|
|
|
}
|
2021-05-09 14:19:13 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:26:40 -04:00
|
|
|
|
|
|
|
if(inNodeMajor.Link[inKey].Set.length == 1)
|
|
|
|
{
|
|
|
|
delete inNodeMajor.Link[inKey];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remove(inNodeMajor.Link[inKey].Set, inNodeMinor);
|
|
|
|
}
|
|
|
|
if(inNodeMinor.Link[inKey].Get.length == 1)
|
|
|
|
{
|
|
|
|
delete inNodeMinor.Link[inKey];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remove(inNodeMinor.Link[inKey].Get, inNodeMajor);
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:18:08 -04:00
|
|
|
},
|
|
|
|
Step(inNode, inKey, inForward, inForceCreate)
|
|
|
|
{
|
|
|
|
let connectionGroup = inNode.Link[inKey];
|
|
|
|
if(!connectionGroup)
|
|
|
|
{
|
|
|
|
if(inForceCreate)
|
|
|
|
{
|
|
|
|
inNode.Link[inKey] = connectionGroup = {Get:[], Set:[]};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (inForward === undefined || inForward === true) ? connectionGroup.Set : connectionGroup.Get;
|
|
|
|
|
2021-05-05 21:53:04 -04:00
|
|
|
},
|
2021-05-09 20:26:40 -04:00
|
|
|
Walk(inIterator, inNode, inKey, inForward)
|
2021-05-05 21:53:04 -04:00
|
|
|
{
|
2021-05-11 06:26:18 -04:00
|
|
|
let array = N.Step(inNode, inKey, inForward);
|
2021-05-05 21:53:04 -04:00
|
|
|
for(let i=0; i<array.length; i++)
|
|
|
|
{
|
|
|
|
let next = array[i];
|
2021-05-09 14:19:13 -04:00
|
|
|
if(next.ID.Walk !== N.ID.Walk)
|
2021-05-05 21:53:04 -04:00
|
|
|
{
|
2021-05-09 14:19:13 -04:00
|
|
|
next.ID.Walk = N.ID.Walk;
|
2021-05-05 21:53:04 -04:00
|
|
|
let results = inIterator(next);
|
|
|
|
if(results !== false)
|
|
|
|
{
|
2021-05-09 20:26:40 -04:00
|
|
|
N.Walk(inIterator, next, inKey, inForward);
|
2021-05-05 21:53:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 23:18:08 -04:00
|
|
|
},
|
|
|
|
Path(inArray, inNode, inKey, inForward)
|
|
|
|
{
|
|
|
|
var current = inNode;
|
|
|
|
var direction = inForward||true;
|
|
|
|
for(let i=0; i<inArray.length; i++)
|
|
|
|
{
|
|
|
|
current = N.Step(current, inKey, direction)[inArray[i]];
|
|
|
|
}
|
|
|
|
return current;
|
2021-05-09 20:26:40 -04:00
|
|
|
}
|
|
|
|
};
|
2021-05-11 23:18:08 -04:00
|
|
|
</script>
|
2021-05-09 20:26:40 -04:00
|
|
|
|
2021-05-11 23:18:08 -04:00
|
|
|
<script>
|
2021-05-11 06:26:18 -04:00
|
|
|
`
|
|
|
|
A C
|
|
|
|
\ / \
|
|
|
|
\ / \
|
|
|
|
B D
|
|
|
|
`
|
2021-05-09 20:26:40 -04:00
|
|
|
let a = N.Create({name:"A"});
|
|
|
|
let b = N.Create({name:"B"});
|
|
|
|
let c = N.Create({name:"C"});
|
2021-05-11 06:26:18 -04:00
|
|
|
let d = N.Create({name:"D"});
|
2021-05-09 20:26:40 -04:00
|
|
|
N.Connect(a, b, "parent");
|
2021-05-11 06:26:18 -04:00
|
|
|
N.Connect(c, b, "parent");
|
|
|
|
N.Connect(c, d, "parent");
|
|
|
|
N.Disconnect(a, null, "parent");
|
2021-05-11 23:18:08 -04:00
|
|
|
/*
|
2021-05-11 06:26:18 -04:00
|
|
|
console.log(a.Link);
|
|
|
|
console.log(b.Link);
|
|
|
|
console.log(c.Link);
|
|
|
|
console.log(d.Link);
|
2021-05-11 23:18:08 -04:00
|
|
|
*/
|
2021-05-09 20:26:40 -04:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
var Pivot =
|
|
|
|
{
|
|
|
|
Leaves:{},
|
2021-05-11 06:26:18 -04:00
|
|
|
|
2021-05-09 20:26:40 -04:00
|
|
|
Init(inRows)
|
|
|
|
{
|
|
|
|
Pivot.Leaves = inRows.map(r => N.Create({Row:r}));
|
|
|
|
Pivot.Init = ()=>{};
|
|
|
|
},
|
2021-05-12 19:08:52 -04:00
|
|
|
Pivot(inParent, inPivotIndicies, inSumIndicies, inDepth)
|
2021-05-09 20:26: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
|
|
|
|
let depth = inDepth||0;
|
|
|
|
let uniques = {};
|
|
|
|
inParent.Meta.Leaves.forEach((inLeaf)=>
|
|
|
|
{
|
|
|
|
let row = inLeaf.Meta.Row; // shorthand for the raw "CSV" row in the leaf Node's Meta
|
2021-05-12 19:08:52 -04:00
|
|
|
let value = row[inPivotIndicies[depth]]; // get the pivot column
|
2021-05-09 20:26:40 -04:00
|
|
|
let match = uniques[value]; // check in the uniques list if this pivot column exists
|
|
|
|
if(!match)
|
|
|
|
{
|
|
|
|
// if not, store a value under that key that will be the meta object for a new child
|
2021-05-12 19:08:52 -04:00
|
|
|
|
|
|
|
let clone = row.map(r=>null);
|
|
|
|
inSumIndicies.forEach((inSumIndex, inIndex, inArray)=>
|
|
|
|
{
|
|
|
|
clone[inSumIndex] = row[inSumIndex]
|
|
|
|
});
|
|
|
|
|
2021-05-09 20:26:40 -04:00
|
|
|
match = uniques[value] = {
|
|
|
|
Label:value,
|
2021-05-12 19:08:52 -04:00
|
|
|
Row:clone,
|
2021-05-09 20:26:40 -04:00
|
|
|
Leaves:[]
|
|
|
|
};
|
|
|
|
// grow a child off of the parent using the meta object
|
|
|
|
N.Connect(inParent, N.Create(match), "Hierarchy");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if a match does exist, sum the appropriate columns
|
2021-05-12 19:08:52 -04:00
|
|
|
inSumIndicies.forEach((inSumIndex) => match.Row[inSumIndex] += row[inSumIndex]);
|
2021-05-09 20:26:40 -04:00
|
|
|
}
|
|
|
|
// move the leaves into the child
|
|
|
|
match.Leaves.push(inLeaf);
|
|
|
|
});
|
|
|
|
|
|
|
|
delete inParent.Meta.Leaves;
|
|
|
|
var iterator;
|
2021-05-12 19:08:52 -04:00
|
|
|
if(depth >= inPivotIndicies.length-1)
|
2021-05-09 20:26:40 -04:00
|
|
|
{
|
|
|
|
iterator = inChild =>
|
|
|
|
{
|
|
|
|
inChild.Meta.Leaves.forEach( inLeaf => N.Connect(inChild, inLeaf, "Hierarchy") );
|
|
|
|
delete inChild.Meta.Leaves;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-12 19:08:52 -04:00
|
|
|
iterator = child => Pivot.Pivot(child, inPivotIndicies, inSumIndicies, depth+1);
|
2021-05-09 20:26:40 -04:00
|
|
|
}
|
2021-05-11 06:26:18 -04:00
|
|
|
N.Step(inParent, "Hierarchy").forEach(iterator);
|
2021-05-09 20:26:40 -04:00
|
|
|
return inParent;
|
|
|
|
},
|
2021-05-12 19:08:52 -04:00
|
|
|
Create(inPivotIndicies, inSumIndicies)
|
2021-05-09 20:26:40 -04:00
|
|
|
{
|
2021-05-12 19:08:52 -04:00
|
|
|
return Pivot.Pivot(N.Create({Leaves:Pivot.Leaves}), inPivotIndicies, inSumIndicies);
|
2021-05-05 21:53:04 -04:00
|
|
|
},
|
2021-05-08 08:46:37 -04:00
|
|
|
Modify(inNode)
|
2021-05-08 07:03:41 -04:00
|
|
|
{
|
2021-05-09 14:19:13 -04:00
|
|
|
N.ID.Walk++;
|
2021-05-08 07:03:41 -04:00
|
|
|
|
2021-05-11 23:18:08 -04:00
|
|
|
let modifier = N.Create({});
|
|
|
|
|
2021-05-08 07:03:41 -04:00
|
|
|
let leaves = [];
|
2021-05-11 23:18:08 -04:00
|
|
|
let gatherUp = n => N.Connect(modifier, n, "ModifyUp");
|
2021-05-08 08:46:37 -04:00
|
|
|
let gatherDown = n =>
|
2021-05-08 07:03:41 -04:00
|
|
|
{
|
2021-05-11 23:18:08 -04:00
|
|
|
N.Connect(modifier, n, "ModifyDown");
|
2021-05-11 06:26:18 -04:00
|
|
|
N.Step(n, "Hierarchy").length == 0 ? leaves.push(n) : null;
|
2021-05-08 07:03:41 -04:00
|
|
|
};
|
2021-05-11 23:18:08 -04:00
|
|
|
let gatherOut = n => N.Connect(modifier, n, "ModifyOut");
|
2021-05-08 07:03:41 -04:00
|
|
|
|
2021-05-09 14:19:13 -04:00
|
|
|
N.Walk(gatherUp, inNode, "Hierarchy", false);
|
2021-05-12 07:17:08 -04:00
|
|
|
N.Connect(modifier, inNode, "ModifyAt");
|
2021-05-09 14:19:13 -04:00
|
|
|
N.Walk(gatherDown, inNode, "Hierarchy");
|
|
|
|
leaves.forEach(leaf=>N.Walk(gatherOut, leaf, "Hierarchy", false));
|
2021-05-11 23:18:08 -04:00
|
|
|
|
|
|
|
return modifier;
|
2021-05-09 20:26:40 -04:00
|
|
|
},
|
|
|
|
Unmodify(inNode)
|
2021-05-09 14:19:13 -04:00
|
|
|
{
|
2021-05-09 20:26:40 -04:00
|
|
|
N.Disconnect(inNode, null, "ModifyUp");
|
|
|
|
N.Disconnect(inNode, null, "ModifyDown");
|
|
|
|
N.Disconnect(inNode, null, "ModifyOut");
|
2021-05-09 14:19:13 -04:00
|
|
|
}
|
|
|
|
};
|
2021-05-09 20:26:40 -04:00
|
|
|
</script>
|
2021-05-09 14:19:13 -04:00
|
|
|
|
2021-05-09 20:26:40 -04:00
|
|
|
<script type="module">
|
2021-05-09 14:19:13 -04:00
|
|
|
|
2021-05-11 06:26:18 -04:00
|
|
|
import {h, render, createContext} from 'https://cdn.skypack.dev/preact';
|
|
|
|
import {useReducer} from 'https://cdn.skypack.dev/preact/hooks';
|
|
|
|
import { css, cx } from 'https://cdn.skypack.dev/@emotion/css';
|
2021-05-09 14:19:13 -04:00
|
|
|
|
2021-05-11 06:26:18 -04:00
|
|
|
Pivot.Init([
|
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-09 20:26:40 -04:00
|
|
|
let pivotRoot1 = Pivot.Create([1, 2], [3]);
|
|
|
|
let pivotRoot2 = Pivot.Create([2, 1], [3]);
|
2021-05-08 15:32:40 -04:00
|
|
|
let pivots = [pivotRoot1, pivotRoot2];
|
2021-05-08 13:21:43 -04:00
|
|
|
|
2021-05-11 23:18:08 -04:00
|
|
|
let select = N.Path([0, 1], pivotRoot1, "Hierarchy");
|
|
|
|
let mod = Pivot.Modify(select);
|
|
|
|
|
|
|
|
|
2021-05-11 06:26:18 -04:00
|
|
|
|
|
|
|
let Store = createContext(null);
|
|
|
|
let StoreState =
|
|
|
|
{
|
|
|
|
count:7
|
|
|
|
};
|
|
|
|
let StoreReducer = (inState, inAction) =>
|
|
|
|
{
|
|
|
|
|
2021-05-11 23:18:08 -04:00
|
|
|
};
|
2021-05-11 06:26:18 -04:00
|
|
|
|
2021-05-12 07:17:08 -04:00
|
|
|
let ElNode = ({node, depth}) =>
|
2021-05-08 13:51:19 -04:00
|
|
|
{
|
2021-05-12 07:17:08 -04:00
|
|
|
var color;
|
2021-05-09 14:19:13 -04:00
|
|
|
|
2021-05-12 07:17:08 -04:00
|
|
|
if(N.Step(node, "ModifyDown"))
|
2021-05-11 23:18:08 -04:00
|
|
|
{
|
2021-05-12 07:17:08 -04:00
|
|
|
color = `yellow`;
|
2021-05-11 23:18:08 -04:00
|
|
|
}
|
2021-05-12 07:17:08 -04:00
|
|
|
if(N.Step(node, "ModifyUp"))
|
2021-05-11 23:18:08 -04:00
|
|
|
{
|
2021-05-12 07:17:08 -04:00
|
|
|
color = `skyblue`;
|
2021-05-11 23:18:08 -04:00
|
|
|
}
|
2021-05-12 07:17:08 -04:00
|
|
|
if(N.Step(node, "ModifyOut"))
|
2021-05-11 23:18:08 -04:00
|
|
|
{
|
2021-05-12 07:17:08 -04:00
|
|
|
color = `lightgrey`;
|
2021-05-11 23:18:08 -04:00
|
|
|
}
|
2021-05-12 07:17:08 -04:00
|
|
|
if(N.Step(node, "ModifyAt"))
|
2021-05-08 13:51:19 -04:00
|
|
|
{
|
2021-05-12 07:17:08 -04:00
|
|
|
color = `red`;
|
2021-05-08 13:51:19 -04:00
|
|
|
}
|
2021-05-12 07:17:08 -04:00
|
|
|
|
|
|
|
let nodeBase = css`
|
|
|
|
position:relative;
|
|
|
|
padding:0;
|
|
|
|
margin:0;
|
|
|
|
border-top:1px solid lightgrey;
|
|
|
|
|
2021-05-12 19:08:52 -04:00
|
|
|
& > .Label
|
2021-05-12 07:17:08 -04:00
|
|
|
{
|
|
|
|
display:inline-block;
|
|
|
|
width:200px;
|
|
|
|
background:${color};
|
|
|
|
}
|
2021-05-12 19:08:52 -04:00
|
|
|
& > .Label::before
|
2021-05-12 07:17:08 -04:00
|
|
|
{
|
|
|
|
content:" ";
|
|
|
|
display:inline-block;
|
|
|
|
width:${depth*15}px;
|
|
|
|
}
|
|
|
|
.Table
|
2021-05-11 06:26:18 -04:00
|
|
|
{
|
2021-05-12 07:17:08 -04:00
|
|
|
display:inline-block;
|
|
|
|
text-align:right;
|
|
|
|
}
|
|
|
|
.Cell
|
|
|
|
{
|
|
|
|
width:50px;
|
|
|
|
display:inline-block;
|
|
|
|
padding:10px;
|
|
|
|
}
|
|
|
|
`;
|
2021-05-09 14:19:13 -04:00
|
|
|
|
2021-05-12 19:08:52 -04:00
|
|
|
return h("div", {className:"Node"}, [
|
|
|
|
h("div", {className:cx(css(nodeBase), "Upper")}, [
|
2021-05-12 07:17:08 -04:00
|
|
|
h("div", {className:"Label"}, (node.Meta.Label || "a node") ),
|
|
|
|
h("div", {className:"Table"}, (node.Meta.Row || []).map( cell => h("div", {className:"Cell"}, cell)) )
|
|
|
|
]),
|
|
|
|
h("div", {className:"Nodes"}, N.Step(node, "Hierarchy").map(child=>h(ElNode, {node:child, depth:depth+1})) )
|
|
|
|
]);
|
2021-05-08 13:51:19 -04:00
|
|
|
}
|
2021-05-08 15:32:40 -04:00
|
|
|
let ElPivot = ({pivot}) =>
|
|
|
|
{
|
2021-05-12 07:17:08 -04:00
|
|
|
return h("div", {style:{display:"inline-block", width:"500px"}}, [
|
|
|
|
h(ElNode, {node:pivot, depth:0})
|
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
|
|
|
{
|
2021-05-11 06:26:18 -04:00
|
|
|
let [get, set] = useReducer(StoreReducer, StoreState);
|
|
|
|
|
|
|
|
return h(Store.Provider, {value:{get, set}}, [
|
|
|
|
h("div", null, [
|
|
|
|
h("h3", null, "tree view"),
|
|
|
|
...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>
|