forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprofileeditor.js
More file actions
986 lines (891 loc) · 33 KB
/
profileeditor.js
File metadata and controls
986 lines (891 loc) · 33 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
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
"use strict";
var init = function init() {
//for the tests window isn't the global object
var $ = window.$ || require("jquery");
var _ = window._;
var Nightscout = window.Nightscout;
var client = Nightscout.client;
var c_profile = null;
//some commonly used selectors
var peStatus = $(".pe_status");
var timezoneInput = $("#pe_timezone");
var databaseRecords = $("#pe_databaserecords");
var timeInput = $("#pe_time");
var dateInput = $("#pe_date");
client.init(function loaded() {
console.log("LOADING CLIENT INIT");
if (c_profile !== null) {
return; // already loaded so don't load again
}
var translate = client.translate;
var defaultprofile = {
//General values
dia: 3,
carbratio: [
{
time: "00:00",
value: 30,
},
],
carbs_hr: 20,
delay: 20,
sens: [
{
time: "00:00",
value: 100,
},
],
timezone: "UTC",
//perGIvalues style values
perGIvalues: false,
carbs_hr_high: 30,
carbs_hr_medium: 30,
carbs_hr_low: 30,
delay_high: 15,
delay_medium: 20,
delay_low: 20,
basal: [
{
time: "00:00",
value: 0.1,
},
],
target_low: [
{
time: "00:00",
value: 0,
},
],
target_high: [
{
time: "00:00",
value: 0,
},
],
startDate: new Date(0).toISOString(),
};
// , 'startDate': new Date()
// defaultprofile.startDate.setSeconds(0);
// defaultprofile.startDate.setMilliseconds(0);
var icon_add =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABa0lEQVQ4T6WTzysEYRjHP+/Mrv2hHOTuqJRNOfgPSORHokg4OClHcnVzVygHF6WIcuHMnyCHVRyI3ZYxa23vzOzs7LzamaI0e5i89fTWt/f5vPV5n1cQsXLbHepvfLv5JaLORoZNwMbyFo5vYfsWB0c7xAasLa5T/vCg45Oj48P4gJWFVYxCA63L5PzkND5gfm4Jo+Chd5W5OrtsDYgS1pQ1OTuNUfTQO8tcX9xE+QugYnS/X81MzGP7MpTWkEFVZY1KxcVPV3h27zAtA+oCagIcDfWUCgEje31qfHwK06gHjaF5iXQcHCV5lHmqqgQCNEAI0IsavCVDwNBurxo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+fMOZ3H2WButc7MD3AQWyaYIRZEqANpt9HHCiwvIhWim8gPiqArJailVmdhJGEI3USk4rAQc1MnTdMitZhtfm86ncvcYhMls2i9l+/Hj//zvA+B/1kmcKSArGIohmNIZoIMkRVE1O95sOQaOXCCK+KUYgWcy99gne2zRA1Q9VR1/u7DVcVcIdyrLshFCtQPNyIqgG6gtWImduB69lVM+r9EQqvEajQM8dEBokcxnSWas0fSElMw5B1EdlIuLM43GJxz1P0zgfhhjDFDmsFf1pVixOeASqTGgt+Llk+tEwCt2wDuQgsSWghgxwXYNxvL8iwbIoLjN3KvsQQVRCAUgIyWg3c0wRdYPmoz2C0E7qE2NUFdnpa4D73TfZgLLNThIirCCGuW8MWpRcYC9TF8DYwhPU6DtvEO2Kb666zF9sidMBC6lVsJES1EiCDwZPQZHHMfzBJB7ONdcbLOm3mVcP9wQi5UwO2bQdP7pxOi9aDOYrB7NgATQnfyb2Mq4AJN0eDiU9A82oL+6UFcyS7DXqkSfixDKpDB1P8Ai98Xdd1661aZBO6jNif5YPm5zFOYXZsCQzHYE8dhZP4jspK1mAm6oYpR49Xn1+iZtNW81fdU/zp8kSaSDSSfpzxkPK85jfn1WawSK9BI9mMJXkiFSXAvzYAfMNu79O90v0/u1jdKzPER5IzmJLyhefiJFchFyWApKUzWRnj8Hp1F370VfRPaNgfKZgWfIlEZL2WVQsAASUIZ2sY60Ou01bQXdm2L/kcgvJn5PJ3nElTG0qwSuHwutAy32tsLunZE/ysQPsh5caCaoYRlQgHjoUnC8PK4ZUf0TeAnGJ/iJEGClrwAAAAASUVORK5CYII=';
var mongorecords = [];
var currentrecord = 0;
var currentprofile = null;
var dirty = false;
// Fetch data from mongo
peStatus
.hide()
.text(translate("Loading profile records ..."))
.fadeIn("slow");
$.ajax("/api/v1/profile.json?count=20", {
headers: client.headers(),
success: function (records) {
if (!records.length) {
records.push(defaultprofile);
}
client.profilefunctions.loadData(records); // do a conversion if needed
mongorecords = _.cloneDeep(client.profilefunctions.data);
// create new profile to be edited from last record
if (mongorecords.length) {
_.each(mongorecords, function eachMongoProfile(mongoprofile) {
_.each(mongoprofile.store, function eachStoredProfile(p) {
// allign with default profile
for (var key in defaultprofile) {
if (
Object.prototype.hasOwnProperty.call(defaultprofile, key) &&
!Object.prototype.hasOwnProperty.call(p, key)
) {
p[key] = defaultprofile[key];
}
}
for (key in p) {
if (
Object.prototype.hasOwnProperty.call(p, key) &&
!Object.prototype.hasOwnProperty.call(defaultprofile, key)
) {
delete p[key];
}
}
convertToRanges(p);
});
});
peStatus.hide().text(translate("Values loaded.")).fadeIn("slow");
} else {
mongorecords.push({
defaultProfile: "Default",
store: {
Default: _.cloneDeep(defaultprofile),
},
startDate: new Date().toISOString(),
});
peStatus
.hide()
.text(translate("Default values used."))
.fadeIn("slow");
}
},
error: function () {
mongorecords.push({
defaultProfile: "Default",
store: {
Default: _.cloneDeep(defaultprofile),
},
startDate: new Date().toISOString(),
});
peStatus
.hide()
.text(translate("Error. Default values used."))
.fadeIn("slow");
},
}).done(initeditor);
// convert simple values to ranges if needed
function convertToRanges(profile) {
if (typeof profile.carbratio !== "object") {
profile.carbratio = [{ time: "00:00", value: profile.carbratio }];
}
if (typeof profile.sens !== "object") {
profile.sens = [{ time: "00:00", value: profile.sens }];
}
if (typeof profile.target_low !== "object") {
profile.target_low = [{ time: "00:00", value: profile.target_low }];
}
if (typeof profile.target_high !== "object") {
profile.target_high = [{ time: "00:00", value: profile.target_high }];
}
if (typeof profile.basal !== "object") {
profile.basal = [{ time: "00:00", value: profile.basal }];
}
if (profile.target_high.length !== profile.target_low.length) {
window.alert(
translate(
"Time ranges of target_low and target_high don't match. Values are restored to defaults."
)
);
profile.target_low = _.cloneDeep(defaultprofile.target_low);
profile.target_high = _.cloneDeep(defaultprofile.target_high);
}
}
function initeditor() {
$("#pe_history").toggle(
client.settings.extendedSettings.profile &&
client.settings.extendedSettings.profile.history
);
$("#pe_multiple").toggle(
client.settings.extendedSettings.profile &&
client.settings.extendedSettings.profile.multiple
);
// Load timezones
timezoneInput.empty();
client.ctx.timezones.forEach(function addTz(tz) {
timezoneInput.append('<option value="' + tz + '">' + tz + "</option>");
});
$("#pe_form").find("button").click(profileSubmit);
$("#pe_profiles").unbind().bind("change", profileChange);
$("#pe_profile_add").unbind().bind("click", profileAdd);
$("#pe_profile_remove").unbind().bind("click", profileRemove);
$("#pe_profile_clone").unbind().bind("click", profileClone);
$("#pe_databaserecords").unbind().bind("change", recordChange);
$("#pe_records_add").unbind().bind("click", recordAdd);
$("#pe_records_remove").unbind().bind("click", recordRemove);
$("#pe_records_clone").unbind().bind("click", recordClone);
// Add handler for style switching
$("#pe_perGIvalues").unbind().on("change", switchStyle);
// display status
$("#pe_units").text(client.settings.units);
$("#pe_timeformat").text(client.settings.timeFormat + "h");
$("#pe_title").text(client.settings.customTitle);
currentprofile = mongorecords[currentrecord].defaultProfile;
// prepare basal profiles
initRecord();
// hide unused style of ratios
switchStyle();
console.log("Done initeditor()");
}
function initRecord() {
databaseRecords.empty();
for (var r = 0; r < mongorecords.length; r++) {
databaseRecords.append(/* HTML */
`
<option value="${r}">
${translate("Valid from:")}
${new Date(mongorecords[r].startDate).toLocaleString()}
</option>
`);
}
databaseRecords.val(currentrecord);
timeInput.val(
client.ctx.moment(mongorecords[currentrecord].startDate).format("HH:mm")
);
dateInput.val(
client.ctx
.moment(mongorecords[currentrecord].startDate)
.format("YYYY-MM-DD")
);
initProfile();
}
function timeDiffMinutes(time1, time2) {
var minutes1 = toMinutesFromMidnight(time1);
var minutes2 = toMinutesFromMidnight(time2);
if (minutes2 <= minutes1) {
minutes2 += 24 * 60;
}
return minutes2 - minutes1;
}
function refreshTotalBasal() {
GUIToObject();
var total = 0;
for (var i = 0, len = c_profile["basal"].length; i < len; i++) {
var time1 = c_profile["basal"][i].time;
var time2 = c_profile["basal"][(i + 1) % len].time;
var value = c_profile["basal"][i].value;
total += (timeDiffMinutes(time1, time2) * value) / 60;
}
$("#pe_basalTotal_placeholder").html(Math.round(total * 1000) / 1000);
}
function initProfile() {
var record = mongorecords[currentrecord];
// fill profilenames
$("#pe_profiles").empty();
for (var key in record.store) {
if (Object.prototype.hasOwnProperty.call(record.store, key)) {
$("#pe_profiles").append(/* HTML */
`
<option value="${key}">${key}</option>
`);
}
}
$("#pe_profiles").val(currentprofile);
$("#pe_profile_name").val(currentprofile);
c_profile = mongorecords[currentrecord].store[currentprofile];
mongorecords[currentrecord].defaultProfile = currentprofile;
// Set values from profile to html
fillTimeRanges();
refreshTotalBasal();
}
// Handling of record list box change
function recordChange(event) {
if (
dirty &&
window.confirm(
translate("Save current record before switching to new?")
)
) {
profileSubmit();
}
currentrecord = databaseRecords.val();
currentprofile = mongorecords[currentrecord].defaultProfile;
initRecord();
dirty = false;
maybePreventDefault(event);
}
function recordAdd(event) {
if (
dirty &&
window.confirm(
translate("Save current record before switching to new?")
)
) {
profileSubmit();
}
mongorecords.push({
startDate: new Date().toISOString(),
defaultProfile: "Default",
store: {
Default: _.cloneDeep(defaultprofile),
},
});
currentrecord = mongorecords.length - 1;
currentprofile = "Default";
initRecord();
dirty = true;
maybePreventDefault(event);
}
function recordRemove(event) {
if (
mongorecords.length > 1 &&
window.confirm(translate("Delete record") + "?")
) {
if (mongorecords[currentrecord]._id) {
$.ajax({
method: "DELETE",
url: "/api/v1/profile/" + mongorecords[currentrecord]._id,
headers: client.headers(),
})
.done(function postSuccess() {
console.info("profile deleted");
peStatus.hide().text(status).fadeIn("slow");
mongorecords.splice(currentrecord, 1);
currentrecord = 0;
currentprofile = mongorecords[currentrecord].defaultProfile;
initRecord();
dirty = false;
})
.fail(function (xhr, status, errorThrown) {
console.error("Profile not removed", status, errorThrown);
peStatus.hide().text(status).fadeIn("slow");
});
} else {
mongorecords.splice(currentrecord, 1);
currentrecord = 0;
currentprofile = mongorecords[currentrecord].defaultProfile;
initRecord();
dirty = false;
}
}
maybePreventDefault(event);
return false;
}
function recordClone(event) {
if (
dirty &&
window.confirm(
translate("Save current record before switching to new?")
)
) {
profileSubmit();
}
GUIToObject();
mongorecords.push(
_.omit(mongorecords[currentrecord], [
"_id",
"srvModified",
"srvCreated",
"identifier",
"mills",
])
);
currentrecord = mongorecords.length - 1;
mongorecords[currentrecord].startDate = new Date().toISOString();
currentprofile = mongorecords[currentrecord].defaultProfile;
initRecord();
dirty = true;
maybePreventDefault(event);
}
// Handling of profile list box change
function profileChange(event) {
var record = mongorecords[currentrecord];
var newpr = $("#pe_profiles").val();
// copy values from html to c_profile
GUIToObject();
var newname = $("#pe_profile_name").val();
if (!isNaN(newname)) newname = "Profile" + newname;
if (currentprofile !== newname) {
// rename if already exists
while (record.store[newname]) {
newname += "1";
}
record.store[newname] = record.store[currentprofile];
delete record.store[currentprofile];
dirty = true;
}
if (newpr === currentprofile) {
// fake call to update values
newpr = newname;
}
currentprofile = newpr;
initProfile();
maybePreventDefault(event);
return false;
}
function profileAdd(event) {
var record = mongorecords[currentrecord];
var newname = "New profile";
while (record.store[newname]) {
newname += "1";
}
record.store[newname] = _.cloneDeep(defaultprofile);
currentprofile = newname;
dirty = true;
initProfile();
maybePreventDefault(event);
return false;
}
function profileRemove(event) {
var record = mongorecords[currentrecord];
var availableProfile = getFirstAvailableProfile(record);
if (availableProfile) {
delete record.store[currentprofile];
currentprofile = availableProfile;
initProfile();
dirty = true;
}
maybePreventDefault(event);
return false;
}
function profileClone(event) {
GUIToObject();
var record = mongorecords[currentrecord];
var newname = $("#pe_profile_name").val() + " (copy)";
while (record.store[newname]) {
newname += "1";
}
record.store[newname] = _.cloneDeep(record.store[currentprofile]);
currentprofile = newname;
dirty = true;
initProfile();
maybePreventDefault(event);
return false;
}
// Handling html events and setting/getting values
function switchStyle(event) {
if (!$("#pe_perGIvalues").is(":checked")) {
$("#pe_simple").show("slow");
$("#pe_advanced").hide("slow");
} else {
$("#pe_simple").hide("slow");
$("#pe_advanced").show("slow");
}
maybePreventDefault(event);
}
function fillTimeRanges(event) {
if (event) {
GUIToObject();
}
function shouldAddTime(i, time, array) {
if (i === 0 && time === 0) {
return true;
} else if (i === 0) {
return false;
} else {
var minutesFromMidnight = toMinutesFromMidnight(
c_profile[array][i - 1].time
);
return !isNaN(minutesFromMidnight) && minutesFromMidnight < time * 30;
}
}
function addSingleLine(e, i) {
var tr = $("<tr>");
var select = $("<select>")
.attr("class", "pe_selectabletime")
.attr("id", e.prefix + "_from_" + i);
var lowest = -1;
for (var t = 0; t < 48; t++) {
if (shouldAddTime(i, t, e.array)) {
if (lowest === -1) {
lowest = t * 30;
}
select.append(/* HTML */
`
<option value="${toTimeString(t * 30)}">
${toDisplayTime(t * 30)}
</option>
`);
}
}
var selectedValue =
toMinutesFromMidnight(c_profile[e.array][i].time) * 30;
select.val(selectedValue);
tr.append(
$("<td>")
.append(translate("From") + ": ")
.append(select)
);
tr.append(
$("<td>")
.append(e.label)
.append(
$('<input type="text">')
.attr("id", e.prefix + "_val_" + i)
.attr("value", c_profile[e.array][i].value)
.attr("class", e.prefix + "_value")
)
);
var icons_td = $("<td>").append(
$("<img>")
.attr("class", "addsingle")
.attr("style", "cursor:pointer")
.attr("title", translate("Add new interval before"))
.attr("src", icon_add)
.attr("array", e.array)
.attr("pos", i)
);
if (c_profile[e.array].length > 1) {
icons_td.append(
$("<img>")
.attr("class", "delsingle")
.attr("style", "cursor:pointer")
.attr("title", translate("Delete interval"))
.attr("src", icon_remove)
.attr("array", e.array)
.attr("pos", i)
);
}
tr.append(icons_td);
if (lowest > toMinutesFromMidnight(c_profile[e.array][i].time)) {
c_profile[e.array][i].time = toTimeString(lowest);
}
return tr[0].outerHTML;
}
// Fill dropdown boxes
_.each(
[
{
prefix: "pe_basal",
array: "basal",
label: `${translate("Basal rate")} : `,
},
{
prefix: "pe_ic",
array: "carbratio",
label: `${translate("I:C")} : `,
},
{ prefix: "pe_isf", array: "sens", label: `${translate("ISF")} : ` },
],
function (e) {
var html = "<table>";
for (var i = 0; i < c_profile[e.array].length; i++) {
html += addSingleLine(e, i);
}
html +=
'<tr><td></td><td></td><td><img class="addsingle" style="cursor:pointer" title="' +
translate("Add new interval before") +
'" src="' +
icon_add +
'" array="' +
e.array +
'" pos="' +
i +
'" href="#"></td></tr>';
html += "</table>";
$("#" + e.prefix + "_placeholder").html(html);
}
);
$(".pe_basal_value").on("change keyup paste", refreshTotalBasal);
$(".addsingle").click(function addsingle_click() {
var array = $(this).attr("array");
var pos = $(this).attr("pos");
GUIToObject();
c_profile[array].splice(pos, 0, { time: "00:00", value: 0 });
var retVal = fillTimeRanges();
refreshTotalBasal();
return retVal;
});
$(".delsingle").click(function delsingle_click() {
var array = $(this).attr("array");
var pos = $(this).attr("pos");
GUIToObject();
c_profile[array].splice(pos, 1);
c_profile[array][0].time = "00:00";
var retVal = fillTimeRanges();
refreshTotalBasal();
return retVal;
});
function addBGLine(i) {
var tr = $("<tr>");
var select = $("<select>")
.attr("class", "pe_selectabletime")
.attr("id", "pe_targetbg_from_" + i);
var lowesttime = -1;
for (var t = 0; t < 48; t++) {
if (shouldAddTime(i, t, "target_low")) {
if (lowesttime === -1) {
lowesttime = t * 30;
}
select.append(
'<option value="' +
toTimeString(t * 30) +
'">' +
toDisplayTime(t * 30) +
"</option>"
);
}
}
var selectedValue =
toMinutesFromMidnight(c_profile.target_low[i].time) * 30;
select.val(selectedValue);
tr.append(
$("<td>")
.append(translate("From") + ": ")
.append(select)
);
tr.append(
$("<td>")
.append(translate("Low") + " : ")
.append(
$('<input type="text">')
.attr("id", "pe_targetbg_low_" + i)
.attr("value", c_profile.target_low[i].value)
)
);
tr.append(
$("<td>")
.append(translate("High") + " : ")
.append(
$('<input type="text">')
.attr("id", "pe_targetbg_high_" + i)
.attr("value", c_profile.target_high[i].value)
)
);
var icons_td = $("<td>").append(
$("<img>")
.attr("class", "addtargetbg")
.attr("style", "cursor:pointer")
.attr("title", translate("Add new interval before"))
.attr("src", icon_add)
.attr("pos", i)
);
if (c_profile.target_low.length > 1) {
icons_td.append(
$("<img>")
.attr("class", "deltargetbg")
.attr("style", "cursor:pointer")
.attr("title", translate("Delete interval"))
.attr("src", icon_remove)
.attr("pos", i)
);
}
tr.append(icons_td);
// Fix time to correct value after add or change
if (lowesttime > toMinutesFromMidnight(c_profile.target_low[i].time)) {
c_profile.target_low[i].time = toTimeString(lowesttime);
}
return tr[0].outerHTML;
}
// target BG
var html = "<table>";
for (var i = 0; i < c_profile.target_low.length; i++) {
html += addBGLine(i);
}
html +=
/* HTML */
`
<tr>
<td></td>
<td></td>
<td></td>
<td>
<img
class="addtargetbg"
style="cursor:pointer"
title="${translate("Add new interval before")}"
src="${icon_add}"
pos="${i}"
href="#"
/>
</td>
</tr>
`;
html += "</table>";
$("#pe_targetbg_placeholder").html(html);
$(".addtargetbg").click(function addtargetbg_click() {
var pos = $(this).attr("pos");
GUIToObject();
c_profile.target_low.splice(pos, 0, { time: "00:00", value: 0 });
c_profile.target_high.splice(pos, 0, { time: "00:00", value: 0 });
dirty = true;
return fillTimeRanges();
});
$(".deltargetbg").click(function deltargetbg_click() {
var pos = $(this).attr("pos");
GUIToObject();
c_profile.target_low.splice(pos, 1);
c_profile.target_high.splice(pos, 1);
c_profile.target_low[0].time = "00:00";
c_profile.target_high[0].time = "00:00";
dirty = true;
return fillTimeRanges();
});
$(".pe_selectabletime")
.unbind()
.on("change", fillTimeRanges)
.on("change", refreshTotalBasal);
objectToGUI();
maybePreventDefault(event);
return false;
}
// fill GUI with values from c_profile object
function objectToGUI() {
$("#pe_dia").val(c_profile.dia);
$("#pe_hr").val(c_profile.carbs_hr);
$("#pe_perGIvalues").prop("checked", c_profile.perGIvalues);
$("#pe_hr_high").val(c_profile.carbs_hr_high);
$("#pe_hr_medium").val(c_profile.carbs_hr_medium);
$("#pe_hr_low").val(c_profile.carbs_hr_low);
$("#pe_delay_high").val(c_profile.delay_high);
$("#pe_delay_medium").val(c_profile.delay_medium);
$("#pe_delay_low").val(c_profile.delay_low);
// find the right zone regardless of string case
var foundCase = c_profile.timezone;
if (foundCase != "") {
var lcZone = c_profile.timezone.toLowerCase();
client.ctx.timezones.forEach(function testCase(tz) {
if (tz.toLowerCase() == lcZone) foundCase = tz;
});
}
timezoneInput.val(foundCase);
var index;
[
{ prefix: "pe_basal", array: "basal" },
{ prefix: "pe_ic", array: "carbratio" },
{ prefix: "pe_isf", array: "sens" },
].forEach(function (e) {
for (index = 0; index < c_profile[e.array].length; index++) {
$("#" + e.prefix + "_from_" + index).val(
c_profile[e.array][index].time
);
$("#" + e.prefix + "_val_" + index).val(
c_profile[e.array][index].value
);
}
});
for (index = 0; index < c_profile.target_low.length; index++) {
$("#pe_targetbg_from_" + index).val(c_profile.target_low[index].time);
$("#pe_targetbg_low_" + index).val(c_profile.target_low[index].value);
$("#pe_targetbg_high_" + index).val(c_profile.target_high[index].value);
}
//console.info(JSON.stringify(c_profile));
}
// Grab values from html GUI to object
function GUIToObject() {
var oldProfile = _.cloneDeep(c_profile);
c_profile.units = client.settings.units;
c_profile.dia = parseFloat($("#pe_dia").val());
c_profile.carbs_hr = parseInt($("#pe_hr").val());
c_profile.delay = 20;
c_profile.perGIvalues = $("#pe_perGIvalues").is(":checked");
c_profile.carbs_hr_high = parseInt($("#pe_hr_high").val());
c_profile.carbs_hr_medium = parseInt($("#pe_hr_medium").val());
c_profile.carbs_hr_low = parseInt($("#pe_hr_low").val());
c_profile.delay_high = parseInt($("#pe_delay_high").val());
c_profile.delay_medium = parseInt($("#pe_delay_medium").val());
c_profile.delay_low = parseInt($("#pe_delay_low").val());
// If the zone in the profile matches the editor profile
// but case is different, preserve case
var zone = timezoneInput.val();
if (c_profile.timezone.toLowerCase() == timezoneInput.val().toLowerCase())
zone = c_profile.timezone;
c_profile.timezone = zone;
var index;
[
{ prefix: "pe_basal", array: "basal" },
{ prefix: "pe_ic", array: "carbratio" },
{ prefix: "pe_isf", array: "sens" },
].forEach(function (e) {
for (index = 0; index < c_profile[e.array].length; index++) {
c_profile[e.array][index].time = $(
"#" + e.prefix + "_from_" + index
).val();
c_profile[e.array][index].value = parseFloat(
$("#" + e.prefix + "_val_" + index).val()
);
}
});
for (index = 0; index < c_profile.target_low.length; index++) {
var input = $("#pe_targetbg_from_" + index);
c_profile.target_low[index].time = input.val();
c_profile.target_low[index].value = parseFloat(
$("#pe_targetbg_low_" + index).val()
);
c_profile.target_high[index].time = input.val();
c_profile.target_high[index].value = parseFloat(
$("#pe_targetbg_high_" + index).val()
);
}
if (!_.isEqual(oldProfile, c_profile)) {
dirty = true;
}
}
function toMinutesFromMidnight(time) {
var split = time.split(":");
return parseInt(split[0]) * 60 + parseInt(split[1]);
}
function toTimeString(minfrommidnight) {
return client.ctx.moment
.utc()
.startOf("day")
.add(minfrommidnight, "minutes")
.format("HH:mm"); // using utc to avoid daylight saving offset
}
function toDisplayTime(minfrommidnight) {
var time = client.ctx.moment
.utc()
.startOf("day")
.add(minfrommidnight, "minutes"); // using utc to avoid daylight saving offset
return client.settings.timeFormat === 24
? time.format("HH:mm")
: time.format("h:mm A");
}
function profileSubmit(event) {
if (!client.hashauth.isAuthenticated()) {
window.alert(translate("Your device is not authenticated yet"));
return false;
}
profileChange(event);
var record = mongorecords[currentrecord];
record.startDate = new Date(
client.utils.mergeInputTime(timeInput.val(), dateInput.val())
).toISOString();
record.created_at = new Date().toISOString();
record.srvModified = new Date().getTime(); // remove when switching to v3 API
var adjustedRecord = _.cloneDeep(record);
for (var key in adjustedRecord.store) {
if (Object.prototype.hasOwnProperty.call(adjustedRecord.store, key)) {
var profile = adjustedRecord.store[key];
if (!profile.perGIvalues) {
delete profile.perGIvalues;
delete profile.carbs_hr_high;
delete profile.carbs_hr_medium;
delete profile.carbs_hr_low;
delete profile.delay_high;
delete profile.delay_medium;
delete profile.delay_low;
}
}
}
adjustedRecord.defaultProfile = currentprofile;
adjustedRecord.units = client.settings.units;
delete record.convertedOnTheFly;
delete adjustedRecord.convertedOnTheFly;
console.info("saving profile");
peStatus.hide().text(translate("Saving profile")).fadeIn("slow");
// Hide the form until the ajax PUT is done.
// This is a crude way of preventing the user from changing the inputs whilst waiting.
// If the user was able to make changes, they'd be lost when the done callback redraws anyway.
$("#pe_form").hide();
var headers = client.headers();
headers["Content-Type"] = "application/json";
$.ajax({
method: "PUT",
url: "/api/v1/profile/",
data: JSON.stringify(adjustedRecord),
headers: headers,
})
.done(function postSuccess(data, status) {
console.info("profile saved", data);
$("#pe_form").show(); // allow edits again
peStatus.hide().text(status).fadeIn("slow");
record._id = data._id;
initRecord();
dirty = false;
})
.fail(function (xhr, status, errorThrown) {
console.error("Profile not saved", status, errorThrown);
$("#pe_form").show(); // allow edits again
peStatus.hide().text(status).fadeIn("slow");
});
return false;
}
function getFirstAvailableProfile(record) {
var availableProfiles = [];
for (var key in record.store) {
if (Object.prototype.hasOwnProperty.call(record.store, key)) {
if (key !== currentprofile) {
availableProfiles.push(key);
}
}
}
return availableProfiles.length ? availableProfiles[0] : null;
}
function maybePreventDefault(event) {
if (event) {
event.preventDefault();
}
}
});
};
module.exports = init;