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"
|
2020-10-08 16:46:29 +02:00
|
|
|
import getTemplates from "builderStore/store/screenTemplates"
|
2020-10-08 15:53:06 +02:00
|
|
|
import { some } from "lodash/fp"
|
|
|
|
|
|
|
|
const CONTAINER = "@budibase/standard-components/container"
|
|
|
|
|
|
|
|
let name = ""
|
|
|
|
let routeError
|
|
|
|
let baseComponent = CONTAINER
|
2020-10-08 16:46:29 +02:00
|
|
|
let templateIndex = 0
|
|
|
|
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-08 16:46:29 +02:00
|
|
|
const templateChanged = ev => {
|
|
|
|
const newTemplateIndex = ev.target.value
|
|
|
|
if (!newTemplateIndex) return
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-08 16:46:29 +02:00
|
|
|
draftScreen.props._instanceName = name
|
|
|
|
draftScreen.props._component = baseComponent
|
|
|
|
draftScreen.route = route
|
|
|
|
|
|
|
|
store.createScreen(draftScreen)
|
|
|
|
|
|
|
|
finished()
|
|
|
|
}
|
2020-10-08 15:53:06 +02:00
|
|
|
|
2020-10-08 16:46:29 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const componentShortName = fullname => {
|
|
|
|
const parts = fullname.split("/")
|
|
|
|
const shortName = parts[parts.length - 1]
|
|
|
|
// make known ones a bit prettier
|
|
|
|
if (shortName === "container") return "Container"
|
|
|
|
if (shortName === "recorddetail") return "Record Detail"
|
|
|
|
if (shortName === "newrecord") return "New Record"
|
|
|
|
return shortName
|
|
|
|
}
|
|
|
|
</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
|
|
|
|
on:change={templateChanged}>
|
|
|
|
{#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} />
|
|
|
|
|
|
|
|
<Select label="Screen Type" bind:value={baseComponent} secondary>
|
|
|
|
{#each baseComponents as component}
|
|
|
|
<option value={component}>{componentShortName(component)}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
|
|
|
|
</ModalContent>
|