-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
153 lines (135 loc) · 5.95 KB
/
main.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
var pendingAlarm = false;
var currentAlarm = null;
function makePage(){
var mainDiv = document.createElement("div");
mainDiv.setAttribute("class", "mainDiv");
var title = document.createElement("h2");
title.textContent = "Alarms";
title.setAttribute("class", "title");
var plus = document.createElement("h2");
plus.textContent = "+";
plus.setAttribute("class", "plusButton");
mainDiv.appendChild(title);
mainDiv.appendChild(plus);
plus.addEventListener("click", function(){
if(pendingAlarm === false){
pendingAlarm = true;
var alarmSpacer = document.createElement("div");
alarmSpacer.setAttribute("class", "alarmSpacer");
alarmSpacer.setAttribute("id", "alarmSpacer");
var div = document.createElement("div");
div.setAttribute("class", "alarm");
div.setAttribute("id", "alarm");
currentAlarm = div;
var invisibleiframe = document.createElement("iframe");
invisibleiframe.setAttribute("name", "invisibleiframe");
invisibleiframe.setAttribute("style", "display: none;");
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("id", "newAlarmForm");
form.setAttribute("action", "/newAlarm/");
form.setAttribute("class", "alarmForm");
form.setAttribute("target", "invisibleiframe");
var timeInput = document.createElement("input");
timeInput.setAttribute("type", "time");
timeInput.setAttribute("name", "time");
timeInput.setAttribute("id", "time");
var br = document.createElement("br");
var submit = document.createElement("input");
submit.setAttribute("type", "button");
submit.setAttribute("value", "Create");
submit.setAttribute("class", "submitButton");
submit.addEventListener("click", function(){
fetch(`/checkAlarm?time=${document.getElementById("time").value}`).then(response => response.json()).then(json => {
var exists = json['exists'];
if(exists){
window.alert("Could not create alarm:\nAlarm already exists or is blank");
}
else{
// document.getElementById("newAlarmForm").submit();
document.getElementById("alarmSpacer").remove();
document.getElementById("alarm").remove();
renderAlarm(timeInput.value);
currentAlarm = null;
pendingAlarm = false;
}
});
});
var cancel = document.createElement("input");
cancel.setAttribute("type", "submit");
cancel.setAttribute("value", "x");
cancel.setAttribute("class", "cancelButton");
cancel.addEventListener("click", function(){
document.getElementById("alarmSpacer").remove();
document.getElementById("alarm").remove();
currentAlarm = null;
pendingAlarm = false;
});
form.appendChild(timeInput);
form.appendChild(br);
form.appendChild(submit);
document.body.appendChild(alarmSpacer);
div.appendChild(form);
div.appendChild(cancel);
div.appendChild(invisibleiframe);
document.body.appendChild(div);
}
else{
highlightActive();
}
});
document.body.appendChild(mainDiv);
fetch('/alarms').then(response => response.json()).then(json => {
for(i = 0; i < Object.keys(json).length; i++){
renderAlarm(json[i]);
}
});
}
function renderAlarm(alarmTime){
var alarmSpacer = document.createElement("div");
alarmSpacer.setAttribute("class", "alarmSpacer");
alarmSpacer.setAttribute("id", "alarmSpacer" + alarmTime);
var div = document.createElement("div");
div.setAttribute("class", "alarm");
div.setAttribute("id", "alarm" + alarmTime);
var timeH = document.createElement("h3");
timeH.textContent = alarmTime;
timeH.setAttribute("class", "alarmTime");
var cancel = document.createElement("input");
cancel.setAttribute("type", "submit");
cancel.setAttribute("value", "x");
cancel.setAttribute("class", "cancelButton existingCancel");
cancel.addEventListener("click", function(){
fetch(`/delete?time=${timeH.textContent}`).then(response => response.json()).then(json => {
var success = json['success'];
if(success){
document.getElementById("alarmSpacer" + alarmTime).remove();
document.getElementById("alarm" + alarmTime).remove();
}
else{
window.alert("Deletion Failed");
}
});
});
document.body.appendChild(alarmSpacer);
div.appendChild(cancel);
div.appendChild(timeH);
document.body.appendChild(div);
}
async function highlightActive() {
currentAlarm.setAttribute("style", "border:2px solid #0F0; background:#8F8;");
await sleep(150);
currentAlarm.setAttribute("style", "border:2px solid #AAA; background:#DDD;");
await sleep(200);
currentAlarm.setAttribute("style", "border:2px solid #0F0; background:#8F8;");
await sleep(150);
currentAlarm.setAttribute("style", "border:2px solid #AAA; background:#DDD;");
await sleep(200);
currentAlarm.setAttribute("style", "border:2px solid #0F0; background:#8F8;");
await sleep(150);
currentAlarm.setAttribute("style", "border:2px solid #AAA; background:#DDD;");
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
makePage();