Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 26, 2023 09:46
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/4ac182b56e8b2bae0f6db153370872b7 to your computer and use it in GitHub Desktop.
Save code-boxx/4ac182b56e8b2bae0f6db153370872b7 to your computer and use it in GitHub Desktop.
Javascript Budget Planner PWA

JAVASCRIPT BUDGET PLANNER PWA

https://code-boxx.com/simple-budget-planner-javascript/

IMAGES

favicon icon-512

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 Budget</title>
<meta charset="utf-8">
<meta name="description" content="Budget Planner">
<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="Budget">
<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="3a-manifest.json">
<!-- SERVICE WORKER -->
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("3b-worker.js");
}
</script>
<!-- STYLESHEET + JAVASCRIPT -->
<link rel="stylesheet" href="2-js-budget.css">
<script src="2-js-budget.js"></script>
</head>
<body>
<!-- (A) SUMMARY -->
<div id="summary">
<!-- (A1) BALANCE -->
<div id="balance">
<div id="balanceTxt">Balance</div>
<div id="balanceAmt">$0</div>
</div>
<!-- (A2) INCOME -->
<div id="income">
<div id="incomeTxt">Income</div>
<div id="incomeAmt">$0</div>
</div>
<!-- (A3) EXPENSE -->
<div id="expense">
<div id="expenseTxt">Expense</div>
<div id="expenseAmt">$0</div>
</div>
</div>
<!-- (B) ENTRIES LIST -->
<div id="entries">
<div id="dummy" onclick="bg.toggle(true)">New Entry</div>
<div id="list"></div>
</div>
<!-- (C) ENTRY FORM -->
<div id="form"><form onsubmit="return bg.save()">
<div id="formEnd" onclick="bg.toggle(false)">X</div>
<input type="hidden" id="formID">
<label>Income/Expense</label>
<select id="formSign" required>
<option value="+">Income</option>
<option value="-">Expense</option>
</select>
<label>Description</label>
<input type="text" id="formTxt" required>
<label>Amount</label>
<input type="number" id="formAmt" step="0.01" required>
<input type="submit" value="Save" id="formGo">
</form></div>
</body>
</html>
/* (A) ENTIRE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
max-width: 600px;
margin: 0 auto;
background: #f2f2f2;
}
/* (B) SUMMARY */
/* (B1) WRAPPER */
#summary {
display: grid;
grid-template-areas: "b b" "i e";
grid-gap: 10px;
padding: 20px;
font-weight: 700;
background: #1c5d95;
}
#balance, #income, #expense { padding: 10px; }
#balance { grid-area: b; }
#income { grid-area: i; }
#expense { grid-area: e; }
/* (B2) BALANCE */
#balanceTxt { color: #7b9cd7; }
#balanceAmt {
font-size: 38px;
color: #fff;
}
/* (B3) INCOME & EXPENSE */
#income, #expense {
border-radius: 10px;
background: #fff;
}
#incomeAmt, #expenseAmt { font-size: 28px; }
#incomeTxt { color: #92e7a1; }
#expenseTxt { color: #e5a5a5; }
#incomeAmt { color: #41b941; }
#expenseAmt { color: #d72f2f; }
/* (C) ENTRIES LIST */
#entries { padding: 10px; }
#dummy, .eDel, .eEdit { cursor: pointer; }
#dummy, .entry {
border-radius: 15px;
margin: 20px 0;
padding: 20px;
}
#dummy {
font-weight: 700;
color: #959595;
border: 3px dashed #d3d3d3;
}
.entry {
display: flex;
background: #fff;
align-items: center;
font-size: 18px;
}
.eDel, .eTxt, .eAmt, .eEdit { padding: 0 10px; }
.eDel, .eAmt, .eEdit { font-weight: 700; }
.eDel { color: #d72f2f; }
.eTxt { flex-grow: 1; }
.eEdit, .income .eTxt, .expense .eTxt { color: #959595; }
.income .eAmt { color: #41b941; }
.expense .eAmt { color: #d72f2f; }
/* (D) ITEM FORM */
#form {
/* (D1) FULLSCREEN */
position: fixed;
top: 0; left: 0; z-index: 999;
width: 100vw; height: 100vh;
background: rgba(0, 0, 0, 0.5);
/* (D2) CENTERED CONTAINER */
display: flex;
align-items: center;
justify-content: center;
/* (D3) HIDDEN BY DEFAULT */
visibility: hidden;
opacity: 0;
transition: opacity 0.3s;
}
/* (D4) SHOW FORM */
#form.show {
visibility: visible;
opacity: 1;
}
/* (D5) FORM ITSELF */
#form form {
min-width: 400px;
max-width: 600px;
padding: 20px;
border-radius: 10px;
background: #fff;
position: relative;
}
/* (D6) FORM ELEMENTS */
#formEnd, #formGo { cursor: pointer; }
#formEnd {
position: absolute;
top: 0; right: 0;
padding: 10px;
font-weight: 700;
color: #fff;
background: #ab2020;
}
#form label, #form input, #form select {
display: block;
width: 100%;
}
#form label {
margin: 10px 0;
color: #b3b3b3;
}
#form input, #form select { padding: 15px; }
#formGo {
margin-top: 20px;
border: 0;
color: #fff;
background: #1f3e95;
}
var bg = {
// (A) INITIALIZE
data : null, // entries data
hBal : null, // html balance amount
hInc : null, // html income amount
hExp : null, // html expense amount
hList : null, // html entries list
hForm : null, // html form wrapper
fID : null, fSign : null, fTxt : null, fAmt : null, // html form fields
init : () => {
// (A1) GET HTML ELEMENTS
bg.hBal = document.getElementById("balanceAmt");
bg.hInc = document.getElementById("incomeAmt");
bg.hExp = document.getElementById("expenseAmt");
bg.hList = document.getElementById("list");
bg.hForm = document.getElementById("form");
bg.fID = document.getElementById("formID");
bg.fSign = document.getElementById("formSign");
bg.fTxt = document.getElementById("formTxt");
bg.fAmt = document.getElementById("formAmt");
// (A2) LOAD ENTRIES
bg.entries = localStorage.getItem("entries");
if (bg.entries==null) { bg.entries = []; }
else { bg.entries = JSON.parse(bg.entries); }
// (A3) DRAW ENTRIES
bg.draw();
},
// (B) TOGGLE FORM
toggle : id => {
if (id===false) {
bg.fID.value = "";
bg.fSign.value = "+";
bg.fTxt.value = "";
bg.fAmt.value = "";
bg.hForm.classList.remove("show");
} else {
if (Number.isInteger(id)) {
bg.fID.value = id;
bg.fSign.value = bg.entries[id].s;
bg.fTxt.value = bg.entries[id].t;
bg.fAmt.value = bg.entries[id].a;
}
bg.hForm.classList.add("show");
}
},
// (C) DRAW ENTRIES
draw : () => {
// (C1) BALANCE, INCOME, EXPENSES
let bal = 0, inc = 0, exp = 0, row;
// (C2) LOOP & DRAW HTML ENTRIES
bg.hList.innerHTML = "";
bg.entries.forEach((entry, i) => {
if (entry.s=="+") {
inc += entry.a;
bal += entry.a;
} else {
exp += entry.a;
bal -= entry.a;
}
row = document.createElement("div");
row.className = `entry ${entry.s=="+"?"income":"expense"}`;
row.innerHTML = `<div class="eDel" onclick="bg.del(${i})">X</div>
<div class="eTxt">${entry.t}</div>
<div class="eAmt">$${parseFloat(entry.a).toFixed(2)}</div>
<div class="eEdit" onclick="bg.toggle(${i})">&#9998;</div>`;
bg.hList.appendChild(row);
});
// (C3) UPDATE TOTALS
bg.hBal.innerHTML = bal<0 ? `-$${Math.abs(bal).toFixed(2)}` : `$${bal.toFixed(2)}` ;
bg.hInc.innerHTML = `$${inc.toFixed(2)}`;
bg.hExp.innerHTML = `$${exp.toFixed(2)}`;
},
// (D) SAVE ENTRY
save : () => {
// (D1) GET DATA
let data = {
s : bg.fSign.value,
t : bg.fTxt.value,
a : parseFloat(bg.fAmt.value)
};
// (D2) UPDATE ENTRIES ARRAY
if (bg.fID.value=="") { bg.entries.push(data); }
else { bg.entries[parseInt(bg.fID.value)] = data; }
localStorage.setItem("entries", JSON.stringify(bg.entries));
// (D3) UPDATE HTML INTERFACE
bg.toggle(false);
bg.draw();
return false;
},
// (E) DELETE ENTRY
del : id => { if (confirm("Delete entry?")) {
bg.entries.splice(id, 1);
localStorage.setItem("entries", JSON.stringify(bg.entries));
bg.draw();
}}
};
window.onload = bg.init;
{
"short_name": "Budget",
"name": "Budget Planner",
"icons": [{
"src": "favicon.png",
"sizes": "64x64",
"type": "image/png"
}, {
"src": "icon-512.png",
"sizes": "512x512",
"type": "image/png"
}],
"start_url": "1-js-budget.html",
"scope": "/",
"background_color": "white",
"theme_color": "white",
"display": "standalone"
}
// (A) CREATE/INSTALL CACHE
self.addEventListener("install", evt => {
self.skipWaiting();
evt.waitUntil(
caches.open("JSBudget")
.then(cache => cache.addAll([
"favicon.png",
"icon-512.png",
"1-js-budget.html",
"2-js-budget.js",
"2-js-budget.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