budibase/packages/builder/src/components/userInterface/NewScreenModal.svelte

120 lines
2.8 KiB
Svelte
Raw Normal View History

2020-10-08 15:53:06 +02:00
<script>
import { store, backendUiStore } from "builderStore"
2020-10-08 17:15:12 +02:00
import { Input, Button, Spacer, Select, ModalContent } from "@budibase/bbui"
import getTemplates from "builderStore/store/screenTemplates"
2020-10-08 15:53:06 +02:00
import { some } from "lodash/fp"
import analytics from "analytics"
2020-10-08 15:53:06 +02:00
const CONTAINER = "@budibase/standard-components/container"
let name = ""
let routeError
let baseComponent = CONTAINER
2020-10-09 12:58:46 +02:00
let templateIndex
let draftScreen
2020-10-08 15:53:06 +02:00
$: templates = getTemplates($store, $backendUiStore.models)
$: route = !route && $store.screens.length === 0 ? "*" : route
$: 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
const template = templates[newTemplateIndex]
draftScreen = templates[newTemplateIndex].create()
if (draftScreen.props._instanceName) {
name = draftScreen.props._instanceName
}
if (draftScreen.props._component) {
baseComponent = draftScreen.props._component
}
if (draftScreen.route) {
route = draftScreen.route
}
}
2020-10-08 15:53:06 +02:00
const save = () => {
if (!route) {
routeError = "Url is required"
} else {
if (routeNameExists(route)) {
routeError = "This url is already taken"
} else {
routeError = ""
}
}
if (routeError) return false
draftScreen.props._instanceName = name
draftScreen.props._component = baseComponent
draftScreen.route = route
store.createScreen(draftScreen)
if (templateIndex !== undefined) {
const template = templates[templateIndex]
analytics.captureEvent("Screen Created", {
template: template.id || template.name,
})
}
finished()
}
2020-10-08 15:53:06 +02:00
const finished = () => {
templateIndex = 0
2020-10-08 15:53:06 +02:00
name = ""
route = ""
baseComponent = CONTAINER
}
const routeNameExists = route => {
return $store.screens.some(
screen => screen.route.toLowerCase() === route.toLowerCase()
)
}
const routeChanged = event => {
if (!event.target.value.startsWith("/")) {
route = "/" + event.target.value
}
}
</script>
2020-10-08 17:15:12 +02:00
<ModalContent title="New Screen" confirmText="Create Screen" onConfirm={save}>
<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>
<Input label="Name" bind:value={name} />
<Input
label="Url"
error={routeError}
bind:value={route}
on:change={routeChanged} />
</ModalContent>