Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (173 loc) · 6.64 KB
/
Copy pathscript.js
File metadata and controls
214 lines (173 loc) · 6.64 KB
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
document.addEventListener('DOMContentLoaded', function(){ // on dom ready
var nodes = new Promise((resolve, reject) => {
Papa.parse('/data/jhotdraw-stereotypes.csv', {
download: true, header: true, skipEmptyLines: true, complete: (res)=>resolve(transformData(res,prepNodes))});
});
var edges = new Promise((resolve, reject) => {
Papa.parse('/data/jhotdraw-connections.csv', {
download: true, header: true, skipEmptyLines: true, complete: (res)=>resolve(transformData(res,prepEdges))});
});
function transformData(es,fn) {
var f = es.data.map((e) => { return { data: e } });
if (fn) {
return fn(f);
}
return f;
}
function prepNodes(nodes) {
nodes.forEach((node) => {
var annot = node.data.abstraction ? node.data.abstraction !== "concrete" ? `«${node.data.abstraction}»\n` : '' : '';
// var names = node.data.id.split(".");
var name = node.data.id;
// node.data.label = `${annot}${names[names.length - 1]}`;
node.data.label = `${annot}${name}`;
});
return nodes;
}
function prepEdges(edges) {
return edges;
// return edges.filter((edge) => /*edge.data.conn_type === 'has'
// ||*/ edge.data.conn_type === 'inherits'
// || edge.data.conn_type === 'subpackage');
}
var toText = function(obj){ return obj.text(); };
var style = fetch('style.cycss').then(toText);
Promise.all([ nodes, edges, style ]).then(initCy);
function initCy(payload) {
// console.log(payload[0]); // nodes
// console.log(payload[1]); // edges
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
style: payload[2],
elements: {
nodes: payload[0],
edges: payload[1]
},
layout: {
name: 'cola',
directed: true,
nodeSpacing: function (node) { return 32; },
flow: { axis: 'y', minSeparation: -32 },
edgeSymDiffLength: 8,
/* for 'klay'
direction: 'DOWN',
// fixedAlignment: 'LEFTUP',
inLayerSpacingFactor: 0.5, */
}
});
const checkboxes = document.querySelectorAll('input[name="showrels"]');
checkboxes.forEach((checkbox) => {
setVisible(checkbox);
});
constraints = [];
// place subpackages below their parent packages
payload[1]
.filter((e) => ["subpackage"].includes(e.data.conn_type))
.forEach((e) => {
c = { "axis": "y", "left": cy.$id(e.data.target), "right": cy.$id(e.data.source), "gap": 128 };
constraints.push(c);
});
// place subclasses below their superclasses
payload[1]
.filter((e) => ["inherits", "realizes"].includes(e.data.conn_type))
.forEach((e) => {
c = { "axis": "y", "left": cy.$id(e.data.target), "right": cy.$id(e.data.source), "gap": 128 };
constraints.push(c);
});
// place dependants to the left of the dependency
payload[1]
.filter((e) => !["inherits", "realizes", "subpackage"].includes(e.data.conn_type))
.forEach((e) => {
c = { "axis": "x", "left": cy.$id(e.data.source), "right": cy.$id(e.data.target), "gap": 128 };
constraints.push(c);
});
// console.log(constraints);
cy.layout({
name: 'cola', animate: true,
directed: true,
nodeSpacing: function (node) { return 32; },
flow: { axis: 'y', minSeparation: -32 },
edgeSymDiffLength: 8,
gapInequalities: constraints
}).run();
bindRouters();
}
// const layoutConfig = {
// name: "cola",
// handleDisconnected: true,
// animate: true,
// avoidOverlap: false,
// infinite: false,
// unconstrIter: 1,
// userConstIter: 0,
// allConstIter: 1,
// ready: e => {
// e.cy.fit()
// e.cy.center()
// }
// }
// let nodeid = 1;
function bindRouters() {
cy.on('tap', 'node', evt => {
// cy.nodes().forEach(node => {
// node.lock();
// });
// const currentNodeId = nodeid++;
// const targetId = evt.target.data('id'); //cy.nodes()[Math.floor(Math.random() * cy.nodes().length)].data('id')
// cy.add([
// {
// group: 'nodes',
// data: {
// id: currentNodeId
// }
// },
// {
// group: 'edges',
// data: {
// id: currentNodeId + '-edge',
// source: currentNodeId,
// target: targetId
// }
// }
// ]);
// const layout = cy.makeLayout(layoutConfig);
// layout.run();
// layout.on("layoutstop", () => {
// cy.nodes().forEach(node => {
// node.unlock();
// })
// })
});
}
}); // on dom ready
var saveAsSvg = function (filename) {
var svgContent = cy.svg({scale: 1, full: true, bg: 'beige'});
var blob = new Blob([svgContent], {type:"image/svg+xml;charset=utf-8"});
saveAs(blob, "class-diagram.svg");
};
var getSvgUrl = function () {
var svgContent = cy.svg({scale: 1, full: true, bg: 'beige'});
var blob = new Blob([svgContent], {type:"image/svg+xml;charset=utf-8"});
var url = URL.createObjectURL(blob);
return url;
};
var setVisible = function (ele) {
cy.edges('[conn_type = "' + ele.value + '"]').style("display", ele.checked ? "element" : "none");
};
var setLineBends = function (ele) {
console.log(ele.name);
if (ele.checked) {
cy.edges('[conn_type = "' + ele.name + '"]').style("curve-style", ele.value);
}
};
var relayout = function (layout) {
console.log(layout);
cy.layout({
name: layout, animate: true,
directed: true,
nodeSpacing: function (node) { return 32; },
flow: { axis: 'y', minSeparation: -32 },
edgeSymDiffLength: 8,
gapInequalities: constraints
}).run();
};