2020-11-13 16:42:32 +01:00
|
|
|
<script>
|
2021-12-15 09:55:22 +01:00
|
|
|
import { setContext, getContext, onMount } from "svelte"
|
2021-07-30 15:01:01 +02:00
|
|
|
import Router, { querystring } from "svelte-spa-router"
|
2021-12-15 09:55:22 +01:00
|
|
|
import { routeStore, stateStore } from "stores"
|
2020-11-13 16:42:32 +01:00
|
|
|
import Screen from "./Screen.svelte"
|
2021-12-15 09:55:22 +01:00
|
|
|
import { get } from "svelte/store"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-20 10:50:10 +01:00
|
|
|
const { styleable } = getContext("sdk")
|
2020-11-24 12:02:10 +01:00
|
|
|
const component = getContext("component")
|
2021-06-11 14:34:37 +02:00
|
|
|
setContext("screenslot", true)
|
2020-11-20 10:50:10 +01:00
|
|
|
|
2020-12-11 15:24:19 +01:00
|
|
|
// Only wrap this as an array to take advantage of svelte keying,
|
|
|
|
// to ensure the svelte-spa-router is fully remounted when route config
|
|
|
|
// changes
|
2021-02-10 16:49:23 +01:00
|
|
|
$: config = {
|
|
|
|
routes: getRouterConfig($routeStore.routes),
|
|
|
|
id: $routeStore.routeSessionId,
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2021-07-30 15:01:01 +02:00
|
|
|
// Keep query params up to date
|
2021-12-15 09:55:22 +01:00
|
|
|
$: routeStore.actions.setQueryParams(parseQueryString($querystring))
|
|
|
|
|
|
|
|
const parseQueryString = query => {
|
2021-07-30 15:01:01 +02:00
|
|
|
let queryParams = {}
|
2021-12-15 09:55:22 +01:00
|
|
|
if (query) {
|
|
|
|
const urlSearchParams = new URLSearchParams(query)
|
2021-07-30 15:01:01 +02:00
|
|
|
for (const [key, value] of urlSearchParams) {
|
|
|
|
queryParams[key] = value
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 09:55:22 +01:00
|
|
|
return queryParams
|
2021-07-30 15:01:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-19 19:39:22 +01:00
|
|
|
const getRouterConfig = routes => {
|
|
|
|
let config = {}
|
|
|
|
routes.forEach(route => {
|
|
|
|
config[route.path] = Screen
|
2020-11-13 16:42:32 +01:00
|
|
|
})
|
2020-11-17 13:08:24 +01:00
|
|
|
|
|
|
|
// Add catch-all route so that we serve the Screen component always
|
2020-11-19 19:39:22 +01:00
|
|
|
config["*"] = Screen
|
|
|
|
return config
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-19 19:39:22 +01:00
|
|
|
const onRouteLoading = ({ detail }) => {
|
2020-11-17 13:08:24 +01:00
|
|
|
routeStore.actions.setActiveRoute(detail.route)
|
2020-11-13 16:42:32 +01:00
|
|
|
}
|
2021-12-15 09:55:22 +01:00
|
|
|
|
|
|
|
// Initialise state store from query string on initial load
|
|
|
|
onMount(() => {
|
|
|
|
const queryParams = parseQueryString(get(querystring))
|
|
|
|
if (queryParams.state) {
|
|
|
|
try {
|
|
|
|
const state = JSON.parse(atob(queryParams.state))
|
|
|
|
stateStore.actions.initialise(state)
|
|
|
|
} catch (error) {
|
|
|
|
// Swallow error and do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-11-13 16:42:32 +01:00
|
|
|
</script>
|
|
|
|
|
2021-02-10 16:49:23 +01:00
|
|
|
{#key config.id}
|
2020-11-24 12:02:10 +01:00
|
|
|
<div use:styleable={$component.styles}>
|
2020-12-11 15:24:19 +01:00
|
|
|
<Router on:routeLoading={onRouteLoading} routes={config.routes} />
|
2020-11-17 13:08:24 +01:00
|
|
|
</div>
|
2021-02-10 16:49:23 +01:00
|
|
|
{/key}
|
2020-11-25 19:30:09 +01:00
|
|
|
|
|
|
|
<style>
|
|
|
|
div {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
</style>
|