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
"use strict";
import Antifurto from "./antifurto.js";
import Scenari from "./scenari.js";
import { luciToggleListener } from "./toggles.js";
import { makeElementBounce } from "./alerts.js";
export interface luceTemplate {
"luogo": String,
"stato": Boolean,
"id": String
};
class Luci {
/**
* the div where everything related to the Luci is stored
*/
static luciContainer = document.getElementById('luciContainer')!;
/**
* the array where all of the user's luci are stored
*/
static luciBucket: luceTemplate[] = [];
/**
* describes how many lights have already been added
*/
static index: number = 0;
constructor() {
Luci.init();
Luci.modalListener();
}
static init(): void {
Luci.fillTable();
const luciBtn = document.getElementById('mainButtonContainer')!.children[0];
luciBtn.addEventListener('click', () => {
Luci.toggleContainer(true);
Scenari.toggleContainer(false);
Antifurto.toggleContainer(false);
}, false);
// listen for the closing modal
const cancelBtn = document.querySelector('#luci-modal .btn.btn-secondary')!;
cancelBtn.addEventListener('click', () => {
// delay 0.5s to allow the modal to close (otherwise it's not pretty)
setTimeout(() => {
Luci.selectReset();
}, 500);
}, false);
}
/**
* shows and listens for the modal that allows the user to add a new light
*/
static modalListener(): void {
const select = document.getElementById('luogoLuce')! as HTMLSelectElement;
const altroInput = document.getElementById('nuovaStanzaDIV')!;
const altroClassList = altroInput.classList;
const form = document.getElementById('search-form')!;
const inputField = altroInput.querySelector('input')!;
// show or hide the text input for the new room
select.addEventListener('change', event => {
event.preventDefault();
const selected = (event.target! as HTMLSelectElement).value;
if (selected === "altro") {
altroClassList.remove('invisible');
// set altroInput to required
inputField.setAttribute('required', 'required');
} else {
altroClassList.add('invisible');
// remove required from altroInput
inputField.removeAttribute('required');
}
}, false);
form.addEventListener('submit', event => {
event.preventDefault();
const selectedValue = select.value;
if (selectedValue === "unselected") {
alert("Selezionare una stanza nel menu a tendina.");
return;
}
var stanza = select.options[select.selectedIndex].text.trim(); // get selected option text
if (selectedValue === "altro") {
const nuovaStanza = altroInput.querySelector('input')!.value.trim();
if (nuovaStanza == null || nuovaStanza == "" || nuovaStanza.toLowerCase() === "altro") {
alert("Inserire un nome valido per la stanza.");
return;
}
for (const stanza of Luci.luciBucket)
if (stanza.luogo === nuovaStanza) {
alert("Stanza già esistente");
return;
}
stanza = nuovaStanza;
// reset the select
Luci.selectReset();
}
stanza = stanza[0].toUpperCase() + stanza.substring(1, stanza.length);
// TODO server send stanza
// add stanza to table
const luceJSON = Luci.createLight(stanza, false);
Luci.showNewLight(luceJSON);
// close modal
const closeBtn = document.querySelector('#luci-modal .btn.btn-secondary')! as HTMLButtonElement;
closeBtn.click();
}, false);
}
/**
* creates a new light and adds it to the bucket
* @param {String} luogo the name of the room
* @param {Boolean} stato the status of the light
* @returns {luceTemplate} the new light
*/
static createLight(luogo: String, stato: Boolean): luceTemplate {
const luce: luceTemplate = {
"luogo": luogo,
"stato": stato,
"id": `luce-${Luci.index}`,
};
Luci.luciBucket.push(luce);
Luci.index++;
return luce;
}
/**
* shows a new light in the table after creating it
* @param {luceTemplate} luceJSON the light to show
*/
static showNewLight(luceJSON: luceTemplate): void {
const luce = luceJSON.id == null ? Luci.createLight(luceJSON.luogo,luceJSON.stato) : luceJSON;
const table = document.getElementById('table-row-luci')!;
const row = document.createElement('tr');
row.innerHTML = `
<th scope="row" id="${luce.id}">${luce.luogo}</th>
<td>
<div class="switch-container no-box-sizing">
<div class="toggle-button no-box-sizing ${luce.stato ? 'active' : ''}">
<div class="inner-circle no-box-sizing" />
</div>
</div>
</td>`;
const lastRow = table.lastElementChild;
table.insertBefore(row, lastRow);
// add listener for switch
const toggle: HTMLElement = row.querySelector('.toggle-button')!;
luciToggleListener(toggle, luce);
// remove options from the select
const select = document.getElementById('luogoLuce')! as HTMLSelectElement;
const options = select.options;
const luogoLowercase = luce.luogo.toLowerCase();
for (const option of options) {
if (option.text.toLowerCase() === luogoLowercase) {
select.removeChild(option);
break;
}
}
}
/**
* resets the select menu used to add a new light
*/
static selectReset(): void {
const select = document.getElementById('luogoLuce') as HTMLSelectElement;
const altroInput = document.getElementById('nuovaStanzaDIV')!;
const inputField = altroInput.querySelector('input')!;
select.selectedIndex = 0;
altroInput.classList.add('invisible');
inputField.removeAttribute('required');
inputField.value = "";
}
/**
* fills the table with the user's luci from the server
*/
static fillTable(): void {
// TODO server get all lights
const luci = [];
// for each, add to table and to luciBucket
for (const luce of luci) {
Luci.showNewLight(luce);
}
}
/**
* toggle the visibility of the luci container
* @param {Boolean} visible
*/
static toggleContainer(visible: boolean): void {
if (visible) {
Luci.luciContainer.classList.remove('invisible');
if (Luci.luciBucket.length === 0) {
// make the + button bounce
const plusBtn = document.querySelector('.fa-circle-plus');
makeElementBounce(plusBtn);
}
} else {
Luci.luciContainer.classList.add('invisible');
}
}
}
export default Luci;