123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- "use strict"
- var NOTIFY = false
- if ("Notification" in window) {
- NOTIFY = true
- }
- var SERVICEWORKER = false
- if('serviceWorker' in navigator) {
- SERVICEWORKER = true
- }
- var serviceWorker = null
- function initNotify() {
- if(NOTIFY === true) {
- if (Notification.permission !== "denied") {
- Notification.requestPermission().then(function (permission) {
- if (permission !== "granted") {
- NOTIFY = false
- }
- });
- }
- }
- }
- function notify(msg) {
- if(NOTIFY === false) {
- alert(msg)
- return
- }
- if(SERVICEWORKER) {
- console.log("notify: uso la modernità")
- serviceWorker.postMessage({cmd: 'show-notify', msg: msg})
- } else {
- console.log("notify: uso fallback")
- new Notification(msg, {
- vibrate: [200,100, 100,100, 200],
- silent: false,
- lang: 'it',
- });
- }
- }
- function prenotatiWorker(worker, prenotato) {
- worker.postMessage({cmd: "open", baseurl: WS_BASE})
- worker.onmessage = function(e) {
- console.log("ricevo", e)
- console.log("data=", e.data)
- if(e.data >= prenotato) {
- console.log("chiudo")
- notify("sbrigati: siamo al " + e.data)
- worker.postMessage({cmd: "close"})
- } else {
- var field = document.querySelector('#tuonumero');
- field.value = e.data
- }
- }
- }
- function prenotati(prenotato) {
- var url = WS_BASE + "/ws/counter/0";
- let socket = new WebSocket(url);
- socket.onmessage = function(evt) {
- var data = JSON.parse(evt.data)
- if (data.value >= prenotato) {
- notify("eddaje sbrigate")
- socket.close()
- } else {
- var field = document.querySelector('#tuonumero');
- field.value = data.value
- }
- }
- }
- function inizializzaNumero() {
- var url = (WS_BASE + "/counter/0").replace("wss:", "https:");
- var req = new Request(url)
- fetch(req).then((response) => {
- response.json().then((data) => {
- console.log("mi dicono", data)
- var field = document.querySelector('#tuonumero');
- field.value = parseInt(data.value, 10) + 10
- document.getElementById('form').classList.remove('hide')
- })
- })
- }
- (function() {
- var webSocketWorker = null;
- if(SERVICEWORKER) {
- // Create shared worker.
- webSocketWorker = new Worker('/static/js/prenota-webworker-ws.js');
- navigator.serviceWorker.register('/static/js/prenota-sw.js') .then((reg) => {
- serviceWorker = reg.active
- }).catch((error) => {
- console.error("Error in registration of Service Worker", error)
- SERVICEWORKER = false
- webSocketWorker.terminate()
- console.log("fallback a tecnologie piu vecchie")
- })
- }
- inizializzaNumero()
- document.getElementById('btn-prenotati').onclick = function () {
- var val = document.querySelector('#form > input').value
- initNotify()
- var field = document.querySelector('#tuonumero');
- field.setAttribute('disabled', 'disabled')
- var value = parseInt(val,10)
- if(SERVICEWORKER) {
- prenotatiWorker(webSocketWorker, value)
- } else {
- prenotati(value)
- }
- field.value = ""
- }
- })()
|