mirror of
https://github.com/samsonjs/spirit-tracker.git
synced 2026-03-25 09:25:51 +00:00
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
/**
|
|
* Hash routes:
|
|
* #/ search
|
|
* #/item/<sku> detail
|
|
* #/link sku linker (local-write only)
|
|
* #/store/<store> store page (in-stock only)
|
|
* #/stats statistics
|
|
*/
|
|
|
|
import { destroyChart } from "./item_page.js";
|
|
import { renderSearch } from "./search_page.js";
|
|
import { renderItem } from "./item_page.js";
|
|
import { renderSkuLinker } from "./linker_page.js";
|
|
import { renderStore } from "./store_page.js";
|
|
import { renderStats, destroyStatsChart } from "./stats_page.js";
|
|
|
|
function route() {
|
|
const $app = document.getElementById("app");
|
|
if (!$app) return;
|
|
|
|
// always clean up chart when navigating
|
|
destroyChart();
|
|
destroyStatsChart();
|
|
|
|
const h = location.hash || "#/";
|
|
const parts = h.replace(/^#\/?/, "").split("/").filter(Boolean);
|
|
|
|
if (parts.length === 0) return renderSearch($app);
|
|
if (parts[0] === "item" && parts[1]) return renderItem($app, decodeURIComponent(parts[1]));
|
|
if (parts[0] === "store" && parts[1]) return renderStore($app, decodeURIComponent(parts[1]));
|
|
if (parts[0] === "link") return renderSkuLinker($app);
|
|
if (parts[0] === "stats") return renderStats($app);
|
|
|
|
return renderSearch($app);
|
|
}
|
|
|
|
window.addEventListener("hashchange", route);
|
|
route();
|