forked from tsai/budibase
44 changed files with 169 additions and 138 deletions
@ -0,0 +1,24 @@ |
|||||
|
<script> |
||||
|
import { onMount, getContext, setContext } from "svelte" |
||||
|
import { createDataContextStore } from "../store" |
||||
|
|
||||
|
export let row |
||||
|
|
||||
|
// Get current contexts |
||||
|
const dataContext = getContext("data") |
||||
|
|
||||
|
// Clone current context to this context |
||||
|
const newDataContext = createDataContextStore($dataContext) |
||||
|
setContext("data", newDataContext) |
||||
|
|
||||
|
// Add additional layer to context |
||||
|
let loaded = false |
||||
|
onMount(() => { |
||||
|
newDataContext.actions.addContext(row) |
||||
|
loaded = true |
||||
|
}) |
||||
|
</script> |
||||
|
|
||||
|
{#if loaded} |
||||
|
<slot /> |
||||
|
{/if} |
||||
@ -0,0 +1,43 @@ |
|||||
|
import { writable, get } from "svelte/store" |
||||
|
import { enrichDataBinding } from "../utils" |
||||
|
import { cloneDeep } from "lodash/fp" |
||||
|
|
||||
|
const initialValue = { |
||||
|
parent: null, |
||||
|
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 => { |
||||
|
store.update(state => { |
||||
|
if (state.data) { |
||||
|
state.parent = { |
||||
|
parent: state.parent, |
||||
|
data: state.data, |
||||
|
} |
||||
|
} |
||||
|
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,3 +1,4 @@ |
|||||
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" |
||||
|
|||||
@ -1,2 +1,3 @@ |
|||||
export { getAppId } from "./getAppId" |
export { getAppId } from "./getAppId" |
||||
export { styleable } from "./styleable" |
export { styleable } from "./styleable" |
||||
|
export { enrichDataBinding } from "./enrichDataBinding" |
||||
|
|||||
@ -1,7 +1,5 @@ |
|||||
<script> |
<script> |
||||
import Form from "./Form.svelte" |
import Form from "./Form.svelte" |
||||
|
|
||||
export let styles |
|
||||
</script> |
</script> |
||||
|
|
||||
<Form {styles} wide={false} /> |
<Form wide={false} /> |
||||
|
|||||
@ -1,7 +1,5 @@ |
|||||
<script> |
<script> |
||||
import Form from "./Form.svelte" |
import Form from "./Form.svelte" |
||||
|
|
||||
export let styles |
|
||||
</script> |
</script> |
||||
|
|
||||
<Form {styles} wide /> |
<Form wide /> |
||||
|
|||||
@ -1,19 +0,0 @@ |
|||||
<script> |
|
||||
import { setContext, onMount } from "svelte" |
|
||||
import { createDataProviderStore } from "./stores/dataProvider" |
|
||||
|
|
||||
const dataProviderStore = createDataProviderStore() |
|
||||
setContext("data", dataProviderStore) |
|
||||
|
|
||||
export let row |
|
||||
let loaded = false |
|
||||
|
|
||||
onMount(async () => { |
|
||||
await dataProviderStore.actions.setRow(row) |
|
||||
loaded = true |
|
||||
}) |
|
||||
</script> |
|
||||
|
|
||||
{#if loaded} |
|
||||
<slot /> |
|
||||
{/if} |
|
||||
@ -1,26 +0,0 @@ |
|||||
import { getContext } from "svelte" |
|
||||
import { writable } from "svelte/store" |
|
||||
|
|
||||
export const createDataProviderStore = () => { |
|
||||
const { API } = getContext("app") |
|
||||
|
|
||||
const store = writable({ |
|
||||
row: {}, |
|
||||
table: null, |
|
||||
}) |
|
||||
const setRow = async row => { |
|
||||
let table |
|
||||
if (row && row.tableId) { |
|
||||
table = await API.fetchTableDefinition(row.tableId) |
|
||||
} |
|
||||
store.update(state => { |
|
||||
state.row = row |
|
||||
state.table = table |
|
||||
return state |
|
||||
}) |
|
||||
} |
|
||||
return { |
|
||||
subscribe: store.subscribe, |
|
||||
actions: { setRow }, |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue