Files
packager/ui/src/App.svelte

61 lines
1.6 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";
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;
} else if (urlParts[0] == "lists" && urlParts.length == 2) {
console.log("=> PackageList");
currentRoute = PackageList;
data = {id: urlParts[1]};
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>