Skip to content
Snippets Groups Projects
toggles.js 3.64 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    "use strict";
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    import Scenari from './scenari.js';
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    import { showAlert, makeElementBounce } from './alerts.js';
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    import Api from './mqtt/api.js';
    
    Elisa Giglio's avatar
    Elisa Giglio committed
    import {getToken} from './authentication/script.js';
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    
    
    /**
     * activates all the toggles available
     */
    
    function setToggleMovement() {//TODO call this function after everything is loaded
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        const toggles = document.querySelectorAll('.toggle-button');
    
        console.log(toggles.length);
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        for (const toggle of toggles) {
    
            setToggleMovement2(toggle);
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        }
    };
    
    /**
     * changes the state of the given toggle
     * @param {HTMLElement} toggle the toggle to move
     */
    
    function setToggleMovement2(toggle) {
        toggle.addEventListener('click', event => {
    
            event.preventDefault();
    
            toggle.classList.toggle('active');
    
        }, false);
    
     * 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');
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        toggle.addEventListener('click', event => {
    
            event.preventDefault();
            if(registerBtn.innerText !== "Registra"){//sto registrando
                showAlert("Attenzione", "Stai registrando uno scenario, termina la registrazione per attivare lo scenario.", false);
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                makeElementBounce(registerBtn);
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            // select the option from the dropdown
    
            Scenari.addNameHere.value = scenarioID;
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            // 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');
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            if (toggle.classList.contains('active')) {
                children[0].innerText = "Disabilitando questo scenario, disabiliterai l'antifurto.";
                children[2].innerText = "Disabilitare lo scenario?";
    
                children[3].classList.add('invisible');
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                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.";
    
                    children[3].classList.remove('invisible');
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                } else {
                    children[2].innerText = "Continuare l'attivazione?";
    
                    children[3].classList.add('invisible');
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                }
                okBtn.innerText = "Attiva";
            }
            // launch the modal
    
            const modalLauncher = document.getElementById('launch-modal-' + scenarioID);
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            modalLauncher.click();
        }, false);
    }
    
    /**
     * 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) {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        toggle.addEventListener('click', event => {
            event.preventDefault();
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            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);
            }
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        }, false);
    }
    
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    function luciToggleCallback(toggle){
        toggle.classList.toggle('active');
    }
    
    
    Elisa Giglio's avatar
    Elisa Giglio committed
    export { setToggleMovement, scenariToggleListener, luciToggleListener};