1
0
Fork 0
mirror of synced 2024-09-12 23:43:09 +12:00
budibase/packages/client/src/stores/screens.js

96 lines
2.8 KiB
JavaScript
Raw Normal View History

import { derived } from "svelte/store"
import { routeStore } from "./routes"
import { builderStore } from "./builder"
import { appStore } from "./app"
const createScreenStore = () => {
const store = derived(
[appStore, routeStore, builderStore],
([$appStore, $routeStore, $builderStore]) => {
let activeLayout, activeScreen
let screens
if ($builderStore.inBuilder) {
// Use builder defined definitions if inside the builder preview
activeScreen = $builderStore.screen
screens = [activeScreen]
// Legacy - allow the builder to specify a layout
if ($builderStore.layout) {
activeLayout = $builderStore.layout
}
} else {
// Find the correct screen by matching the current route
screens = $appStore.screens
if ($routeStore.activeRoute) {
activeScreen = screens.find(
2021-05-04 22:32:22 +12:00
screen => screen._id === $routeStore.activeRoute.screenId
)
}
// Legacy - find the custom layout for the selected screen
if (activeScreen) {
const screenLayout = $appStore.layouts?.find(
2021-05-04 22:32:22 +12:00
layout => layout._id === activeScreen.layoutId
)
if (screenLayout) {
activeLayout = screenLayout
}
}
}
// If we don't have a legacy custom layout, build a layout structure
// from the screen navigation settings
if (!activeLayout) {
let navigationSettings = {
navigation: "None",
}
2022-04-28 23:18:08 +12:00
if (activeScreen?.showNavigation) {
navigationSettings =
$builderStore.navigation || $appStore.application?.navigation || {}
// Default navigation to top
if (!navigationSettings.navigation) {
navigationSettings.navigation = "Top"
}
// Default title to app name
if (!navigationSettings.title && !navigationSettings.hideTitle) {
navigationSettings.title = $appStore.application?.name
}
}
activeLayout = {
_id: "layout",
props: {
_component: "@budibase/standard-components/layout",
_children: [
{
_component: "screenslot",
_id: "screenslot",
_styles: {
normal: {
flex: "1 1 auto",
display: "flex",
"flex-direction": "column",
"justify-content": "flex-start",
"align-items": "stretch",
},
},
},
],
...navigationSettings,
},
}
}
return { screens, activeLayout, activeScreen }
}
)
return {
subscribe: store.subscribe,
}
}
export const screenStore = createScreenStore()