2022-04-28 18:30:48 +02:00
|
|
|
import { derived } 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"
|
2022-06-17 11:18:39 +02:00
|
|
|
import { RoleUtils } from "@budibase/frontend-core"
|
2022-10-06 10:17:26 +02:00
|
|
|
import {
|
|
|
|
findComponentById,
|
|
|
|
findComponentPathById,
|
|
|
|
} from "../utils/components.js"
|
|
|
|
import { Helpers } from "@budibase/bbui"
|
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
|
2022-10-06 10:17:26 +02:00
|
|
|
activeScreen = Helpers.cloneDeep($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
|
2022-06-17 11:18:39 +02:00
|
|
|
screens = $appStore.screens || []
|
2021-05-20 15:47:17 +02:00
|
|
|
if ($routeStore.activeRoute) {
|
2022-10-06 10:17:26 +02:00
|
|
|
activeScreen = Helpers.cloneDeep(
|
|
|
|
screens.find(
|
|
|
|
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
|
|
|
|
2022-10-06 10:17:26 +02:00
|
|
|
// Insert DND placeholder if required
|
2022-10-07 15:34:47 +02:00
|
|
|
const { dndParent, dndIndex, selectedComponentId } = $builderStore
|
2022-10-07 09:05:44 +02:00
|
|
|
const insert = true
|
|
|
|
if (insert && activeScreen && dndParent && dndIndex != null) {
|
2022-10-07 15:34:47 +02:00
|
|
|
let selectedComponent = findComponentById(
|
|
|
|
activeScreen.props,
|
|
|
|
selectedComponentId
|
|
|
|
)
|
|
|
|
delete selectedComponent._component
|
2022-10-06 10:17:26 +02:00
|
|
|
const placeholder = {
|
2022-10-07 09:05:44 +02:00
|
|
|
_component: "placeholder",
|
2022-10-06 10:17:26 +02:00
|
|
|
_id: "placeholder",
|
|
|
|
static: true,
|
|
|
|
}
|
2022-10-07 09:05:44 +02:00
|
|
|
let parent = findComponentById(activeScreen.props, dndParent)
|
|
|
|
if (!parent._children?.length) {
|
|
|
|
parent._children = [placeholder]
|
2022-10-06 10:17:26 +02:00
|
|
|
} else {
|
2022-10-07 09:05:44 +02:00
|
|
|
parent._children.splice(dndIndex, 0, placeholder)
|
2022-10-06 10:17:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 11:18:39 +02:00
|
|
|
// Assign ranks to screens, preferring higher roles and home screens
|
|
|
|
screens.forEach(screen => {
|
|
|
|
const roleId = screen.routing.roleId
|
|
|
|
let rank = RoleUtils.getRolePriority(roleId)
|
|
|
|
if (screen.routing.homeScreen) {
|
|
|
|
rank += 100
|
|
|
|
}
|
|
|
|
screen.rank = rank
|
|
|
|
})
|
|
|
|
|
|
|
|
// Sort screens so the best route is first
|
|
|
|
screens = screens.sort((a, b) => {
|
|
|
|
// First sort by rank
|
|
|
|
if (a.rank !== b.rank) {
|
|
|
|
return a.rank > b.rank ? -1 : 1
|
|
|
|
}
|
|
|
|
// Then sort alphabetically
|
|
|
|
return a.routing.route < b.routing.route ? -1 : 1
|
|
|
|
})
|
|
|
|
|
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-28 18:30:48 +02:00
|
|
|
let navigationSettings = {
|
2022-04-27 13:47:54 +02:00
|
|
|
navigation: "None",
|
2022-05-10 15:54:53 +02:00
|
|
|
pageWidth: activeScreen?.width || "Large",
|
2022-04-27 13:47:54 +02:00
|
|
|
}
|
2022-04-28 13:18:08 +02:00
|
|
|
if (activeScreen?.showNavigation) {
|
2022-05-10 15:54:53 +02:00
|
|
|
navigationSettings = {
|
|
|
|
...navigationSettings,
|
|
|
|
...($builderStore.navigation || $appStore.application?.navigation),
|
|
|
|
}
|
2022-04-27 13:47:54 +02:00
|
|
|
|
2022-05-06 14:51:27 +02:00
|
|
|
// Default navigation to top
|
2022-04-28 18:30:48 +02:00
|
|
|
if (!navigationSettings.navigation) {
|
|
|
|
navigationSettings.navigation = "Top"
|
2022-04-27 14:28:17 +02:00
|
|
|
}
|
2022-05-06 14:51:27 +02:00
|
|
|
|
|
|
|
// Default title to app name
|
|
|
|
if (!navigationSettings.title && !navigationSettings.hideTitle) {
|
|
|
|
navigationSettings.title = $appStore.application?.name
|
|
|
|
}
|
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-28 18:30:48 +02:00
|
|
|
...navigationSettings,
|
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
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
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()
|