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

113 lines
2.6 KiB
Svelte
Raw Normal View History

<script>
import { store } from "builderStore"
import Button from "components/common/Button.svelte"
import ActionButton from "components/common/ActionButton.svelte"
import ButtonGroup from "components/common/ButtonGroup.svelte"
import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { pipe } from "components/common/core"
2020-02-03 10:50:30 +01:00
import UIkit from "uikit"
import { isRootComponent } from "./pagesParsing/searchComponents"
import { splitName } from "./pagesParsing/splitRootComponentName.js"
2020-08-18 18:14:35 +02:00
import { Input, Select } from "@budibase/bbui"
2020-02-03 10:50:30 +01:00
import { find, filter, some, map, includes } from "lodash/fp"
import { assign } from "lodash"
export const show = () => {
dialog.show()
2020-02-03 10:50:30 +01:00
}
let dialog
2020-02-03 10:50:30 +01:00
let layoutComponents
let layoutComponent
let screens
let name = ""
2020-08-18 18:14:35 +02:00
let routeError
2020-02-25 16:21:23 +01:00
2020-05-07 11:53:34 +02:00
$: layoutComponents = Object.values($store.components).filter(
componentDefinition => componentDefinition.container
)
2020-02-03 10:50:30 +01:00
2020-02-25 11:01:07 +01:00
$: layoutComponent = layoutComponent
2020-05-07 11:53:34 +02:00
? layoutComponents.find(
component => component._component === layoutComponent._component
)
2020-02-25 16:21:23 +01:00
: layoutComponents[0]
2020-02-03 10:50:30 +01:00
$: route = !route && $store.screens.length === 0 ? "*" : route
2020-02-03 10:50:30 +01:00
const save = () => {
if (!route) {
2020-08-18 18:14:35 +02:00
routeError = "Url is required"
} else {
if (routeNameExists(route)) {
2020-08-18 18:14:35 +02:00
routeError = "This url is already taken"
} else {
routeError = ""
}
}
if (routeError) return false
2020-02-03 10:50:30 +01:00
store.createScreen(name, route, layoutComponent._component)
name = ""
route = ""
dialog.hide()
2020-02-03 10:50:30 +01:00
}
const cancel = () => {
dialog.hide()
2020-02-03 10:50:30 +01:00
}
2020-02-25 11:01:07 +01:00
const routeNameExists = route => {
2020-05-07 11:53:34 +02:00
return $store.screens.some(
screen => screen.route.toLowerCase() === route.toLowerCase()
)
2020-02-25 11:01:07 +01:00
}
const routeChanged = event => {
if (!event.target.value.startsWith("/")) {
route = "/" + event.target.value
2020-02-25 11:01:07 +01:00
}
}
</script>
<ConfirmDialog
bind:this={dialog}
title="New Screen"
onCancel={cancel}
onOk={save}
okText="Create Screen">
2020-08-18 22:43:13 +02:00
<div data-cy="new-screen-dialog">
<div class="uk-margin">
<Input label="Name" bind:value={name} />
</div>
<div class="uk-margin">
2020-08-18 18:14:35 +02:00
<Input
label="Url"
error={routeError}
bind:value={route}
on:change={routeChanged} />
</div>
2020-02-03 10:50:30 +01:00
<div class="uk-margin">
2020-08-18 18:14:35 +02:00
<label>Layout Component</label>
<Select bind:value={layoutComponent} secondary>
2020-08-18 18:14:35 +02:00
{#each layoutComponents as { _component, name }}
<option value={_component}>{name}</option>
{/each}
</Select>
2019-10-07 07:03:41 +02:00
</div>
2020-02-03 10:50:30 +01:00
</div>
2019-10-07 07:03:41 +02:00
</ConfirmDialog>
2020-03-24 11:56:48 +01:00
<style>
2020-03-27 17:58:32 +01:00
.uk-margin {
display: flex;
flex-direction: column;
}
</style>