vue fa meglio

This commit is contained in:
boyska 2022-08-20 17:14:36 +02:00
parent c7c69a8c90
commit a0dc1d5715
3 changed files with 40 additions and 6 deletions

View file

@ -1,5 +1,8 @@
<!doctype html>
<html>
<head>
<title>Eliminacode</title>
</head>
<body data-cid="0">
<div id="app">
</div>

View file

@ -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)
}

View file

@ -1,7 +1,20 @@
const template = ` <div class="counter">{{counter}}</div>`
const template = `<div class="counter">Fila {{queue_desc}}: {{counter}}</div>`
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)
}
}
}