2020-11-17 13:08:24 +01:00
|
|
|
import { writable, derived } from "svelte/store"
|
|
|
|
import { routeStore } from "./routes"
|
2020-11-19 19:39:22 +01:00
|
|
|
import * as API from "../api"
|
|
|
|
import { getAppId } from "../utils"
|
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: [],
|
|
|
|
page: {},
|
|
|
|
})
|
|
|
|
const store = derived([config, routeStore], ([$config, $routeStore]) => {
|
|
|
|
const { screens, page } = $config
|
2020-11-18 22:06:12 +01:00
|
|
|
const activeScreen =
|
2020-11-19 19:39:22 +01:00
|
|
|
screens.length === 1
|
|
|
|
? screens[0]
|
|
|
|
: screens.find(
|
|
|
|
screen => screen.routing.route === $routeStore.activeRoute
|
|
|
|
)
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
2020-11-19 19:39:22 +01:00
|
|
|
screens,
|
|
|
|
page,
|
2020-11-17 13:08:24 +01:00
|
|
|
activeScreen,
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
})
|
|
|
|
|
2020-11-19 19:39:22 +01:00
|
|
|
const fetchScreens = async () => {
|
2020-11-23 12:29:24 +01: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-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()
|