2019-08-02 15:54:10 +02:00
|
|
|
<script>
|
|
|
|
|
|
|
|
import {
|
|
|
|
keys,
|
|
|
|
map,
|
2019-08-14 23:11:59 +02:00
|
|
|
some,
|
|
|
|
includes,
|
|
|
|
cloneDeep,
|
2019-08-16 16:48:45 +02:00
|
|
|
isEqual,
|
|
|
|
sortBy,
|
2019-08-19 09:51:01 +02:00
|
|
|
filter,
|
|
|
|
difference
|
2019-08-02 15:54:10 +02:00
|
|
|
} from "lodash/fp";
|
2019-08-14 23:11:59 +02:00
|
|
|
import { pipe } from "../common/core";
|
2019-08-16 16:48:45 +02:00
|
|
|
import {
|
|
|
|
getComponentInfo ,
|
|
|
|
getInstanceProps
|
|
|
|
} from "./pagesParsing/createProps";
|
2019-08-14 23:11:59 +02:00
|
|
|
import { getExactComponent } from "./pagesParsing/searchComponents";
|
2019-08-07 10:03:49 +02:00
|
|
|
import Checkbox from "../common/Checkbox.svelte";
|
|
|
|
import Textbox from "../common/Textbox.svelte";
|
|
|
|
import Dropdown from "../common/Dropdown.svelte";
|
2019-08-14 23:11:59 +02:00
|
|
|
import { validateProps } from "./pagesParsing/validateProps";
|
2019-08-16 16:48:45 +02:00
|
|
|
import ComponentPropSelector from "./ComponentPropSelector.svelte";
|
|
|
|
import PropControl from "./PropControl.svelte";
|
|
|
|
import IconButton from "../common/IconButton.svelte";
|
2019-08-02 15:54:10 +02:00
|
|
|
|
2019-08-14 23:11:59 +02:00
|
|
|
export let shouldValidate = true;
|
|
|
|
export let onValidate = () => {};
|
|
|
|
export let componentInfo;
|
2019-08-16 16:48:45 +02:00
|
|
|
export let instanceProps = null;
|
2019-08-14 23:11:59 +02:00
|
|
|
export let onPropsChanged = () => {};
|
2019-08-19 09:51:01 +02:00
|
|
|
export let onEditComponentProp = () => {};
|
2019-08-14 23:11:59 +02:00
|
|
|
|
|
|
|
let errors = [];
|
|
|
|
let props = {};
|
2019-08-16 16:48:45 +02:00
|
|
|
let propsDefinitions = [];
|
|
|
|
let inheritedPropsDefinitions = [];
|
|
|
|
let inheritedExpanded = false;
|
2019-08-19 09:51:01 +02:00
|
|
|
let isInstance = false;
|
2019-08-16 16:48:45 +02:00
|
|
|
|
|
|
|
const isPropInherited = name =>
|
|
|
|
includes(name)(componentInfo.inheritedProps);
|
2019-08-14 23:11:59 +02:00
|
|
|
|
|
|
|
$: {
|
2019-08-16 16:48:45 +02:00
|
|
|
if(componentInfo)
|
2019-08-14 23:11:59 +02:00
|
|
|
{
|
2019-08-19 09:51:01 +02:00
|
|
|
isInstance = !!instanceProps;
|
|
|
|
props = isInstance
|
2019-08-16 16:48:45 +02:00
|
|
|
? getInstanceProps(componentInfo, instanceProps)
|
|
|
|
: cloneDeep(componentInfo.fullProps);
|
2019-08-14 23:11:59 +02:00
|
|
|
|
2019-08-16 16:48:45 +02:00
|
|
|
propsDefinitions = pipe(componentInfo.propsDefinition, [
|
2019-08-14 23:11:59 +02:00
|
|
|
keys,
|
2019-08-16 16:48:45 +02:00
|
|
|
filter(k => !isPropInherited(k)),
|
|
|
|
map(k => ({...componentInfo.propsDefinition[k], ____name:k})),
|
|
|
|
sortBy("____name")
|
2019-08-14 23:11:59 +02:00
|
|
|
]);
|
|
|
|
|
2019-08-16 16:48:45 +02:00
|
|
|
inheritedPropsDefinitions = pipe(componentInfo.propsDefinition, [
|
|
|
|
keys,
|
|
|
|
filter(k => isPropInherited(k)),
|
|
|
|
map(k => ({...componentInfo.propsDefinition[k], ____name:k})),
|
|
|
|
sortBy("____name")
|
|
|
|
]);
|
2019-08-14 23:11:59 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-04 23:21:16 +02:00
|
|
|
|
2019-08-02 15:54:10 +02:00
|
|
|
|
2019-08-16 16:48:45 +02:00
|
|
|
let setProp = (name, value) => {
|
2019-08-14 23:11:59 +02:00
|
|
|
const newProps = cloneDeep(props);
|
2019-08-02 15:54:10 +02:00
|
|
|
|
2019-08-19 09:51:01 +02:00
|
|
|
let finalProps = isInstance ? newProps : cloneDeep(componentInfo.component.props);
|
2019-08-14 23:11:59 +02:00
|
|
|
|
2019-08-19 09:51:01 +02:00
|
|
|
if(!isInstance) {
|
|
|
|
const nowSet = [];
|
|
|
|
for(let p of componentInfo.unsetProps) {
|
|
|
|
if(!isEqual(newProps[p])(componentInfo.rootDefaultProps[p])) {
|
|
|
|
finalProps[p] = newProps[p];
|
|
|
|
nowSet.push(p);
|
|
|
|
}
|
2019-08-14 23:11:59 +02:00
|
|
|
}
|
2019-08-19 09:51:01 +02:00
|
|
|
componentInfo.unsetProps = difference(nowSet)(componentInfo.unsetProps);
|
2019-08-14 23:11:59 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 09:51:01 +02:00
|
|
|
newProps[name] = value;
|
|
|
|
finalProps[name] = value;
|
2019-08-14 23:11:59 +02:00
|
|
|
props = newProps;
|
|
|
|
if(validate(finalProps))
|
|
|
|
onPropsChanged(finalProps);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const validate = (finalProps) => {
|
|
|
|
errors = validateProps(componentInfo.propsDefinition, finalProps, [], false);
|
|
|
|
onValidate(errors);
|
|
|
|
return errors.length === 0;
|
|
|
|
}
|
2019-08-04 23:21:16 +02:00
|
|
|
|
2019-08-14 23:11:59 +02:00
|
|
|
const fieldHasError = (propName) =>
|
|
|
|
some(e => e.propName === propName)(errors);
|
2019-08-07 10:03:49 +02:00
|
|
|
|
2019-08-19 09:51:01 +02:00
|
|
|
const onEditComponent = (propName) => () => {
|
|
|
|
onEditComponentProp(propName);
|
|
|
|
}
|
2019-08-16 16:48:45 +02:00
|
|
|
|
2019-08-02 15:54:10 +02:00
|
|
|
</script>
|
|
|
|
|
2019-08-04 23:21:16 +02:00
|
|
|
<div class="root">
|
|
|
|
|
2019-08-16 16:48:45 +02:00
|
|
|
<form class="uk-form-stacked">
|
2019-08-19 09:51:01 +02:00
|
|
|
{#each propsDefinitions as propDef, index}
|
2019-08-16 16:48:45 +02:00
|
|
|
|
|
|
|
<PropControl {errors}
|
|
|
|
{setProp}
|
|
|
|
{fieldHasError}
|
|
|
|
{propDef}
|
|
|
|
{props}
|
2019-08-19 09:51:01 +02:00
|
|
|
{index}
|
|
|
|
onEditComponent={onEditComponent(propDef.____name)}
|
2019-08-16 16:48:45 +02:00
|
|
|
disabled={false} />
|
|
|
|
|
|
|
|
{/each}
|
|
|
|
|
|
|
|
{#if inheritedPropsDefinitions.length > 0}
|
2019-08-19 09:51:01 +02:00
|
|
|
<div class="inherited-title padding">
|
2019-08-16 16:48:45 +02:00
|
|
|
<div>Inherited</div>
|
|
|
|
<div>
|
|
|
|
<IconButton icon={inheritedExpanded ? "chevron-down" : "chevron-right"}
|
|
|
|
on:click={() => inheritedExpanded = !inheritedExpanded}/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
{#if inheritedExpanded}
|
2019-08-19 09:51:01 +02:00
|
|
|
{#each inheritedPropsDefinitions as propDef, index}
|
2019-08-16 16:48:45 +02:00
|
|
|
|
|
|
|
<PropControl {errors}
|
|
|
|
{setProp}
|
|
|
|
{fieldHasError}
|
|
|
|
{propDef}
|
|
|
|
{props}
|
2019-08-19 09:51:01 +02:00
|
|
|
{index}
|
2019-08-16 16:48:45 +02:00
|
|
|
disabled={true} />
|
|
|
|
|
|
|
|
{/each}
|
|
|
|
{/if}
|
2019-08-07 10:03:49 +02:00
|
|
|
</form>
|
2019-08-16 16:48:45 +02:00
|
|
|
|
|
|
|
|
2019-08-04 23:21:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
</div>
|
2019-08-02 15:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
<style>
|
2019-08-04 23:21:16 +02:00
|
|
|
|
|
|
|
.root {
|
2019-08-07 10:03:49 +02:00
|
|
|
font-size:10pt;
|
|
|
|
}
|
|
|
|
|
2019-08-19 09:51:01 +02:00
|
|
|
.padding {
|
|
|
|
padding: 0 10px;
|
|
|
|
}
|
|
|
|
|
2019-08-16 16:48:45 +02:00
|
|
|
.inherited-title {
|
|
|
|
margin-top: 40px;
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: [name] 1fr [actions] auto;
|
|
|
|
border-style: solid;
|
|
|
|
border-width: 0px 0px 1px 0px;
|
|
|
|
border-color: var(--lightslate);
|
|
|
|
font-style: italic;
|
2019-08-07 10:03:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 16:48:45 +02:00
|
|
|
.inherited-title > div:nth-child(1) {
|
|
|
|
grid-column-start: name;
|
|
|
|
color: var(--slate);
|
2019-08-07 10:03:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 16:48:45 +02:00
|
|
|
.inherited-title > div:nth-child(2) {
|
|
|
|
grid-column-start: actions;
|
|
|
|
color: var(--secondary100);
|
2019-08-04 23:21:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 15:54:10 +02:00
|
|
|
</style>
|