2019-08-07 10:03:49 +02:00
|
|
|
<script>
|
|
|
|
|
|
|
|
import ComponentSearch from "./ComponentSearch.svelte";
|
|
|
|
import { store } from "../builderStore";
|
|
|
|
import PropsView from "./PropsView.svelte";
|
|
|
|
import Textbox from "../common/Textbox.svelte";
|
2019-08-14 23:11:59 +02:00
|
|
|
import Button from "../common/Button.svelte";
|
|
|
|
import ButtonGroup from "../common/ButtonGroup.svelte";
|
|
|
|
import { pipe } from "../common/core";
|
|
|
|
import UIkit from "uikit";
|
|
|
|
import {
|
|
|
|
getNewComponentInfo
|
|
|
|
} from "./pagesParsing/createProps";
|
|
|
|
import { isRootComponent } from "./pagesParsing/searchComponents";
|
2019-08-07 10:03:49 +02:00
|
|
|
|
2019-08-14 23:11:59 +02:00
|
|
|
import {
|
|
|
|
cloneDeep,
|
|
|
|
join,
|
|
|
|
split,
|
|
|
|
map,
|
|
|
|
keys,
|
|
|
|
isUndefined
|
|
|
|
} from "lodash/fp";
|
|
|
|
import { assign } from "lodash";
|
2019-08-07 10:03:49 +02:00
|
|
|
|
2019-08-14 23:11:59 +02:00
|
|
|
let component;
|
|
|
|
let modalElement;
|
|
|
|
let errors = {};
|
|
|
|
let componentInfo;
|
|
|
|
|
|
|
|
let name = "";
|
|
|
|
let description = "";
|
|
|
|
let tagsString = "";
|
|
|
|
let propsValidationErrors = [];
|
|
|
|
let inheritedProps;
|
|
|
|
let nameInvalid = "";
|
|
|
|
let propsDefinition;
|
|
|
|
|
|
|
|
const onBasedOnChosen = (allComponents) => (c) => {
|
|
|
|
componentInfo = getNewComponentInfo(allComponents, c.name);
|
|
|
|
tagsString = join(", ")(componentInfo.component.tags);
|
|
|
|
inheritedProps = componentInfo.inheritedProps;
|
|
|
|
propsDefinition = componentInfo.propsDefinition;
|
|
|
|
component = componentInfo.component;
|
|
|
|
}
|
|
|
|
|
|
|
|
const createComponent = () => {
|
|
|
|
|
|
|
|
if(!validate()) return;
|
|
|
|
|
|
|
|
component.name = name;
|
|
|
|
component.description = description;
|
|
|
|
component.tags = pipe(tagsString, [
|
|
|
|
split(","),
|
|
|
|
map(s => s.trim())
|
|
|
|
]);
|
|
|
|
|
|
|
|
store.saveDerivedComponent(component);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
component = null;
|
|
|
|
componentInfo = null;
|
|
|
|
UIkit.modal(modalElement).hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
const onPropsValidate = result => {
|
|
|
|
propsValidationErrors = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onPropsChanged = props => {
|
2019-08-15 09:49:15 +02:00
|
|
|
assign(component.props, props);
|
2019-08-14 23:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const validate = () => {
|
|
|
|
const fieldInvalid = (field, err) =>
|
|
|
|
errors[field] = err;
|
|
|
|
const fieldValid = field =>
|
|
|
|
errors[field] && delete errors[field];
|
|
|
|
|
|
|
|
if(!name) nameInvalid = "component name i not supplied";
|
|
|
|
else nameInvalid = "";
|
|
|
|
|
|
|
|
return (!nameInvalid && propsValidationErrors.length === 0);
|
2019-08-07 10:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
2019-08-14 23:11:59 +02:00
|
|
|
<div bind:this={modalElement} id="new-component-modal" uk-modal>
|
|
|
|
<div class="uk-modal-dialog">
|
|
|
|
|
|
|
|
<div class="uk-modal-header">
|
|
|
|
<h1>New Component</h1>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="uk-modal-body">
|
|
|
|
<form class="uk-form-horizontal">
|
|
|
|
{#if componentInfo}
|
|
|
|
|
|
|
|
<Textbox label="Name"
|
|
|
|
infoText="use forward slash to store in subfolders"
|
|
|
|
bind:text={name}
|
|
|
|
hasError={!!nameInvalid}/>
|
|
|
|
<div class="info-text"></div>
|
|
|
|
<Textbox label="Description"
|
|
|
|
bind:text={description}/>
|
|
|
|
<Textbox label="Tags"
|
|
|
|
infoText="comma separated"
|
|
|
|
bind:text={tagsString}/>
|
|
|
|
<p class="uk-heading-line props-header"><span>Properties</span></p>
|
|
|
|
<PropsView allComponents={$store.allComponents}
|
|
|
|
onValidate={onPropsValidate}
|
|
|
|
showTitle={false}
|
|
|
|
{componentInfo}
|
|
|
|
{onPropsChanged} />
|
|
|
|
|
|
|
|
|
|
|
|
{:else}
|
|
|
|
|
|
|
|
|
2019-08-07 10:03:49 +02:00
|
|
|
<ComponentSearch allComponents={$store.allComponents}
|
2019-08-14 23:11:59 +02:00
|
|
|
onComponentChosen={onBasedOnChosen($store.allComponents)} />
|
|
|
|
|
|
|
|
|
|
|
|
{/if}
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
{#if component}
|
|
|
|
<div class="uk-modal-footer">
|
|
|
|
<ButtonGroup>
|
|
|
|
<Button grouped
|
|
|
|
on:click={close}
|
|
|
|
color="secondary" >Cancel</Button>
|
|
|
|
<Button grouped
|
|
|
|
on:click={createComponent}>Create Component</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
{/if}
|
2019-08-07 10:03:49 +02:00
|
|
|
</div>
|
2019-08-14 23:11:59 +02:00
|
|
|
</div>
|
2019-08-07 10:03:49 +02:00
|
|
|
|
|
|
|
<style>
|
2019-08-14 23:11:59 +02:00
|
|
|
h1 {
|
|
|
|
font-size:1.2em;
|
|
|
|
}
|
|
|
|
|
|
|
|
.props-header {
|
|
|
|
font-style: italic;
|
|
|
|
}
|
2019-08-07 10:03:49 +02:00
|
|
|
|
|
|
|
</style>
|