-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
1001 lines (878 loc) · 29.5 KB
/
script.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
/*
IntREST: JavaScript, CSS, and HTML crafted with love and lots of coffee.
(c) 2017, Ron Royston, MIT License
https://rack.pub
*/
document.addEventListener('DOMContentLoaded', function() {
// VARIABLES
var doc = document;
window.snackbarContainer = doc.querySelector('#toast');
var dialog = doc.querySelector('dialog');
dialogPolyfill.registerDialog(dialog);
var credentials = [];
var devices = [];
var cards = doc.getElementsByClassName('mdl-card');
var layout = doc.querySelector('.mdl-layout');
var dialogConfirmText = doc.getElementById('dialog-confirm-text');
//var dialogCancelButton = doc.getElementById('dialog-cancel');
//var dialogOkButton = doc.getElementById('dialog-ok');
//Unsupported Card
var unsupportedCard = doc.getElementById('unsupported-card');
//Intro Card
var introCard = doc.getElementById('intro-card');
var introCardShowCredentialCardButton = doc.getElementById('show-credential-card');
var introCardShowDeviceCardButton = doc.getElementById('show-device-card');
//Credential Card
var credentialCard = doc.getElementById('credential-card');
var credentialCardIdInput = doc.getElementById('credential-id');
var credentialCardUsernameInput = doc.getElementById('username');
var credentialCardPasswordInput = doc.getElementById('password');
var credentialCardCancelButton = doc.getElementById('credential-cancel');
var credentialCardSaveButton = doc.getElementById('credential-save');
//Device Card
var deviceCard = doc.getElementById('device-card');
var deviceCardIdInput = doc.getElementById('device-id');
var deviceCardAddressInput = doc.getElementById('device-address');
var deviceCardCredentialsCheckbox = doc.getElementById('credentials-checkbox');
var deviceCardCredentialsSelect = doc.getElementById('credentials-select');
var deviceCardCancelButton = doc.getElementById('cancel-device');
var deviceCardSaveButton = doc.getElementById('device-save');
//Nav Menu
var drawer = doc.getElementsByClassName('mdl-layout__drawer')[0];
var navLinks = drawer.getElementsByClassName('mdl-navigation')[0];
var navMenuCredentials = doc.getElementById('nav-menu-credentials');
var navMenuCredentialsPanel = doc.getElementById('nav-menu-credentials-panel');
var navMenuAddCredentialButton = doc.getElementById('nav-menu-add-credential-button');
var navMenuDevices = doc.getElementById('nav-menu-devices');
var navMenuDevicesPanel = doc.getElementById('nav-menu-devices-panel');
var navMenuAddDeviceButton = doc.getElementById('nav-menu-add-device-button');
var endpointCards = doc.getElementsByClassName('endpoint-card');
var endpointCardsSliders = doc.getElementsByClassName('mdl-slider');
var pollingMinutesSpans = doc.getElementsByClassName('polling-minutes-span');
var configurationCards = doc.getElementsByClassName('configuration-card');
var testButton = doc.getElementById('test-button');
initializePage();
initializeEndpointCards();
function initializePage (){
// INITIALIZE PAGE
if(ls == false){
// if browser does not support html5 local storage alert user and stop
showUnsupportedCard();
} else {
try {
var devicesString = localStorage.getItem('devices');
if (devicesString === null){
devicesString = '';
}
if(devicesString.length > 2){
devices = JSON.parse(devicesString);
devices.forEach(function (arrayItem){
if (arrayItem.id){
navMenuAdd('device',arrayItem.id);
}
});
} else {
// no devices are configured show intro card
showIntroCard();
}
} catch(e) {
console.log('error is ' + e);
}
// LOAD CREDENTIALS
try {
var credentialsString = localStorage.getItem('credentials');
if (credentialsString === null){
credentialsString = '';
}
if(credentialsString.length > 2){
credentials = JSON.parse(credentialsString);
credentials.forEach(function (arrayItem){
if (arrayItem.id){
// add credential to nav menu
navMenuAdd('credential',arrayItem.id);
//add credential to select
var opt = document.createElement('option');
opt.value = arrayItem.id;
opt.innerHTML = arrayItem.id;
deviceCardCredentialsSelect.appendChild(opt);
}
});
}
} catch(e) {
console.log('error is ' + e);
}
//Setup Nav Menu Accordian
var acc = document.getElementsByClassName("accordion");
try {
for (var i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
};
}
} catch(e) {
console.log('error is ' + e);
}
}
}
// ---------------------------------------------------------- *******************************************************************************
var card1 = doc.getElementById('card1');
card1.style.display = "inline";
function navMenuAdd(type,id){
var newOuterDiv = doc.createElement('div');
newOuterDiv.classList.add('mdl-navigation__link',type);
var newSpan = doc.createElement('span');
var spanContent = doc.createTextNode(id);
var newDiv = doc.createElement('div');
newDiv.classList.add('float-right');
var newButton = doc.createElement('button');
newButton.classList.add('mdl-button','mdl-js-button','mdl-button--icon','mdl-color-text--red-A700','nav-delete-button');
newButton.addEventListener('click', navMenuDeleteHandler, false);
var newIcon = doc.createElement('i');
var newIconText = doc.createTextNode('delete');
newIcon.classList.add('material-icons','delete-icon');
newIcon.appendChild(newIconText);
newSpan.appendChild(spanContent);
newDiv.appendChild(newButton);
newButton.appendChild(newIcon);
newOuterDiv.appendChild(newSpan);
newOuterDiv.appendChild(newDiv);
newOuterDiv.addEventListener('click', navMenuClickHandler, false);
if (type === 'credential'){
navMenuCredentialsPanel.appendChild(newOuterDiv);
} else if (type === 'device'){
navMenuDevicesPanel.appendChild(newOuterDiv);
}
}
function navMenuDeleteHandler(){
var grandparent = this.parentElement.parentElement;
var id = this.parentElement.previousSibling.innerHTML;
var obj = {};
if(grandparent.classList.contains('credential')){
//deleteObject('credential',id);
obj = getCredentialObject(id);
dialogConfirm(obj,{action:'delete'});
} else if (grandparent.classList.contains('device')){
//deleteObject('device',id);
obj = getDeviceObject(id);
dialogConfirm(obj,{action:'delete'});
}
}
function deleteObject(type, id){
var obj = {};
if (type === 'credential'){
obj = credentials;
deleteThis(obj);
//save credentials array to local storage
try {
localStorage.setItem('credentials', JSON.stringify(credentials));
toast('Credential ' + id + ' deleted.');
} catch(e) {
console.log(e);
}
} else if (type === 'device'){
obj = devices;
deleteThis(obj);
//save devices array to local storage
try {
localStorage.setItem('devices', JSON.stringify(devices));
toast('Device ' + id + ' deleted.');
} catch(e) {
console.log(e);
}
}
deleteNavMenuItem(id,type);
function deleteThis(o){
try {
for (var i=0, iLen=o.length; i<iLen; i++) {
if (o[i].id === id){
o.splice(i,1);
}
}
} catch(e) {
console.log(e);
}
}
}
function deleteNavMenuItem(id,type){
try {
var items = doc.getElementsByClassName(type);
for (i = 0; i < items.length; i++) {
if(items[i].firstChild.innerHTML === id){
items[i].parentElement.removeChild(items[i]);
}
}
} catch(e) {
console.log(e);
}
}
//Use this to layout if only a single card
function makeMdlSpacer(){
//<div class="mdl-layout-spacer"></div>
var newMdlSpacer = doc.createElement("div");
newMdlSpacer.classList.add('mdl-layout-spacer');
return newMdlSpacer;
}
function navMenuClickHandler(element){
//we need to clear all cards, look in classlist for which thing
hideEndpointsCards();
if(this.classList.contains("credential")){
var credential = getCredentialObject(this.firstElementChild.innerHTML);
showCredentialCard();
credentialCardIdInput.value = credential.id;
credentialCardUsernameInput.value = credential.username;
credentialCardPasswordInput.value = credential.password;
//mdlCleanUp();
} else if (this.classList.contains("device")){
var device = getDeviceObject(this.firstElementChild.innerHTML);
showDeviceCard();
deviceCardIdInput.value = device.id;
deviceCardAddressInput.value = device.address;
deviceCardCredentialsSelect.selected = device.credentialId;
}
//Clean up the js text fields
mdlCleanUp();
//Close the drawer
layout.MaterialLayout.toggleDrawer();
}
endpointCardsSliders[0].addEventListener("input", function(){
pollingMinutesSpans[0].innerHTML = this.value;
});
function initializeEndpointCards(){
google.charts.load('current', {
'packages': ['gauge','line']
});
google.charts.setOnLoadCallback(drawGuage);
google.charts.setOnLoadCallback(drawChart);
function drawGuage() {
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['Memory', 80],
['CPU', 55],
['Network', 68]
]);
var guageOptions = {
//width: '100%',
height: 120,
redFrom: 90,
redTo: 100,
yellowFrom: 75,
yellowTo: 90,
minorTicks: 5
};
var guage = new google.visualization.Gauge(document.getElementById('guage-div'));
google.visualization.events.addListener(guage, 'ready', resetTableStyle);
guage.draw(data, guageOptions);
function resetTableStyle(){
var myDiv = document.getElementById('guage-div');
var myTable = myDiv.getElementsByTagName('table')[0];
myTable.style.margin = 'auto';
}
}
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Day');
data.addColumn('number', 'Guardians of the Galaxy');
data.addColumn('number', 'The Avengers');
data.addColumn('number', 'Transformers: Age of Extinction');
data.addRows([
[1, 37.8, 80.8, 41.8],
[2, 30.9, 69.5, 32.4],
[3, 25.4, 57, 25.7],
[4, 11.7, 18.8, 10.5],
[5, 11.9, 17.6, 10.4],
[6, 8.8, 13.6, 7.7],
[7, 7.6, 12.3, 9.6],
[8, 12.3, 29.2, 10.6],
[9, 16.9, 42.9, 14.8],
[10, 12.8, 30.9, 11.6],
[11, 5.3, 7.9, 4.7],
[12, 6.6, 8.4, 5.2],
[13, 4.8, 6.3, 3.6],
[14, 4.2, 6.2, 3.4]
]);
var options = {
// chart: {
// title: 'Box Office Earnings in First Two Weeks of Opening',
// subtitle: 'in millions of dollars (USD)',
// },
legend : { position:"none"},
width: 400,
height: 300
};
var chart = new google.charts.Line(document.getElementById('chart-div'));
google.visualization.events.addListener(chart, 'ready', resetChartDivStyle);
chart.draw(data, options);
function resetChartDivStyle(){
var chartDiv = document.getElementById('chart-div');
var targetDiv = chartDiv.getElementsByTagName('div')[0];
targetDiv.style.margin = 'auto';
}
}
}
navMenuAddCredentialButton.addEventListener("click", function(){
layout.MaterialLayout.toggleDrawer();
showCredentialCard();
});
navMenuAddDeviceButton.addEventListener("click", function(){
layout.MaterialLayout.toggleDrawer();
showDeviceCard();
});
introCardShowDeviceCardButton.addEventListener("click", function(){
showDeviceCard();
});
introCardShowCredentialCardButton.addEventListener("click", function(){
showCredentialCard();
});
deviceCardCancelButton.addEventListener("click", function(){
deviceCardIdInput.value = '';
deviceCardAddressInput.value = '';
credentialCardPasswordInput.value = '';
mdlCleanUp();
deviceCardSaveButton.disabled = true;
// if no devices show intro card; else show guage adder
if(devices[0] !== undefined){
hideConfigurationCards();
showEndpointCards();
} else {
// show intro card
showIntroCard();
}
});
deviceCardCredentialsSelect.addEventListener("click", function(e){
this.classList.add('active');
});
deviceCardCredentialsCheckbox.addEventListener("click", function(e){
if (this.checked){
deviceCardCredentialsSelect.disabled = false;
deviceCardCredentialsSelect.classList.add('active');
} else {
deviceCardCredentialsSelect.selectedIndex = 0;
deviceCardCredentialsSelect.disabled = true;
deviceCardCredentialsSelect.classList.remove('active');
}
});
credentialCardCancelButton.addEventListener("click", function(){
credentialCardIdInput.value = '';
credentialCardUsernameInput.value = '';
credentialCardPasswordInput.value = '';
mdlCleanUp();
credentialCardSaveButton.disabled = true;
// show endpoint cards : if no devices show intro card
if(devices[0] !== undefined){
hideConfigurationCards();
showEndpointCards();
} else {
showIntroCard();
}
});
// function check(input) {
// if (input.value != registrationInputPassword.value) {
// input.setCustomValidity('Passwords Must Match');
// submitButton.disabled = true;
// } else {
// // VALID INPUT - RESET ERROR MESSAGE
// input.setCustomValidity('');
// submitButton.disabled = false;
// }
// }
credentialCardSaveButton.addEventListener("click", function(){
//get the input values
var id = credentialCardIdInput.value;
var username = credentialCardUsernameInput.value;
var password = credentialCardPasswordInput.value;
var newValues = {};
newValues.id = id;
newValues.username = username;
newValues.password = password;
newValues.type = 'credential';
//test for duplicates
if (credentials !== null && credentials.length > 0) {
if (duplicate(id, credentials) === true){
dialogConfirm(newValues,{action:'update'});
return;
}
}
//push object to credentials array
try {
credentials.push(new Credential(id,username,password));
} catch(e) {
toast('Error: Failed Adding Credential ' + id + '.');
return;
}
//save to local storage
try {
localStorage.setItem('credentials', JSON.stringify(credentials));
toast('Credential ' + id + ' saved to localStorage.');
} catch(e) {
toast('Error: Failed storing credential ' + id + '.');
return;
}
// add credential to nav menu
navMenuAdd('credential',id);
//add credential to select
var opt = document.createElement('option');
opt.value = id;
opt.innerHTML = id;
deviceCardCredentialsSelect.appendChild(opt);
});
deviceCardSaveButton.addEventListener("click", function(){
//get the input values
var id = deviceCardIdInput.value;
var address = deviceCardAddressInput.value;
var basicAuthentication = false;
var credentialId = '';
var token = '';
var expiration = '';
var newValues = {};
newValues.id = id;
newValues.address = address;
newValues.credentialId = credentialId;
newValues.type = 'device';
if (deviceCardCredentialsCheckbox.checked){
basicAuthentication = true;
credentialId = deviceCardCredentialsSelect.value;
}
//test for duplicates
if (devices !== null && devices.length > 0) {
if (duplicate(id, devices) === true){
dialogConfirm(newValues,{action:'update'});
return;
}
}
//push object to devices array
try {
devices.push(new Device(id,address,basicAuthentication,credentialId,token,expiration));
} catch(e) {
toast('Error: Failed adding device ' + id + '.');
return;
}
//save to local storage
try {
localStorage.setItem('devices', JSON.stringify(devices));
toast('Device ' + id + ' saved to localStorage.');
} catch(e) {
toast('Error: Failed storing device ' + id + '.');
return;
}
// add device to nav menu
navMenuAdd('device',id);
});
credentialCardIdInput.addEventListener("blur", function(event) {
if(credentialInputsPopulated()){
credentialCardSaveButton.disabled = false;
}
}, true);
credentialCardUsernameInput.addEventListener("blur", function(event) {
if(credentialInputsPopulated()){
credentialCardSaveButton.disabled = false;
} else {
credentialCardSaveButton.disabled = true;
}
}, true);
// credentialCardPasswordInput.addEventListener("blur", function(event) {
// if(credentialInputsPopulated()){
// credentialCardSaveButton.disabled = false;
// }
// }, true);
credentialCardPasswordInput.addEventListener("keyup", function(e) {
if(credentialInputsPopulated()){
credentialCardSaveButton.disabled = false;
} else {
credentialCardSaveButton.disabled = true;
}
//enable pressing enter
e.preventDefault();
if (e.keyCode == 13) {
credentialCardSaveButton.click();
}
});
deviceCardIdInput.addEventListener("blur", function(event) {
if(deviceInputsPopulated()){
deviceCardSaveButton.disabled = false;
} else {
deviceCardSaveButton.disabled = true;
}
}, true);
deviceCardAddressInput.addEventListener("blur", function(event) {
if(deviceInputsPopulated()){
deviceCardSaveButton.disabled = false;
} else {
deviceCardSaveButton.disabled = true;
}
}, true);
function credentialInputsPopulated(){
var result = false;
if(credentialCardIdInput.value !== '' && credentialCardUsernameInput.value !== '' && credentialCardPasswordInput.value !== ''){
result = true;
}
return result;
}
function deviceInputsPopulated(){
var result = false;
if(deviceCardIdInput.value !== '' && deviceCardAddressInput.value !== ''){
result = true;
}
return result;
}
function showIntroCard(){
introCard.style.display = "inline";
deviceCard.style.display = "none";
credentialCard.style.display = "none";
unsupportedCard.style.display = "none";
}
function showCredentialCard(){
//hide other cards
introCard.style.display = "none";
unsupportedCard.style.display = "none";
hideConfigurationCards();
hideEndpointsCards();
//show credential card
credentialCard.style.display = "inline";
//select and focus first input
credentialCardIdInput.focus();
mdlCleanUp();
}
function showDeviceCard(){
introCard.style.display = "none";
unsupportedCard.style.display = "none";
hideConfigurationCards();
hideEndpointsCards();
//show device card
deviceCard.style.display = "inline";
//select and focus first input
deviceCardIdInput.focus();
mdlCleanUp();
}
function showEndpointCards(){
try {
for (var i = 0, l = endpointCards.length; i < l; i++) {
endpointCards[i].style.display = 'inline';
}
} catch(e) {
}
}
function hideEndpointsCards(){
try {
for (var i = 0, l = endpointCards.length; i < l; i++) {
endpointCards[i].style.display = 'none';
}
} catch(e) {
}
}
function hideConfigurationCards(){
try {
for (var i = 0, l = configurationCards.length; i < l; i++) {
configurationCards[i].style.display = 'none';
}
} catch(e) {
}
}
function showUnsupportedCard(){
introCard.style.display = "none";
deviceCard.style.display = "none";
credentialCard.style.display = "none";
unsupportedCard.style.display = "inline";
}
// Object Constructor Functions
function Credential(id, username, password) {
this.id = id;
this.username = username;
this.password = password;
this.type = 'credential';
}
function Device(id, address, basicAuthentication, credentialId,token,expiration) {
this.id = id;
this.address = address;
this.basicAuthentication = basicAuthentication;
this.credential = credentialId;
this.token = token;
this.expiration = expiration;
this.type = 'device';
}
//LOCAL STORAGE TEST
var ls = {
get: function () {
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}
};
function duplicate(id,obj){
var result = false;
obj.forEach( function (arrayItem){
if (arrayItem.id == id){
result = true;
}
});
return result;
}
//MDL Text Input Cleanup
function mdlCleanUp(){
var mdlInputs = doc.querySelectorAll('.mdl-js-textfield');
for (var i = 0, l = mdlInputs.length; i < l; i++) {
mdlInputs[i].MaterialTextfield.checkDirty();
}
}
function callApi(deviceId, path){
var device = getDeviceObject(deviceId);
if(device === undefined){
toast('Error: Device ' + deviceId + ' not found.');
} else {
//var credential = getCredentialObject(device.credential);
if(device.expiration < clock.now){
// we don't have a good token
getToken(deviceId).then(function(token) {
//for TESTING PURPOSES let's make it GET
return httpReq('GET', device.address, path, token);
//return httpReq(method, device.address, path, token); // ---------------------------------------------------------------------------
}).then(function(data) {
console.log(data);
}).catch(function(err) {
console.log(err);
});
} else {
console.log('we have a good token: ' + device.token + ', which expires ' + device.expiration);
//so let's GET a resource now
//for TESTING PURPOSES let's make it GET
httpReq('GET', device.address, path, device.token).then(function(data) {
console.log(data);
}, function(status) {
console.log(status);
});
}
}
}
// testButton.addEventListener("click", function(){
// callApi('csr-home','/api/v1/global/host-name');
// });
function getToken(deviceId){
var device = getDeviceObject(deviceId);
var credential = getCredentialObject(device.credential);
var address = 'https://' + device.address + '/api/v1/auth/token-services';
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("POST", address, true);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader ("Authorization", "Basic " + window.btoa(credential.username + ":" + credential.password));
xhr.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function() {
var status = xhr.status;
if (status == 200 || status == 201 || status == 202) {
var response = JSON.parse(xhr.response);
var expiration = new Date(response["expiry-time"]).getTime();
try {
updateDeviceToken(deviceId, response["token-id"], expiration);
toast(deviceId + ' token recieved. Lifetime is ' + clock.until(expiration));
} catch(e) {
console.log(e);
}
resolve(response["token-id"]);
}
else {
reject(status);
}
};
xhr.send();
});
}
function updateDeviceObject(id, address, credentialId){
//update devices array
try {
for (var i=0, iLen=devices.length; i<iLen; i++) {
if (devices[i].id == id){
devices[i].address = address;
devices[i].credentialId = credentialId;
if(credentialId){
devices[i].basicAuthentication = true;
} else {
devices[i].basicAuthentication = false;
}
devices[i].type = 'device';
}
}
} catch(e) {
console.log(e);
}
//save devices array to local storage
try {
localStorage.setItem('devices', JSON.stringify(devices));
} catch(e) {
console.log(e);
}
}
function updateDeviceToken(id, token, expiration){
try {
for (var i=0, iLen=devices.length; i<iLen; i++) {
if (devices[i].id === id){
devices[i].token = token;
devices[i].expiration = expiration;
devices[i].type = 'device';
}
}
} catch(e) {
console.log(e);
}
//save devices array to local storage
try {
localStorage.setItem('devices', JSON.stringify(devices));
toast('Device ' + id + ' updated.');
} catch(e) {
console.log(e);
}
}
function updateCredentialObject(id, username, password){
//update devices array
try {
for (var i=0, iLen=credentials.length; i<iLen; i++) {
if (credentials[i].id === id){
credentials[i].username = username;
credentials[i].password = password;
credentials[i].type = 'credential';
}
}
} catch(e) {
console.log(e);
}
//save devices array to local storage
try {
localStorage.setItem('credentials', JSON.stringify(credentials));
toast('Credential ' + id + ' updated.');
} catch(e) {
console.log(e);
}
}
function getDeviceObject(id){
try {
for (var i=0, iLen=devices.length; i<iLen; i++) {
if (devices[i].id == id){
return devices[i];
}
}
} catch(e) {
return false;
}
}
function getCredentialObject(id){
try {
for (var i=0, iLen=credentials.length; i<iLen; i++) {
if (credentials[i].id == id){
return credentials[i];
}
}
} catch(e) {
return false;
}
}
function httpReq(method, host, path, token) {
if(method === "DELETE" || method === "GET"|| method === "POST" || method === "PUT" ){
var address = 'https://' + host + path;
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(method, address, true);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader ("X-auth-token", token);
//xhr.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function() {
var status = xhr.status;
if (status == 200 || status == 201 || status == 202) {
resolve(xhr.response);
}
else {
reject(status);
}
};
xhr.send();
});
} else {
console.log('invalid method');
}
};
function redirect(path){
//console.log('redirect ran with path = ' + path);
var baseURL = window.location.protocol + '//' + window.location.host + '/firebase-auth';
var hasSlash = path.charAt(0) == '/';
if(!hasSlash){
path = '/' + path;
}
//console.log ('hasSlash = ' + hasSlash + ', and path = ' + path);
//console.log(baseURL + path); // https://rack.pub/firebase-authhttps://rack.pub/firebase-auth
var onThisPage = (window.location.href.indexOf(baseURL + path) > -1);
if (!onThisPage) {
//redirect them to login page for message
location = baseURL + path;
}
}
function getArg(param) {
var vars = {};
window.location.href.replace( location.hash, '' ).replace(
/[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
function( m, key, value ) { // callback
vars[key] = value !== undefined ? value : '';
}
);
if ( param ) {
return vars[param] ? vars[param] : null;
}
return vars;
}
var toast = function(msg,timeout){
if(!timeout){timeout = 2750}
var data = {
message: msg,
timeout: timeout
};
snackbarContainer.MaterialSnackbar.showSnackbar(data);
};
function dialogConfirm(obj,task){
dialogConfirmText.innerHTML = task.action + ' ' + obj.id;
bindDialogListeners(obj,task);
dialog.showModal();
}
function bindDialogListeners(obj,task){
var arr = [];
arr.push(obj);
doc.querySelector('#dialog-cancel').onclick = null;
doc.querySelector('#dialog-ok').onclick = null;
doc.querySelector('#dialog-cancel').onclick = function() {
task.val = false;
arr.push(task);
dialog.close(arr);
};
doc.querySelector('#dialog-ok').onclick = function() {
task.val = true;
arr.push(task);
dialog.close(arr);
};
}
doc.querySelector('dialog').addEventListener('close', function() {
//an object turns up here.
var obj = this.returnValue[0];
var task = this.returnValue[1];
if(obj.type === 'credential'){
if(task.val === true){
if(task.action === 'update'){
updateCredentialObject(obj.id, obj.username, obj.password);
}
if(task.action === 'delete'){
deleteObject('credential', obj.id);
}
}
} else if (obj.type === 'device'){
if(task.val === true){
if(task.action === 'update'){
updateDeviceObject(obj.id, obj.address, obj.credentialId);
}
if(task.action === 'delete'){
deleteObject('device', obj.id);
}
}
}
});
// END
}, false);