pivot-editor/index.html

506 lines
16 KiB
HTML
Raw Normal View History

2021-05-29 17:09:14 -04:00
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script type="importmap">
{
"imports":
{
"react": "https://esm.sh/react@18",
"react-dom/client": "https://esm.sh/react-dom/client",
"@emotion": "https://esm.sh/@emotion",
"@emotion/": "https://esm.sh/@emotion/",
"htm": "https://esm.sh/htm/react"
}
}
</script>
2021-05-29 17:09:14 -04:00
<script src="./data/csv.js"></script>
</head>
<body>
<div id="app"></div>
<!-- initialize table -->
2022-04-09 12:32:25 -04:00
<script type="module">
import { createElement as h, createContext, Fragment, useReducer, useState } from 'react';
import { createRoot } from "react-dom/client";
import { css, cx } from '@emotion/css';
import { html } from 'htm';
2022-04-09 12:32:25 -04:00
import N from "./src/n.js";
import Pivot from "./src/pivot.js";
2021-05-29 17:09:14 -04:00
let columnNames = CSV.shift();
let columnTypes = ([...columnNames]).fill("hidden");
columnTypes[29] = "sum";
columnTypes[30] = "sum";
columnTypes[31] = "sum";
columnTypes[32] = "sum";
columnTypes[33] = "sum";
columnTypes[7 ] = "label";
columnTypes[8 ] = "label";
columnTypes[9 ] = "label";
columnTypes[10] = "label";
columnTypes[11] = "label";
Pivot.Init(
columnTypes,
columnNames,
CSV
);
2022-04-09 12:32:25 -04:00
2021-05-29 17:09:14 -04:00
let PivotForm = props =>
2021-05-05 21:53:04 -04:00
{
2021-05-29 17:09:14 -04:00
let styles = css`
position:realtive;
box-sizing: border-box;
padding: 10px;
color:black;
font-family:sans-serif;
.Title
2021-05-14 07:11:23 -04:00
{
2021-05-29 17:09:14 -04:00
font-size:24px;
font-weight:100;
2021-05-14 07:11:23 -04:00
}
2021-05-29 17:09:14 -04:00
.Section
2021-05-09 14:19:13 -04:00
{
2021-05-29 17:09:14 -04:00
padding:10px 0 10px 0;
.Heading
2021-05-09 20:26:40 -04:00
{
2021-05-29 17:09:14 -04:00
display:inline-block;
color:#666;
font-family:sans-serif;
font-size:12px;
font-weight:900;
text-transform:uppercase;
2021-05-09 20:26:40 -04:00
}
2021-05-29 17:09:14 -04:00
.Group
2021-05-09 20:26:40 -04:00
{
2021-05-29 17:09:14 -04:00
display:inline-block;
padding:5px;
border-radius:5px;
margin:3px;
background:rgba(0, 0, 0, 0.3)
2021-05-09 20:26:40 -04:00
}
}
2021-05-29 17:09:14 -04:00
`;
let pivotColumns = N.Step(Pivot.Schema, "label")||[];
let pivotColumnsUsed = N.Step(Pivot.Proto, "used-pivot")||[];
let sumColumns = N.Step(Pivot.Schema, "sum")||[];
//let sumColumnsUsed = N.Step(Pivot.Proto, "used-sum")||[];
let indiciesPivot = pivotColumnsUsed.map(node=>node.Meta.Index);
let indiciesSum = sumColumns.map(node=>node.Meta.Index);
//let indiciesSum = sumColumnsUsed.map(node=>node.Meta.Index);
2021-05-11 23:18:08 -04:00
2021-05-29 17:09:14 -04:00
let displayPivotsAll = html`
<div class="Section">
<div class="Heading">Available Columns</div>
<div class="Group">
${pivotColumns.map( columnPivot =>
2021-05-05 21:53:04 -04:00
{
2021-05-29 17:09:14 -04:00
let attributes = {};
if(N.Step(columnPivot, "used-pivot", false))
2021-05-05 21:53:04 -04:00
{
2021-05-29 17:09:14 -04:00
attributes.disabled = true;
2021-05-05 21:53:04 -04:00
}
2021-05-28 20:52:56 -04:00
else
{
2021-05-29 17:09:14 -04:00
attributes.onClick = e=>
2021-05-14 07:11:23 -04:00
{
2021-05-29 17:09:14 -04:00
N.Connect(Pivot.Proto, columnPivot, "used-pivot");
Render();
2021-05-14 07:11:23 -04:00
}
2021-05-29 17:09:14 -04:00
}
return html`<button ...${attributes}>${columnPivot.Meta.Label}</button>`;
})}
</div>
</div>
`;
let displayPivotsPending = null;
if(pivotColumnsUsed.length)
2021-05-13 19:54:42 -04:00
{
2021-05-29 17:09:14 -04:00
displayPivotsPending = html`
<div class="Section">
<div class="Heading">Pending Pivot</div>
<div class="Group">
${pivotColumnsUsed.map(columnPivot=>html`
<button onClick=${e=>{N.Disconnect(Pivot.Proto, columnPivot, "used-pivot");Render();}}>
${columnPivot.Meta.Label}
</button>
`)}
</div>
<button onClick=${e=>{
N.Disconnect(Pivot.Proto, null, "used-pivot");
N.Disconnect(Pivot.Proto, null, "used-sum");
Pivot.Create(pivotColumnsUsed.map(column=>column.Meta.Label).join("|"), indiciesPivot, indiciesSum);
Render();
}}>Create</button>
</div>
`;
}
2021-05-29 17:09:14 -04:00
return html`
<div class=${styles}>
<div class="Title">Create New Pivot</div>
${displayPivotsAll}
${displayPivotsPending}
</div>
`;
2021-05-09 14:19:13 -04:00
}
2021-05-29 17:09:14 -04:00
let Section = props =>
2021-05-29 15:25:19 -04:00
{
2021-05-29 17:09:14 -04:00
let styles = css`
2021-05-26 22:54:50 -04:00
.Heading
{
2021-05-29 17:09:14 -04:00
padding:6px 0 6px 0;
2021-05-26 22:54:50 -04:00
color:#666;
font-weight:900;
2021-05-29 17:09:14 -04:00
font-size:12px;
2021-05-26 22:54:50 -04:00
text-transform:uppercase;
2021-05-29 17:09:14 -04:00
cursor:pointer;
span
{
display:inline-block;
width:20px;
height:20px;
margin-right:10px;
border-radius:20px;
background:black;
color:white;
text-align:center;
}
2021-05-26 22:54:50 -04:00
}
2021-05-29 17:09:14 -04:00
.Heading:hover
2021-05-26 22:54:50 -04:00
{
2021-05-29 17:09:14 -04:00
color:black;
2021-05-26 22:54:50 -04:00
}
2021-05-29 17:09:14 -04:00
.Body
2021-05-26 22:54:50 -04:00
{
2021-05-29 17:09:14 -04:00
position:relative;
padding:10px 0 20px 30px;
&::before
2021-05-26 22:54:50 -04:00
{
2021-05-29 17:09:14 -04:00
content: " ";
display:block;
position:absolute;
top:-8px;
left:8px;
width:3px;
height:100%;
background:black;
2021-05-26 22:54:50 -04:00
}
}
2021-05-29 17:09:14 -04:00
`;
let [openGet, openSet] = useState(false);
return html`
<div class=${styles}>
<div class="Heading" onClick=${e=>openSet(!openGet)}>
<span>${openGet ? `` : `+`}</span>
${props.label}
</div>
${ openGet ? html`<div class="Body">${props.children}</div>` : null }
2021-05-26 22:54:50 -04:00
</div>
`;
}
2021-05-29 17:09:14 -04:00
let ModificationsIcon = ({node}) =>
{
let modsUp = N.Step(node, "ModifyUp", false)||[];
let modsDown = N.Step(node, "ModifyDown", false)||[];
let modsAt = N.Step(node, "ModifyAt", false)||[];
let modsOut = N.Step(node, "ModifyOut", false)||[];
var button = null;
if(modsAt.length)
2021-05-26 22:54:50 -04:00
{
2021-05-29 17:09:14 -04:00
button = html`<button onClick=${e=>{Pivot.Unmodify(modsAt[0]); Render();}}>-</button>`;
}
else
2021-05-26 22:54:50 -04:00
{
2021-05-29 17:09:14 -04:00
button = html`<button onClick=${e=>{Pivot.Modify(node); Render();}}>+</button>`;
2021-05-26 22:54:50 -04:00
}
2021-05-29 17:09:14 -04:00
let padding = 7;
let icon = 0;
let styles = css`
2021-05-26 22:54:50 -04:00
position:relative;
2021-05-28 16:14:52 -04:00
display:inline-block;
2021-05-29 17:09:14 -04:00
vertical-align:middle;
2021-05-28 19:11:15 -04:00
width:${padding*2 + icon}px;
height:${padding*2 + icon}px;
2021-05-29 17:09:14 -04:00
margin:${padding*2};
.Icon
2021-05-28 16:14:52 -04:00
{
position:absolute;
2021-05-29 17:09:14 -04:00
display:inline-block;
width:${padding*2 + icon}px;
height:${padding*2 + icon}px;
text-align:center;
font-size:9px;
font-family:sans-serif;
font-weight:900;
line-height:${padding*2 + icon}px;
2021-05-28 16:14:52 -04:00
&::after
{
2021-05-29 17:09:14 -04:00
content:" ";
display:block;
position:absolute;
width:${icon}px;
height:${icon}px;
border:${padding}px solid transparent;
}
&.Down
{
left:0;
bottom:100%;
&::after
{
top:100%;
border-top-color:green;
border-bottom:0px solid transparent;
}
}
&.At
{
top:0;
left:100%;
&::after
{
top:0;
right:100%;
border-right-color:red;
border-left:0px solid transparent;
}
}
&.Up
{
left:0;
2021-05-28 16:14:52 -04:00
top:100%;
2021-05-29 17:09:14 -04:00
&::after
{
bottom:100%;
border-bottom-color:orange;
border-top:0px solid transparent;
}
2021-05-28 16:14:52 -04:00
}
2021-05-29 17:09:14 -04:00
&.Out
2021-05-28 16:14:52 -04:00
{
top:0;
right:100%;
2021-05-29 17:09:14 -04:00
&::after
{
top:0;
left:100%;
border-left-color:grey;
border-right:0px solid transparent;
}
2021-05-28 16:14:52 -04:00
}
}
2021-05-29 17:09:14 -04:00
`;
return html`
<div class=${styles}>
${modsDown.length ? html`<div class="Icon Down">${modsDown.length}</div>` : null}
${modsAt.length ? html`<span class="Icon At">${modsAt.length}</span>` : null}
${modsUp.length ? html`<span class="Icon Up">${modsUp.length}</span>` : null}
${modsOut.length ? html`<span class="Icon Out">${modsOut.length}</span>` : null}
</div>
${button}
`;
};
let PivotBranch = props =>
{
let row = props.node.Meta.Row;
let displayCellsModify = row.map(column=>false);
props.node.Meta.IndexSum.forEach(i=>
{
displayCellsModify[i] = html`<td><input type="number" value=${row[i]}/></td>`;
});
displayCellsModify.forEach((cell, i)=>
{
if(!cell)
2021-05-28 16:14:52 -04:00
{
2021-05-29 17:09:14 -04:00
displayCellsModify[i] = html`<td>${row[i]}</td>`
2021-05-28 16:14:52 -04:00
}
2021-05-29 17:09:14 -04:00
});
let displayCells = (node, visible) =>
{
let output = [];
node.Meta.Row.forEach((column, i)=>
2021-05-28 16:14:52 -04:00
{
2021-05-29 17:09:14 -04:00
if(visible[i])
2021-05-28 16:14:52 -04:00
{
2021-05-29 17:09:14 -04:00
output.push( h("td", null, column) );
2021-05-28 16:14:52 -04:00
}
}
2021-05-29 17:09:14 -04:00
);
return output;
2021-05-28 16:14:52 -04:00
}
2021-05-29 17:09:14 -04:00
let stylesLeaf = css`
background:#ddd;
color:#333;
`;
return html`
<tbody>
<tr>
<td>
<strong class=${css`margin-left:${props.node.Meta.Depth*10}px;`}>${props.node.Meta.Label}</strong>
</td>
<td>
<${ModificationsIcon} node=${props.node}><//>
</td>
${displayCells(props.node, props.visible)}
</tr>
${(N.Step(props.node, "Leaf")||[]).map(leaf =>
{
return html`
<tr class=${stylesLeaf}>
<td></td>
<td><${ModificationsIcon} node=${leaf}><//></td>
${displayCells(leaf, props.visible)}
</tr>
`;
}
)}
</tbody>
`;
};
let PivotRoot = ({pivot}) =>
2021-05-29 16:04:38 -04:00
{
2021-05-29 17:09:14 -04:00
let labelsDefault = (N.Step(Pivot.Schema, "hidden")||[]).map(column => column.Meta.Index);
let labelsSum = (N.Step(Pivot.Schema, "sum")||[]).map(column => column.Meta.Index);
let labelsPivot = (N.Step(Pivot.Schema, "label")||[]).map(column => column.Meta.Index);
let labelsAll = (N.Step(Pivot.Schema, "all")||[]).map(column => column.Meta.Label);
let labelsAllState = useState(labelsAll.map(column=>true))
2021-05-29 17:09:14 -04:00
let headersDisplay = [];
labelsAllState[0].forEach((visible, index)=>
2021-05-29 16:04:38 -04:00
{
2021-05-29 17:09:14 -04:00
if(visible)
2021-05-29 16:04:38 -04:00
{
2021-05-29 17:09:14 -04:00
headersDisplay.push(html`<th>${labelsAll[index]}</th>`);
2021-05-29 16:04:38 -04:00
}
}
);
2021-05-29 17:09:14 -04:00
let stylesRoot = css`
display:block;
box-sizing:border-box;
padding:15px;
font-family:sans-serif;
`;
let stylesHeading = css`
margin: 0 0 15px 0;
font-weight:0;
font-size:24px;
`;
let modifiers = N.Step(pivot, "ModifyUp", false) || [];
let handleDelete = ()=>
2021-05-29 15:25:19 -04:00
{
2021-05-29 17:09:14 -04:00
Pivot.Delete(pivot);
Render();
};
let displayColumnGroup = (inAllLabels, inAllState, inIndicies) =>
2021-05-29 16:55:02 -04:00
{
2021-05-29 17:09:14 -04:00
return html`
<div>
<button onClick=${e=>
{
let clone = [...inAllState[0]];
inIndicies.forEach(index =>clone[index] = true);
inAllState[1](clone);
}
}>Show All</button>
<button onClick=${e=>
{
let clone = [...inAllState[0]];
inIndicies.forEach(index =>clone[index] = false);
inAllState[1](clone);
}
}>Hide All</button>
</div>
<div>
${inIndicies.map((index) => html`<button onClick=${e=>
{
let clone = [...inAllState[0]];
clone[index] = !clone[index];
inAllState[1](clone);
}
}>${inAllLabels[index]} ${inAllState[0][index] ? `visible`:`hidden`}</button>`)}
</div>`;
};
let rows = [];
N.ID.Walk++;
N.Walk(n=>rows.push(h(PivotBranch, {node:n, visible:labelsAllState[0]}, null)), pivot, "Hierarchy");
2021-05-29 16:55:02 -04:00
return html`
2021-05-29 17:09:14 -04:00
<div class=${stylesRoot}>
<div key="heading" class=${stylesHeading}>${pivot.Meta.Label}</div>
<${Section} key="settings" label=${`Settings`}>
<button onClick=${handleDelete}>Destroy Pivot</button>
<//>
<${Section} key="columns" label="Columns">
<h3>Unused</h3>
${displayColumnGroup(labelsAll, labelsAllState, labelsDefault)}
<h3>Summation</h3>
${displayColumnGroup(labelsAll, labelsAllState, labelsSum)}
<h3>Pivot</h3>
${displayColumnGroup(labelsAll, labelsAllState, labelsPivot)}
<//>
<${Section} key="tree" label=${"Tree"}>
<table>
<thead>
<tr>
<th>Label</th><th>Modifications</th>${headersDisplay}
</th>
</thead>
${rows}
</table>
<//>
2021-05-29 16:55:02 -04:00
</div>
2021-05-29 17:09:14 -04:00
`;
2021-05-29 16:55:02 -04:00
};
2021-05-29 17:09:14 -04:00
let ElRoot = props =>
{
let pivots = N.Step(Pivot.Root, "Pivot")||[];
return h("div", null, [
h(PivotForm),
pivots.map(pivot=>h(PivotRoot, {key:pivot.Meta.Label, pivot}))
])
};
2022-04-09 12:32:25 -04:00
const Root = createRoot(document.querySelector("#app"));
const Render = () => Root.render(h(ElRoot));
2021-05-29 17:09:14 -04:00
Render();
2021-05-29 17:09:14 -04:00
</script>
</body>
</html>