2019-08-07 10:03:49 +02:00
|
|
|
<script>
|
2021-06-08 12:42:39 +02:00
|
|
|
import { store, allScreens, selectedAccessRole } from "builderStore"
|
2021-04-01 11:29:47 +02:00
|
|
|
import { tables } from "stores/backend"
|
|
|
|
import { roles } from "stores/backend"
|
2020-12-09 15:53:17 +01:00
|
|
|
import { Input, Select, ModalContent, Toggle } from "@budibase/bbui"
|
2020-10-08 16:46:29 +02:00
|
|
|
import getTemplates from "builderStore/store/screenTemplates"
|
2020-10-12 16:51:03 +02:00
|
|
|
import analytics from "analytics"
|
2020-10-08 15:53:06 +02:00
|
|
|
|
|
|
|
const CONTAINER = "@budibase/standard-components/container"
|
|
|
|
|
2020-02-03 10:50:30 +01:00
|
|
|
let name = ""
|
2020-08-18 18:14:35 +02:00
|
|
|
let routeError
|
2020-10-08 15:53:06 +02:00
|
|
|
let baseComponent = CONTAINER
|
2020-10-09 12:58:46 +02:00
|
|
|
let templateIndex
|
2020-10-08 16:46:29 +02:00
|
|
|
let draftScreen
|
2020-10-17 19:20:31 +02:00
|
|
|
let createLink = true
|
2021-06-08 12:42:39 +02:00
|
|
|
let roleId = $selectedAccessRole || "BASIC"
|
2020-02-25 16:21:23 +01:00
|
|
|
|
2021-03-31 14:23:58 +02:00
|
|
|
$: templates = getTemplates($store, $tables.list)
|
2020-11-05 18:47:27 +01:00
|
|
|
$: route = !route && $allScreens.length === 0 ? "*" : route
|
2020-10-09 12:58:46 +02:00
|
|
|
$: {
|
|
|
|
if (templates && templateIndex === undefined) {
|
|
|
|
templateIndex = 0
|
|
|
|
templateChanged(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
const templateChanged = newTemplateIndex => {
|
2020-10-09 12:58:46 +02:00
|
|
|
if (newTemplateIndex === undefined) return
|
2020-10-08 16:46:29 +02:00
|
|
|
draftScreen = templates[newTemplateIndex].create()
|
|
|
|
if (draftScreen.props._instanceName) {
|
|
|
|
name = draftScreen.props._instanceName
|
|
|
|
}
|
|
|
|
|
|
|
|
if (draftScreen.props._component) {
|
|
|
|
baseComponent = draftScreen.props._component
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:35:20 +01:00
|
|
|
if (draftScreen.routing) {
|
|
|
|
route = draftScreen.routing.route
|
2020-10-08 16:46:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 19:20:31 +02:00
|
|
|
const save = async () => {
|
2020-08-18 21:59:34 +02:00
|
|
|
if (!route) {
|
2020-12-09 19:39:49 +01:00
|
|
|
routeError = "URL is required"
|
2020-08-18 18:14:35 +02:00
|
|
|
} else {
|
2020-12-09 19:39:49 +01:00
|
|
|
if (routeExists(route, roleId)) {
|
|
|
|
routeError = "This URL is already taken for this access role"
|
2020-08-18 18:14:35 +02:00
|
|
|
} else {
|
|
|
|
routeError = ""
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 15:53:06 +02:00
|
|
|
|
|
|
|
if (routeError) return false
|
|
|
|
|
2020-10-08 16:46:29 +02:00
|
|
|
draftScreen.props._instanceName = name
|
|
|
|
draftScreen.props._component = baseComponent
|
2020-12-09 15:53:17 +01:00
|
|
|
draftScreen.routing = { route, roleId }
|
2020-10-08 16:46:29 +02:00
|
|
|
|
2021-01-15 15:23:27 +01:00
|
|
|
await store.actions.screens.create(draftScreen)
|
2020-10-17 19:20:31 +02:00
|
|
|
if (createLink) {
|
2020-11-05 12:44:18 +01:00
|
|
|
await store.actions.components.links.save(route, name)
|
2020-10-17 19:20:31 +02:00
|
|
|
}
|
2020-11-20 12:41:17 +01:00
|
|
|
await store.actions.routing.fetch()
|
2020-10-08 16:46:29 +02:00
|
|
|
|
2020-10-12 16:51:03 +02:00
|
|
|
if (templateIndex !== undefined) {
|
|
|
|
const template = templates[templateIndex]
|
|
|
|
analytics.captureEvent("Screen Created", {
|
|
|
|
template: template.id || template.name,
|
|
|
|
})
|
|
|
|
}
|
2020-02-03 10:50:30 +01:00
|
|
|
}
|
|
|
|
|
2020-12-09 19:39:49 +01:00
|
|
|
const routeExists = (route, roleId) => {
|
2020-11-05 18:47:27 +01:00
|
|
|
return $allScreens.some(
|
2021-05-04 12:32:22 +02:00
|
|
|
screen =>
|
2020-12-09 19:39:49 +01:00
|
|
|
screen.routing.route.toLowerCase() === route.toLowerCase() &&
|
|
|
|
screen.routing.roleId === roleId
|
2020-05-07 11:53:34 +02:00
|
|
|
)
|
2020-02-25 11:01:07 +01:00
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
const routeChanged = event => {
|
2021-04-20 13:48:37 +02:00
|
|
|
if (!event.detail.startsWith("/")) {
|
|
|
|
route = "/" + event.detail
|
2020-02-25 11:01:07 +01:00
|
|
|
}
|
2021-07-28 09:09:37 +02:00
|
|
|
route = route.replaceAll(' ', '_')
|
2020-02-25 11:01:07 +01:00
|
|
|
}
|
2019-08-07 10:03:49 +02:00
|
|
|
</script>
|
|
|
|
|
2020-10-08 10:34:22 +02:00
|
|
|
<ModalContent title="New Screen" confirmText="Create Screen" onConfirm={save}>
|
2020-10-08 17:15:12 +02:00
|
|
|
<Select
|
|
|
|
label="Choose a Template"
|
|
|
|
bind:value={templateIndex}
|
2021-05-04 12:32:22 +02:00
|
|
|
on:change={ev => templateChanged(ev.detail)}
|
2021-04-20 13:48:37 +02:00
|
|
|
options={templates}
|
|
|
|
placeholder={null}
|
2021-05-04 12:32:22 +02:00
|
|
|
getOptionLabel={x => x.name}
|
2021-05-04 12:04:42 +02:00
|
|
|
getOptionValue={(x, idx) => idx}
|
|
|
|
/>
|
2020-10-05 12:13:09 +02:00
|
|
|
<Input label="Name" bind:value={name} />
|
|
|
|
<Input
|
|
|
|
label="Url"
|
|
|
|
error={routeError}
|
|
|
|
bind:value={route}
|
2021-05-04 12:04:42 +02:00
|
|
|
on:change={routeChanged}
|
|
|
|
/>
|
2021-04-20 13:48:37 +02:00
|
|
|
<Select
|
|
|
|
label="Access"
|
|
|
|
bind:value={roleId}
|
|
|
|
options={$roles}
|
2021-05-04 12:32:22 +02:00
|
|
|
getOptionLabel={x => x.name}
|
|
|
|
getOptionValue={x => x._id}
|
2021-05-04 12:04:42 +02:00
|
|
|
/>
|
2021-04-20 13:48:37 +02:00
|
|
|
<Toggle text="Create link in navigation bar" bind:value={createLink} />
|
2020-10-08 10:34:22 +02:00
|
|
|
</ModalContent>
|