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

154 lines
3.7 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"
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-02-25 16:21:23 +01:00
2020-02-03 10:50:30 +01:00
let saveAttempted = false
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 = () => {
saveAttempted = true
const isValid =
2020-02-25 16:21:23 +01:00
name.length > 0 &&
!screenNameExists(name) &&
route.length > 0 &&
!routeNameExists(route) &&
layoutComponent
2020-02-03 10:50:30 +01:00
if (!isValid) return
store.createScreen(name, route, layoutComponent._component)
dialog.hide()
2020-02-03 10:50:30 +01:00
}
const cancel = () => {
dialog.hide()
2020-02-03 10:50:30 +01:00
}
const screenNameExists = name => {
2020-05-07 11:53:34 +02:00
return $store.screens.some(
screen => screen.name.toLowerCase() === name.toLowerCase()
)
}
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">
<div class="uk-form-horizontal">
<div class="uk-margin">
<label class="uk-form-label">Name</label>
<div class="uk-form-controls">
<input
class="uk-input uk-form-small"
class:uk-form-danger={saveAttempted && (name.length === 0 || screenNameExists(name))}
bind:value={name} />
</div>
</div>
<div class="uk-margin">
<label class="uk-form-label">Route (URL)</label>
<div class="uk-form-controls">
<input
class="uk-input uk-form-small"
class:uk-form-danger={saveAttempted && (route.length === 0 || routeNameExists(route))}
bind:value={route}
on:change={routeChanged} />
2020-02-03 10:50:30 +01:00
</div>
</div>
2020-02-03 10:50:30 +01:00
<div class="uk-margin">
<label class="uk-form-label">Layout Component</label>
<div class="uk-form-controls">
<select
class="uk-select uk-form-small"
bind:value={layoutComponent}
class:uk-form-danger={saveAttempted && !layoutComponent}>
{#each layoutComponents as { _component, name }}
<option value={_component}>{name}</option>
{/each}
</select>
2020-02-03 10:50:30 +01:00
</div>
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;
}
2020-03-24 11:56:48 +01:00
2020-03-27 17:58:32 +01:00
.uk-form-controls {
margin-left: 0 !important;
}
2020-03-24 11:56:48 +01:00
2020-03-27 17:58:32 +01:00
.uk-form-label {
padding-bottom: 10px;
font-weight: 500;
font-size: 16px;
color: var(--grey-7);
2020-03-27 17:58:32 +01:00
}
2020-03-24 11:56:48 +01:00
2020-03-27 17:58:32 +01:00
.uk-input {
height: 40px !important;
border-radius: 3px;
}
.uk-select {
height: 40px !important;
font-weight: 500px;
color: var(--grey-5);
border: 1px solid var(--grey-2);
2020-03-27 17:58:32 +01:00
border-radius: 3px;
}
</style>