-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-dependency-force-layout.js
325 lines (303 loc) · 11.7 KB
/
go-dependency-force-layout.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
var max_x = 0;
var max_y = 0;
var drawGraph = function(graph) {
var width = window.innerWidth,
height = window.innerHeight*0.96,
radius = 70,
color = d3.scale.category20();
var force = d3.layout.force()
.charge(-300)
.linkDistance(100)
.size([width, height]);
var svg = d3.select("#graph").append("svg")
var group = svg
.attr("width", width)
.attr("height", height)
.attr("preserveAspectRatio", 'meet')
.attr("viewBox", '0 0 '+width+' '+height)
.append('g')
force
.nodes(graph.nodes)
.links(graph.links)
.start();
group.append("svg:defs").selectAll("marker")
.data(["arrow"])
.enter()
.append("marker")
.attr("id", function(d) { return d; })
.attr("viewBox", "0 -5 10 10")
.attr("refX", 10)
.attr("refY", 0)
.attr("markerWidth", 10)
.attr("markerHeight", 10)
.attr("orient", "auto")
.append("path")
.style("stroke", function(d) { return "grey"; })
.style("fill", function(d) { return "grey"; })
.attr("d", "M0,-5L10,0L0,5");
var links = group.selectAll(".link")
.data(graph.links)
.enter()
.append("svg:path")
.style("fill", function(d) { return "none"; })
.style("stroke", function(d) { return "grey"; })
.attr("marker-end", function(d) { return "url(#arrow)"; });
var gnodes = group.selectAll('g.gnode')
.data(graph.nodes)
.enter()
.append('g')
.classed('gnode', true);
var node = gnodes.append("path")
.attr("d", d3.svg.symbol()
.size(function(d) { return radius+d.size*radius; })
.type(function(d) { return d.shape; })
)
.style("fill", function(d) { return color(d.group); })
.style("stroke", function(d) { return d3.rgb(color(d.group)).darker(); })
.call(force.drag);
var labels = gnodes.append("text")
.text(function(d) { return d.name; })
.attr("x", function(d) { return 10 + d.size; })
.attr("y", "3")
.attr("font-family", "sans-serif")
.attr("font-size","7pt")
.attr("fill", function(d) { return d3.rgb(color(d.group)).darker(); });
force.on("tick", function() {
links.attr('d', linkRespectingRadius);
gnodes.attr("transform", function(d) {
if ( d.x >= max_x ) { max_x = d.x;}
if ( d.y >= max_y ) { max_y = d.y;}
if ( d.x <= max_x ) { min_x = d.x;}
if ( d.y <= max_y ) { min_y = d.y;}
return 'translate(' + [d.x, d.y] + ')';
});
//force.size([max_x-min_x, max_y-min_y])
//svg.attr("viewBox", '0 0 '+(max_x-min_x)+' '+(max_y-min_y))
});
};
var GRAPH_DATA_PARAMETER='g'
var GRAPH_DATA_URL_PARAMETER='u'
if (getParameterByName('configUrl')) {
dataIsLoaded(buildGraphFromXML(loadConfigDocument()))
}
else if (getParameterByName(GRAPH_DATA_PARAMETER)) {
var decompressed = decompressFromString(getParameterByName(GRAPH_DATA_PARAMETER))
dataIsLoaded(decompressed)
}
else if (getParameterByName(GRAPH_DATA_URL_PARAMETER)) {
var script = document.createElement('script');
script.src = getParameterByName(GRAPH_DATA_URL_PARAMETER)
document.getElementsByTagName('head')[0].appendChild(script);
}
else {
dataIsLoaded(buildGraphFromXML(loadConfigDocument()))
}
function dataIsLoaded(data) {
var graph = buildD3Data(data)
clearOut("graph")
clearOut("graph-link-placeholder")
drawGraph(graph)
addControls(data)
}
function clearOut(elementId) {
var myNode = document.getElementById(elementId);
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
}
function showLabels() { return getParameterByName('h') === null }
function buildGraphFromXML (xmlDoc) {
var data = {},
nodes = [],
groups = [],
pipelineGroups=xmlDoc.getElementsByTagName("pipelines"),
templatePipelineElements=xmlDoc.getElementsByTagName("templates")[0].getElementsByTagName("pipeline")
data.p = []
data.r = {}
data.t = {}
data.k = {}
data.l = {}
data.l.links = {}
for(templateIndex=0; templateIndex < templatePipelineElements.length; templateIndex++) {
foundTemplatePipeline(templatePipelineElements[templateIndex].getAttribute('name'), nodes, data)
}
for(groupIndex=0; groupIndex < pipelineGroups.length; groupIndex++) {
var pipelineGroup = pipelineGroups[groupIndex],
groupName = pipelineGroup.getAttribute('group'),
pipelineElements = pipelineGroup.getElementsByTagName('pipeline')
foundGroup(groupName, groups)
for(var pipelineIndex=0; pipelineIndex < pipelineElements.length; pipelineIndex++) {
var pipeline = pipelineElements[pipelineIndex],
pipelineName=pipeline.getAttribute('name')
if (pipelineName) {
var pipelineNodeIndex = foundPipeline(pipelineName, groupName, nodes, groups, data)
if (pipeline.getAttribute('template')) {
var templateNodeIndex = foundTemplatePipeline(pipeline.getAttribute('template'), nodes, data)
linkFrom(pipelineNodeIndex, templateNodeIndex, data)
}
var materialsTag = pipeline.getElementsByTagName('materials')[0]
if (materialsTag) {
var materials=materialsTag.children
for (materialIndex=0; materialIndex < materials.length; materialIndex++) {
var material=materials[materialIndex],
materialName,
materialNodeIndex = -1
if (material.tagName==='pipeline') {
materialName=material.getAttribute('pipelineName')
materialNodeIndex = foundPipeline(materialName, groupName, nodes, groups, data)
}
else if (material.tagName==='package') {
var packageId = material.getAttribute('ref')
materialNodeIndex = foundPackage(packageId, nodes, data)
}
else {
materialName = material.getAttribute('url')
materialNodeIndex = foundRepo(materialName, material.tagName, nodes, data)
}
linkFrom(pipelineNodeIndex, materialNodeIndex, data)
}
}
}
}
}
return data
}
function linkFrom(sourceNodeIndex, targetNodeIndex, data) {
if (!data.l.links[targetNodeIndex]) data.l.links[targetNodeIndex]=[]
data.l.links[targetNodeIndex].push(sourceNodeIndex)
}
function foundGroup (groupName, groups) {
if (groups.indexOf(groupName) < 0) groups.push(groupName)
return groups.indexOf(groupName)
}
function foundPipeline(pipelineName, groupName, nodes, groups, data) {
var groupNumber=foundGroup(groupName, groups)
var newNodeAction = function(nodeIndex) { data.p.push([nodeIndex, groupNumber, hideLabelIfRequired(pipelineName)]) }
return indexOfNode(pipelineName, newNodeAction, nodes)
}
function foundRepo(repoUrl, repoType, nodes, data) {
var newNodeAction = function(repoNodeIndex) { data.r[repoNodeIndex]=hideLabelIfRequired(repoUrl) }
return indexOfNode(repoUrl, newNodeAction, nodes)
}
function foundTemplatePipeline(templateName, nodes, data) {
var newNodeAction = function(nodeIndex) { data.t[nodeIndex]=hideLabelIfRequired(templateName) }
return indexOfNode(templateName, newNodeAction, nodes)
}
function foundPackage(packageId, nodes, data) {
var newNodeAction = function(nodeIndex) { data.k[nodeIndex]=hideLabelIfRequired(packageId) }
return indexOfNode(packageId, newNodeAction, nodes)
}
function hideLabelIfRequired(label) { return showLabels() ? label : "" }
function indexOfNode(name, newNodeAction, nodes) {
if (nodes.indexOf(name) < 0) {
nodes.push(name)
newNodeAction(nodes.indexOf(name))
}
return nodes.indexOf(name);
}
function compressToString(object) {
return new JSZip().file('object', JSON.stringify(object)).generate({type:"base64", compression:"deflate"})
}
function decompressFromString(compressed) {
return JSON.parse(new JSZip().load(compressed, {base64: true, compression:"deflate"}).file('object').asText());
}
function buildD3Data(data) {
data.l.sizeOf = function (nodeIndex) { return data.l.links[nodeIndex] ? data.l.links[nodeIndex].length : 0 }
var graph = {'nodes': [], 'links': []}
var templates = data.t, repositories = data.r, packages = data.k, pipelines = data.p, nodeLinks = data.l
for (var index in templates) {
graph.nodes[index]={'name': templates[index], 'group': 'templates', 'shape': 'square', 'size': nodeLinks.sizeOf(index)}
}
for (var index in repositories) {
graph.nodes[index]={'name': repositories[index], 'group': 'repositories', 'shape': 'triangle-up', 'size': nodeLinks.sizeOf(index)}
}
for (var index in packages) {
graph.nodes[index]={'name': packages[index], 'group': 'packages', 'shape': 'triangle-down', 'size': nodeLinks.sizeOf(index)}
}
for (var index=0; index < pipelines.length; index++) {
var pipeline = pipelines[index]
graph.nodes[pipeline[0]]={'name': pipeline[2], 'group': pipeline[1], 'shape': 'circle', 'size': nodeLinks.sizeOf(pipeline[0])}
}
for (var target in nodeLinks.links) {
for (var sourceIndex=0; sourceIndex < nodeLinks.links[target].length; sourceIndex++) {
graph.links.push({'source': nodeLinks.links[target][sourceIndex], 'target': parseInt(target)})
}
}
return graph
}
function addControls(dependencies) {
shareLinkUrl = '?'+GRAPH_DATA_PARAMETER+'='+encodeURIComponent(compressToString(dependencies))
var labelsControl
if (showLabels()) {
if (document.location.search.length === 0) {
labelsControl = '<a href="' + document.location + '?h=true">hide labels</a>'
}
else {
labelsControl = '<a href="' + document.location + '&h=true">hide labels</a>'
}
}
else {
labelsControl = '<a href="' + String(document.location).replace(/[?&]h=true/,'') + '">show labels</a>'
}
document.getElementById('graph-link-placeholder').innerHTML += labelsControl
var CHROME_URL_LIMIT = 10000
if (shareLinkUrl.length < CHROME_URL_LIMIT) {
document.getElementById('graph-link-placeholder').innerHTML += ' <a href="'+shareLinkUrl+'">share graph</a> ';
}
else {
document.getElementById('graph-link-placeholder').innerHTML += ' too big to share. try hiding labels.';
}
}
function loadConfigDocument() {
SYNCHRONOUS=false
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp=new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
configUrl=getParameterByName("configUrl") || "sample.xml";
xmlHttp.open("GET", configUrl, SYNCHRONOUS)
xmlHttp.withCredentials = "true"; // Tells the browser to send authentication cookies.
xmlHttp.send();
return xmlHttp.responseXML;
}
function getParameterByName(name, url) {
var url = typeof url !== 'undefined' ? url : window.location.href
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
if(results == null)
return null;
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function linkRespectingRadius(d) {
// Total difference in x and y from source to target
diffX = d.target.x - d.source.x;
diffY = d.target.y - d.source.y;
// Length of path from center of source node to center of target node
pathLength = Math.sqrt((diffX * diffX) + (diffY * diffY));
radiusOfTarget=d.target.size+7
// x and y distances from center to outside edge of target node
offsetX = (diffX * radiusOfTarget) / pathLength;
offsetY = (diffY * radiusOfTarget) / pathLength;
return "M" + d.source.x + "," + d.source.y + "L" + (d.target.x - offsetX) + "," + (d.target.y - offsetY);
}
function parseXMLFromForm() {
var xml = document.getElementById('config-xml').value;
var xmlDoc
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(xml,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xml);
}
dataIsLoaded(buildGraphFromXML(xmlDoc))
}