2020-11-18 01:08:24 +13:00
|
|
|
import { writable, derived } from "svelte/store"
|
|
|
|
import { routeStore } from "./routes"
|
2020-11-20 07:39:22 +13:00
|
|
|
import * as API from "../api"
|
|
|
|
import { getAppId } from "../utils"
|
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: [],
|
|
|
|
page: {},
|
|
|
|
})
|
|
|
|
const store = derived([config, routeStore], ([$config, $routeStore]) => {
|
|
|
|
const { screens, page } = $config
|
2020-11-19 10:06:12 +13:00
|
|
|
const activeScreen =
|
2020-11-20 07:39:22 +13:00
|
|
|
screens.length === 1
|
|
|
|
? screens[0]
|
|
|
|
: screens.find(
|
|
|
|
screen => screen.routing.route === $routeStore.activeRoute
|
|
|
|
)
|
2020-11-18 01:08:24 +13:00
|
|
|
return {
|
2020-11-20 07:39:22 +13:00
|
|
|
screens,
|
|
|
|
page,
|
2020-11-18 01:08:24 +13:00
|
|
|
activeScreen,
|
|
|
|
}
|
2020-11-14 04:42:32 +13:00
|
|
|
})
|
|
|
|
|
2020-11-20 07:39:22 +13:00
|
|
|
const fetchScreens = async () => {
|
2020-11-24 00:29:24 +13:00
|
|
|
let screens
|
|
|
|
let page
|
|
|
|
const inBuilder = !!window["##BUDIBASE_IN_BUILDER##"]
|
|
|
|
if (inBuilder) {
|
|
|
|
// Load screen and page from the window object if in the builder
|
|
|
|
screens = [window["##BUDIBASE_PREVIEW_SCREEN##"]]
|
|
|
|
page = window["##BUDIBASE_PREVIEW_PAGE##"]
|
|
|
|
} else {
|
|
|
|
// Otherwise load from API
|
|
|
|
const appDefinition = await API.fetchAppDefinition(getAppId())
|
|
|
|
screens = appDefinition.screens
|
|
|
|
page = appDefinition.page
|
|
|
|
}
|
|
|
|
config.set({ screens, page })
|
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()
|