Period totals footer, category reorder, mobile fixes, PWA with update prompt
This commit is contained in:
109
app.js
109
app.js
@@ -18,8 +18,37 @@ var CATS = {
|
||||
|
||||
var FREQ_LABEL = { once: "One-time", daily: "Daily", weekly: "Weekly", monthly: "Monthly" };
|
||||
var STORE_KEY = "expense-tracker.expenses.v1";
|
||||
var ORDER_KEY = "expense-tracker.catorder.v1";
|
||||
|
||||
var state = { tab: "daily", q: "", cat: "all", freq: "all", editingId: null, screen: "add", selMonth: null, selYear: null, selWeek: null, selDate: null };
|
||||
/* Category order, persisted so drag-reorder survives reloads. Unknown saved
|
||||
names are dropped; new categories append in their default position. */
|
||||
function getCatOrder() {
|
||||
var names = Object.keys(CATS);
|
||||
try {
|
||||
var saved = JSON.parse(localStorage.getItem(ORDER_KEY) || "[]");
|
||||
if (!Array.isArray(saved)) return names;
|
||||
var known = saved.filter(function (n) { return CATS[n] !== undefined; });
|
||||
names.forEach(function (n) { if (known.indexOf(n) === -1) known.push(n); });
|
||||
return known;
|
||||
} catch (e) {
|
||||
return names;
|
||||
}
|
||||
}
|
||||
|
||||
function saveCatOrder(names) {
|
||||
try { localStorage.setItem(ORDER_KEY, JSON.stringify(names)); } catch (e) { /* session-only order */ }
|
||||
}
|
||||
|
||||
function refillCatOptions() {
|
||||
var sel = $("fCat");
|
||||
var cur = sel.value;
|
||||
sel.innerHTML = getCatOrder().map(function (c) {
|
||||
return '<option value="' + c + '">' + c + "</option>";
|
||||
}).join("");
|
||||
sel.value = cur;
|
||||
}
|
||||
|
||||
var state = { tab: "daily", q: "", cat: "all", period: "month", editingId: null, screen: "add", selMonth: null, selYear: null, selWeek: null, selDate: null };
|
||||
var MONTHS_SHORT = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
|
||||
/* All amounts display in INR. */
|
||||
@@ -170,7 +199,6 @@ function filtered(list) {
|
||||
if (mondayOf(e.date) !== state.selWeek) return false;
|
||||
}
|
||||
if (state.cat !== "all" && e.category !== state.cat) return false;
|
||||
if (state.freq !== "all" && e.frequency !== state.freq) return false;
|
||||
if (q && (e.description + " " + e.category).toLowerCase().indexOf(q) === -1) return false;
|
||||
return true;
|
||||
});
|
||||
@@ -335,23 +363,77 @@ function renderNav(list) {
|
||||
$("countWeekly").textContent = c.weekly;
|
||||
$("countMonthly").textContent = c.monthly;
|
||||
$("countAll").textContent = c.all;
|
||||
var names = Object.keys(CATS);
|
||||
var names = getCatOrder();
|
||||
var html = '<li><button type="button" class="cat-item' + (state.cat === "all" ? " active" : "") +
|
||||
'" data-cat="all"><span class="dot" style="background:#c9c9c9"></span><span>All categories</span>' +
|
||||
'<span class="count">' + list.length + "</span></button></li>";
|
||||
html += names.map(function (n) {
|
||||
return '<li><button type="button" class="cat-item' + (state.cat === n ? " active" : "") +
|
||||
return '<li draggable="true" data-order="' + esc(n) + '"><button type="button" class="cat-item' + (state.cat === n ? " active" : "") +
|
||||
'" data-cat="' + esc(n) + '"><span class="dot" style="background:' + (CATS[n] || "#c9c9c9") + '"></span>' +
|
||||
"<span>" + esc(n) + "</span>" +
|
||||
'<span class="count">' + (byCat[n] || 0) + "</span></button></li>";
|
||||
}).join("");
|
||||
$("catList").innerHTML = html;
|
||||
var m = 0;
|
||||
list.forEach(function (e) { if (e.date.slice(0, 7) === mo) m += e.amount; });
|
||||
$("sideMonth").textContent = money.format(m);
|
||||
var d = 0, w = 0, m = 0;
|
||||
list.forEach(function (e) {
|
||||
if (e.date === t) d += e.amount;
|
||||
if (mondayOf(e.date) === wk) w += e.amount;
|
||||
if (e.date.slice(0, 7) === mo) m += e.amount;
|
||||
});
|
||||
var labels = { day: "Today", week: "This week", month: "This month" };
|
||||
var totals = { day: d, week: w, month: m };
|
||||
$("periodLabel").textContent = labels[state.period] || labels.month;
|
||||
$("sideMonth").textContent = money.format(totals[state.period] === undefined ? m : totals[state.period]);
|
||||
Array.prototype.forEach.call(document.querySelectorAll("[data-period]"), function (b) {
|
||||
b.classList.toggle("active", b.getAttribute("data-period") === state.period);
|
||||
});
|
||||
$("topMonth").textContent = money.format(m);
|
||||
}
|
||||
|
||||
/* Drag-to-reorder for the sidebar categories. Only the named categories move;
|
||||
"All categories" stays pinned at the top. */
|
||||
var dragName = null;
|
||||
function wireCatDnD() {
|
||||
var list = $("catList");
|
||||
list.addEventListener("dragstart", function (e) {
|
||||
var li = e.target.closest("li[data-order]");
|
||||
if (!li) { e.preventDefault(); return; }
|
||||
dragName = li.getAttribute("data-order");
|
||||
e.dataTransfer.effectAllowed = "move";
|
||||
try { e.dataTransfer.setData("text/plain", dragName); } catch (err) { /* some browsers need the try */ }
|
||||
li.classList.add("dragging");
|
||||
});
|
||||
list.addEventListener("dragend", function () {
|
||||
dragName = null;
|
||||
Array.prototype.forEach.call(list.querySelectorAll(".dragging, .drag-over"), function (el) {
|
||||
el.classList.remove("dragging", "drag-over");
|
||||
});
|
||||
});
|
||||
list.addEventListener("dragover", function (e) {
|
||||
var li = e.target.closest("li[data-order]");
|
||||
if (!li || !dragName) return;
|
||||
e.preventDefault();
|
||||
e.dataTransfer.dropEffect = "move";
|
||||
Array.prototype.forEach.call(list.querySelectorAll(".drag-over"), function (el) {
|
||||
if (el !== li) el.classList.remove("drag-over");
|
||||
});
|
||||
li.classList.add("drag-over");
|
||||
});
|
||||
list.addEventListener("drop", function (e) {
|
||||
var li = e.target.closest("li[data-order]");
|
||||
if (!li || !dragName) return;
|
||||
e.preventDefault();
|
||||
var target = li.getAttribute("data-order");
|
||||
if (target === dragName) return;
|
||||
var names = getCatOrder();
|
||||
names.splice(names.indexOf(dragName), 1);
|
||||
names.splice(names.indexOf(target), 0, dragName);
|
||||
saveCatOrder(names);
|
||||
refillCatOptions();
|
||||
renderAll();
|
||||
});
|
||||
}
|
||||
|
||||
/* Landing shows only the add form; records live behind the menu drawer. */
|
||||
var RECORD_SECTIONS = ["viewHead", "statsSection", "sumCard", "listSection", "templatesCard"];
|
||||
|
||||
@@ -489,10 +571,7 @@ function showErr(msg) {
|
||||
}
|
||||
|
||||
function init() {
|
||||
var catSel = $("fCat");
|
||||
catSel.innerHTML = Object.keys(CATS).map(function (c) {
|
||||
return '<option value="' + c + '">' + c + "</option>";
|
||||
}).join("");
|
||||
refillCatOptions();
|
||||
$("fDate").value = todayStr();
|
||||
|
||||
document.querySelectorAll(".nav-item[data-tab]").forEach(function (btn) {
|
||||
@@ -591,7 +670,13 @@ function init() {
|
||||
}
|
||||
$("datePicker").addEventListener("change", function (e) { pickDateInput(e.target.closest("[data-dinput]")); });
|
||||
$("datePicker").addEventListener("input", function (e) { pickDateInput(e.target.closest("[data-dinput]")); });
|
||||
$("fFreqFilter").addEventListener("change", function (e) { state.freq = e.target.value; renderAll(); });
|
||||
document.querySelector(".period-pick").addEventListener("click", function (e) {
|
||||
var b = e.target.closest("[data-period]");
|
||||
if (!b) return;
|
||||
state.period = b.getAttribute("data-period");
|
||||
renderAll();
|
||||
});
|
||||
wireCatDnD();
|
||||
|
||||
$("expForm").addEventListener("submit", function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
Reference in New Issue
Block a user