forked from tsai/budibase
31 changed files with 180 additions and 148 deletions
@ -1,19 +1,19 @@ |
|||||
<script> |
<script> |
||||
import { getContext, setContext } from "svelte" |
import { getContext, setContext } from "svelte" |
||||
import { createDataContextStore } from "../store" |
import { createDataStore } from "../store" |
||||
|
|
||||
export let row |
export let row |
||||
|
|
||||
// Get current contexts |
// Get current contexts |
||||
const dataContext = getContext("data") |
const data = getContext("data") |
||||
const styles = getContext("style") |
const component = getContext("component") |
||||
|
|
||||
// Clone current context to this context |
// Clone current context to this context |
||||
const newDataContext = createDataContextStore($dataContext) |
const newData = createDataStore($data) |
||||
setContext("data", newDataContext) |
setContext("data", newData) |
||||
|
|
||||
// Add additional layer to context |
// Add additional layer to context |
||||
$: newDataContext.actions.addContext(row, $styles.id) |
$: newData.actions.addContext(row, $component.id) |
||||
</script> |
</script> |
||||
|
|
||||
<slot /> |
<slot /> |
||||
|
|||||
@ -0,0 +1,21 @@ |
|||||
|
import { writable } from "svelte/store" |
||||
|
|
||||
|
const createBindingStore = () => { |
||||
|
const store = writable({}) |
||||
|
|
||||
|
const setBindableValue = (value, componentId) => { |
||||
|
store.update(state => { |
||||
|
if (componentId) { |
||||
|
state[componentId] = value |
||||
|
} |
||||
|
return state |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
return { |
||||
|
subscribe: store.subscribe, |
||||
|
actions: { setBindableValue }, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export const bindingStore = createBindingStore() |
||||
@ -0,0 +1,27 @@ |
|||||
|
import { writable } from "svelte/store" |
||||
|
import { cloneDeep } from "lodash/fp" |
||||
|
|
||||
|
const initialValue = { |
||||
|
data: null, |
||||
|
} |
||||
|
|
||||
|
export const createDataStore = existingContext => { |
||||
|
const initial = existingContext ? cloneDeep(existingContext) : initialValue |
||||
|
const store = writable(initial) |
||||
|
|
||||
|
// Adds a context layer to the data context tree
|
||||
|
const addContext = (row, componentId) => { |
||||
|
store.update(state => { |
||||
|
if (componentId) { |
||||
|
state[componentId] = row |
||||
|
state.data = row |
||||
|
} |
||||
|
return state |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
return { |
||||
|
subscribe: store.subscribe, |
||||
|
actions: { addContext }, |
||||
|
} |
||||
|
} |
||||
@ -1,39 +0,0 @@ |
|||||
import { writable, get } from "svelte/store" |
|
||||
import { enrichDataBinding } from "../utils" |
|
||||
import { cloneDeep } from "lodash/fp" |
|
||||
|
|
||||
const initialValue = { |
|
||||
data: null, |
|
||||
} |
|
||||
|
|
||||
export const createDataContextStore = existingContext => { |
|
||||
const initial = existingContext ? cloneDeep(existingContext) : initialValue |
|
||||
const store = writable(initial) |
|
||||
|
|
||||
// Adds a context layer to the data context tree
|
|
||||
const addContext = (row, componentId) => { |
|
||||
store.update(state => { |
|
||||
if (row && componentId) { |
|
||||
state[componentId] = row |
|
||||
state.data = row |
|
||||
} |
|
||||
return state |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
// Enriches props by running mustache and filling in any data bindings present
|
|
||||
// in the prop values
|
|
||||
const enrichDataBindings = props => { |
|
||||
const state = get(store) |
|
||||
let enrichedProps = {} |
|
||||
Object.entries(props).forEach(([key, value]) => { |
|
||||
enrichedProps[key] = enrichDataBinding(value, state) |
|
||||
}) |
|
||||
return enrichedProps |
|
||||
} |
|
||||
|
|
||||
return { |
|
||||
subscribe: store.subscribe, |
|
||||
actions: { addContext, enrichDataBindings }, |
|
||||
} |
|
||||
} |
|
||||
@ -1,5 +1,8 @@ |
|||||
export { authStore } from "./auth" |
export { authStore } from "./auth" |
||||
export { routeStore } from "./routes" |
export { routeStore } from "./routes" |
||||
export { screenStore } from "./screens" |
export { screenStore } from "./screens" |
||||
export { createDataContextStore } from "./dataContext" |
|
||||
export { builderStore } from "./builder" |
export { builderStore } from "./builder" |
||||
|
export { bindingStore } from "./binding" |
||||
|
|
||||
|
// Data stores are layered and duplicated, so it is not a singleton
|
||||
|
export { createDataStore } from "./data" |
||||
|
|||||
@ -1,21 +1,12 @@ |
|||||
<script> |
<script> |
||||
import { getContext } from "svelte" |
import { getContext } from "svelte" |
||||
|
|
||||
const { styleable } = getContext("sdk") |
const { styleable, setBindableValue } = getContext("sdk") |
||||
const styles = getContext("style") |
const component = getContext("component") |
||||
|
|
||||
export let value = "" |
// Keep bindable value up to date |
||||
export let className = "" |
let value |
||||
export let type = "text" |
$: setBindableValue(value, $component.id) |
||||
|
|
||||
const onchange = ev => { |
|
||||
value = ev.target.value |
|
||||
} |
|
||||
</script> |
</script> |
||||
|
|
||||
<input |
<input bind:value on:change={onchange} use:styleable={$component.styles} /> |
||||
class={className} |
|
||||
{type} |
|
||||
{value} |
|
||||
on:change={onchange} |
|
||||
use:styleable={$styles} /> |
|
||||
|
|||||
Loading…
Reference in new issue