ratatouille/static/home.js
2025-06-24 01:41:50 +02:00

88 lines
No EOL
3 KiB
JavaScript

function update_dom_to_light_on(room) {
document.getElementById(`lamp-${room}`).querySelector("img").src = "/static/lightbulb-on.svg"
document.getElementById(`lamp-${room}`).querySelector("img").alt = "on"
}
function update_dom_to_light_off(room) {
document.getElementById(`lamp-${room}`).querySelector("img").src = "/static/lightbulb-off.svg"
document.getElementById(`lamp-${room}`).querySelector("img").alt = "off"
}
function light_on(room, callback) {
const req = new XMLHttpRequest();
req.addEventListener("load", function() {
if(this.responseText != "ok") {
alert("Erreur lors de l'allumage de la lampe " + room)
return
}
update_dom_to_light_on(room)
callback()
});
req.open("GET", `/light_on?room=${room}`);
req.send();
}
function light_off(room, callback) {
const req = new XMLHttpRequest();
req.addEventListener("load", function() {
if(this.responseText != "ok") {
alert("Erreur lors de l'extinction de la lampe" + room)
return
}
update_dom_to_light_off(room)
callback()
});
req.open("GET", `/light_off?room=${room}`);
req.send();
}
// cuisine
function light_on_cuisine() {
light_on("cuisine", function() {
document.getElementById("lamp-cuisine").removeEventListener("click", light_on_cuisine);
document.getElementById("lamp-cuisine").addEventListener("click", light_off_cuisine);
})
}
function light_off_cuisine() {
light_off("cuisine", function() {
document.getElementById("lamp-cuisine").removeEventListener("click", light_off_cuisine);
document.getElementById("lamp-cuisine").addEventListener("click", light_on_cuisine);
})
}
document.getElementById("lamp-cuisine").addEventListener("click", light_on_cuisine);
// salon
function light_on_salon() {
light_on("salon", function() {
document.getElementById("lamp-salon").removeEventListener("click", light_on_salon);
document.getElementById("lamp-salon").addEventListener("click", light_off_salon);
})
}
function light_off_salon() {
light_off("salon", function() {
document.getElementById("lamp-salon").removeEventListener("click", light_off_salon);
document.getElementById("lamp-salon").addEventListener("click", light_on_salon);
})
}
document.getElementById("lamp-salon").addEventListener("click", light_on_salon);
// bureau
function light_on_bureau() {
light_on("bureau", function() {
document.getElementById("lamp-bureau").removeEventListener("click", light_on_bureau);
document.getElementById("lamp-bureau").addEventListener("click", light_off_bureau);
})
}
function light_off_bureau() {
light_off("bureau", function() {
document.getElementById("lamp-bureau").removeEventListener("click", light_off_bureau);
document.getElementById("lamp-bureau").addEventListener("click", light_on_bureau);
})
}
document.getElementById("lamp-bureau").addEventListener("click", light_on_bureau);