-
Notifications
You must be signed in to change notification settings - Fork 104
/
graphviz.js
216 lines (213 loc) · 7.2 KB
/
graphviz.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
import * as d3 from "d3-selection";
import {dispatch} from "d3-dispatch";
import render from "./render.js";
import graphvizVersion from "./graphvizVersion.js";
import {layout} from "./dot.js";
import dot from "./dot.js";
import data from "./data.js";
import {initViz} from "./dot.js";
import renderDot from "./renderDot.js";
import transition from "./transition.js";
import {active} from "./transition.js";
import options from "./options.js";
import width from "./width.js";
import height from "./height.js";
import scale from "./scale.js";
import fit from "./fit.js";
import attributer from "./attributer.js";
import engine from "./engine.js";
import images from "./images.js";
import keyMode from "./keyMode.js";
import fade from "./fade.js";
import tweenPaths from "./tweenPaths.js";
import tweenShapes from "./tweenShapes.js";
import convertEqualSidedPolygons from "./convertEqualSidedPolygons.js";
import tweenPrecision from "./tweenPrecision.js";
import growEnteringEdges from "./growEnteringEdges.js";
import zoom from "./zoom.js";
import {resetZoom} from "./zoom.js";
import {zoomBehavior} from "./zoom.js";
import {zoomSelection} from "./zoom.js";
import {zoomScaleExtent} from "./zoom.js";
import {zoomTranslateExtent} from "./zoom.js";
import on from "./on.js";
import onerror from "./onerror.js";
import logEvents from "./logEvents.js";
import destroy from "./destroy.js";
import {drawEdge} from "./drawEdge.js";
import {updateDrawnEdge} from "./drawEdge.js";
import {moveDrawnEdgeEndPoint} from "./drawEdge.js";
import {insertDrawnEdge} from "./drawEdge.js";
import {removeDrawnEdge} from "./drawEdge.js";
import {drawnEdgeSelection} from "./drawEdge.js";
import {drawNode} from "./drawNode.js";
import {updateDrawnNode} from "./drawNode.js";
import {moveDrawnNode} from "./drawNode.js";
import {insertDrawnNode} from "./drawNode.js";
import {removeDrawnNode} from "./drawNode.js";
import {drawnNodeSelection} from "./drawNode.js";
import {workerCode} from "./workerCode.js";
import {sharedWorkerCode} from "./workerCode.js";
import {workerCodeBody} from "./workerCode.js";
import {Graphviz as hpccWasmGraphviz} from "@hpcc-js/wasm/graphviz";
export function Graphviz(selection, options) {
this._options = {
useWorker: true,
useSharedWorker: false,
engine: 'dot',
keyMode: 'title',
fade: true,
tweenPaths: true,
tweenShapes: true,
convertEqualSidedPolygons: true,
tweenPrecision: 1,
growEnteringEdges: true,
zoom: true,
zoomScaleExtent: [0.1, 10],
zoomTranslateExtent: [[-Infinity, -Infinity], [+Infinity, +Infinity]],
width: null,
height: null,
scale: 1,
fit: false,
};
if (options instanceof Object) {
for (var option of Object.keys(options)) {
this._options[option] = options[option];
}
} else if (typeof options == 'boolean') {
this._options.useWorker = options;
}
var useWorker = this._options.useWorker;
var useSharedWorker = this._options.useSharedWorker;
if (typeof Worker == 'undefined') {
useWorker = false;
}
if (typeof SharedWorker == 'undefined') {
useSharedWorker = false;
}
if (useWorker || useSharedWorker) {
var scripts = d3.selectAll('script');
var vizScript = scripts.filter(function() {
return d3.select(this).attr('type') == 'javascript/worker' || (d3.select(this).attr('src') && d3.select(this).attr('src').match(/.*\/@hpcc-js\/wasm/));
});
if (vizScript.size() == 0) {
console.warn('No script tag of type "javascript/worker" was found and "useWorker" is true. Not using web worker.');
useWorker = false;
useSharedWorker = false;
} else {
this._vizURL = vizScript.attr('src');
if (!this._vizURL) {
console.warn('No "src" attribute of was found on the "javascript/worker" script tag and "useWorker" is true. Not using web worker.');
useWorker = false;
useSharedWorker = false;
}
}
}
if (useSharedWorker) {
const url = 'data:application/javascript;base64,' + btoa(workerCodeBody.toString() + '(' + sharedWorkerCode.toString() + ')()');
this._worker = this._worker = new SharedWorker(url);
this._workerPort = this._worker.port;
this._workerPortClose = this._worker.port.close.bind(this._workerPort);
this._worker.port.start();
this._workerCallbacks = [];
}
else if (useWorker) {
var blob = new Blob([workerCodeBody.toString() + '(' + workerCode.toString() + ')()']);
var blobURL = window.URL.createObjectURL(blob);
this._worker = new Worker(blobURL);
this._workerPort = this._worker;
this._workerPortClose = this._worker.terminate.bind(this._worker);
this._workerCallbacks = [];
} else {
hpccWasmGraphviz.load().then(((graphviz) => {
this._graphvizVersion = graphviz.version();
}).bind(this));
}
this._selection = selection;
this._active = false;
this._busy = false;
this._jobs = [];
this._queue = [];
this._keyModes = new Set([
'title',
'id',
'tag-index',
'index'
]);
this._images = [];
this._translation = undefined;
this._scale = undefined;
this._rotation = undefined;
this._eventTypes = [
'initEnd',
'start',
'layoutStart',
'layoutEnd',
'dataExtractEnd',
'dataProcessPass1End',
'dataProcessPass2End',
'dataProcessEnd',
'renderStart',
'renderEnd',
'transitionStart',
'transitionEnd',
'restoreEnd',
'end',
'zoom'
];
this._dispatch = dispatch(...this._eventTypes);
initViz.call(this);
selection.node().__graphviz__ = this;
}
export default function graphviz(selector, options) {
var g = d3.select(selector).graphviz(options);
return g;
}
Graphviz.prototype = graphviz.prototype = {
constructor: Graphviz,
engine: engine,
addImage: images,
keyMode: keyMode,
fade: fade,
tweenPaths: tweenPaths,
tweenShapes: tweenShapes,
convertEqualSidedPolygons: convertEqualSidedPolygons,
tweenPrecision: tweenPrecision,
growEnteringEdges: growEnteringEdges,
zoom: zoom,
resetZoom: resetZoom,
zoomBehavior: zoomBehavior,
zoomSelection: zoomSelection,
zoomScaleExtent: zoomScaleExtent,
zoomTranslateExtent: zoomTranslateExtent,
render: render,
layout: layout,
dot: dot,
data: data,
renderDot: renderDot,
transition: transition,
active: active,
options: options,
width: width,
height: height,
scale: scale,
fit: fit,
attributer: attributer,
on: on,
onerror: onerror,
logEvents: logEvents,
destroy: destroy,
drawEdge: drawEdge,
updateDrawnEdge: updateDrawnEdge,
moveDrawnEdgeEndPoint,
insertDrawnEdge,
removeDrawnEdge, removeDrawnEdge,
drawnEdgeSelection, drawnEdgeSelection,
drawNode: drawNode,
updateDrawnNode: updateDrawnNode,
moveDrawnNode: moveDrawnNode,
insertDrawnNode,
removeDrawnNode, removeDrawnNode,
drawnNodeSelection, drawnNodeSelection,
graphvizVersion: graphvizVersion,
};