From a0dc1d5715f6ec8fe1334970ea0ff62b12cf964f Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 20 Aug 2022 17:14:36 +0200 Subject: [PATCH] vue fa meglio --- pizzicore/pages/spa.html | 5 ++++- pizzicore/static/js/spa.js | 24 +++++++++++++++++++++--- pizzicore/static/vue/counter.js | 17 +++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/pizzicore/pages/spa.html b/pizzicore/pages/spa.html index 5ff43bc..dba231e 100644 --- a/pizzicore/pages/spa.html +++ b/pizzicore/pages/spa.html @@ -1,5 +1,8 @@ + - + + Eliminacode +
diff --git a/pizzicore/static/js/spa.js b/pizzicore/static/js/spa.js index d10843f..fa0e360 100644 --- a/pizzicore/static/js/spa.js +++ b/pizzicore/static/js/spa.js @@ -46,17 +46,35 @@ function get_url(cid) return url; } -function do_websocket(cid, callback) +function do_websocket(cid, cb_on_new_value) { const socket = new WebSocket(get_url(cid)); socket.onmessage = function(evt) { var msg = JSON.parse(evt.data) - callback(msg.value) + cb_on_new_value(msg.value) } socket.onclose = function() { setTimeout(do_websocket, 3000) } socket.onerror = socket.onclose } -do_websocket() + +/* thanks, https://stackoverflow.com/a/60033403 */ +var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +function printToLetter(number, recursive){ + number-- + if(number < 0) { + if(recursive === true) { + return alphabet[alphabet.length - 1] + } else { + return 'ΓΈ' + } + } + if(number < alphabet.length) { + return alphabet[number] + } + var remainder = number % alphabet.length + var quotient = Math.floor((number-1)/alphabet.length) + return printToLetter(quotient, true) + printToLetter(remainder, true) +} diff --git a/pizzicore/static/vue/counter.js b/pizzicore/static/vue/counter.js index 0f840a8..743c7a1 100644 --- a/pizzicore/static/vue/counter.js +++ b/pizzicore/static/vue/counter.js @@ -1,7 +1,20 @@ -const template = `
{{counter}}
` +const template = `
Fila {{queue_desc}}: {{counter}}
` + export default { props: ["queue_num"], - data() { return { counter: -1 } }, + data() { + var me = this; + console.log("I am", this); + do_websocket(me.queue_num, function(new_value) { + me.counter = new_value + }) + return { counter: -1 } + }, template: template, + computed: { + queue_desc: function() { + return printToLetter(parseInt(this.queue_num, 10) + 1) + } + } }