-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathdot.js
366 lines (340 loc) · 13.7 KB
/
dot.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { Graphviz } from "@hpcc-js/wasm/graphviz";
import * as d3 from "d3-selection";
import {extractAllElementsData, extractElementData, createElementWithAttributes} from "./element.js";
import {convertToPathData} from "./svg.js";
import {pathTweenPoints} from "./tweening.js";
import {isEdgeElement} from "./data.js";
import {getEdgeTitle} from "./data.js";
export function initViz() {
// force JIT compilation of @hpcc-js/wasm
try {
Graphviz.load().then(graphviz => {
graphviz.layout("", "svg", "dot");
this.layoutSync = graphviz.layout.bind(graphviz);
if (this._worker == null) {
this._dispatch.call("initEnd", this);
}
if (this._afterInit) {
this._afterInit();
}
});
// after the port to ESM modules, we don't know how to trigger this so
// we just disable it from coverage
/* c8 ignore start */
} catch(error) {
// we end up here when the the script tag type used to load
// the "@hpcc-js/wasm" script is not "application/javascript"
// or "text/javascript", but typically "javascript/worker". In
// this case the browser does not load the script since it's
// unnecessary because it's loaded by the web worker
// instead. This is expected so we just ignore the error.
}
/* c8 ignore stop */
if (this._worker != null) {
var vizURL = this._vizURL;
var graphvizInstance = this;
this._workerPort.onmessage = function(event) {
var callback = graphvizInstance._workerCallbacks.shift();
callback.call(graphvizInstance, event);
}
if (!vizURL.match(/^https?:\/\/|^\/\//i)) {
// Local URL. Prepend with local domain to be usable in web worker
vizURL = (new window.URL(vizURL, document.location.href)).href;
}
postMessage.call(this, {type: "layout", dot: "", engine: 'dot', vizURL: vizURL}, function (event) {
switch (event.data.type) {
case "init":
break;
}
});
postMessage.call(this, { type: "version" }, function (event) {
switch (event.data.type) {
case "version":
graphvizInstance._graphvizVersion = event.data.version;
graphvizInstance._dispatch.call("initEnd", this);
break;
}
});
}
}
function postMessage(message, callback) {
this._workerCallbacks.push(callback);
this._workerPort.postMessage(message);
}
export function layout(src, engine, vizOptions, callback) {
if (this._worker) {
postMessage.call(this, {
type: "layout",
dot: src,
engine: engine,
options: vizOptions,
}, function (event) {
callback.call(this, event.data);
});
} else {
try {
var svgDoc = this.layoutSync(src, "svg", engine, vizOptions);
callback.call(this, {type: 'done', svg: svgDoc});
}
catch(error) {
callback.call(this, {type: 'error', error: error.message});
}
}
}
export default function(src, callback) {
var graphvizInstance = this;
var worker = this._worker;
var engine = this._options.engine;
var images = this._images;
this._dispatch.call("start", this);
this._busy = true;
this._dispatch.call("layoutStart", this);
var vizOptions = {
images: images,
};
if (!this._worker && this.layoutSync == null) {
this._afterInit = this.dot.bind(this, src, callback);
return this;
}
this.layout(src, engine, vizOptions, function (data) {
switch (data.type) {
case "error":
if (graphvizInstance._onerror) {
graphvizInstance._onerror(data.error);
} else {
throw data.error.message
}
break;
case "done":
var svgDoc = data.svg;
layoutDone.call(this, svgDoc, callback);
break;
}
});
return this;
};
function layoutDone(svgDoc, callback) {
var keyMode = this._options.keyMode;
var tweenPaths = this._options.tweenPaths;
var tweenShapes = this._options.tweenShapes;
if (typeof this._options.tweenPrecision == 'string' && this._options.tweenPrecision.includes('%')) {
var tweenPrecision = +this._options.tweenPrecision.split('%')[0] / 100;
var tweenPrecisionIsRelative = this._options.tweenPrecision.includes('%');
} else {
var tweenPrecision = this._options.tweenPrecision;
var tweenPrecisionIsRelative = false;
}
var growEnteringEdges = this._options.growEnteringEdges;
var dictionary = {};
var prevDictionary = this._dictionary || {};
var nodeDictionary = {};
var prevNodeDictionary = this._nodeDictionary || {};
function setKey(datum, index) {
var tag = datum.tag;
if (keyMode == 'index') {
datum.key = index;
} else if (tag[0] != '#') {
if (keyMode == 'id') {
datum.key = datum.attributes.id;
} else if (keyMode == 'title') {
var title = datum.children.find(function (childData) {
return childData.tag == 'title';
});
if (title) {
if (title.children.length > 0) {
datum.key = title.children[0].text;
} else {
datum.key = '';
}
}
}
}
if (datum.key == null) {
if (tweenShapes) {
if (tag == 'ellipse' || tag == 'polygon') {
tag = 'path';
}
}
datum.key = tag + '-' + index;
}
}
function setId(datum, parentData) {
var id = (parentData ? parentData.id + '.' : '') + datum.key;
datum.id = id;
}
function addToDictionary(datum) {
dictionary[datum.id] = datum;
}
function calculateAlternativeShapeData(datum, prevDatum) {
if (tweenShapes && datum.id in prevDictionary) {
if ((prevDatum.tag == 'polygon' || prevDatum.tag == 'ellipse' || prevDatum.tag == 'path') && (prevDatum.tag != datum.tag || datum.tag == 'polygon')) {
if (prevDatum.tag != 'path') {
datum.alternativeOld = convertToPathData(prevDatum, datum);
}
if (datum.tag != 'path') {
datum.alternativeNew = convertToPathData(datum, prevDatum);
}
}
}
}
function calculatePathTweenPoints(datum, prevDatum) {
if (tweenPaths && prevDatum && (prevDatum.tag == 'path' || (datum.alternativeOld && datum.alternativeOld.tag == 'path'))) {
var attribute_d = (datum.alternativeNew || datum).attributes.d;
if (datum.alternativeOld) {
var oldNode = createElementWithAttributes(datum.alternativeOld);
} else {
var oldNode = createElementWithAttributes(prevDatum);
}
(datum.alternativeOld || (datum.alternativeOld = {})).points = pathTweenPoints(oldNode, attribute_d, tweenPrecision, tweenPrecisionIsRelative);
}
}
function postProcessDataPass1Local(datum, index=0, parentData) {
setKey(datum, index);
setId(datum, parentData);
var id = datum.id;
var prevDatum = prevDictionary[id];
addToDictionary(datum);
calculateAlternativeShapeData(datum, prevDatum);
calculatePathTweenPoints(datum, prevDatum);
var childTagIndexes = {};
datum.children.forEach(function (childData) {
var childTag = childData.tag;
if (childTag == 'ellipse' || childTag == 'polygon') {
childTag = 'path';
}
if (childTagIndexes[childTag] == null) {
childTagIndexes[childTag] = 0;
}
var childIndex = childTagIndexes[childTag]++;
postProcessDataPass1Local(childData, childIndex, datum);
});
}
function addToNodeDictionary(datum) {
var tag = datum.tag;
if (growEnteringEdges && datum.parent) {
if (datum.parent.attributes.class == 'node') {
if (tag == 'title') {
if (datum.children.length > 0) {
var child = datum.children[0];
var nodeId = child.text;
} else {
var nodeId = '';
}
nodeDictionary[nodeId] = datum.parent;
}
}
}
}
function extractGrowingEdgesData(datum) {
var id = datum.id;
var tag = datum.tag;
var prevDatum = prevDictionary[id];
if (growEnteringEdges && !prevDatum && datum.parent) {
if (isEdgeElement(datum)) {
if (tag == 'path' || tag == 'polygon') {
if (tag == 'polygon') {
var path = datum.parent.children.find(function (e) {
return e.tag == 'path';
});
if (path) {
datum.totalLength = path.totalLength;
}
}
var title = getEdgeTitle(datum);
var child = title.children[0];
var nodeIds = child.text.split('->');
if (nodeIds.length != 2) {
nodeIds = child.text.split('--');
}
var startNodeId = nodeIds[0];
var startNode = nodeDictionary[startNodeId];
if (Object.hasOwn(prevNodeDictionary, startNodeId)) {
var prevStartNode = prevNodeDictionary[startNodeId];
var i = startNode.children.findIndex(function (element, index) {
return element.tag == 'g';
});
if (i >= 0) {
var j = startNode.children[i].children.findIndex(function (element, index) {
return element.tag == 'a';
});
startNode = startNode.children[i].children[j];
}
var i = prevStartNode.children.findIndex(function (element, index) {
return element.tag == 'g';
});
if (i >= 0) {
var j = prevStartNode.children[i].children.findIndex(function (element, index) {
return element.tag == 'a';
});
prevStartNode = prevStartNode.children[i].children[j];
}
var startShapes = startNode.children;
for (var i = 0; i < startShapes.length; i++) {
if (startShapes[i].tag == 'polygon' || startShapes[i].tag == 'ellipse' || startShapes[i].tag == 'path' || startShapes[i].tag == 'text') {
var startShape = startShapes[i];
break;
}
}
var prevStartShapes = prevStartNode.children;
for (var i = 0; i < prevStartShapes.length; i++) {
if (prevStartShapes[i].tag == 'polygon' || prevStartShapes[i].tag == 'ellipse' || prevStartShapes[i].tag == 'path' || prevStartShapes[i].tag == 'text') {
var prevStartShape = prevStartShapes[i];
break;
}
}
if (prevStartShape && startShape) {
datum.offset = {
x: prevStartShape.center.x - startShape.center.x,
y: prevStartShape.center.y - startShape.center.y,
}
} else {
datum.offset = {x: 0, y: 0};
}
}
}
}
}
}
function postProcessDataPass2Global(datum) {
addToNodeDictionary(datum);
extractGrowingEdgesData(datum);
datum.children.forEach(function (childData) {
postProcessDataPass2Global(childData);
});
}
this._dispatch.call("layoutEnd", this);
var newDoc = d3.select(document.createDocumentFragment())
.append('div');
var parser = new window.DOMParser();
var doc = parser.parseFromString(svgDoc, "image/svg+xml");
newDoc
.append(function() {
return doc.documentElement;
});
var newSvg = newDoc
.select('svg');
var data = extractAllElementsData(newSvg);
this._dispatch.call('dataExtractEnd', this);
postProcessDataPass1Local(data);
this._dispatch.call('dataProcessPass1End', this);
postProcessDataPass2Global(data);
this._dispatch.call('dataProcessPass2End', this);
this._data = data;
this._dictionary = dictionary;
this._nodeDictionary = nodeDictionary;
this._extractData = function (element, childIndex, parentData) {
var data = extractAllElementsData(element);
postProcessDataPass1Local(data, childIndex, parentData);
postProcessDataPass2Global(data);
return data;
}
this._busy = false;
this._dispatch.call('dataProcessEnd', this);
if (callback) {
callback.call(this);
}
if (this._queue.length > 0) {
var job = this._queue.shift();
job.call(this);
}
}