2021-08-20 10:27:38 +02:00
|
|
|
import { 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"
|
2021-06-30 21:35:02 +02:00
|
|
|
import { appStore } from "./app"
|
2021-08-20 10:27:38 +02:00
|
|
|
import {
|
|
|
|
findComponentPathById,
|
|
|
|
findChildrenByType,
|
|
|
|
findComponentById,
|
|
|
|
} from "../utils/components"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
const createScreenStore = () => {
|
2020-11-24 10:31:54 +01:00
|
|
|
const store = derived(
|
2021-06-30 21:35:02 +02:00
|
|
|
[appStore, routeStore, builderStore],
|
|
|
|
([$appStore, $routeStore, $builderStore]) => {
|
2021-07-07 12:28:53 +02:00
|
|
|
let activeLayout, activeScreen
|
2022-04-27 13:30:35 +02:00
|
|
|
let screens
|
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
if ($builderStore.inBuilder) {
|
|
|
|
// Use builder defined definitions if inside the builder preview
|
|
|
|
activeScreen = $builderStore.screen
|
2021-07-07 12:28:53 +02:00
|
|
|
screens = [activeScreen]
|
2021-05-20 15:47:17 +02:00
|
|
|
|
2022-04-27 13:30:35 +02:00
|
|
|
// Legacy - allow the builder to specify a layout
|
|
|
|
if ($builderStore.layout) {
|
|
|
|
activeLayout = $builderStore.layout
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-20 15:47:17 +02:00
|
|
|
// Find the correct screen by matching the current route
|
2021-07-07 12:48:38 +02:00
|
|
|
screens = $appStore.screens
|
2021-05-20 15:47:17 +02:00
|
|
|
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
|
|
|
)
|
|
|
|
}
|
2022-04-27 13:30:35 +02:00
|
|
|
|
|
|
|
// Legacy - find the custom layout for the selected screen
|
2020-12-04 15:04:07 +01:00
|
|
|
if (activeScreen) {
|
2022-04-27 13:30:35 +02:00
|
|
|
const screenLayout = $appStore.layouts?.find(
|
2021-05-04 12:32:22 +02:00
|
|
|
layout => layout._id === activeScreen.layoutId
|
2020-12-04 15:04:07 +01:00
|
|
|
)
|
2022-04-27 13:30:35 +02:00
|
|
|
if (screenLayout) {
|
|
|
|
activeLayout = screenLayout
|
|
|
|
}
|
2020-12-04 15:04:07 +01:00
|
|
|
}
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
2022-04-27 13:30:35 +02:00
|
|
|
|
|
|
|
// If we don't have a legacy custom layout, build a layout structure
|
|
|
|
// from the screen navigation settings
|
|
|
|
if (!activeLayout) {
|
2022-04-27 13:47:54 +02:00
|
|
|
let navigationProps = {
|
|
|
|
navigation: "None",
|
|
|
|
}
|
|
|
|
if (activeScreen.showNavigation) {
|
|
|
|
navigationProps = activeScreen.navigation
|
|
|
|
|
|
|
|
// Legacy - if this is a legacy screen without any navigation
|
|
|
|
// settings fall back to just showing the app title
|
|
|
|
if (!navigationProps) {
|
|
|
|
navigationProps = {
|
|
|
|
title: activeScreen.navigation ?? $appStore.application?.name,
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 14:28:17 +02:00
|
|
|
if (!navigationProps.navigation) {
|
|
|
|
navigationProps.navigation = "Top"
|
|
|
|
}
|
2022-04-27 13:47:54 +02:00
|
|
|
}
|
2022-04-27 13:30:35 +02:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2022-04-27 13:47:54 +02:00
|
|
|
...navigationProps,
|
2022-04-27 13:30:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { screens, 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
|
|
|
|
2021-08-20 10:27:38 +02:00
|
|
|
// Utils to parse component definitions
|
|
|
|
const actions = {
|
|
|
|
findComponentById: componentId => {
|
|
|
|
const { activeScreen, activeLayout } = get(store)
|
|
|
|
let result = findComponentById(activeScreen?.props, componentId)
|
|
|
|
if (result) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return findComponentById(activeLayout?.props)
|
|
|
|
},
|
|
|
|
findComponentPathById: componentId => {
|
|
|
|
const { activeScreen, activeLayout } = get(store)
|
|
|
|
let result = findComponentPathById(activeScreen?.props, componentId)
|
|
|
|
if (result) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return findComponentPathById(activeLayout?.props)
|
|
|
|
},
|
|
|
|
findChildrenByType: (componentId, type) => {
|
|
|
|
const component = actions.findComponentById(componentId)
|
|
|
|
if (!component || !component._children) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
let children = []
|
|
|
|
findChildrenByType(component, type, children)
|
|
|
|
return children
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
2021-08-20 10:27:38 +02:00
|
|
|
actions,
|
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()
|