2019-07-10 22:18:00 +00:00
|
|
|
class Grid {
|
2019-07-13 22:30:37 +00:00
|
|
|
constructor(container) {
|
2019-07-10 22:43:29 +00:00
|
|
|
this.container_ = container;
|
|
|
|
|
this.container_.classList.add('grid');
|
|
|
|
|
|
|
|
|
|
this.toSize_ = [];
|
|
|
|
|
addEventListener('resize', (e) => { this.onResize(e); });
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 05:06:48 +00:00
|
|
|
onResize() {
|
2019-07-10 22:43:29 +00:00
|
|
|
this.fixSizes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw(steps) {
|
|
|
|
|
this.container_.innerHTML = '';
|
2019-07-13 21:44:51 +00:00
|
|
|
this.toSize_.length = 0;
|
2019-07-10 22:43:29 +00:00
|
|
|
|
|
|
|
|
for (let step of steps) {
|
|
|
|
|
switch (step.type) {
|
|
|
|
|
case 'size':
|
|
|
|
|
this.drawGrid(step.size);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'arrow':
|
2019-07-13 21:44:51 +00:00
|
|
|
this.drawArrow(step.id, step.pos, step.cls, step.highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'graphLabel':
|
2019-07-13 21:44:51 +00:00
|
|
|
this.drawGraphLabel(step.id, step.min, step.max, step.label);
|
2019-07-10 22:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'group':
|
2019-07-13 21:44:51 +00:00
|
|
|
this.drawGroup(step.id, step.min, step.max, step.label,
|
|
|
|
|
step.highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'line':
|
2019-07-13 21:44:51 +00:00
|
|
|
this.drawLine(step.id, step.pos, step.cls, step.highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'linkLabel':
|
2019-07-13 21:44:51 +00:00
|
|
|
this.drawLinkLabel(step.id, step.pos, step.label);
|
2019-07-10 22:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'node':
|
2019-07-11 05:12:08 +00:00
|
|
|
this.drawNode(step.id, step.label, step.pos, step.highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.fixSizes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
drawGrid(size) {
|
|
|
|
|
this.container_.style.gridTemplateColumns =
|
|
|
|
|
'repeat(' + size[0] + ',1fr)';
|
|
|
|
|
this.container_.style.gridTemplateRows =
|
|
|
|
|
'repeat(' + size[1] +
|
|
|
|
|
',minmax(0, calc((100vw - var(--editor-width)) / ' +
|
|
|
|
|
size[0] + ')))';
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 21:44:51 +00:00
|
|
|
drawArrow(id, pos, cls, highlight) {
|
2019-07-10 22:43:29 +00:00
|
|
|
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
2019-07-13 22:30:37 +00:00
|
|
|
this.container_.appendChild(svg);
|
2019-07-10 22:43:29 +00:00
|
|
|
svg.classList.add('gridArrow');
|
2019-07-13 22:30:37 +00:00
|
|
|
svg.classList.add('grid-' + id);
|
2019-07-10 22:43:29 +00:00
|
|
|
svg.style.gridColumn = pos[0] + 1;
|
|
|
|
|
svg.style.gridRow = pos[1] + 1;
|
2019-07-10 23:41:41 +00:00
|
|
|
svg.classList.toggle('highlight', highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
|
|
|
|
|
let use = document.createElementNS('http://www.w3.org/2000/svg', 'use');
|
|
|
|
|
svg.appendChild(use);
|
|
|
|
|
use.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#' + cls);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 21:44:51 +00:00
|
|
|
drawGraphLabel(id, min, max, label) {
|
2019-07-10 22:43:29 +00:00
|
|
|
let elem = document.createElement('div');
|
|
|
|
|
this.container_.appendChild(elem);
|
|
|
|
|
elem.classList.add('gridGraphLabel');
|
2019-07-13 22:30:37 +00:00
|
|
|
elem.classList.add('grid-' + id);
|
2019-07-10 22:43:29 +00:00
|
|
|
elem.style.gridColumn = (min[0] + 1) + ' / ' + (max[0] + 2);
|
|
|
|
|
elem.style.gridRow = (min[1] + 1) + ' / ' + (max[1] + 2);
|
|
|
|
|
elem.innerText = label;
|
|
|
|
|
this.toSize_.push(elem);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 21:44:51 +00:00
|
|
|
drawGroup(id, min, max, label, highlight) {
|
2019-07-10 22:43:29 +00:00
|
|
|
let group = document.createElement('div');
|
|
|
|
|
this.container_.appendChild(group);
|
|
|
|
|
group.classList.add('gridGroup');
|
2019-07-13 22:30:37 +00:00
|
|
|
group.classList.add('grid-' + id);
|
2019-07-10 22:43:29 +00:00
|
|
|
group.style.gridColumn = (min[0] + 1) + ' / ' + (max[0] + 2);
|
|
|
|
|
group.style.gridRow = (min[1] + 1) + ' / ' + (max[1] + 2);
|
2019-07-10 23:46:25 +00:00
|
|
|
group.classList.toggle('highlight', highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
|
|
|
|
|
if (label != '') {
|
|
|
|
|
let labelNode = document.createElement('div');
|
|
|
|
|
this.container_.appendChild(labelNode);
|
|
|
|
|
labelNode.classList.add('gridGroupLabel');
|
2019-07-13 22:30:37 +00:00
|
|
|
labelNode.classList.add('grid-' + id);
|
2019-07-10 22:43:29 +00:00
|
|
|
labelNode.innerText = label;
|
|
|
|
|
labelNode.style.gridColumn = (min[0] + 1) + ' / ' + (max[0] + 2);
|
|
|
|
|
labelNode.style.gridRow = min[1] + 1;
|
|
|
|
|
this.toSize_.push(labelNode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 21:44:51 +00:00
|
|
|
drawLine(id, pos, cls, highlight) {
|
2019-07-10 22:43:29 +00:00
|
|
|
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
2019-07-13 22:30:37 +00:00
|
|
|
this.container_.appendChild(svg);
|
2019-07-10 22:43:29 +00:00
|
|
|
svg.classList.add('gridLines');
|
2019-07-13 22:30:37 +00:00
|
|
|
svg.classList.add('grid-' + id);
|
2019-07-10 22:43:29 +00:00
|
|
|
svg.style.gridColumn = pos[0] + 1;
|
|
|
|
|
svg.style.gridRow = pos[1] + 1;
|
2019-07-10 23:41:41 +00:00
|
|
|
svg.classList.toggle('highlight', highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
|
|
|
|
|
let use = document.createElementNS('http://www.w3.org/2000/svg', 'use');
|
|
|
|
|
svg.appendChild(use);
|
|
|
|
|
use.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#' + cls);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 21:44:51 +00:00
|
|
|
drawLinkLabel(id, pos, label) {
|
2019-07-10 22:43:29 +00:00
|
|
|
let elem = document.createElement('div');
|
|
|
|
|
this.container_.appendChild(elem);
|
2019-07-13 22:30:37 +00:00
|
|
|
elem.classList.add('gridLinkLabel');
|
|
|
|
|
elem.classList.add('grid-' + id);
|
2019-07-10 22:43:29 +00:00
|
|
|
elem.innerText = label;
|
|
|
|
|
elem.style.gridColumn = pos[0] + 1;
|
|
|
|
|
elem.style.gridRow = pos[1] + 1;
|
|
|
|
|
this.toSize_.push(elem);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 05:12:08 +00:00
|
|
|
drawNode(id, label, pos, highlight) {
|
2019-07-10 22:43:29 +00:00
|
|
|
let node = document.createElement('div');
|
|
|
|
|
this.container_.appendChild(node);
|
2019-07-13 22:30:37 +00:00
|
|
|
node.classList.add('gridNode');
|
|
|
|
|
node.classList.add('grid-' + id);
|
2019-07-10 22:43:29 +00:00
|
|
|
node.innerText = label;
|
2019-07-10 23:19:39 +00:00
|
|
|
node.classList.toggle('highlight', highlight);
|
2019-07-10 22:43:29 +00:00
|
|
|
node.style.gridColumn = pos[0] + 1;
|
|
|
|
|
node.style.gridRow = pos[1] + 1;
|
|
|
|
|
this.toSize_.push(node);
|
2019-07-11 05:12:08 +00:00
|
|
|
|
2019-07-13 05:06:48 +00:00
|
|
|
node.addEventListener('click', () => {
|
2019-07-11 05:12:08 +00:00
|
|
|
let editorElem = document.getElementById(id);
|
|
|
|
|
editorElem.classList.toggle('highlight');
|
|
|
|
|
editorElem.setAttribute('data-arch-refresh', '');
|
|
|
|
|
});
|
2019-07-10 22:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fixSizes() {
|
|
|
|
|
for (let node of this.toSize_) {
|
|
|
|
|
node.style.fontSize = null;
|
|
|
|
|
for (let size = 30;
|
|
|
|
|
size && (node.scrollWidth > node.clientWidth ||
|
|
|
|
|
node.scrollHeight > node.clientHeight);
|
|
|
|
|
--size) {
|
|
|
|
|
node.style.fontSize = size + 'px';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-10 22:18:00 +00:00
|
|
|
}
|