prenota.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict"
  2. var NOTIFY = false
  3. if ("Notification" in window) {
  4. NOTIFY = true
  5. }
  6. var SERVICEWORKER = false
  7. if('serviceWorker' in navigator) {
  8. SERVICEWORKER = true
  9. }
  10. var serviceWorker = null
  11. function initNotify() {
  12. if(NOTIFY === true) {
  13. if (Notification.permission !== "denied") {
  14. Notification.requestPermission().then(function (permission) {
  15. if (permission !== "granted") {
  16. NOTIFY = false
  17. }
  18. });
  19. }
  20. }
  21. }
  22. function notify(msg) {
  23. if(NOTIFY === false) {
  24. alert(msg)
  25. return
  26. }
  27. if(SERVICEWORKER) {
  28. console.log("notify: uso la modernità")
  29. serviceWorker.postMessage({cmd: 'show-notify', msg: msg})
  30. } else {
  31. console.log("notify: uso fallback")
  32. new Notification(msg, {
  33. vibrate: [200,100, 100,100, 200],
  34. silent: false,
  35. lang: 'it',
  36. });
  37. }
  38. }
  39. function prenotatiWorker(worker, prenotato) {
  40. worker.postMessage({cmd: "open", baseurl: WS_BASE})
  41. worker.onmessage = function(e) {
  42. console.log("ricevo", e)
  43. console.log("data=", e.data)
  44. if(e.data >= prenotato) {
  45. console.log("chiudo")
  46. notify("sbrigati: siamo al " + e.data)
  47. worker.postMessage({cmd: "close"})
  48. } else {
  49. var field = document.querySelector('#now');
  50. field.textContent = e.data
  51. }
  52. }
  53. }
  54. function prenotati(prenotato) {
  55. var url = WS_BASE + "/ws/counter/0";
  56. let socket = new WebSocket(url);
  57. socket.onmessage = function(evt) {
  58. var data = JSON.parse(evt.data)
  59. if (data.value >= prenotato) {
  60. notify("eddaje sbrigate")
  61. socket.close()
  62. } else {
  63. var field = document.querySelector('#tuonumero');
  64. field.value = data.value
  65. }
  66. }
  67. }
  68. function inizializzaNumero() {
  69. var url = (WS_BASE + "/counter/0").replace("wss:", "https:");
  70. var req = new Request(url)
  71. fetch(req).then((response) => {
  72. response.json().then((data) => {
  73. console.log("mi dicono", data)
  74. var field = document.querySelector('#tuonumero');
  75. field.value = parseInt(data.value, 10) + 10
  76. document.getElementById('form').classList.remove('hide')
  77. })
  78. })
  79. }
  80. (function() {
  81. var webSocketWorker = null;
  82. if(SERVICEWORKER) {
  83. // Create shared worker.
  84. webSocketWorker = new Worker('/static/js/prenota-webworker-ws.js');
  85. navigator.serviceWorker.register('/static/js/prenota-sw.js') .then((reg) => {
  86. serviceWorker = reg.active
  87. }).catch((error) => {
  88. console.error("Error in registration of Service Worker", error)
  89. SERVICEWORKER = false
  90. webSocketWorker.terminate()
  91. console.log("fallback a tecnologie piu vecchie")
  92. })
  93. }
  94. inizializzaNumero()
  95. var btn = document.getElementById('btn-prenotati')
  96. btn.onclick = function () {
  97. var val = document.querySelector('#form > input').value
  98. initNotify()
  99. var field = document.querySelector('#tuonumero');
  100. field.setAttribute('disabled', 'disabled')
  101. document.getElementById('now-section').classList.remove('hide')
  102. btn.setAttribute('disabled', 'disabled')
  103. var value = parseInt(val,10)
  104. if(SERVICEWORKER) {
  105. prenotatiWorker(webSocketWorker, value)
  106. } else {
  107. prenotati(value)
  108. }
  109. }
  110. })()