Files
packager/ui/src/App.svelte

73 lines
2.1 KiB
Svelte
Raw Normal View History

2021-09-06 15:43:30 +02:00
<svelte:head>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</svelte:head>
<script lang="ts">
2021-09-06 18:11:51 +02:00
import Home from "./routes/Home.svelte";
2021-09-07 00:02:53 +02:00
import PackageLists from "./routes/PackageLists.svelte";
import PackageList from "./routes/PackageList.svelte";
2021-10-29 18:42:04 +02:00
import Preparation from "./routes/Preparation.svelte";
import Trips from "./routes/Trips.svelte";
2021-09-07 00:02:53 +02:00
function normalize(path) {
return path.replace(/\/+$/, '') + "/";
}
let currentRoute;
let data;
2021-09-06 15:43:30 +02:00
2021-09-07 00:02:53 +02:00
function route(path) {
path = normalize(path);
console.log(`Routing path "${path}"`);
data = {}
2021-09-06 18:08:52 +02:00
2021-09-07 00:02:53 +02:00
let urlParts = path.split("/").slice(1, -1);
2021-09-06 18:08:52 +02:00
2021-09-07 00:02:53 +02:00
if (path === "/") {
console.log("=> Home");
currentRoute = Home;
} else if (urlParts[0] == "lists" && urlParts.length == 1) {
console.log("=> PackageLists");
currentRoute = PackageLists;
2021-10-29 18:42:04 +02:00
} else if (urlParts[0] == "trips" && urlParts.length == 1) {
console.log("=> Trips");
currentRoute = Trips;
2021-09-07 00:02:53 +02:00
} else if (urlParts[0] == "lists" && urlParts.length == 2) {
console.log("=> PackageList");
currentRoute = PackageList;
data = {id: urlParts[1]};
2021-10-29 18:42:04 +02:00
} else if (urlParts.length == 5
&& urlParts[0] == "lists"
&& urlParts[2] == "items"
&& urlParts[4] == "preparation") {
console.log("=> PackageList");
currentRoute = Preparation;
data = {list_id: urlParts[1], item_id: urlParts[3]};
2021-09-06 18:08:52 +02:00
} else {
console.log("No matching route found");
}
2021-09-06 15:43:30 +02:00
}
2021-09-07 00:02:53 +02:00
window.onload = e => {
route(window.location.pathname);
}
function redirect(path) {
history.pushState({id: path}, "", path);
route(path);
}
window.addEventListener("locationchange", function() {
route(window.location.pathname);
});
window.addEventListener("popstate", event => {
route(window.location.pathname);
});
2021-09-06 15:43:30 +02:00
</script>
<main>
2021-09-07 00:02:53 +02:00
<svelte:component this={currentRoute} redirect={redirect} data={data}/>
2021-09-06 15:43:30 +02:00
</main>