forked from rwth-acis/OCD-Web-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphEdit.js
290 lines (267 loc) · 10.5 KB
/
graphEdit.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
// var visualization;
// var forceGraph;
// var initialGraph;
// var graphId;
// var lastNode;
// var lastId;
// var undirectedChecked;
// var nodeMode = true;
// var nodeSelected = false;
// var edgeFormat;
// $(document).ready(function() {
// graphId = getUrlVar("graphId");
// var graphIdQueryExtension = "";
// if(graphId) {
// graphIdQueryExtension = "&graphId=" + graphId;
// }
// initialize(graphId);
// });
// // GET the graph and execute the visualization function on that graph
// function initialize() {
// sendRequest("get", "visualization/graph/" + graphId + "/outputFormat/JSON/layout/ORGANIC", "",
// function (response) {
// initialGraph = JSON.parse(response);
// console.log(initialGraph);
// getForceVis();
// },
// /* Error handler */
// function (errorData) {
// /*
// * GraphIds request failed
// */
// showConnectionErrorMessage("Visualization was not received.", errorData);
// });
// }
// function getForceVis() {
// var nodes = initialGraph.nodes;
// var links = initialGraph.links;
// console.log("vis Links: " + links);
// if(Object.keys(links) === 0){
// edgeFormat = {style:0};
// } else {
// edgeFormat = JSON.parse(JSON.stringify(links[0]));
// }
// const elem = document.getElementById("editForceVisualizationGraphic");
// // First initialization of the Visualiuation
// // Setup of the general desing
// forceGraph = ForceGraph()(elem)
// .onNodeClick(editNode)
// .onBackgroundRightClick(addNode)
// .onLinkClick(removeLink)
// .backgroundColor("#fafafa")
// .centerAt(0,0)
// .graphData(initialGraph)
// .linkWidth(3)
// .linkDirectionalArrowLength(3)
// .linkDirectionalArrowRelPos(1)
// .d3Force("center", d3.forceCenter().strength(1))
// .d3Force("charge", d3.forceManyBody()
// .strength(-10)
// .distanceMin(0.5)
// .distanceMax(50)
// )
// // Set Dimensions and Parameters for the force engine
// forceGraph.d3Force("collide", d3.forceCollide(forceGraph.nodeRelSize()))
// .d3Force("link",d3.forceLink(forceGraph.links).distance(20))
// forceGraph.width($('.editForceVisualizationContent').width());
// forceGraph.height($('.editForceVisualizationContent').height());
// // Functions that are executed by events, e.g. left click on a button
// function addNode(e) {
// const { nodes, links } = forceGraph.graphData();
// const id = nodes.length;
// forceGraph.graphData({
// nodes: [...nodes, {"color":"rgba(204.0,204.0,255.0,1.0)","name":id,"id":id,"label":""}],
// links: links
// });
// }
// function editNode(node) {
// if(nodeMode){
// let {nodes, links} = forceGraph.graphData();
// links = links.filter(l => l.source !== node && l.target !== node);
// nodes.splice(node.id,1);
// nodes.forEach((n,idx) => {n.id = idx; });
// forceGraph.graphData({ nodes, links });
// console.log(links);
// // Select node or connect 2 selected nodes
// // Connect 2 nodes in both directions if undirected box is checked
// }else {
// // If no node is selected
// if(!nodeSelected){
// lastNode = node;
// lastId = node.id;
// node.color = "orange";
// // if the selected nod e is clicked again, unselect it
// } else if(lastNode == node){
// lastNode.color = "rgba(204.0,204.0,255.0,1.0)";
// // Connect two selected nodes with directed edge
// } else if(!undirectedChecked) {
// const{ nodes, links } = forceGraph.graphData();
// var containsEdge = false;
// var edge = JSON.parse(JSON.stringify(edgeFormat));
// edge.target = node;
// edge.source = lastNode;
// console.log(links);
// for(key in links){
// if(links[key].source.id == edge.source.id && links[key].target.id == edge.target.id){
// containsEdge = true;
// break;
// }
// }
// if(!containsEdge){
// console.log("ho");
// forceGraph.graphData({
// nodes: nodes,
// links: [...links, edge]
// })
// lastNode.color = "rgba(204.0,204.0,255.0,1.0)";
// } else {
// lastNode.color = "rgba(204.0,204.0,255.0,1.0)";
// alert("Edge already exists!");
// }
// // Connect two selected nodes with undirected edge
// } else if(undirectedChecked){
// const{ nodes, links } = forceGraph.graphData();
// var containsEdge = false;
// var secondEdge = JSON.parse(JSON.stringify(edgeFormat));
// var edge = JSON.parse(JSON.stringify(edgeFormat));
// edge.target = node;
// edge.source = lastNode;
// secondEdge.source = node;
// secondEdge.target = lastNode;
// for(key in links){
// if((links[key].source.id == edge.source.id && links[key].target.id == edge.target.id)){
// containsEdge = true;
// break;
// }
// }
// for(key in links){
// if((links[key].source.id == secondEdge.source.id && links[key].target.id == secondEdge.target.id)){
// containsEdge = true;
// break;
// }
// }
// if(!containsEdge){
// forceGraph.graphData({
// nodes: nodes,
// links: [...links, edge, secondEdge]
// })
// lastNode.color = "rgba(204.0,204.0,255.0,1.0)";
// } else {
// lastNode.color = "rgba(204.0,204.0,255.0,1.0)";
// alert("Edge already exists!");
// }
// }
// nodeSelected = !nodeSelected;
// }
// }
// }
// // remove edge
// function removeLink(link){
// if(!nodeMode){
// let { nodes, links } = forceGraph.graphData();
// for(key in links){
// if(link.source.id == links[key].source.id && link.target.id == links[key].target.id){
// links.splice(key,1);
// if(undirectedChecked){
// for(key2 in links){
// if(link.source.id == links[key2].target.id && link.target.id == links[key2].source.id){
// links.splice(key2,1);
// break;
// }
// }
// }
// }
// }
// forceGraph.graphData({ nodes, links });
// }
// }
// // remove the node
// function removeNode(node){
// let {nodes, links} = forceGraph.graphData();
// links = links.filter(l => l.source !== node && l.target !== node);
// nodes.splice(node.id,1);
// nodes.forEach((n,idx) => {n.id = idx;});
// forceGraph.graphData({ nodes, links });
// }
// // Button and Checkbox functionalities
// function updateCheckbox(){
// undirectedChecked = $("#editUndirected").is(":checked");
// if(undirectedChecked){
// forceGraph.linkDirectionalArrowLength(0);
// }else{
// forceGraph.linkDirectionalArrowLength(3);
// }
// }
// function reloadGraph(){
// if (confirm("Reload: Unsaved changes will be lost!") == true) {
// initialize();
// }
// }
// function deleteGraph(){
// if (confirm("Are you sure you want to delete your graph?") == true){
// let { nodes , links } = forceGraph.graphData();
// forceGraph.graphData({
// nodes: [],
// links: []
// })
// }
// }
// function saveGraph() {
// var content = jsonToXml();
// console.log(content);
// console.log(graphId);
// sendRequest("put","update/" + graphId, content,
// function(response) {
// showSuccess(response);
// alert("Graph saved");
// },
// function (errorData) {
// showConnectionErrorMessage("Graph update failed", errorData);
// });
// }
// function switchMode() {
// nodeMode = !nodeMode;
// if (!nodeMode) {
// $("#modeBtn").html("Edge Mode");
// }else {
// $("#modeBtn").html("Node Mode");
// }
// }
// // Shortcuts for the Buttons:
// // Switch modes by pressing N
// document.addEventListener('keydown', function(e) {
// const key = e.code;
// if(key === "KeyN"){
// switchMode();
// }
// })
// // Delete selected node by pressing DELETE
// document.addEventListener('keydown', function(e) {
// const key = e.key;
// if(key === "Delete" && nodeSelected){
// removeNode(lastNode);
// nodeSelected = false;
// console.log("pressed delete");
// }
// })
// // Translate JSON graph format to XML String
// function jsonToXml(){
// var xmlString = '<?xml version="1.0" encoding="UTF-8"?>\n<graphml xmlns="http://graphml.graphdrawing.org/xmlns">\n<key attr.name="number" attr.type="integer" for="node" id="number"/>\n<key attr.name="label" attr.type="string" for="edge" id="label"/>\n<graph edgedefault="directed">'
// let { nodes, links } = forceGraph.graphData();
// console.log(links);
// for(var key in nodes){
// xmlString += '\n<node id="' + nodes[key].id + '">';
// xmlString += '\n</node>'
// }
// for(var key in links){
// xmlString += '\n<edge source="' + links[key].source.id + '" target="' + links[key].target.id + '">';
// xmlString += '\n</edge>'
// }
// xmlString += '\n</graph>';
// xmlString += '\n</graphml>';
// return xmlString
// }
// function initCollabSession(){
// const roomNumber = uuidv4();
// window.location.href = "collaborativeEditing.html?graphId=" + graphId + "&room=" + roomNumber;
// }