Newer
Older
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
"use strict";
import { showAlert } from './alerts.js';
import Luci from './luci.js';
import Scenari from './scenari.js';
class Antifurto {
/**
* the div where everything related to the AntiFurto is stored
*/
static antifurtoContainer = document.getElementById('antifurtoContainer')!;
/**
* the progress bar used to show the user how much is left until the alarm is triggered
*/
static progressBar = document.getElementById('antifurto-progress')!;
/**
* describes the current status of the progress bar
*/
static currentPercentage: number = parseInt(Antifurto.progressBar!.style.width);
/**
* shows if the antitheft is on or off
*/
static status: Boolean = false;
/**
* contains the user's preferences regarding when to trigger the alarm
*/
static soglia: undefined | number = undefined;
/**
* shows if the alarm is on or off
*/
static alarmStatus: Boolean = false;
constructor() {
Antifurto.init();
Antifurto.timer();
Antifurto.setupModalListener();
}
static init(): void {
Antifurto.fillTable();
const antifurtoBtn = document.getElementById('mainButtonContainer')!.children[2];
antifurtoBtn.addEventListener('click', () => {
Antifurto.toggleContainer(true);
Luci.toggleContainer(false);
Scenari.toggleContainer(false);
}, false);
Antifurto.antifurtoBtnListener();
const bell = document.getElementById('antifurto-bell')!;
bell.addEventListener('click', () => {
Antifurto.activateAlarm(false);
// TODO ask if they want to turn off the antitheft
}, false);
}
/**
* listener for the form in the modal used to retrieve the soglia value from the user
*/
static setupModalListener(): void {
const modalForm: HTMLFormElement = document.getElementById('setup-antifurto-form')! as HTMLFormElement;
const sogliaInput: HTMLInputElement = document.getElementById('sogliaAntifurto')! as HTMLInputElement;
const closeModal: HTMLButtonElement = modalForm.querySelector('.btn.btn-danger')!;
modalForm.addEventListener('submit', event => {
event.preventDefault();
// get the value from the input
const inputVal = sogliaInput!.value.trim();
if (inputVal == null || inputVal === '') {
alert('Inserisci un valore valido');
return;
}
const soglia = parseInt(inputVal);
if (isNaN(soglia) || soglia < 0 || soglia > 100) {
alert('Inserisci un valore valido');
return;
}
// close modal
closeModal.click();
// empty the modal
modalForm.reset();
// setup soglia and progress bar
Antifurto.setupSoglia(soglia);
}, false);
}
/**
* sets up the progress bar to better accomodate the user's preferences
* @param {number} soglia the integer value from which the alarm will be triggered
*/
static setupSoglia(soglia: number): void {
Antifurto.soglia = soglia;
Antifurto.progressBar.setAttribute('aria-valuemax', Antifurto.maxProgressBar().toString());//the progress bar goes a little bit more than the soglia
Antifurto.resetProgressBar();
}
/**
* edits and shows the modal to toggle the antitheft
*/
static antifurtoBtnListener(): void {
const antifurtoBtn = document.getElementById('antifurto-stato-antifurto')! as HTMLButtonElement;
const modal = document.getElementById('toggle-antifurto-modal')!;
const modalBody = modal.querySelector('.modal-body')!;
const modalLauncher = document.getElementById('antifurto-modal-launcher')!;
const okBtn: HTMLButtonElement = modal.querySelector('#toggle-antifurto-btn')!;
antifurtoBtn.addEventListener('click', () => {
const children = modalBody.children as HTMLCollectionOf<HTMLParagraphElement>;
// p, p
/*
<p>Stai per abilitare l'antifurto.</p>
<p>Vuoi continuare?</p>
*/
if (antifurtoBtn.innerText === 'ON') {
// the antitheft is on
children[0].innerText = "Stai per disabilitare l'antifurto.";
okBtn.innerText = 'Disattiva';
} else {
if (Antifurto.soglia === undefined) {
showAlert('Soglia', "Non hai ancora impostato la soglia di sicurezza. Impostala prima di attivare l'antifurto", false);
return;
}
// the antitheft is off
children[0].innerText = "Stai per abilitare l'antifurto.";
okBtn.innerText = 'Attiva';
}
// launch the modal
modalLauncher.click();
}, false);
const closeModal: HTMLButtonElement = modal.querySelector('.btn.btn-danger')!;
okBtn.addEventListener('click', () => {
// close the modal
closeModal.click();
// toggle the antitheft
const activating = antifurtoBtn.innerText === 'OFF';
Antifurto.activate(activating);
}, false);
}
/**
* function to activate or deactivate the antitheft and syncronize the button in the scenari
* @param {Boolean} activating whether to activate or deactivate the antitheft
* @param {Boolean} fromScenari usually false or undefined; true only if the function is called from the scenari
*/
static activate(activating: Boolean, fromScenari: Boolean = false): void {
if (fromScenari !== true)
Scenari.correctlySetAntifurto(activating, true);
// TODO server set the status of the antitheft
const antifurtoBtn = document.getElementById('antifurto-stato-antifurto')!;
if (activating) {
if (antifurtoBtn.innerText === 'ON')
return;
antifurtoBtn.innerText = 'ON';
antifurtoBtn.classList.remove('btn-danger');
antifurtoBtn.classList.add('btn-success');
} else {
if (antifurtoBtn.innerText === 'OFF')
return;
antifurtoBtn.innerText = 'OFF';
antifurtoBtn.classList.remove('btn-success');
antifurtoBtn.classList.add('btn-danger');
Antifurto.activateAlarm(false);
}
Antifurto.status = activating;
}
/**
* toggles the bell if the alarm is on
* @param {Boolean} activating if true, the bell will turn on
*/
static activateAlarm(activating: Boolean): void {
// if the antitheft is off, the bell shouldn't turn on
// if the alarmStatus is already set, nothing else should be done
if (Antifurto.status === false || Antifurto.alarmStatus === activating)
return;
Antifurto.alarmStatus = activating;
const bell = document.getElementById('antifurto-bell')!;
if (activating) {
bell.classList.add('fa-shake');
} else {
bell.classList.remove('fa-shake');
// empty the progress bar
Antifurto.resetProgressBar();
}
}
/**
* inputs a delta and takes care of updating the progress bar and the server
* @param {number} percentage the percentage to add to the progress bar (+/-)
*/
static changeProgressBar(percentage: number): void {
// if the antitheft is turned off, the progress bar shouldn't change
if (!Antifurto.status) {
showAlert('Antifurto', "L'antifurto è spento", false);
return;
}
if (Antifurto.soglia === undefined) {
showAlert('Soglia', "Non hai ancora impostato la soglia di sicurezza. Impostala prima di attivare l'antifurto", false);
return;
}
const oldPercentage = Antifurto.currentPercentage;
var newPercentage = oldPercentage + percentage;
newPercentage = Math.floor(newPercentage);
const maxProgressAvailable = Antifurto.maxProgressBar();
if (newPercentage > maxProgressAvailable)
newPercentage = maxProgressAvailable;
if (newPercentage < 0)
newPercentage = 0;
// TODO server send the new percentage
Antifurto.progressBar.style.width = newPercentage + '%';
Antifurto.progressBar.setAttribute('aria-valuenow', newPercentage.toString());
Antifurto.progressBar.innerHTML = newPercentage + '%';
Antifurto.currentPercentage = newPercentage;
// check if the soglia is reached
Antifurto.activateAlarm(newPercentage >= Antifurto.soglia);
const quarterSoglia = Antifurto.soglia / 4;
// change colors of the progress bar
if (newPercentage <= quarterSoglia) {
if (Antifurto.progressBar.classList.contains('bg-info'))
return;
Antifurto.progressBar.classList.add('bg-info');
Antifurto.progressBar.classList.remove('bg-success');
Antifurto.progressBar.classList.remove('bg-warning');
Antifurto.progressBar.classList.remove('bg-danger');
} else if (newPercentage <= 2 * quarterSoglia) {
if (Antifurto.progressBar.classList.contains('bg-success'))
return;
Antifurto.progressBar.classList.remove('bg-info');
Antifurto.progressBar.classList.add('bg-success');
Antifurto.progressBar.classList.remove('bg-warning');
Antifurto.progressBar.classList.remove('bg-danger');
} else if (newPercentage <= 3 * quarterSoglia) {
if (Antifurto.progressBar.classList.contains('bg-warning'))
return;
Antifurto.progressBar.classList.remove('bg-info');
Antifurto.progressBar.classList.remove('bg-success');
Antifurto.progressBar.classList.add('bg-warning');
Antifurto.progressBar.classList.remove('bg-danger');
} else {
if (Antifurto.progressBar.classList.contains('bg-danger'))
return;
Antifurto.progressBar.classList.remove('bg-info');
Antifurto.progressBar.classList.remove('bg-success');
Antifurto.progressBar.classList.remove('bg-warning');
Antifurto.progressBar.classList.add('bg-danger');
}
}
/**
* every minute, the progress bar recevies a delta of -5%
*/
static timer(): void {
setInterval(() => {
Antifurto.changeProgressBar(-5);
}, 60000);
//FIXME test to remove the following lines
const github = document.getElementById('boost-progressbar')!;
github.addEventListener('click', () => {
Antifurto.changeProgressBar(3);
if (!Antifurto.status)
alert("attiva l'antifurto");
}, false);
}
/**
* resets the progress bar to 0
*/
static resetProgressBar(): void {
Antifurto.changeProgressBar(-Antifurto.currentPercentage);
}
/**
* get all user's data from the server and show them in the table
*/
static fillTable(): void {
// TODO server get all sensors for the antitheft
const sensori = [];
// TODO server get antitheft status
// Antifurto.status = true/false;
}
/**
* toggle the visibility of the antifurto container
* @param {Boolean} visible whether the container should be visible or not
*/
static toggleContainer(visible: Boolean): void {
if (visible) {
Antifurto.antifurtoContainer.classList.remove('invisible');
} else {
Antifurto.antifurtoContainer.classList.add('invisible');
}
}
/**
* @returns the maximum value of the progress bar
*/
static maxProgressBar(): number {
if(Antifurto.soglia == null)
return 100;
return Antifurto.soglia + (Antifurto.soglia * 0.2);
}
}
export default Antifurto;