2019-08-02 15:54:10 +02:00
|
|
|
<script>
|
|
|
|
|
|
|
|
import {
|
2020-01-18 00:06:42 +01:00
|
|
|
keys, map, some, includes,
|
|
|
|
cloneDeep, isEqual, sortBy,
|
|
|
|
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";
|
2020-01-18 00:06:42 +01:00
|
|
|
import { getInstanceProps } from "./pagesParsing/createProps";
|
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 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 = () => {};
|
|
|
|
|
|
|
|
let errors = [];
|
|
|
|
let props = {};
|
2019-08-16 16:48:45 +02:00
|
|
|
let propsDefinitions = [];
|
2019-08-19 09:51:01 +02:00
|
|
|
let isInstance = false;
|
2019-08-16 16:48:45 +02:00
|
|
|
|
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
|
|
|
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) => {
|
2020-01-18 00:06:42 +01:00
|
|
|
errors = validateProps(componentInfo.rootComponent, finalProps, [], false);
|
2019-08-14 23:11:59 +02:00
|
|
|
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-02 15:54:10 +02:00
|
|
|
</script>
|
|
|
|
|
2019-08-04 23:21:16 +02:00
|
|
|
<div class="root">
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
<form class="uk-form-stacked form-root">
|
2019-08-19 09:51:01 +02:00
|
|
|
{#each propsDefinitions as propDef, index}
|
2019-08-16 16:48:45 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
<div class="prop-container">
|
2019-08-16 16:48:45 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
<PropControl {setProp}
|
|
|
|
{fieldHasError}
|
|
|
|
{propDef}
|
|
|
|
{props}
|
|
|
|
{index}
|
|
|
|
disabled={false} />
|
2019-08-16 16:48:45 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
</div>
|
2019-08-16 16:48:45 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
{/each}
|
|
|
|
|
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;
|
2020-01-18 00:06:42 +01:00
|
|
|
width: 100%;
|
2019-08-07 10:03:49 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
.form-root {
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
2019-08-07 10:03:49 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
.prop-container {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
min-width: 250px;
|
2019-08-04 23:21:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 15:54:10 +02:00
|
|
|
</style>
|