-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex2.js
217 lines (194 loc) · 6.1 KB
/
index2.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
const result = document.getElementById("data-div");
document
.querySelector("#sendDefaultData")
.addEventListener("click", function (e) {
var but = document.querySelector("#sendDefaultData");
but.classList.toggle("sending");
but.blur();
// send a POST request to the server
const xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8081", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({ type: "union", content: [1, 2, 3] }));
xhr.onload = () => {
if (xhr.status === 200) {
console.log(xhr.response);
result.innerHTML = xhr.response;
result.className = "visible floating-div";
but.classList.remove("sending");
but.blur();
}
};
});
document.querySelector("#unionData").addEventListener("click", function (e) {
var but = document.querySelector("#unionData");
but.classList.toggle("sending");
but.blur();
// send a POST request to the server
let message = document.querySelector("#union_message").value;
console.log("message: ", message);
message = message
.split(",")
.map((x) => parseInt(x))
.sort((a, b) => a - b);
console.log(message);
console.log(typeof message);
const xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8081", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({ type: "union", content: message }));
xhr.onload = () => {
if (xhr.status === 200) {
console.log(xhr.response);
result.innerHTML = xhr.response;
result.className = "visible floating-div";
but.classList.remove("sending");
but.blur();
}
};
});
const networkResultContainer = document.getElementById("mynetworkresult");
const sendGraphBtn = document
.querySelector("#sendGraph")
.addEventListener("click", function (e) {
console.log("sendGraphBtn clicked");
const sendGraphBtn = document.querySelector("#sendGraph");
sendGraphBtn.classList.toggle("sending");
const edgesData = Object.entries(network.body.edges).map((edge) => {
console.log("edge : ", edge);
return {
fromId: edge[1].fromId,
toId: edge[1].toId,
label: parseInt(edge[1].labelModule.elementOptions.label),
};
});
console.log("edgesData : ", edgesData);
const xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8081", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({ type: "apsp", content: edgesData }));
xhr.onload = () => {
if (xhr.status === 200) {
console.log(xhr.response);
sendGraphBtn.classList.remove("sending");
sendGraphBtn.blur();
const responseData = JSON.parse(xhr.response);
let responseEdges = responseData.map((edge) => ({
from: edge.fromId,
to: edge.toId,
label: String(edge.label),
}));
let responseNodes = [];
responseData.forEach((edge) => {
responseNodes.push({ id: edge.fromId, label: "Node " + edge.fromId });
responseNodes.push({ id: edge.toId, label: "Node " + edge.toId });
});
// Remove duplicate nodes
responseNodes = responseNodes.filter(
(node, index, self) =>
index === self.findIndex((n) => n.id === node.id)
);
const responseNetworkData = {
nodes: new vis.DataSet(responseNodes),
edges: new vis.DataSet(responseEdges),
};
const options = {
nodes: {
color: {
background: "#ff80ff",
border: "#ffccff",
},
},
};
new vis.Network(networkResultContainer, responseNetworkData, options);
}
};
});
// create an array with nodes
var nodesArray = [
{ id: 1, label: "Node 1" },
{ id: 2, label: "Node 2" },
{ id: 3, label: "Node 3" },
];
// create an array with edges
var edgesArray = [
{ from: 1, to: 2, label: "5" },
{ from: 1, to: 3, label: "5" },
{ from: 2, to: 3, label: "1" },
];
var nodes = new vis.DataSet(nodesArray);
var edges = new vis.DataSet(edgesArray);
// create a network
var container = document.getElementById("mynetwork");
var data = {
nodes: nodes,
edges: edges,
};
var options = {};
var network = new vis.Network(container, data, options);
var lastClickedNodes = [];
network.on("click", function (params) {
if (params.nodes.length > 0) {
lastClickedNodes.unshift(params.nodes[0]);
if (lastClickedNodes.length > 2) {
lastClickedNodes.pop();
}
}
});
function addNode() {
var newNodeId = nodesArray.length + 1;
var newNode = { id: newNodeId, label: "Node " + newNodeId };
nodesArray.push(newNode);
nodes.add(newNode);
}
function addEdge() {
if (nodesArray.length > 1) {
var fromNode, toNode;
if (lastClickedNodes.length >= 2) {
fromNode = lastClickedNodes[0];
toNode = lastClickedNodes[1];
} else {
fromNode = nodesArray.length;
toNode = nodesArray.length - 1;
}
var weight = prompt("Please enter the weight for the edge", "1");
if (weight !== null) {
var newEdge = {
from: fromNode,
to: toNode,
label: weight,
};
edgesArray.push(newEdge);
edges.add(newEdge);
}
}
}
function removeNode() {
if (lastClickedNodes.length > 0) {
// Remove associated edges
var nodeId = lastClickedNodes[0];
var associatedEdges = edges.get({
filter: function (edge) {
return edge.from === nodeId || edge.to === nodeId;
},
});
associatedEdges.forEach(function (edge) {
edges.remove(edge.id);
});
// Remove the node
nodes.remove(nodeId);
// Update nodesArray
nodesArray = nodesArray.filter((node) => node.id !== nodeId);
// Remove the node from the lastClickedNodes array
lastClickedNodes = lastClickedNodes.filter((node) => node !== nodeId);
}
}
function removeEdge() {
if (edgesArray.length > 0) {
// Remove the last edge from the edges DataSet
var lastEdge = edgesArray[edgesArray.length - 1];
edges.remove(lastEdge.id);
// Update the edgesArray
edgesArray.pop();
}
}