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