better state bindings for defaults

This commit is contained in:
Martin McKeaveney 2020-02-10 17:11:22 +00:00
parent 936bce711e
commit 4fbdde2f5e
3 changed files with 13 additions and 41 deletions

View File

@ -3,43 +3,17 @@
import { store } from "../builderStore"; import { store } from "../builderStore";
import { isBinding, getBinding, setBinding } from "../common/binding"; import { isBinding, getBinding, setBinding } from "../common/binding";
export let value = "";
export let onChanged = () => {}; export let onChanged = () => {};
export let type = ""; export let value = "";
let isOpen = false; let isOpen = false;
let stateBindings = []; let stateBindings = [];
let isBound = false;
let bindingPath = ""; let bindingPath = "";
let bindingFallbackValue = ""; let bindingFallbackValue = "";
let bindingSource = "store"; let bindingSource = "store";
let isExpanded = false;
let forceIsBound = false;
let canOnlyBind = false;
$: { const clearBinding = () => onChanged("");
canOnlyBind = type === "state";
if (!forceIsBound && canOnlyBind) forceIsBound = true;
isBound = forceIsBound || isBinding(value);
if (isBound) {
const binding = getBinding(value);
bindingPath = binding.path;
bindingFallbackValue = binding.fallback;
bindingSource = binding.source || "store";
} else {
bindingPath = "";
bindingFallbackValue = "";
bindingSource = "store";
}
}
const clearBinding = () => {
forceIsBound = false;
onChanged("");
};
const bind = (path, fallback, source) => { const bind = (path, fallback, source) => {
if (!path) { if (!path) {
@ -50,10 +24,8 @@
onChanged(binding); onChanged(binding);
}; };
const setBindingPath = value => { const setBindingPath = value =>
forceIsBound = canOnlyBind;
bind(value, bindingFallbackValue, bindingSource); bind(value, bindingFallbackValue, bindingSource);
};
const setBindingFallback = value => bind(bindingPath, value, bindingSource); const setBindingFallback = value => bind(bindingPath, value, bindingSource);
@ -61,10 +33,15 @@
bind(bindingPath, bindingFallbackValue, value); bind(bindingPath, bindingFallbackValue, value);
$: { $: {
const binding = getBinding(value);
bindingPath = binding.path
bindingFallbackValue = binding.fallback
console.log({ console.log({
bindingFallbackValue, bindingFallbackValue,
bindingPath, bindingPath,
value value,
}); });
const currentScreen = $store.screens.find( const currentScreen = $store.screens.find(
@ -80,7 +57,7 @@
class="uk-input uk-form-small" class="uk-input uk-form-small"
value={bindingFallbackValue} value={bindingFallbackValue}
on:change={e => { on:change={e => {
setBindingFallback(e.target.value) setBindingFallback(e.target.value);
}} /> }} />
<button on:click={() => (isOpen = !isOpen)}> <button on:click={() => (isOpen = !isOpen)}>
<span <span

View File

@ -28,11 +28,7 @@
{/each} {/each}
</select> </select>
{:else} {:else}
<PropertyCascader <PropertyCascader {onChanged} {value} />
{onChanged}
{type}
on:change={ev => onChanged(ev.target.value)}
/>
{/if} {/if}
</div> </div>

View File

@ -146,7 +146,6 @@ export const setupBinding = (store, rootProps, coreApi, context, rootPath) => {
} }
newProps[boundHandler.propName] = async context => { newProps[boundHandler.propName] = async context => {
console.log(closuredHandlers);
for (let runHandler of closuredHandlers) { for (let runHandler of closuredHandlers) {
await runHandler(context) await runHandler(context)
} }