2021-01-08 03:53:56 +13:00
|
|
|
import { writable, derived, get } from "svelte/store"
|
2020-11-18 01:08:24 +13:00
|
|
|
import { routeStore } from "./routes"
|
2020-11-24 22:31:54 +13:00
|
|
|
import { builderStore } from "./builder"
|
2020-11-20 07:39:22 +13:00
|
|
|
import * as API from "../api"
|
2020-11-14 04:42:32 +13:00
|
|
|
|
2020-11-19 08:18:18 +13:00
|
|
|
const createScreenStore = () => {
|
2020-11-20 07:39:22 +13:00
|
|
|
const config = writable({
|
|
|
|
screens: [],
|
2020-12-03 07:10:46 +13:00
|
|
|
layouts: [],
|
2020-11-20 07:39:22 +13:00
|
|
|
})
|
2020-11-24 22:31:54 +13:00
|
|
|
const store = derived(
|
|
|
|
[config, routeStore, builderStore],
|
|
|
|
([$config, $routeStore, $builderStore]) => {
|
2020-11-28 05:36:31 +13:00
|
|
|
let activeLayout
|
2020-11-24 22:31:54 +13:00
|
|
|
let activeScreen
|
|
|
|
if ($builderStore.inBuilder) {
|
|
|
|
// Use builder defined definitions if inside the builder preview
|
2020-11-28 05:36:31 +13:00
|
|
|
activeLayout = $builderStore.layout
|
2020-11-24 22:31:54 +13:00
|
|
|
activeScreen = $builderStore.screen
|
|
|
|
} else {
|
2021-05-21 01:47:17 +12:00
|
|
|
activeLayout = { props: { _component: "screenslot" } }
|
|
|
|
|
|
|
|
// Find the correct screen by matching the current route
|
2020-12-05 03:04:07 +13:00
|
|
|
const { screens, layouts } = $config
|
2021-05-21 01:47:17 +12:00
|
|
|
if ($routeStore.activeRoute) {
|
2020-12-05 03:04:07 +13:00
|
|
|
activeScreen = screens.find(
|
2021-05-04 22:32:22 +12:00
|
|
|
screen => screen._id === $routeStore.activeRoute.screenId
|
2020-12-05 03:04:07 +13:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if (activeScreen) {
|
|
|
|
activeLayout = layouts.find(
|
2021-05-04 22:32:22 +12:00
|
|
|
layout => layout._id === activeScreen.layoutId
|
2020-12-05 03:04:07 +13:00
|
|
|
)
|
|
|
|
}
|
2020-11-24 22:31:54 +13:00
|
|
|
}
|
2020-11-28 05:36:31 +13:00
|
|
|
return { activeLayout, activeScreen }
|
2020-11-18 01:08:24 +13:00
|
|
|
}
|
2020-11-24 22:31:54 +13:00
|
|
|
)
|
2020-11-14 04:42:32 +13:00
|
|
|
|
2020-11-20 07:39:22 +13:00
|
|
|
const fetchScreens = async () => {
|
2021-01-08 03:53:56 +13:00
|
|
|
const appDefinition = await API.fetchAppDefinition(get(builderStore).appId)
|
2020-11-24 22:31:54 +13:00
|
|
|
config.set({
|
|
|
|
screens: appDefinition.screens,
|
2020-11-28 05:36:31 +13:00
|
|
|
layouts: appDefinition.layouts,
|
2020-11-24 22:31:54 +13:00
|
|
|
})
|
2020-11-14 04:42:32 +13:00
|
|
|
}
|
2020-11-18 01:08:24 +13:00
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: { fetchScreens },
|
2020-11-14 04:42:32 +13:00
|
|
|
}
|
2020-11-18 01:08:24 +13:00
|
|
|
}
|
2020-11-14 04:42:32 +13:00
|
|
|
|
2020-11-19 08:18:18 +13:00
|
|
|
export const screenStore = createScreenStore()
|