OpenDeck/public/javascripts/index.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-03-08 11:54:28 +01:00
const socket = io();
2021-03-08 14:16:18 +01:00
const deck = document.getElementById("deck");
2021-03-08 11:54:28 +01:00
2021-03-08 14:16:18 +01:00
socket.on("connected", () => {
2021-03-08 11:54:28 +01:00
console.log("Connected !");
2021-03-08 14:16:18 +01:00
socket.emit("getDeck");
});
socket.on("getDeck", data => {
deck.innerHTML = "";
for (let x = 0; x < data.x; x++) {
const r = document.createElement("div");
r.id = "r" + x;
for (let y = 0; y < data.y; y++) {
const c = document.createElement("div");
c.id = r.id+"c"+y;
r.insertAdjacentElement("beforeend", c);
}
deck.insertAdjacentElement("beforeend", r);
}
for (const [x, cols] of Object.entries(data.rows))
for (const [y, col] of Object.entries(cols)) {
const e = document.getElementById(`r${x}c${y}`);
if (e) {
if (col.image)
e.insertAdjacentHTML("beforeend", `<img src="${col.image}" alt="${col.text}">`);
else if (col.text)
e.insertAdjacentHTML("beforeend", `<p>${col.text}</p>`);
e.addEventListener("click", ev => {
ev.stopPropagation();
socket.emit("trigger", [x, y]);
})
}
}
});
socket.on("trigger", data => {
if (data.error)
alert(data.error)
2021-03-08 11:54:28 +01:00
});