Newer
Older
"use strict";
import Antifurto from "./antifurto.js";
import Scenari from "./scenari.js";
import { luciToggleListener } from "./toggles.js";
import { showAlert, makeElementBounce } from "./alerts.js";
const luceTemplate = {
"luogo": "",
"stato": 0,
"id": ""
};
Luci.luciContainer = document.getElementById('luciContainer');
const select = document.getElementById('luogoLuce');
const altroInput = document.getElementById('nuovaStanzaDIV');
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.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');
}
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
const nuovaStanza = altroInput.querySelector('#nuovaStanza').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 = stanza[0].toUpperCase() + stanza.substring(1, stanza.length);
const luceJSON = Luci.createLight(stanza, false);
Luci.showNewLight(luceJSON);
const closeBtn = document.querySelector('#luci-modal .btn.btn-secondary');
const luciBtn = document.getElementById('mainButtonContainer').children[0];
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);
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
static toggleContainer(visible) {
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);
}
Luci.luciContainer.classList.add('invisible');
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
static createLight(name, status) {
const luce = Object.assign({}, luceTemplate);
luce.luogo = name;
luce.stato = status ? 1 : 0;
luce.id = `luce-${Luci.index}`;
Luci.luciBucket.push(luce);
Luci.index++;
return luce;
}
static showNewLight(luceJSON) {
const luce = luceJSON.id == null ? Luci.createLight({ luceJSON }) : 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 === 1 ? '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 = row.querySelector('.toggle-button');
luciToggleListener(toggle,luce);
// remove options from the select
const select = document.getElementById('luogoLuce');
const options = select.options;
const luogoLowercase = luce.luogo.toLowerCase();
for (const option of options) {
if (option.text.toLowerCase() === luogoLowercase) {
select.removeChild(option);
break;
}
}
}
static selectReset() {
const select = document.getElementById('luogoLuce');
const altroInput = document.getElementById('nuovaStanzaDIV');
const inputField = altroInput.querySelector('input');
select.selectedIndex = 0;
altroInput.classList.add('invisible');
inputField.removeAttribute('required');
inputField.value = "";
}