Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 26, 2023 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/819496373eb40597648417bee07d2d13 to your computer and use it in GitHub Desktop.
Save code-boxx/819496373eb40597648417bee07d2d13 to your computer and use it in GitHub Desktop.
Javascript Shopping List

JAVASCRIPT SHOPPING LIST

https://code-boxx.com/shopping-list-vanilla-javascript/

IMAGES

favicon

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.

/* (A) SHARED */
#shop-wrap input {
padding: 10px;
border: 0;
}
#shop-wrap input[type=button], #shop-wrap input[type=submit] {
cursor: pointer;
color: #fff;
background: #5a75d6;
}
#shop-form, .item-row {
display: flex;
align-items: stretch;
}
#shop-item, .item-name { flex-grow: 1; }
/* (B) WRAPPER */
#shop-wrap {
max-width: 500px;
padding: 15px;
background: #f2f2f2;
border: 2px solid #eee;
}
/* (C) SHOPPING LIST */
.item-row { margin-top: 10px; }
.item-name {
padding: 10px;
background: #fff;
}
.item-name.item-got { background: #f5fffa; }
.item-name.item-got:before {
content: "\02713";
margin-right: 5px;
font-weight: bold;
color: #00d036;
}
.item-del { background: #de1919 !important; }
/* (X) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
<!DOCTYPE html>
<html>
<head>
<title>
Simple Javascript Shopping List
</title>
<meta charset="utf-8">
<!-- WEB APP & ICONS -->
<link rel="icon" href="favicon.png" type="image/png">
<meta name="mobile-web-app-capable" content="yes">
<meta name="theme-color" content="white">
<link rel="apple-touch-icon" href="favicon.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 Shopping List">
<meta name="msapplication-TileImage" content="favicon.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("3-worker.js");
}</script>
<!-- STYLESHEET + JAVASCRIPT -->
<link href="1-shop-list.css" rel="stylesheet">
<script src="2-shop-list.js"></script>
</head>
<body>
<h1>SHOPPING LIST</h1>
<div id="shop-wrap">
<!-- (A) ADD NEW ITEM -->
<form id="shop-form">
<input type="text" id="shop-item" placeholder="Item Name" required disabled>
<input type="submit" id="shop-add" value="Add" disabled>
</form>
<!-- (B) SHOPPING LIST -->
<div id="shop-list"></div>
</div>
</body>
</html>
var slist = {
// (A) INITIALIZE SHOPPING LIST
items : [], // current shopping list
hform : null, // html add item <form>
hitem : null, // html add item <input> field
hadd : null, // html add item submit button
hlist : null, // html <div> shopping list
init : () => {
// (A1) GET HTML ELEMENTS
slist.hform = document.getElementById("shop-form");
slist.hitem = document.getElementById("shop-item");
slist.hadd = document.getElementById("shop-add");
slist.hlist = document.getElementById("shop-list");
// (A2) "ACTIVATE" HTML ADD ITEM FORM
slist.hitem.setAttribute("autocomplete", "off");
slist.hform.onsubmit = slist.add;
slist.hitem.disabled = false;
slist.hadd.disabled = false;
// (A3) RESTORE PREVIOUS SHOPPING LIST
if (localStorage.items == undefined) { localStorage.items = "[]"; }
slist.items = JSON.parse(localStorage.items);
// (A4) DRAW HTML SHOPPING LIST
slist.draw();
},
// (B) SAVE SHOPPING LIST INTO LOCAL STORAGE
save : () => {
if (localStorage.items == undefined) { localStorage.items = "[]"; }
localStorage.items = JSON.stringify(slist.items);
},
// (C) ADD NEW ITEM TO THE LIST
add : evt => {
// (C1) PREVENT FORM SUBMIT
evt.preventDefault();
// (C2) ADD NEW ITEM TO LIST
slist.items.push({
name : slist.hitem.value, // item name
done : false // true for "got it", false for "not yet"
});
slist.hitem.value = "";
slist.save();
// (C3) REDRAW HTML SHOPPING LIST
slist.draw();
},
// (D) DELETE SELECTED ITEM
delete : id => { if (confirm("Remove this item?")) {
slist.items.splice(id, 1);
slist.save();
slist.draw();
}},
// (E) TOGGLE ITEM BETWEEN "GOT IT" OR "NOT YET"
toggle : id => {
slist.items[id].done = !slist.items[id].done;
slist.save();
slist.draw();
},
// (F) DRAW THE HTML SHOPPING LIST
draw : () => {
// (F1) RESET HTML LIST
slist.hlist.innerHTML = "";
// (F2) NO ITEMS
if (slist.items.length == 0) {
slist.hlist.innerHTML = "<div class='item-row item-name'>No items found.</div>";
}
// (F3) DRAW ITEMS
else {
for (let i in slist.items) {
// ITEM ROW
let row = document.createElement("div");
row.className = "item-row";
slist.hlist.appendChild(row);
// ITEM NAME
let name = document.createElement("div");
name.innerHTML = slist.items[i].name;
name.className = "item-name";
if (slist.items[i].done) { name.classList.add("item-got"); }
row.appendChild(name);
// DELETE BUTTON
let del = document.createElement("input");
del.className = "item-del";
del.type = "button";
del.value = "Delete";;
del.onclick = () => { slist.delete(i); };
row.appendChild(del);
// COMPLETED/NOT YET BUTTON
let ok = document.createElement("input");
ok.className = "item-ok";
ok.type = "button";
ok.value = slist.items[i].done ? "Not Yet" : "Got It";
ok.onclick = () => { slist.toggle(i); };
row.appendChild(ok);
}
}
}
};
window.addEventListener("load", slist.init);
{
"short_name": "JS Shopping List",
"name": "JS Shopping List",
"icons": [{
"src": "favicon.png",
"sizes": "512x512",
"type": "image/png"
}],
"start_url": "shop-list.html",
"scope": "/",
"background_color": "white",
"theme_color": "white",
"display": "standalone"
}
// (A) CREATE/INSTALL CACHE
self.addEventListener("install", evt => {
self.skipWaiting();
evt.waitUntil(
caches.open("ShopList")
.then(cache => cache.addAll([
"favicon.png",
"1-shop-list.css",
"1-shop-list.html",
"2-shop-list.js",
"3-manifest.json"
]))
.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