Newer
Older
import { showAlert, makeElementBounce } from './alerts.js';
/**
* activates all the toggles available
*/
function setToggleMovement() {//TODO call this function after everything is loaded
const toggles = document.querySelectorAll('.toggle-button');
/**
* changes the state of the given toggle
* @param {HTMLElement} toggle the toggle to move
*/
function setToggleMovement2(toggle) {
toggle.addEventListener('click', event => {
* edits the modal used for activating/deactivating a scenario when the user clicks on a toggle
* @param {HTMLElement} toggle the toggle to move
* @param {string} scenarioID the id of the scenario which is being toggled
*/
function scenariToggleListener(toggle, scenarioID) {
const registerBtn = document.getElementById('scenari-registra');
event.preventDefault();
if(registerBtn.innerText !== "Registra"){//sto registrando
showAlert("Attenzione", "Stai registrando uno scenario, termina la registrazione per attivare lo scenario.", false);
Scenari.addNameHere.value = scenarioID;
// change text of the modal based on toggle state
const modalBody = document.getElementById('attiva-scenari-modal').querySelector('.modal-body');
const children = modalBody.children;
//p, fieldset, p.more-margin-top, p
//0, 1, 2, 3
const okBtn = document.getElementById('attiva-scenari-btn');
if (toggle.classList.contains('active')) {
children[0].innerText = "Disabilitando questo scenario, disabiliterai l'antifurto.";
children[2].innerText = "Disabilitare lo scenario?";
okBtn.innerText = "Disabilita";
}
else {
children[0].innerText = "Attivando questo scenario, abiliterai l'antifurto.";
if (Scenari.scenarioAttivoToggle != null) {
children[2].innerText = "Inoltre disabiliterai gli altri scenari attivi.";
} else {
children[2].innerText = "Continuare l'attivazione?";
}
okBtn.innerText = "Attiva";
}
// launch the modal
const modalLauncher = document.getElementById('launch-modal-' + scenarioID);
/**
* switches the toggle and updates the status of the light
* @param {luceTemplate} luce the luce JSON luce (used to change its status)
* @param {HTMLElement} toggle the toggle to move
function luciToggleListener(luce,toggle) {
toggle.addEventListener('click', event => {
event.preventDefault();
luce.stato = !toggle.classList.contains('active');
// HERE server send the toggle state
try{
Api.setLuciStato(luce,toggle,luciToggleCallback);
} catch (error){
console.log(error);
showAlert("Errore",error.message,false);
}
function luciToggleCallback(toggle){
toggle.classList.toggle('active');
}
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
async function statoDomainToggle(domain, toggle) {
toggle.addEventListener('click', event => {
event.preventDefault();
const token = getToken();
const stop = toggle.classList.contains('active');
if(stop) {
// toggle attiva
const response = await fetch('http://localhost:3001/stop', {
method: 'POST',
headers: , // DA FARE: inserire il token
body: JSON.stringify({domain: domain.nome})
});
if(response.ok) {
toggle.classList.remove('active');
}
else {
throw new Error('Impossibile fermare il dominio, provare piu\' tardi');
}
}
else {
const response = await fetch('http://localhost:3001/start', {
method: 'POST',
headers: , // DA FARE: inserire il token
body: JSON.stringify({domain: domain.nome})
});
if(response.ok) {
toggle.classList.add('active');
}
else {
throw new Error('Impossibile far partire il dominio, provare piu\' tardi');
}
}
});
}
export { setToggleMovement, scenariToggleListener, luciToggleListener, statoDomainToggle};