-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
383 lines (344 loc) · 14 KB
/
index.html
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<!DOCTYPE html>
<html>
<head>
<title>ipreg</title>
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
<style>
.error input {
border: 2px solid red;
}
</style>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">ipreg</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Subnets <b class="caret"></b></a>
<ul class="dropdown-menu" id="subnet-nav">
<!-- ko foreach: subnets -->
<li>
<a data-bind="href:'#', click: $parent.updateSubnet">
<span data-bind="text: name"></span>
</a>
</li>
<!-- /ko -->
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<div id="refresh-button">
<button type="button" class="btn btn-default navbar-btn"
data-bind="click: refreshSubnet">
<span class="glyphicon glyphicon-refresh"></span> Refresh Status
</button>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="jumbotron">
<h2>IP Address Monitor and Registration</h2>
<p>Select a subnet from the drop down above to see a listing of IP addresses.</p>
</div>
</div>
<div id="main" class="container">
<table class="table table-striped">
<tr>
<td><b>Host Status</b>
</td>
<td><b>Registration Action</b>
</td>
<td><b>Registered User</b>
</td>
<td><b>Notes</b>
</td>
</tr>
<!-- ko foreach: addresses -->
<tr>
<td>
<h4 data-bind="visible: isUp">
<span data-bind="text: address"
class="label label-success"></span>
</h4>
<h4 data-bind="visible: !isUp()">
<span data-bind="text: address"
class="label label-danger"></span>
</h4>
</td>
<td>
<button type="button" class="btn btn-default btn-md" data-bind="click: $parent.doRegister">
<span class="glyphicon glyphicon-plus"></span> Update Registration
</button>
<button type="button" class="btn btn-default btn-md" data-bind="visible: !isAvailable(), click: $parent.doUnregister">
<span class="glyphicon glyphicon-remove"></span> Unregister
</button>
</td>
<td>
<span data-bind="visible: !isAvailable(), text:user"></span>
</td>
<td>
<span data-bind="visible: !isAvailable(), text:note"></span>
</td>
</tr>
<!-- /ko -->
</table>
</div>
<div id="edit" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editRegLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title" id="editRegLabel">Edit Registration</h3>
</div>
<div class="modal-body">
<form class="form-horizontal" id="edit-form">
<div class="control-group">
<label class="control-label" for="inputUser">User</label>
<div class="controls">
<input data-bind="value: name" type="text" name="inputUser" id="inputUser" placeholder="Name" style="width: 150px;">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input data-bind="value: email" type="text" name="inputEmail" id="inputEmail" placeholder="Email" style="width: 300px;">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputNote">Note</label>
<div class="controls">
<input data-bind="value: note" type="text" name="inputNote" id="inputNote" placeholder="Notes" style="width: 300px;">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button data-bind="click: editAddress" class="btn btn-primary">Update Registration</button>
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
</div>
</div>
</div>
</div>
<div id="ajax-error" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Error</h3>
</div>
<div id="error-body" class="modal-body">
<p></p>
</div>
</div>
</div>
</div>
<div id="ajax-success" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Successful Update</h3>
</div>
<div id="success-body" class="modal-body">
<p></p>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#edit-form').validate({
rules: {
inputUser: {
required: true
},
inputEmail: {
required: true,
email: true
},
},
highlight: function (element) {
$(element).closest('.control-group').removeClass('success').addClass('error');
},
success: function (element) {
element.addClass('valid')
.closest('.control-group').removeClass('error').addClass('success');
}
});
});
var currentSubnet = "";
var ipregServer = "localhost";
var ipregServerPort = "8080";
function AddressesViewModel() {
var self = this;
self.addressesUri = 'http://' + ipregServer + ':' + ipregServerPort + '/addresses/';
self.statusUri = 'http://' + ipregServer + ':' + ipregServerPort + '/status/';
self.addresses = ko.observableArray();
self.ajax = function (uri, method, data) {
var request = {
url: uri,
type: method,
cache: false,
dataType: 'json',
data: JSON.stringify(data)
};
return $.ajax(request);
}
self.checkStatus = function (statusString) {
if (statusString == "Up") {
return true;
}
return false;
}
self.checkAvailable = function (email) {
if (email == "") {
return true;
}
return false;
}
self.doRegister = function (address) {
editStatusViewModel.setAddress(address);
// force a reset
$('#edit-form').valid();
$("#edit").modal("show");
}
self.updateAddress = function (address, newAddress) {
var index = self.addresses.indexOf(address);
self.addresses()[index].isAvailable(false);
self.addresses()[index].name(newAddress.name);
self.addresses()[index].email(newAddress.email);
self.addresses()[index].note(newAddress.note);
self.addresses()[index].user(newAddress.name + "(" + newAddress.email + ")");
}
self.setRegister = function (address, data) {
self.ajax(self.statusUri + address.address, 'PUT', data).done(function (res) {
self.updateAddress(address, res);
$("#success-body > p").text("Registration updated for " + address.address);
$("#ajax-success").modal("show");
}).fail(function () {
$("#error-body > p").text("Failed to update registration");
$("#ajax-error").modal("show");
});
}
self.doUnregister = function (address) {
self.ajax(self.statusUri + address.address,
'DELETE').done(function () {
self.addresses()[address.index].isAvailable(true);
self.addresses()[address.index].user("");
self.addresses()[address.index].name("");
self.addresses()[address.index].email("");
self.addresses()[address.index].note("");
$("#success-body > p").text("Registration deleted for " + address.address);
$("#ajax-success").modal("show");
}
).fail(function () {
$("#error-body > p").text("Failed to delete registration");
$("#ajax-error").modal("show");
});
}
self.getSubnet = function (subnet) {
self.addresses.removeAll();
currentSubnet = subnet;
self.ajax(self.addressesUri + subnet, 'GET').done(function (data) {
for (var i = 0; i < data.length; i++) {
self.addresses.push({
address: data[i].Address,
isUp: ko.observable(self.checkStatus(data[i].Status)),
isAvailable: ko.observable(self.checkAvailable(data[i].Email)),
user: ko.observable(data[i].Name + " (" + data[i].Email + ")"),
name: ko.observable(data[i].Name),
email: ko.observable(data[i].Email),
note: ko.observable(data[i].Note),
index: i
});
}
}).fail(function () {
$("#error-body > p").text("Failed to get subnet information");
$("#ajax-error").modal("show");
});
}
}
var addressesViewModel = new AddressesViewModel;
ko.applyBindings(addressesViewModel, $('#main')[0]);
function SubnetViewModel() {
var self = this;
self.subnetsUri = 'http://' + ipregServer + ':' + ipregServerPort + '/subnets';
self.subnets = ko.observableArray();
self.updateSubnet = function (subnet) {
addressesViewModel.getSubnet(subnet.name());
}
self.ajax = function (uri, method, data) {
var request = {
url: uri,
type: method,
cache: false,
dataType: 'json',
data: JSON.stringify(data)
};
return $.ajax(request);
}
self.ajax(self.subnetsUri, 'GET').done(function (data) {
for (var i = 0; i < data.length; i++) {
self.subnets.push({
name: ko.observable(data[i][0]),
link: data[i][0]
});
}
}).fail(function () {
$("#error-body > p").text("Failed to get available subnets");
$("#ajax-error").modal("show");
});
}
ko.applyBindings(new SubnetViewModel(), $('#subnet-nav')[0]);
function EditStatusViewModel() {
var self = this;
self.name = ko.observable();
self.email = ko.observable();
self.note = ko.observable();
self.setAddress = function (address) {
self.address = address;
self.name(address.name());
self.email(address.email());
self.note(address.note());
}
self.editAddress = function () {
if (!$('#edit-form').valid()) {
return;
}
$('#edit').modal('hide');
addressesViewModel.setRegister(self.address, {
name: self.name(),
email: self.email(),
note: self.note()
});
}
}
var editStatusViewModel = new EditStatusViewModel;
ko.applyBindings(editStatusViewModel, $('#edit')[0]);
function RefreshStatusViewModel() {
var self = this;
self.refreshSubnet = function() {
if (currentSubnet == "") {
return;
}
addressesViewModel.getSubnet(currentSubnet);
}
}
ko.applyBindings(new RefreshStatusViewModel(), $('#refresh-button')[0]);
</script>
</body>
</html>