nodes.js
5.73 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
var nodes = [];
var sensors = [];
var index = -1;
var nodeName = document.getElementById("nodeName");
var nodeIP = document.getElementById("nodeIP");
var nodeArch = document.getElementById("nodeArch");
var sensorName = document.getElementById("sensorName");
var addSensor = document.getElementById("addSensor");
var prevNode = document.getElementById("prevNode");
var allDone = document.getElementById("allDone");
var nextNode = document.getElementById("nextNode");
var addNode = document.getElementById("addNode");
var delNode = document.getElementById("delNode");
//var editNode = document.getElementById("editNode");
function displayNetwork(){
var divNetwork = document.getElementById("network");
divNetwork.innerHTML = "";
console.log(nodes.length);
var firstUl = document.createElement("UL");
firstUl.setAttribute("class", "list-group");
for(var i=0;i<nodes.length;i++){
console.log("SALUT");
var firstIl = document.createElement("IL");
firstIl.setAttribute("class", "list-group-item");
var tempText = document.createTextNode(nodes[i].name);
firstIl.appendChild(tempText);
var secondUl = document.createElement("UL");
secondUl.setAttribute("class", "list-group");
for(var j=0;j<nodes[i].sensors.length;j++){
var secondIl = document.createElement("IL");
secondIl.setAttribute("class", "list-group-item");
tempText = document.createTextNode(nodes[i].sensors[j].name);
secondIl.appendChild(tempText);
secondUl.appendChild(secondIl);
}
firstIl.appendChild(secondUl);
firstUl.appendChild(firstIl);
}
divNetwork.appendChild(firstUl);
//var container = document.getElementsByClassName("container");
//container.appendChild(divNetwork);
}
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
var name = r.parentNode.parentNode.firstElementChild.innerText;
function search(array) {
return array.name === name;
}
var idx = sensors.findIndex(search);
if (idx > -1) {
sensors.splice(idx, 1);
}
document.getElementById("sensorsTable").deleteRow(i);
}
addSensor.addEventListener('click', function() {
if(sensorName.value !== "") {
var table = document.getElementById("sensorsTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = sensorName.value;
cell2.innerHTML = "<button type=\"button\" class=\"close\" aria-label=\"Close\" onclick=\"deleteRow(this)\">\n" +
"<span aria-hidden=\"true\">×</span>\n" +
"</button>";
sensors.push({"name": sensorName.value });
sensorName.value = "";
}
})
function displayNode(index) {
clearAll();
nodeName.value = nodes[index].name;
nodeIP.value = nodes[index].ip;
nodeArch.value = nodes[index].arch;
sensors = nodes[index].sensors.slice(0);
var table = document.getElementById("sensorsTable");
for(var i = 0; i < sensors.length; i++) {
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = sensors[i].name;
cell2.innerHTML = "<button type=\"button\" class=\"close\" aria-label=\"Close\" onclick=\"deleteRow(this)\">\n" +
"<span aria-hidden=\"true\">×</span>\n" +
"</button>";
}
}
function clearAll() {
nodeName.value = "";
nodeIP.value = "";
nodeArch.value = "";
sensorName.value = "";
sensors.splice(0, sensors.length);
document.getElementById("sensorsTable").innerHTML = "";
}
addNode.addEventListener('click', function() {
var node = {
'name': nodeName.value,
'ip': nodeIP.value,
'arch': nodeArch.value,
'sensors': sensors.slice(0)
};
if(node.name === "" || node.ip === "" || node.arch === "" || sensors.length === 0) {
$("#warningConf").modal();
}
else {
nodes.push(node);
clearAll();
index = nodes.length;
}
displayNetwork();
})
editNode.addEventListener('click', function() {
console.log("edit : " + index);
if(index !== -1 && index !== nodes.length) {
nodes[index].name = nodeName.value;
nodes[index].ip = nodeIP.value;
nodes[index].arch = nodeArch.value;
nodes[index].sensors = sensors.slice(0);
clearAll();
index = nodes.length;
displayNetwork();
}
})
delNode.addEventListener('click', function() {
if(index != nodes.length) {
nodes.splice(index, 1);
clearAll();
index = nodes.length;
}
})
allDone.addEventListener('click', function() {
if(nodes.length !== 0) {
var divNetwork = document.getElementById("network");
divNetwork.innerHTML = "";
var xhr = new XMLHttpRequest();
var url = "/updatenodes";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
location.reload();
}
};
var data = JSON.stringify(nodes);
console.log(data);
xhr.send(data);
}
})
prevNode.addEventListener('click', function() {
index--;
if(index < 0) {
index = 0;
//index = nodes.length - 1;
}
if(nodes.length != 0) {
displayNode(index);
}
console.log("prev : " + index);
})
nextNode.addEventListener('click', function() {
index++;
if(index >= nodes.length) {
clearAll();
index = nodes.length;
//index = 0;
}
else if(nodes.length != 0) {
displayNode(index);
}
console.log("next : " + index);
})