-
Notifications
You must be signed in to change notification settings - Fork 2
/
device.js
344 lines (305 loc) · 11.3 KB
/
device.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Device prototype
// ----------------
function Device(name, type) {
this.name = name;
this.type = type;
this.size = {width: 200, height: 200};
this.id = Device.count++;
this.expanded = true;
this.ip = "192.168.0." + (this.id+1);
this.static_ip = false;
this.connections = [];
this.n_children = 0;
this.anchor = {
"left": ($('#container').width() / 2 - this.size.width / 2),
"top": ($('#container').height() / 2 - this.size.height / 2)
}
// Physics properties
this.velocity = [0,0];
this.mass = 1;
this.target = [0,0];
// Set content of device settings panel for Accordion Grid layout.
this.advanced_accordion_settings = getDeviceAccordionHTML( this );
// Set content of device settings panel for Grid layout.
this.advanced_settings = getDevicePanelHTML( this );
// for routers
this.router_visibility = true;
// for grid layout wireless icon
if (this.type ==="wireless_network") this.password = "myP@$$w0rd!";
this.addToDom();
}
Device.count = 0;
Device.prototype = {
addToDom: function() {
// Create an element and add it to the DOM
this.el = $("<div><div class='badge'>0</div><div class='icon'></div><div class='info'><div class='name'>" + this.name + "</div><div class='status'></div></div></div>");
this.el.addClass("device " + this.type + " invisible") // Start hidden
this.el.attr("id", "device_" + this.id);
this.el.offset(this.anchor);
$("#container").append(this.el);
var thisthis = this;
this.el.find(".name").click(function() { thisthis.editName(true);});
// For a Wireless network icon (only used in grid layout) do this. NB: not to confuse with other wireless device type "wifi" used in physics layout.
if (this.type === "wireless_network") {
this.el.find(".name").after("<div class='password'>"+ this.password +"</div>");
this.el.find(".password").click(function() { thisthis.editPassword(true);});
}
},
// to show or hide details of a device in hovering pane on click
showDetails: function (b_show) {
if ( b_show === true ) {
// If panel exists already for another device, then remove it first.
var current_panel = $(".device_advanced_panel");
if ( current_panel.length ) {
var current_panel_id = current_panel.attr("id");
var current_panel_device = $.grep(devices, function(device){ return device.id == current_panel_id; });
current_panel_device[0].showDetails(false);
}
var new_panel = $("<div />").addClass("device_advanced_panel " + this.type); // attach device name to as class so we know what device the panel is for.
new_panel.attr("id", this.id);
thisthis = this;
var padding_topAndBottom = 20;
var padding_leftAndRight = 50;
// Bug fix found online: must wait 1 clock cycle (accomplished by the 0ms delay here) so that jQuery can succesfully retrieve the width of this newly created element. Otherwise it returns zero.
setTimeout(function(){
new_panel.css({
"top": thisthis.el.offset().top - $("#container").offset().top,// - padding_topAndBottom,
"left": ( (thisthis.el.offset().left - $("#container").offset().left) + thisthis.el.width()/2 - new_panel.width()/2 ) - padding_leftAndRight,
// "padding-top": padding_topAndBottom,
// "padding-bottom": padding_topAndBottom,
"padding-left": padding_leftAndRight,
"padding-right": padding_leftAndRight
});
// Addtional CSS for the Wireless Network device.
if ( thisthis.type === "wireless_network" ) {
new_panel.css({
"top": thisthis.el.offset().top - $("#container").offset().top - padding_topAndBottom,
"padding-top": padding_topAndBottom,
"padding-bottom": padding_topAndBottom
});
}
},0);
new_panel.html(this.advanced_settings);
new_panel.click(function() { thisthis.showDetails(false);});
new_panel.prependTo("#container");
new_panel.fadeIn( 400 );
} else if ( b_show === false ) {
var current_panel = $(".device_advanced_panel");
current_panel.fadeOut( 400, function() {
current_panel.remove();
} );
}
},
showName: function (b_show) {
if ( b_show === true ) {
this.el.find(".name").show();
} else {
this.el.find(".name").hide();
}
},
moveTo: function(pos) {
},
show: function() {
this.el.fadeIn(500);
},
hide: function() {
this.el.fadeOut(500);
},
changeType: function(t) {
var thisthis = this;
var oldType = this.type;
this.el.find(".icon").fadeOut({duration: 200, complete: function() {
thisthis.el.removeClass(oldType);
thisthis.el.addClass(t);
thisthis.el.find(".icon").fadeIn({duration: 200});
thisthis.type = t;
}});
// For Accordion Grid layout
if ( $(".accordion").length ) {
if (this.is_wireless) {
var this_header = $("#wireless_accordion .accordion").find($("#wireless_accordion .accordion").accordion( "option", "header" ))
.eq(this.id_accordion);
this_header.removeClass(oldType);
this_header.addClass(t);
} else {
var this_header = $("#wired_accordion .accordion").find($("#wired_accordion .accordion").accordion( "option", "header" ))
.eq(this.id_accordion);
this_header.removeClass(oldType);
this_header.addClass(t);
}
}
},
highlight: function(state) {
if(state == true) {
this.el.addClass("highlight");
}
else {
this.el.removeClass("highlight");
}
},
toggleStatus: function() {
var stat = this.el.find(".status");
if(stat.is(":visible"))
stat.slideUp();
else {
this.buildStatus();
stat.slideDown();
}
},
update: function() {
if(this.el) {
// Update anchor position based on actual icon position
var e = this.el.find(".icon");
this.anchor = {left: e.offset().left + e.width() / 2,
top: e.offset().top + e.height() / 2};
// this.anchor = {left: e.position().left + e.width() / 2,
// top: e.position().top + e.height() / 2};
// Set class based on whether this is expanded or not
if(this.expanded) this.el.removeClass("collapsed");
else this.el.addClass("collapsed");
}
// Update connections and count children
this.n_children = 0;
for(var i=0; i<this.connections.length; i++) {
this.connections[i].update();
if(this.connections[i].a == this) this.n_children++;
}
// display number of children nodes in this element's badge
this.el.find(".badge").text(this.n_children);
// hide this node if its a router
if ( this.router_visibility === false ) {
this.el.children().css("opacity", 0);
} else {
this.el.children().css("opacity", 1);
}
// Sometimes expanding nodes will make the page longer, and SVG lines won't draw below the edge of the screen unless we reset the document height on the SVG div.
resetSvgDivHeight();
},
buildStatus: function() {
this.el.find(".status").html("STATUS: ONLINE<br />IP " + (this.static_ip ? "(static)" : "(DHCP)") + ": <div class='ip_slot'>" + this.ip + "</div>");
},
expandSubnodes: function() {
// if opening this node...
if ( this.expanded === false ) {
// close all device settings panels
$.each(devices, function(index, device) {
device.showDetails(false);
});
// close other open nodes first
$.each(routing_devices, function(index, other_dev) {
if ( other_dev.expanded === true && other_dev.name !== "Network Box" && other_dev.name !== "Wireless Network" ) {
other_dev.expanded = false; // Set this to false so when its updated, it closes.
if ( other_dev.type === "router" ) this.router_visibility = true; // Set to true so its icon is re-displayed once closed.
other_dev.update();
}
});
// hide this node if it is a router
if ( this.type === "router" ) this.router_visibility = false;
// then show my subnodes
this.expanded = true;
this.update();
}
},
editName: function(state) {
if(state == true) {
var name_el = this.el.find(".name");
name_el.after("<input type='text' class='edit_name' />");
var edit_el = this.el.find(".edit_name");
name_el.hide();
edit_el.attr("value", this.name);
edit_el.focus();
var thisthis = this;
edit_el.blur(function() {
thisthis.editName(false);
});
edit_el.keydown(function(event) {
if(event.keyCode == 13)
thisthis.editName(false);
});
}
if(state == false) {
// Save new name in current object.
this.name = this.el.find(".edit_name").val();
// Update name in the DOM.
this.el.find(".name").text(this.name);
// For the Accordion Grid layout, here are additionnal places to change the device name.
if ( $(".accordion").length ) {
if (this.is_wireless) {
// Find the wireless accordion header that has the same id_accordion as this object, and change the device name it's 'a' tag text.
$("#wireless_accordion .accordion").find($("#wireless_accordion .accordion").accordion( "option", "header" ))
.eq(this.id_accordion)
.find($("a.header-name")).text(this.name);
// Also change the device name in the panel content.
$("#wireless_accordion .accordion .ui-accordion-content").eq(this.id_accordion).find($("input.content-name")).val(this.name);
} else {
// Find the wireless accordion header that has the same id_accordion as this object, and change it's 'a' tag text.
$("#wired_accordion .accordion").find($("#wireless_accordion .accordion").accordion( "option", "header" ))
.eq(this.id_accordion)
.find($("a.header-name")).text(this.name);
// Also change the device name in the panel content.
$("#wired_accordion .accordion .ui-accordion-content").eq(this.id_accordion).find($("input.content-name")).val(this.name);
}
}
this.el.find(".edit_name").hide();
this.el.find(".name").show();
}
},
editPassword: function(state) {
if(state == true) {
var name_el = this.el.find(".password");
name_el.after("<input type='text' class='edit_password' />");
var edit_el = this.el.find(".edit_password");
name_el.hide();
edit_el.attr("value", this.password);
edit_el.focus();
var thisthis = this;
edit_el.blur(function() {
thisthis.editPassword(false);
});
edit_el.keydown(function(event) {
if(event.keyCode == 13)
thisthis.editPassword(false);
});
}
if(state == false) {
this.password = this.el.find(".edit_password").val();
// Update the DOM in the device area.
this.el.find(".password").text(this.password);
// For the Accordion Grid layout,
if ( $(".accordion").length ) {
// Change the password in the panel content.
$("#wireless_accordion .accordion .ui-accordion-content").eq(this.id_accordion).find($("input.content-password")).val(this.password);
}
this.el.find(".edit_password").hide();
this.el.find(".password").show();
}
},
distanceTo: function(b) {
var dx = b.anchor.left - this.anchor.left;
var dy = b.anchor.top - this.anchor.top;
return Math.sqrt(dx*dx + dy*dy);
},
physicsDistanceTo: function(b) {
var dx = b.target[0] - this.target[0];
var dy = b.target[1] - this.target[1];
return Math.sqrt(dx*dx + dy*dy);
},
physicsDistanceToSquared: function(b) {
var dx = b.target[0] - this.target[0];
var dy = b.target[1] - this.target[1];
return Math.abs(dx*dx + dy*dy);
},
physicsVectorTo: function(b) {
var dx = b.target[0] - this.target[0];
var dy = b.target[1] - this.target[1];
return [dx, dy];
},
die: function() {
this.el.remove();
$.each(this.connections, function(i, conn) {
if(conn)
conn.die();
});
this.connections.length = 0;
}
}