2019-08-07 10:03:49 +02:00
|
|
|
<script>
|
2020-10-13 10:11:36 +02:00
|
|
|
import { goto } from "@sveltech/routify"
|
2020-11-05 18:47:27 +01:00
|
|
|
import { store, backendUiStore, allScreens } from "builderStore"
|
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-12-09 15:53:17 +01:00
|
|
|
import { onMount } from "svelte"
|
|
|
|
import api from "builderStore/api"
|
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
|
2020-12-09 15:53:17 +01:00
|
|
|
let roleId = "BASIC"
|
2020-02-25 16:21:23 +01:00
|
|
|
|
2020-10-12 14:34:32 +02:00
|
|
|
$: templates = getTemplates($store, $backendUiStore.tables)
|
2020-11-05 18:47:27 +01:00
|
|
|
$: route = !route && $allScreens.length === 0 ? "*" : route
|
2020-10-08 15:53:06 +02:00
|
|
|
$: baseComponents = Object.values($store.components)
|
|
|
|
.filter(componentDefinition => componentDefinition.baseComponent)
|
|
|
|
.map(c => c._component)
|
2020-10-09 12:58:46 +02:00
|
|
|
$: {
|
|
|
|
if (templates && templateIndex === undefined) {
|
|
|
|
templateIndex = 0
|
|
|
|
templateChanged(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const templateChanged = newTemplateIndex => {
|
|
|
|
if (newTemplateIndex === undefined) return
|
2020-10-12 16:51:03 +02:00
|
|
|
const template = templates[newTemplateIndex]
|
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(
|
2020-12-09 19:39:49 +01:00
|
|
|
screen =>
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const routeChanged = event => {
|
2020-05-02 16:29:10 +02:00
|
|
|
if (!event.target.value.startsWith("/")) {
|
|
|
|
route = "/" + event.target.value
|
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}
|
|
|
|
secondary
|
2020-10-09 12:58:46 +02:00
|
|
|
on:change={ev => templateChanged(ev.target.value)}>
|
2020-10-08 17:15:12 +02:00
|
|
|
{#if templates}
|
|
|
|
{#each templates as template, index}
|
|
|
|
<option value={index}>{template.name}</option>
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
</Select>
|
2020-10-05 12:13:09 +02:00
|
|
|
<Input label="Name" bind:value={name} />
|
|
|
|
<Input
|
|
|
|
label="Url"
|
|
|
|
error={routeError}
|
|
|
|
bind:value={route}
|
|
|
|
on:change={routeChanged} />
|
2020-12-09 16:35:57 +01:00
|
|
|
<Select label="Access" bind:value={roleId} secondary>
|
2020-12-09 19:18:47 +01:00
|
|
|
{#each $backendUiStore.roles as role}
|
2020-12-09 16:35:57 +01:00
|
|
|
<option value={role._id}>{role.name}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
2020-10-17 19:20:31 +02:00
|
|
|
<Toggle text="Create link in navigation bar" bind:checked={createLink} />
|
2020-10-08 10:34:22 +02:00
|
|
|
</ModalContent>
|