Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 21, 2023 02:00
Show Gist options
  • Save code-boxx/7124311e4a4787ccb2a75ed7378d42c7 to your computer and use it in GitHub Desktop.
Save code-boxx/7124311e4a4787ccb2a75ed7378d42c7 to your computer and use it in GitHub Desktop.
Javascript Customer Queue

JAVASCRIPT CUSTOMER QUEUE SYSTEM

https://code-boxx.com/javascript-customer-queue-system/

NOTES

  1. It is best to have 2 monitors for this to work properly – One screen for the "admin", another to display the "current queue number".
  2. Click on "ALL" to issue a queue number. Click on “NOW” to advance to the next customer.
  3. Gist does not allow mp3 files, had to get creative... Save ding-dong.png below as ding-dong.mp3, or download your own notification sound.

IMAGES

favicon icon-512 ding-dong

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<!-- TITLE + CHARSET + DESCRIPTION + VIEWPORT + FAVICON -->
<title>JS Customer Queue</title>
<meta charset="utf-8">
<meta name="description" content="JS Customer Queue">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.5">
<link rel="icon" href="favicon.png" type="image/png">
<!-- ANDROID + CHROME + APPLE + WINDOWS APP -->
<meta name="mobile-web-app-capable" content="yes">
<meta name="theme-color" content="white">
<link rel="apple-touch-icon" href="icon-512.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="JS Queue">
<meta name="msapplication-TileImage" content="icon-512.png">
<meta name="msapplication-TileColor" content="#ffffff">
<!-- WEB APP MANIFEST -->
<!-- https://web.dev/add-manifest/ -->
<link rel="manifest" href="3-manifest.json">
<!-- SERVICE WORKER -->
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("4-worker.js");
}
</script>
<!-- STYLESHEET + JAVASCRIPT -->
<link rel="stylesheet" href="2-js-queue.css">
<script src="2-js-queue.js"></script>
</head>
<body>
<!-- (A) CURRENT QUEUE NUMBER -->
<div id="qNow" onclick="queue.next()">
<div id="qNowNum">0</div>
<div id="qNowTitle">NOW</div>
</div>
<!-- (B) TOTAL IN QUEUE -->
<div id="qAll" onclick="queue.add()">
<div id="qAllNum">0</div>
<div id="qAllTitle">ALL</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Customer Queue</title>
<link rel="stylesheet" href="2-js-queue.css">
</head>
<body>
<div id="qNowDis">0</div>
</body>
</html>
/* (A) WHOLE PAGE */
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
body {
display: flex;
padding: 0; margin: 0;
}
/* (B) "ADMIN" QUEUE PAGE */
#qNow, #qAll {
width: 50%;
text-align: center;
cursor: pointer;
}
#qNow { background: #d7e4ff; }
#qAll { background: #ffd5d5; }
#qNowNum, #qAllNum, #qNowTitle, #qAllTitle { padding: 20px 0; }
#qNowNum, #qAllNum { font-size: 40px; }
#qNowTitle, #qAllTitle { font-size: 24px; }
/* (C) DISPLAY BOARD QUEUE NUMBER */
#qNowDis {
display: flex;
width: 100vw; height: 100vh;
align-items: center; justify-content: center;
font-size: 60px;
font-weight: 700;
}
var queue = {
// (A) INIT
now : 0, // current queue number
all : 0, // total in queue
alarm : null, // alarm sound
hNow : null, // html current queue number
hAll : null, // html total in queue
hDis : null, // html current queue number (display board)
init : () => {
// (A1) OPEN "DISPLAY BOARD"
let board = window.open("1b-js-queue.html", "_blank", "popup");
// (A2) GET HTML ELEMENTS
queue.hNow = document.getElementById("qNowNum");
queue.hAll = document.getElementById("qAllNum");
board.onload = () => {
queue.hDis = board.document.getElementById("qNowDis");
};
// (A3) LOAD ALARM SOUND
queue.alarm = new Audio("ding-dong.mp3");
},
// (B) ADD CUSTOMER TO QUEUE
add : () => {
// (B1) ADD TO TOTAL QUEUE NUMBER
queue.all++;
queue.hAll.innerHTML = queue.all;
// (B2) PRINT QUEUE NUMBER
// possible to generate a new window with queue number
// but print will always open the "select printer" dialog box
/*
let qslip = window.open();
qslip.onload = () => {
qslip.document.body.innerHTML = queue.all;
qslip.print();
};
*/
},
// (C) ADVANCE QUEUE NUMBER
next : () => { if (queue.now < queue.all) {
// (C1) ADD TO CURRENT QUEUE NUMBER
queue.now++;
queue.hNow.innerHTML = queue.now;
queue.hDis.innerHTML = queue.now;
// (C2) PLAY ALARM SOUND
if (queue.alarm.paused) { queue.alarm.play(); }
else { queue.alarm.currentTime = 0; }
}}
};
window.addEventListener("load", queue.init);
{
"short_name": "Queue",
"name": "JS Queue",
"icons": [{
"src": "favicon.png",
"sizes": "64x64",
"type": "image/png"
}, {
"src": "icon-512.png",
"sizes": "512x512",
"type": "image/png"
}],
"start_url": "1a-js-queue.html",
"scope": "/",
"background_color": "white",
"theme_color": "white",
"display": "standalone"
}
// (A) CREATE/INSTALL CACHE
self.addEventListener("install", evt => {
self.skipWaiting();
evt.waitUntil(
caches.open("JSQueue")
.then(cache => cache.addAll([
"favicon.png",
"icon-512.png",
"ding-dong.mp3",
"1a-js-queue.html",
"1b-js-queue.html",
"2-js-queue.js",
"2-js-queue.css"
]))
.catch(err => console.error(err))
);
});
// (B) CLAIM CONTROL INSTANTLY
self.addEventListener("activate", evt => self.clients.claim());
// (C) LOAD FROM CACHE FIRST, FALLBACK TO NETWORK IF NOT FOUND
self.addEventListener("fetch", evt => evt.respondWith(
caches.match(evt.request).then(res => res || fetch(evt.request))
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment