diff --git a/public/javascripts/index.js b/public/javascripts/index.js index bea0b8a..5d37aac 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -1,14 +1,15 @@ const socket = io(); +const deckSelect = document.getElementById("deck-select"); const deck = document.getElementById("deck"); socket.on("connected", () => { console.log("Connected !"); socket.emit("getDeck"); + socket.emit("getDecks"); }); socket.on("getDeck", d => { let data = d.data, name = d.name; - console.log(name); deck.innerHTML = ""; for (let x = 0; x < data.x; x++) { @@ -27,6 +28,13 @@ socket.on("getDeck", d => { setSlot(name, col, x, y); }); +socket.on("getDecks", data => { + deckSelect.innerHTML = ""; + for (const deck of data) + deckSelect.insertAdjacentHTML("beforeend", ``); + M.FormSelect.init(deckSelect); +}); + socket.on("trigger", data => { if (data.error) alert(data.error) @@ -37,21 +45,33 @@ socket.on("setSlot", data => { setSlot(data.name, data.data, ...data.position) }); +deckSelect.addEventListener("change", ev => { + ev.stopPropagation(); + socket.emit("getDeck", deckSelect.value); +}); + function setSlot(name, data, x, y) { let e = document.getElementById(`r${x}c${y}`); - if (e && data) { - if (data.image) - e.insertAdjacentHTML("beforeend", `${data.text}`); - else if (data.text) - e.insertAdjacentHTML("beforeend", `

${data.text}

`); - e.addEventListener("click", ev => { - ev.stopPropagation(); - socket.emit("trigger", [name, x, y]); - }) - } else if (e) { + if (e) { let d = document.createElement("div"); d.id = e.id; e.replaceWith(d); + e = d; + if (data) { + if (data.image) + e.insertAdjacentHTML("beforeend", `${data.text}`); + else if (data.text) + e.insertAdjacentHTML("beforeend", `

${data.text}

`); + + e.addEventListener("click", ev => { + ev.stopPropagation(); + socket.emit("trigger", [name, x, y]); + }) + } } } + +document.addEventListener("DOMContentLoaded", () => { + M.AutoInit(); +}); diff --git a/public/javascripts/settings/decks.js b/public/javascripts/settings/decks.js index 4ed619d..1ebfb79 100644 --- a/public/javascripts/settings/decks.js +++ b/public/javascripts/settings/decks.js @@ -1,4 +1,5 @@ const socket = io(); +const deckSelect = document.getElementById("deck-select"); const deck = document.getElementById("deck"); const modal = document.getElementById("modal"); const type = document.getElementById("type"); @@ -11,6 +12,7 @@ let modalInstance, types, slot; socket.on("connected", () => { console.log("Connected !"); socket.emit("getDeck"); + socket.emit("getDecks"); socket.emit("getType"); }); @@ -45,6 +47,13 @@ socket.on("getDeck", d => { } }); +socket.on("getDecks", data => { + deckSelect.innerHTML = ""; + for (const deck of data) + deckSelect.insertAdjacentHTML("beforeend", ``); + M.FormSelect.init(deckSelect); +}); + socket.on("getSlot", data => { slot = data; customs.innerHTML = ""; @@ -75,16 +84,22 @@ socket.on("setSlot", data => { else { modalInstance.close(); let e = document.getElementById(`r${data.position[0]}c${data.position[1]}`); - if (e && data.data) { - if (data.data.image) - e.insertAdjacentHTML("beforeend", `${data.data.text}`); - else if (data.data.text) - e.insertAdjacentHTML("beforeend", `

${data.data.text}

`); - } else if (e) + if (e) { e.innerHTML = ""; + if (data.data) + if (data.data.image) + e.insertAdjacentHTML("beforeend", `${data.data.text}`); + else if (data.data.text) + e.insertAdjacentHTML("beforeend", `

${data.data.text}

`); + } } }); +deckSelect.addEventListener("change", ev => { + ev.stopPropagation(); + socket.emit("getDeck", deckSelect.value); +}); + document.getElementById("save").addEventListener("click", ev => { ev.stopPropagation(); diff --git a/sockets/getDecks.js b/sockets/getDecks.js new file mode 100644 index 0000000..0350016 --- /dev/null +++ b/sockets/getDecks.js @@ -0,0 +1,8 @@ +const db = require("../db.json"); + + +module.exports = socket => { + return () => { + socket.emit("getDecks", Object.keys(db.decks)); + } +}; diff --git a/sockets/index.js b/sockets/index.js index 35d28d5..092f94e 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -1,5 +1,6 @@ module.exports = socket => { socket.on("getDeck", require("./getDeck")(socket)); + socket.on("getDecks", require("./getDecks")(socket)); socket.on("getSlot", require("./getSlot")(socket)); socket.on("getType", require("./getType")(socket)); socket.on("setSlot", require("./setSlot")(socket)); diff --git a/views/index.pug b/views/index.pug index 7254d4f..3407f99 100644 --- a/views/index.pug +++ b/views/index.pug @@ -4,4 +4,20 @@ block content h1.center OpenDeck .container#deck + .fixed-action-btn + a(href="#modal").btn-floating.btn-large.green.modal-trigger + i.large.material-icons toc + ul + li + a(href="/settings").btn-floating.red + i.material-icons settings + + #modal.modal + .modal-content + .input-field + select#deck-select + label Deck + .modal-footer + a#save.modal-close.waves-effect.waves-grey.btn-flat(href="#!") Close + script(src="/javascripts/index.js") diff --git a/views/layout.pug b/views/layout.pug index bbaa814..58833a7 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -3,6 +3,7 @@ html head title= "OpenDeck" + (title ? " - " + title: "") link(rel="stylesheet", href="/stylesheets/style.css") + link(rel="stylesheet", href="https://fonts.googleapis.com/icon?family=Material+Icons") script(src="/javascripts/materialize.js") script(src="/socket.io/socket.io.js") body diff --git a/views/settings/decks.pug b/views/settings/decks.pug index 41e629b..3c07d1d 100644 --- a/views/settings/decks.pug +++ b/views/settings/decks.pug @@ -3,7 +3,6 @@ extends ./settings block settings .input-field select#deck-select - option(value="default" selected) Default label Deck .container#deck diff --git a/views/settings/settings.pug b/views/settings/settings.pug index 2f9102d..3d84d22 100644 --- a/views/settings/settings.pug +++ b/views/settings/settings.pug @@ -6,5 +6,5 @@ block content a.brand-logo.right(href="/") OpenDeck ul#nav-mobile.left.hide-on-med-and-down li - a(href="sass.html") Decks + a(href="/settings") Decks block settings