2019-07-03 01:42:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
class Architype {
|
|
|
|
|
constructor(container) {
|
|
|
|
|
this.container_ = container;
|
|
|
|
|
|
|
|
|
|
this.container_.classList.add('architype');
|
|
|
|
|
// TODO: make theme selectable
|
|
|
|
|
this.container_.classList.add('dark');
|
|
|
|
|
|
|
|
|
|
this.container_.addEventListener('keydown', (e) => { this.onKeyDown(e); });
|
|
|
|
|
addEventListener('resize', (e) => { this.onResize(e); });
|
|
|
|
|
|
|
|
|
|
let editorElem = document.createElement('ul');
|
|
|
|
|
this.container_.appendChild(editorElem);
|
|
|
|
|
this.editor_ = new Editor(editorElem);
|
|
|
|
|
|
|
|
|
|
this.lines_ = document.createElement('div');
|
|
|
|
|
this.lines_.innerHTML = `<!--# include file="lines.svg" -->`;
|
|
|
|
|
this.lines_ = this.lines_.firstElementChild;
|
|
|
|
|
this.lines_.classList.add('gridLines');
|
|
|
|
|
this.lines_.classList.add('white');
|
|
|
|
|
|
|
|
|
|
this.grid_ = document.createElement('div');
|
|
|
|
|
this.grid_.classList.add('grid');
|
|
|
|
|
this.container_.appendChild(this.grid_);
|
|
|
|
|
|
|
|
|
|
this.unserialize(JSON.parse(localStorage.getItem('currentState')));
|
|
|
|
|
|
2019-07-03 01:51:13 +00:00
|
|
|
this.observer_ = new MutationObserver(e => { this.onChange(e); });
|
|
|
|
|
this.observer_.observe(editorElem, {
|
2019-07-03 01:42:17 +00:00
|
|
|
attributes: true,
|
|
|
|
|
attributeFilter: ['data-arch-value'],
|
2019-07-03 01:51:13 +00:00
|
|
|
childList: true,
|
2019-07-03 01:42:17 +00:00
|
|
|
subtree: true,
|
|
|
|
|
});
|
|
|
|
|
|
2019-07-03 01:51:13 +00:00
|
|
|
this.onChange();
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serialize() {
|
|
|
|
|
return {
|
|
|
|
|
version: 1,
|
|
|
|
|
editor: this.editor_.serialize(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unserialize(ser) {
|
|
|
|
|
if (!ser) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (ser.version) {
|
|
|
|
|
case 1:
|
|
|
|
|
this.editor_.unserialize(ser.editor);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
console.log('unrecognized localStorage.currentState version', ser);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 01:51:13 +00:00
|
|
|
onChange(e) {
|
2019-07-03 03:19:23 +00:00
|
|
|
let serialized = this.serialize();
|
2019-07-03 18:27:32 +00:00
|
|
|
this.draw(render(serialized));
|
2019-07-03 03:19:23 +00:00
|
|
|
localStorage.setItem('currentState', JSON.stringify(serialized));
|
2019-07-03 18:27:32 +00:00
|
|
|
this.fixSizes();
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onKeyDown(e) {
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case 'z':
|
|
|
|
|
this.exportGraphviz();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onResize(e) {
|
2019-07-03 20:00:05 +00:00
|
|
|
this.fixSizes();
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exportGraphviz() {
|
|
|
|
|
let lines = [
|
|
|
|
|
'digraph G {',
|
|
|
|
|
'\trankdir = "LR";',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (let type of ['nodes', 'links', 'groups']) {
|
|
|
|
|
for (let obj of this.graph_[type]) {
|
|
|
|
|
for (let line of obj.exportGraphviz()) {
|
|
|
|
|
lines.push('\t' + line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines.push('}');
|
|
|
|
|
navigator.clipboard.writeText(lines.join('\n'));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 18:27:32 +00:00
|
|
|
draw(steps) {
|
|
|
|
|
this.grid_.innerHTML = '';
|
|
|
|
|
this.gridNodes_ = [];
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-03 18:27:32 +00:00
|
|
|
for (let step of steps) {
|
|
|
|
|
switch (step.type) {
|
|
|
|
|
case 'size':
|
|
|
|
|
this.drawGrid(step.size);
|
|
|
|
|
break;
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-03 18:27:32 +00:00
|
|
|
case 'node':
|
|
|
|
|
this.drawGridNode(step.label, step.pos);
|
|
|
|
|
break;
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 18:27:32 +00:00
|
|
|
drawGrid(size) {
|
2019-07-03 01:42:17 +00:00
|
|
|
this.grid_.style.gridTemplateColumns =
|
2019-07-03 18:27:32 +00:00
|
|
|
'repeat(' + size[0] + ',1fr)';
|
2019-07-03 01:42:17 +00:00
|
|
|
this.grid_.style.gridTemplateRows =
|
2019-07-03 18:27:32 +00:00
|
|
|
'repeat(' + size[1] +
|
2019-07-03 01:42:17 +00:00
|
|
|
',minmax(0, calc((100vw - var(--editor-width)) / ' +
|
2019-07-03 18:27:32 +00:00
|
|
|
size[0] + ')))';
|
|
|
|
|
}
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-03 18:27:32 +00:00
|
|
|
drawGridNode(label, pos) {
|
|
|
|
|
let node = document.createElement('div');
|
|
|
|
|
node.classList.add('gridNode');
|
|
|
|
|
this.grid_.appendChild(node);
|
|
|
|
|
node.innerText = label;
|
|
|
|
|
node.style.gridColumn = pos[0] + 1;
|
|
|
|
|
node.style.gridRow = pos[1] + 1;
|
2019-07-03 18:35:24 +00:00
|
|
|
this.gridNodes_.push(node);
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-03 18:27:32 +00:00
|
|
|
fixSizes() {
|
|
|
|
|
for (let node of this.gridNodes_) {
|
|
|
|
|
node.style.fontSize = null;
|
|
|
|
|
for (let size = 20;
|
|
|
|
|
size && (node.scrollWidth > node.clientWidth ||
|
|
|
|
|
node.scrollHeight > node.clientHeight);
|
|
|
|
|
--size) {
|
|
|
|
|
node.style.fontSize = size + 'px';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: fix this
|
2019-07-03 01:42:17 +00:00
|
|
|
addLines(pos, cls) {
|
|
|
|
|
let lines = this.lines_.cloneNode(true);
|
|
|
|
|
lines.classList.add(cls);
|
|
|
|
|
lines.style.gridColumn = pos[0] + 1;
|
|
|
|
|
lines.style.gridRow = pos[1] + 1;
|
|
|
|
|
this.grid_.appendChild(lines);
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!--# include file="Editor.js" -->
|
|
|
|
|
<!--# include file="EditorEntryBase.js" -->
|
|
|
|
|
<!--# include file="EditorGroup.js" -->
|
|
|
|
|
<!--# include file="EditorLink.js" -->
|
|
|
|
|
<!--# include file="EditorNode.js" -->
|
|
|
|
|
|
|
|
|
|
<!--# include file="utils.js" -->
|
|
|
|
|
|
2019-07-03 03:24:15 +00:00
|
|
|
<!--# include file="render.js" -->
|
|
|
|
|
|
2019-07-03 01:42:17 +00:00
|
|
|
new Architype(document.getElementById('architype'));
|