-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
298 lines (242 loc) · 8.5 KB
/
app.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
// class to implement time intervals operations
class TimeInterval {
constructor (hour, minute) {
this.minutes = hour * 60 + minute;
}
get minute() {
return this.minutes % 60;
}
get hour() {
return Math.floor(this.minutes/60);
}
sub(timeInterval) {
let newTime = new TimeInterval(0,0);
newTime.minutes = this.minutes - timeInterval.minutes;
return newTime;
}
sum(timeInterval) {
let newTime = new TimeInterval(0,0);
newTime.minutes = this.minutes + timeInterval.minutes;
return newTime;
}
copy() {
let copy = new TimeInterval(0,0);
copy.minutes = this.minutes;
return copy;
}
toString() {
if (this.minute < 10)
return "" + this.hour + ":0" + this.minute;
return "" + this.hour + ":" + this.minute;
}
fromString(timeString) {
if(timeString == null || timeString == ""){
this.minutes = 0;
return;
}
let split = timeString.split(":");
let hour = parseInt(split[0]);
let minute = parseInt(split[1]);
this.minutes = hour * 60 + minute;
}
}
// define constant intervals, they can be changed in case of time regulations changes
const TNV = new TimeInterval(0,15);
const PRANZO = new TimeInterval(0,30);
const TEMPO_BASE = new TimeInterval(8,0);
class App {
constructor () {
this.entrata = new TimeInterval(0,0);
this.uscitaTNV = new TimeInterval(0,0);
this.rientroTNV = new TimeInterval(0,0);
this.uscitaPranzo = new TimeInterval(0,0);
this.rientroPranzo = new TimeInterval(0,0);
this.tempoDovuto = TEMPO_BASE.copy()
this.tempoRimanente = new TimeInterval(0,0);
this.orarioUscita = new TimeInterval(0,0);
this.started = false;
}
setEntrata(entrata_str) {
this.entrata.fromString(entrata_str);
this.started = true;
}
setUscitaTNV(uscitaTNV_str) {
this.uscitaTNV.fromString(uscitaTNV_str);
}
setRientroTNV(rientroTNV_str) {
this.rientroTNV.fromString(rientroTNV_str);
}
setUscitaPranzo(uscitaPranzo_str) {
this.uscitaPranzo.fromString(uscitaPranzo_str);
}
setRientroPranzo(rientroPranzo_str) {
this.rientroPranzo.fromString(rientroPranzo_str);
}
saveState(localStorage) {
localStorage.setItem("entrata", this.entrata.toString());
localStorage.setItem("uscitaTNV", this.uscitaTNV.toString());
localStorage.setItem("rientroTNV", this.rientroTNV.toString());
localStorage.setItem("uscitaPranzo", this.uscitaPranzo.toString());
localStorage.setItem("rientroPranzo", this.rientroPranzo.toString());
app.run();
updateUI();
}
loadState(localStorage) {
if(localStorage.getItem("entrata") != null)
this.setEntrata(localStorage.getItem("entrata"));
else
return;
if(localStorage.getItem("uscitaTNV") != null)
this.setUscitaTNV(localStorage.getItem("uscitaTNV"));
if(localStorage.getItem("rientroTNV") != null)
this.setRientroTNV(localStorage.getItem("rientroTNV"));
if(localStorage.getItem("uscitaPranzo") != null)
this.setUscitaPranzo(localStorage.getItem("uscitaPranzo"));
if(localStorage.getItem("rientroPranzo") != null)
this.setRientroPranzo(localStorage.getItem("rientroPranzo"));
app.run();
updateUI();
update();
}
resetState(localStorage) {
localStorage.clear();
this.entrata = new TimeInterval(0,0);
this.uscitaTNV = new TimeInterval(0,0);
this.rientroTNV = new TimeInterval(0,0);
this.uscitaPranzo = new TimeInterval(0,0);
this.rientroPranzo = new TimeInterval(0,0);
this.tempoDovuto = TEMPO_BASE.copy()
this.tempoRimanente = new TimeInterval(0,0);
this.orarioUscita = new TimeInterval(0,0);
app.run();
updateUI();
this.started = false;
document.getElementById("tempoDovuto").innerHTML = "8:00";
document.getElementById("tempoRimanente").innerHTML = "8:00";
document.getElementById("orarioUscita").innerHTML = "16:00";
}
// this function computes the remaining time and should be runned each minute or when values are modified
run() {
if(!this.started)
return;
let now = new Date();
let ora = new TimeInterval(now.getHours(), now.getMinutes())
this.tempoDovuto = TEMPO_BASE.copy()
if (this.rientroTNV.sub(this.uscitaTNV).minutes > TNV.minutes)
this.tempoDovuto = this.tempoDovuto.sum(this.rientroTNV.sub(this.uscitaTNV).sub(TNV));
if (this.rientroPranzo.sub(this.uscitaPranzo).minutes > PRANZO.minutes)
this.tempoDovuto = this.tempoDovuto.sum(this.rientroPranzo.sub(this.uscitaPranzo).sub(PRANZO));
this.tempoRimanente = this.tempoDovuto.sub(ora.sub(this.entrata));
this.orarioUscita = ora.sum(this.tempoRimanente);
}
getTempoRimanente() {
let tempoRimanente = this.tempoRimanente.toString();
if(this.tempoRimanente.minutes < 0)
{
let temp = this.tempoRimanente.copy()
temp.minutes = Math.abs(temp.minutes)
tempoRimanente = "+" + temp.toString();
}
return tempoRimanente;
}
getTempoDovuto() {
return this.tempoDovuto.toString();
}
getOrarioUscita() {
return this.orarioUscita.toString();
}
}
// create the app
let app = new App();
function update() {
if(!app.started)
return;
app.run();
document.getElementById("tempoDovuto").innerHTML = app.getTempoDovuto();
document.getElementById("tempoRimanente").innerHTML = app.getTempoRimanente();
document.getElementById("orarioUscita").innerHTML = app.getOrarioUscita();
}
function updateUI() {
document.getElementById("entrata").value = app.entrata.minutes === 0 ? "" : app.entrata.toString();
document.getElementById("uscitaTNV").value = app.uscitaTNV.minutes === 0 ? "" : app.uscitaTNV.toString();
document.getElementById("rientroTNV").value = app.rientroTNV.minutes === 0 ? "" : app.rientroTNV.toString();
document.getElementById("uscitaPranzo").value = app.uscitaPranzo.minutes === 0 ? "" : app.uscitaPranzo.toString();
document.getElementById("rientroPranzo").value = app.rientroPranzo.minutes === 0 ? "" : app.rientroPranzo.toString();
}
function autocompleteTime(input) {
let time = input.value;
time = time.replace(":", "");
function inner(time) {
if(time.length <3)
return time;
if(time.length >= 4)
return `${time[0]}${time[1]}:${time[2]}${time[3]}`;
return `${time[0]}:${time[1]}${time[2]}`;
}
input.value = inner(time);
}
function impostaEntrata(now) {
let entrata = "";
if(now)
entrata = nowToText();
else
entrata = document.getElementById("entrata").value;
app.setEntrata(entrata);
update();
}
function impostaUscitaTNV(now) {
let uscitaTNV = "";
if(now)
uscitaTNV = nowToText();
else
uscitaTNV = document.getElementById("uscitaTNV").value;
app.setUscitaTNV(uscitaTNV);
update();
}
function impostaRientroTNV(now) {
let rientroTNV = "";
if(now)
rientroTNV = nowToText();
else
rientroTNV = document.getElementById("rientroTNV").value;
app.setRientroTNV(rientroTNV);
update();
}
function impostaUscitaPranzo(now) {
let uscitaPranzo = "";
if(now)
uscitaPranzo = nowToText();
else
uscitaPranzo = document.getElementById("uscitaPranzo").value;
app.setUscitaPranzo(uscitaPranzo);
update();
}
function impostaRientroPranzo(now) {
let rientroPranzo = "";
if(now)
rientroPranzo = nowToText();
else
rientroPranzo = document.getElementById("rientroPranzo").value;
app.setRientroPranzo(rientroPranzo);
update();
}
function nowToText() {
let now = new Date();
return `${now.getHours()}:${now.getMinutes()}`;
}
function repeatEvery(func, interval) {
let now = new Date();
let delay = interval - now % interval;
function start() {
func();
setInterval(func, interval);
}
setTimeout(start, delay);
}
// this function is runned when each minute change
repeatEvery(() => {
if(!app.started)
return;
app.run();
update()
}, 1000 * 60);