This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
microViz.js
155 lines (140 loc) · 6.42 KB
/
microViz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
function renderMicroViz(env) {
let microVizEvents;
if (env.callerEnv) {
microVizEvents = new MicroVizEvents(globalEnv.programOrSendEvent, globalEnv.sourceLoc);
microVizEvents.eventGroups = [new LocalEventGroup(env.microVizEvents)];
} else {
microVizEvents = env.microVizEvents;
}
microVizDiv.innerHTML = '';
//microVizDiv.appendChild(d('model', {}, renderModel(microVizEvents)));
microVizDiv.appendChild(renderProgram());
microVizDiv.appendChild(renderMicroViz(microVizEvents));
fixHeights();
function renderProgram() {
return d('program', {}, ...lines.map((line, idx) =>
d('line', {startLine: idx + 1, endLine: idx + 1},
d('number', {}, '' + (idx + 1)),
d('text', {}, line))));
}
function renderMicroViz(thing, sourceLoc = thing.sourceLoc, cssClass = '') {
const attributes =
{startLine: sourceLoc.startLineNumber, endLine: sourceLoc.endLineNumber, class: cssClass};
if (thing instanceof Event) {
return d('event', attributes, thing.toMicroVizString());
} else if (thing instanceof MicroVizEvents && thing.eventGroups.length > 0) {
if (thing.eventGroups.length > 1) {
attributes.loopy = true;
}
return d('send', attributes, ...thing.eventGroups.map(eg => renderMicroViz(eg, sourceLoc)));
} else if (thing instanceof MicroVizEvents && thing.eventGroups.length === 0) {
attributes.empty = true;
return d('send', attributes, d('remoteEventGroup', attributes,
d('emptySendDot', {}, '\u00b7')));
} else if (thing instanceof LocalEventGroup) {
let lastPopulatedLineNumber = sourceLoc.startLineNumber - 1;
const children = [];
let lastEventNode = null;
thing.events.forEach((event, idx, events) => {
const lastEvent = events[idx - 1];
if (event.sourceLoc.startLineNumber > lastPopulatedLineNumber) {
range(lastPopulatedLineNumber + 1, event.sourceLoc.startLineNumber - 1).
forEach(lineNumber => children.push(mkSpacer(lineNumber)));
if (lastEventNode) {
lastEventNode.classList.add('lastInLine');
}
lastEventNode = renderMicroViz(event, event.sourceLoc, 'firstInLine');
lastPopulatedLineNumber = event.sourceLoc.endLineNumber;
children.push(lastEventNode);
} else if (event.sourceLoc.startLineNumber === startLine(children[children.length - 1])) {
lastEventNode = renderMicroViz(event, event.sourceLoc);
lastPopulatedLineNumber =
Math.max(lastPopulatedLineNumber, event.sourceLoc.endLineNumber);
children.push(lastEventNode);
} else if (event.sourceLoc.startLineNumber > startLine(children[children.length - 1])) {
lastEventNode = renderMicroViz(event, event.sourceLoc);
lastEventNode.style.clear = 'left';
lastPopulatedLineNumber =
Math.max(lastPopulatedLineNumber, event.sourceLoc.endLineNumber);
const spacers =
range(startLine(children[children.length - 1]), event.sourceLoc.startLineNumber - 1).
map(mkSpacer);
children.push(mkWrapper(...spacers, lastEventNode));
} else if (event.sourceLoc.startLineNumber < startLine(children[children.length - 1])) {
const nodesToWrap = [];
while (event.sourceLoc.startLineNumber <= startLine(children[children.length - 1])) {
nodesToWrap.unshift(children.pop());
}
children.push(mkWrapper(...nodesToWrap));
/* same as case above */
lastEventNode = renderMicroViz(event, event.sourceLoc);
lastEventNode.style.clear = 'left';
lastPopulatedLineNumber =
Math.max(lastPopulatedLineNumber, event.sourceLoc.endLineNumber);
const spacers =
range(startLine(children[children.length - 1]), event.sourceLoc.tartLineNumber - 1).
map(mkSpacer);
children.push(mkWrapper(...spacers, lastEventNode));
} else {
throw new Error('impossible!');
}
});
if (lastEventNode) {
lastEventNode.classList.add('lastInLine');
}
while (lastPopulatedLineNumber < sourceLoc.endLineNumber) {
lastPopulatedLineNumber++;
children.push(mkSpacer(lastPopulatedLineNumber));
}
return d('localEventGroup', {}, ...children);
} else if (thing instanceof RemoteEventGroup) {
return d('remoteEventGroup', attributes,
...thing.events.map(e => renderMicroViz(e, sourceLoc, 'remote firstInLine lastInLine')));
} else {
throw new Error('not sure how to renderMicroViz ' + JSON.stringify(thing));
}
return node;
}
function startLine(node) {
return parseInt(node.getAttribute('startLine'));
}
function endLine(node) {
return parseInt(node.getAttribute('endLine'));
}
function mkSpacer(lineNumber) {
return d('spacer', {startLine: lineNumber, endLine: lineNumber});
}
function mkWrapper(...nodes) {
const wrapper = d('wrapper',
{ startLine: startLine(nodes[0]), endLine: endLine(nodes[nodes.length - 1]) },
...nodes);
if (nodes[0].classList.contains('firstInLine')) {
wrapper.classList.add('firstInLine');
}
if (nodes[nodes.length - 1].classList.contains('lastInLine')) {
wrapper.classList.add('lastInLine');
}
return wrapper;
}
function fixHeights() {
const $ = document.querySelector.bind(document);
const $$ = s => [].slice.call(document.querySelectorAll(s));
for (let lineNumber = 1; lineNumber <= lines.length; lineNumber++) {
const bottom = $$('#microVizDiv *[endLine="' + lineNumber + '"]').
map(element => element.getBoundingClientRect().bottom).
reduce((x, y) => Math.max(x, y));
const line = $('#microVizDiv program line[startLine="' + lineNumber + '"]');
inflate(line, bottom);
const spacers = $$('#microVizDiv spacer[endLine="' + lineNumber + '"]');
spacers.forEach(spacer => inflate(spacer, bottom));
const localEvents = $$('#microVizDiv event[endLine="' + lineNumber + '"]:not(.remote)');
localEvents.forEach(event => inflate(event, bottom));
const remoteEventGroups = $$('#microVizDiv remoteEventGroup[endLine="' + lineNumber + '"]');
remoteEventGroups.forEach(remoteEventGroup => inflate(remoteEventGroup, bottom));
}
}
function inflate(element, bottomY) {
element.style.paddingBottom = bottomY - element.getBoundingClientRect().bottom;
}
}