2020-11-04 18:09:45 +01:00
|
|
|
import { getFrontendStore } from "./store/frontend"
|
2020-12-15 17:41:55 +01:00
|
|
|
import { getAutomationStore } from "./store/automation"
|
2022-09-13 12:52:31 +02:00
|
|
|
import { getTemporalStore } from "./store/temporal"
|
2020-10-30 14:23:49 +01:00
|
|
|
import { getThemeStore } from "./store/theme"
|
2023-05-18 09:57:20 +02:00
|
|
|
import { getUserStore } from "./store/users"
|
2023-07-04 14:18:38 +02:00
|
|
|
import { getDeploymentStore } from "./store/deployments"
|
2022-05-11 13:47:24 +02:00
|
|
|
import { derived } from "svelte/store"
|
2022-03-21 16:17:51 +01:00
|
|
|
import { findComponent, findComponentPath } from "./componentUtils"
|
2022-05-10 14:32:34 +02:00
|
|
|
import { RoleUtils } from "@budibase/frontend-core"
|
2023-02-23 14:55:18 +01:00
|
|
|
import { createHistoryStore } from "builderStore/store/history"
|
|
|
|
import { get } from "svelte/store"
|
2019-07-13 11:35:57 +02:00
|
|
|
|
2020-11-04 18:09:45 +01:00
|
|
|
export const store = getFrontendStore()
|
2020-09-21 14:49:34 +02:00
|
|
|
export const automationStore = getAutomationStore()
|
2020-10-30 14:23:49 +01:00
|
|
|
export const themeStore = getThemeStore()
|
2022-09-13 12:52:31 +02:00
|
|
|
export const temporalStore = getTemporalStore()
|
2023-05-18 09:57:20 +02:00
|
|
|
export const userStore = getUserStore()
|
2023-07-04 14:18:38 +02:00
|
|
|
export const deploymentStore = getDeploymentStore()
|
2019-07-13 11:35:57 +02:00
|
|
|
|
2023-02-23 14:55:18 +01:00
|
|
|
// Setup history for screens
|
|
|
|
export const screenHistoryStore = createHistoryStore({
|
|
|
|
getDoc: id => get(store).screens?.find(screen => screen._id === id),
|
|
|
|
selectDoc: store.actions.screens.select,
|
|
|
|
afterAction: () => {
|
|
|
|
// Ensure a valid component is selected
|
|
|
|
if (!get(selectedComponent)) {
|
|
|
|
store.update(state => ({
|
|
|
|
...state,
|
|
|
|
selectedComponentId: get(selectedScreen)?.props._id,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
store.actions.screens.save = screenHistoryStore.wrapSaveDoc(
|
|
|
|
store.actions.screens.save
|
|
|
|
)
|
|
|
|
store.actions.screens.delete = screenHistoryStore.wrapDeleteDoc(
|
|
|
|
store.actions.screens.delete
|
|
|
|
)
|
|
|
|
|
|
|
|
// Setup history for automations
|
|
|
|
export const automationHistoryStore = createHistoryStore({
|
|
|
|
getDoc: automationStore.actions.getDefinition,
|
|
|
|
selectDoc: automationStore.actions.select,
|
|
|
|
})
|
|
|
|
automationStore.actions.save = automationHistoryStore.wrapSaveDoc(
|
|
|
|
automationStore.actions.save
|
|
|
|
)
|
|
|
|
automationStore.actions.delete = automationHistoryStore.wrapDeleteDoc(
|
|
|
|
automationStore.actions.delete
|
|
|
|
)
|
|
|
|
|
2022-04-25 20:33:43 +02:00
|
|
|
export const selectedScreen = derived(store, $store => {
|
|
|
|
return $store.screens.find(screen => screen._id === $store.selectedScreenId)
|
|
|
|
})
|
|
|
|
|
2022-05-11 13:47:24 +02:00
|
|
|
export const selectedLayout = derived(store, $store => {
|
|
|
|
return $store.layouts?.find(layout => layout._id === $store.selectedLayoutId)
|
|
|
|
})
|
|
|
|
|
|
|
|
export const selectedComponent = derived(
|
|
|
|
[store, selectedScreen],
|
|
|
|
([$store, $selectedScreen]) => {
|
|
|
|
if (!$selectedScreen || !$store.selectedComponentId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return findComponent($selectedScreen?.props, $store.selectedComponentId)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-07-15 09:27:35 +02:00
|
|
|
// For legacy compatibility only, but with the new design UI this is just
|
|
|
|
// the selected screen
|
|
|
|
export const currentAsset = selectedScreen
|
|
|
|
|
2022-05-10 14:32:34 +02:00
|
|
|
export const sortedScreens = derived(store, $store => {
|
|
|
|
return $store.screens.slice().sort((a, b) => {
|
|
|
|
// Sort by role first
|
|
|
|
const roleA = RoleUtils.getRolePriority(a.routing.roleId)
|
|
|
|
const roleB = RoleUtils.getRolePriority(b.routing.roleId)
|
|
|
|
if (roleA !== roleB) {
|
|
|
|
return roleA > roleB ? -1 : 1
|
|
|
|
}
|
|
|
|
// Then put home screens first
|
|
|
|
const homeA = !!a.routing.homeScreen
|
|
|
|
const homeB = !!b.routing.homeScreen
|
|
|
|
if (homeA !== homeB) {
|
|
|
|
return homeA ? -1 : 1
|
|
|
|
}
|
2022-05-11 10:04:33 +02:00
|
|
|
// Then sort alphabetically by each URL param
|
|
|
|
const aParams = a.routing.route.split("/")
|
|
|
|
const bParams = b.routing.route.split("/")
|
|
|
|
let minParams = Math.min(aParams.length, bParams.length)
|
|
|
|
for (let i = 0; i < minParams; i++) {
|
|
|
|
if (aParams[i] === bParams[i]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return aParams[i] < bParams[i] ? -1 : 1
|
|
|
|
}
|
|
|
|
// Then sort by the fewest amount of URL params
|
|
|
|
return aParams.length < bParams.length ? -1 : 1
|
2022-05-10 14:32:34 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-21 16:17:51 +01:00
|
|
|
export const selectedComponentPath = derived(
|
2022-04-26 14:44:21 +02:00
|
|
|
[store, selectedScreen],
|
|
|
|
([$store, $selectedScreen]) => {
|
2022-03-21 16:17:51 +01:00
|
|
|
return findComponentPath(
|
2022-04-26 14:44:21 +02:00
|
|
|
$selectedScreen?.props,
|
2022-03-21 16:17:51 +01:00
|
|
|
$store.selectedComponentId
|
|
|
|
).map(component => component._id)
|
|
|
|
}
|
|
|
|
)
|
2023-02-23 14:55:18 +01:00
|
|
|
|
|
|
|
// Derived automation state
|
|
|
|
export const selectedAutomation = derived(automationStore, $automationStore => {
|
|
|
|
if (!$automationStore.selectedAutomationId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return $automationStore.automations?.find(
|
|
|
|
x => x._id === $automationStore.selectedAutomationId
|
|
|
|
)
|
|
|
|
})
|
2023-07-04 09:58:14 +02:00
|
|
|
|
|
|
|
// Derive map of resource IDs to other users.
|
|
|
|
// We only ever care about a single user in each resource, so if multiple users
|
|
|
|
// share the same datasource we can just overwrite them.
|
|
|
|
export const userSelectedResourceMap = derived(userStore, $userStore => {
|
|
|
|
let map = {}
|
|
|
|
$userStore.forEach(user => {
|
|
|
|
if (user.selectedResourceId) {
|
|
|
|
map[user.selectedResourceId] = user
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return map
|
|
|
|
})
|
2023-07-04 14:18:38 +02:00
|
|
|
|
|
|
|
export const isOnlyUser = derived(userStore, $userStore => {
|
|
|
|
return $userStore.length === 1
|
|
|
|
})
|