pivot-editor/index.html

490 lines
15 KiB
HTML
Raw Normal View History

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-14 07:11:23 -04:00
Connect(inNodeMajor, inNodeMinor, inKey, inUnique)
2021-05-05 21:53:04 -04:00
{
2021-05-14 07:11:23 -04:00
if(inUnique) // bail if the nodes are already connected
{
let check = N.Step(inNodeMajor, inKey, true);
if(check)
{
if(check.indexOf(inNodeMinor) < 0)
{
return;
}
}
}
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 no specific child was passed
2021-05-09 20:26:40 -04:00
if(inNodeMinor === null)
{
// get all the children
2021-05-14 07:11:23 -04:00
let check = N.Step(inNodeMajor, inKey);
if(!check){ return; }
// go down to each child ...
check.forEach( inNodeMinor =>
2021-05-09 14:19:13 -04:00
{
let connections = inNodeMinor.Link[inKey];
remove( connections.Get, inNodeMajor); // ... and remove any reference to the parent
// if after the remove operation, this child has no connections on inKey, scrub the key
if(!connections.Set.length && !connections.Get.length)
2021-05-09 20:26:40 -04:00
{
delete inNodeMinor.Link[inKey];
2021-05-09 20:26:40 -04:00
}
2021-05-09 14:19:13 -04:00
});
// we just wiped out all outgoing connections to the parent, if incoming connections are empty too we can purge the key there as well
2021-05-09 20:26:40 -04:00
if(inNodeMajor.Link[inKey].Get.length == 0)
{
delete inNodeMajor.Link[inKey];
}
2021-05-09 14:19:13 -04:00
return;
}
// if no specific parent was passed
2021-05-09 20:26:40 -04:00
if(inNodeMajor === null)
{
// get all the parents
let check = N.Step(inNodeMinor, inKey, false);
if(!check){ return; }
// go up to each parent ...
check.forEach( inNodeMajor =>
2021-05-09 14:19:13 -04:00
{
let connections = inNodeMajor.Link[inKey];
remove( connections.Set, inNodeMinor); // ... and remove any reference to the child
// if after the remove operation, this parent has no connections on inKey, scrub the key
if( !connections.Set.length && !connections.Get.length )
2021-05-09 20:26:40 -04:00
{
delete inNodeMajor.Link[inKey];
2021-05-09 20:26:40 -04:00
}
2021-05-09 14:19:13 -04:00
});
// we just wiped out all incoming connections to the child, if outgoing connections are empty too we can purge the key there as well
2021-05-09 20:26:40 -04:00
if(inNodeMinor.Link[inKey].Set.length == 0)
{
delete inNodeMinor.Link[inKey];
}
2021-05-09 14:19:13 -04:00
return;
}
// if a specific parent and child were passed
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 === true)
2021-05-11 23:18:08 -04:00
{
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-15 07:30:52 -04:00
Walk(inIterator, inNode, inKey, inForward, inTerminal)
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-15 07:30:52 -04:00
if(!array.length && inTerminal)
2021-05-05 21:53:04 -04:00
{
2021-05-15 07:30:52 -04:00
return inTerminal(inNode);
}
2021-05-13 19:54:42 -04:00
2021-05-15 07:30:52 -04:00
for(let i=0; i<array.length; i++)
{
2021-05-05 21:53:04 -04:00
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-15 07:30:52 -04:00
N.Walk(inIterator, next, inKey, inForward, inTerminal);
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
<script>
var Pivot =
{
Leaves:{},
2021-05-15 12:52:13 -04:00
Root:N.Create({Label:"All Pivots"}),
2021-05-15 07:30:52 -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-15 12:52:13 -04:00
Pivot(inRoot, 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
{
2021-05-14 07:11:23 -04:00
iterator = inLastBranch =>
2021-05-09 20:26:40 -04:00
{
2021-05-15 12:52:13 -04:00
N.Connect(inRoot, inLastBranch, "Terminal");
2021-05-14 07:11:23 -04:00
inLastBranch.Meta.Leaves.forEach( inLeaf =>
{
// collect modifiers effecting leaves
let modifiers = [];
let check = N.Step(inLeaf, "ModifyAt", false);
if(check)
{
modifiers = modifiers.concat(check);
}
check = N.Step(inLeaf, "ModifyDown", false);
if(check)
{
modifiers = modifiers.concat(check);
}
if(modifiers.length)
{
// apply them to the branch
inLastBranch.ID.Walk = N.ID.Walk;
modifiers.forEach( inModifier => N.Connect(inModifier, inLastBranch, "ModifyUp") )
2021-05-15 12:52:13 -04:00
// also walk them up and connect, but with "check unique" enabled
2021-05-14 07:11:23 -04:00
N.Walk( inNode=>
{
modifiers.forEach( inModifier => N.Connect(inModifier, inNode, "ModifyUp", true) )
}
, inLastBranch, "Hierarchy", false);
}
// lastly connect the leaf to the branch
N.Connect(inLastBranch, inLeaf, "Hierarchy");
});
delete inLastBranch.Meta.Leaves;
2021-05-09 20:26:40 -04:00
}
}
else
{
2021-05-15 12:52:13 -04:00
iterator = child => Pivot.Pivot(inRoot, 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-14 07:11:23 -04:00
N.ID.Walk++;
2021-05-15 07:30:52 -04:00
let pivotRoot = N.Create({Label:"Pivot Root", Leaves:Pivot.Leaves});
2021-05-15 07:30:52 -04:00
N.Connect(Pivot.Root, pivotRoot, "Pivot");
2021-05-15 12:52:13 -04:00
return Pivot.Pivot(pivotRoot, pivotRoot, inPivotIndicies, inSumIndicies);
2021-05-05 21:53:04 -04:00
},
2021-05-15 07:30:52 -04:00
Delete(inRoot)
2021-05-08 07:03:41 -04:00
{
2021-05-15 12:52:13 -04:00
// disconnect modifiers
let check = N.Step(inRoot, "Modifier");
if(check)
2021-05-13 19:54:42 -04:00
{
while(check.length>0)
{
Pivot.Unmodify(check[0]);
}
}
2021-05-15 12:52:13 -04:00
// disconnect terminal branches
N.Walk(()=>{}, inRoot, "Terminal", inNode=>{
N.Disconnect(inNode, null, "Hierarchy");
});
// disconnect from app
N.Disconnect(null, inRoot, "Pivot")
2021-05-15 07:30:52 -04:00
},
Modify(inNode)
{
let modified = N.Create({Label:"Modifier"});
2021-05-15 07:30:52 -04:00
N.ID.Walk++;
if(N.Step(inNode, "Hierarchy").length)
{
N.Walk(()=>{}, inNode, "Hierarchy", false, (inRoot)=>
{
N.Connect(inRoot, modified, "Modifier");
});
}
else
{
N.Connect(Pivot.Root, modified, "Modifier");
}
let leaves = [];
let gatherUp = n => N.Connect(modified, n, "ModifyUp");
2021-05-08 08:46:37 -04:00
let gatherDown = n =>
2021-05-08 07:03:41 -04:00
{
2021-05-13 19:54:42 -04:00
N.Connect(modified, 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-15 07:30:52 -04:00
let gatherOut = n => N.Connect(modified, n, "ModifyOut");
2021-05-13 19:54:42 -04:00
N.ID.Walk++;
inNode.ID.Walk = N.ID.Walk;
N.Connect(modified, inNode, "ModifyAt");
2021-05-08 07:03:41 -04:00
2021-05-09 14:19:13 -04:00
N.Walk(gatherUp, inNode, "Hierarchy", false);
N.Walk(gatherDown, inNode, "Hierarchy");
leaves.forEach(leaf=>N.Walk(gatherOut, leaf, "Hierarchy", false));
2021-05-11 23:18:08 -04:00
2021-05-13 19:54:42 -04:00
return modified;
2021-05-09 20:26:40 -04:00
},
Unmodify(inModifier)
2021-05-09 14:19:13 -04:00
{
N.Disconnect(inModifier, null, "ModifyUp");
N.Disconnect(inModifier, null, "ModifyDown");
N.Disconnect(inModifier, null, "ModifyOut");
N.Disconnect(inModifier, null, "ModifyAt");
N.Disconnect(null, inModifier, "Modifier");
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-14 07:11:23 -04:00
import { h, render, createContext, Fragment } from 'https://cdn.skypack.dev/preact';
import { useReducer } from 'https://cdn.skypack.dev/preact/hooks';
2021-05-11 06:26:18 -04:00
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-15 07:30:52 -04:00
Pivot.Create([1, 2], [3]);
2021-05-11 23:18:08 -04:00
2021-05-14 07:11:23 -04:00
let AddNewPivot = () =>
{
2021-05-15 07:30:52 -04:00
Pivot.Create([2], [3]);
Render();
};
2021-05-11 23:18:08 -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
let nodeBase = css`
position:relative;
padding:0;
margin:0;
border-top:1px solid lightgrey;
.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-13 19:54:42 -04:00
let label = css`
display:inline-block;
width:200px;
&::before
{
content:" ";
display:inline-block;
width:${depth*15}px;
}
.Modify
{
float:right;
}
`;
2021-05-14 06:47:30 -04:00
let icon = cx(
2021-05-13 19:54:42 -04:00
{
2021-05-14 06:47:30 -04:00
[css`
display:inline-block;
width:0px;
height:0px;
border:7px solid white;` ]: true,
2021-05-13 19:54:42 -04:00
[css`border-bottom-color:lightblue;`]: N.Step(node, "ModifyUp" ),
[css`border-top-color:orange;` ]: N.Step(node, "ModifyDown"),
[css`border-left-color:grey;` ]: N.Step(node, "ModifyOut" ),
[css`border-right-color:red;` ]: N.Step(node, "ModifyAt" )
},
);
2021-05-09 14:19:13 -04:00
2021-05-14 06:47:30 -04:00
let buttonModify = h("span", {
className:"Icon Modify Add",
onClick:e=>
{
Pivot.Modify(node);
Render();
}
}, "Modify");
let buttonUnmodify = h("span", {
className:"Icon Modify remove",
onClick:e=>
{
Pivot.Unmodify(N.Step(node, "ModifyAt", false)[0]);
2021-05-14 06:47:30 -04:00
Render();
}
}, "Unmodify");
2021-05-12 19:08:52 -04:00
return h("div", {className:"Node"}, [
2021-05-13 19:54:42 -04:00
h("div", {className:cx(nodeBase, "Upper")}, [
h("div", {className:label}, [
h("span", {className:"Icon Expand"}, "+"),
h("span", {className:"Name"}, (node.Meta.Label || "a node")+" "+node.ID.Walk),
2021-05-14 06:47:30 -04:00
h("span", {className: icon}),
N.Step(node, "ModifyAt") ? buttonUnmodify : buttonModify
2021-05-13 19:54:42 -04:00
] ),
2021-05-12 07:17:08 -04:00
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-14 06:47:30 -04:00
};
2021-05-08 15:32:40 -04:00
let ElPivot = ({pivot}) =>
2021-05-15 07:30:52 -04:00
{
2021-05-12 07:17:08 -04:00
return h("div", {style:{display:"inline-block", width:"500px"}}, [
h("button", {onClick:e=>{Pivot.Delete(pivot);Render();}}, "delete?"),
2021-05-15 07:30:52 -04:00
h(ElModifiers, {node:pivot}),
2021-05-12 07:17:08 -04:00
h(ElNode, {node:pivot, depth:0})
2021-05-08 15:32:40 -04:00
]);
2021-05-14 06:47:30 -04:00
};
2021-05-15 07:30:52 -04:00
let ElModifiers = ({node}) =>
{
let modifiers = N.Step(node, "Modifier") || [];
return h("div", null, [
h("strong", null, "modifiers"),
...modifiers.map( m => h("span", {onClick:e=>
{
Pivot.Unmodify(m);
2021-05-15 07:30:52 -04:00
Render();
}
}, "modifier"))
]);
};
2021-05-14 06:47:30 -04:00
let ElRoot = props =>
2021-05-08 13:51:19 -04:00
{
2021-05-15 12:52:13 -04:00
let pivots = N.Step(Pivot.Root, "Pivot")||[];
2021-05-13 19:54:42 -04:00
return h("div", null, [
h("h3", null, "tree view"),
2021-05-15 07:30:52 -04:00
h(ElModifiers, {node:Pivot.Root}),
2021-05-14 07:11:23 -04:00
h("button", { onClick:e=>
{
AddNewPivot();
}
}, "add new pivot"),
2021-05-15 12:52:13 -04:00
pivots.map(pivot=>h(ElPivot, {pivot}))
2021-05-13 19:54:42 -04:00
])
2021-05-08 13:51:19 -04:00
};
2021-05-14 06:47:30 -04:00
let Render = () => render(h(ElRoot), document.querySelector("#app"));
Render();
2021-05-08 13:51:19 -04:00
2021-05-08 13:21:43 -04:00
</script>