forked from tsai/budibase
14 changed files with 198 additions and 222 deletions
@ -1 +0,0 @@ |
|||
export const trimSlash = str => str.replace(/^\/+|\/+$/g, "") |
|||
@ -1,49 +1,10 @@ |
|||
// import { isUndefined, isObject } from "lodash/fp"
|
|||
import { get } from "svelte/store"; |
|||
import getOr from "lodash/fp/getOr"; |
|||
// import { parseBinding, isStoreBinding } from "./parseBinding"
|
|||
import { appStore } from "./store"; |
|||
|
|||
export const getState = (state, path, fallback) => { |
|||
if (!state) return fallback |
|||
export const getState = (path, fallback) => { |
|||
if (!path || path.length === 0) return fallback |
|||
|
|||
return getOr(fallback, path, state); |
|||
|
|||
// if (path === "$") return state
|
|||
|
|||
// const pathParts = path.split(".")
|
|||
// const safeGetPath = (obj, currentPartIndex = 0) => {
|
|||
// const currentKey = pathParts[currentPartIndex]
|
|||
|
|||
// if (pathParts.length - 1 == currentPartIndex) {
|
|||
// const value = obj[currentKey]
|
|||
// if (isUndefined(value)) return fallback
|
|||
// else return value
|
|||
// }
|
|||
|
|||
// if (
|
|||
// obj[currentKey] === null ||
|
|||
// obj[currentKey] === undefined ||
|
|||
// !isObject(obj[currentKey])
|
|||
// ) {
|
|||
// return fallback
|
|||
// }
|
|||
|
|||
// return safeGetPath(obj[currentKey], currentPartIndex + 1)
|
|||
// }
|
|||
|
|||
// return safeGetPath(state)
|
|||
} |
|||
|
|||
// export const getStateOrValue = (globalState, prop, currentContext) => {
|
|||
// if (!prop) return prop
|
|||
|
|||
// const binding = parseBinding(prop)
|
|||
|
|||
// if (binding) {
|
|||
// const stateToUse = isStoreBinding(binding) ? globalState : currentContext
|
|||
|
|||
// return getState(stateToUse, binding.path, binding.fallback)
|
|||
// }
|
|||
|
|||
// return prop
|
|||
// }
|
|||
return getOr(fallback, path, get(appStore)); |
|||
} |
|||
@ -1,67 +1,67 @@ |
|||
export const BB_STATE_BINDINGPATH = "##bbstate" |
|||
export const BB_STATE_BINDINGSOURCE = "##bbsource" |
|||
export const BB_STATE_FALLBACK = "##bbstatefallback" |
|||
// export const BB_STATE_BINDINGPATH = "##bbstate"
|
|||
// export const BB_STATE_BINDINGSOURCE = "##bbsource"
|
|||
// export const BB_STATE_FALLBACK = "##bbstatefallback"
|
|||
|
|||
export const isBound = prop => !!parseBinding(prop) |
|||
// export const isBound = prop => !!parseBinding(prop)
|
|||
|
|||
/** |
|||
* |
|||
* @param {object|string|number} prop - component property to parse for a dynamic state binding |
|||
* @returns {object|boolean} |
|||
*/ |
|||
export const parseBinding = prop => { |
|||
if (!prop) return false |
|||
// /**
|
|||
// *
|
|||
// * @param {object|string|number} prop - component property to parse for a dynamic state binding
|
|||
// * @returns {object|boolean}
|
|||
// */
|
|||
// export const parseBinding = prop => {
|
|||
// if (!prop) return false
|
|||
|
|||
if (isBindingExpression(prop)) { |
|||
return parseBindingExpression(prop) |
|||
} |
|||
// if (isBindingExpression(prop)) {
|
|||
// return parseBindingExpression(prop)
|
|||
// }
|
|||
|
|||
if (isAlreadyBinding(prop)) { |
|||
return { |
|||
path: prop.path, |
|||
source: prop.source || "store", |
|||
fallback: prop.fallback, |
|||
} |
|||
} |
|||
// if (isAlreadyBinding(prop)) {
|
|||
// return {
|
|||
// path: prop.path,
|
|||
// source: prop.source || "store",
|
|||
// fallback: prop.fallback,
|
|||
// }
|
|||
// }
|
|||
|
|||
if (hasBindingObject(prop)) { |
|||
return { |
|||
path: prop[BB_STATE_BINDINGPATH], |
|||
fallback: prop[BB_STATE_FALLBACK] || "", |
|||
source: prop[BB_STATE_BINDINGSOURCE] || "store", |
|||
} |
|||
} |
|||
} |
|||
// if (hasBindingObject(prop)) {
|
|||
// return {
|
|||
// path: prop[BB_STATE_BINDINGPATH],
|
|||
// fallback: prop[BB_STATE_FALLBACK] || "",
|
|||
// source: prop[BB_STATE_BINDINGSOURCE] || "store",
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
|
|||
export const isStoreBinding = binding => binding && binding.source === "store" |
|||
export const isContextBinding = binding => |
|||
binding && binding.source === "context" |
|||
// export const isEventBinding = binding => binding && binding.source === "event"
|
|||
// export const isStoreBinding = binding => binding && binding.source === "store"
|
|||
// export const isContextBinding = binding =>
|
|||
// binding && binding.source === "context"
|
|||
// // export const isEventBinding = binding => binding && binding.source === "event"
|
|||
|
|||
const hasBindingObject = prop => |
|||
typeof prop === "object" && prop[BB_STATE_BINDINGPATH] !== undefined |
|||
// const hasBindingObject = prop =>
|
|||
// typeof prop === "object" && prop[BB_STATE_BINDINGPATH] !== undefined
|
|||
|
|||
const isAlreadyBinding = prop => typeof prop === "object" && prop.path |
|||
// const isAlreadyBinding = prop => typeof prop === "object" && prop.path
|
|||
|
|||
const isBindingExpression = prop => |
|||
typeof prop === "string" && |
|||
(prop.startsWith("state.") || |
|||
prop.startsWith("context.") || |
|||
prop.startsWith("event.") || |
|||
prop.startsWith("route.")) |
|||
// const isBindingExpression = prop =>
|
|||
// typeof prop === "string" &&
|
|||
// (prop.startsWith("state.") ||
|
|||
// prop.startsWith("context.") ||
|
|||
// prop.startsWith("event.") ||
|
|||
// prop.startsWith("route."))
|
|||
|
|||
const parseBindingExpression = prop => { |
|||
let [source, ...rest] = prop.split(".") |
|||
let path = rest.join(".") |
|||
// const parseBindingExpression = prop => {
|
|||
// let [source, ...rest] = prop.split(".")
|
|||
// let path = rest.join(".")
|
|||
|
|||
if (source === "route") { |
|||
source = "state" |
|||
path = `##routeParams.${path}` |
|||
} |
|||
// if (source === "route") {
|
|||
// source = "state"
|
|||
// path = `##routeParams.${path}`
|
|||
// }
|
|||
|
|||
return { |
|||
fallback: "", // TODO: provide fallback support
|
|||
source, |
|||
path, |
|||
} |
|||
} |
|||
// return {
|
|||
// fallback: "", // TODO: provide fallback support
|
|||
// source,
|
|||
// path,
|
|||
// }
|
|||
// }
|
|||
|
|||
@ -1,40 +1,17 @@ |
|||
// import isObject from "lodash/fp/isObject"
|
|||
import set from "lodash/fp/set"; |
|||
import { parseBinding } from "./parseBinding" |
|||
import { appStore } from "./store"; |
|||
|
|||
export const setState = (store, path, value) => { |
|||
export const setState = (path, value) => { |
|||
if (!path || path.length === 0) return |
|||
|
|||
// const pathParts = path.split(".")
|
|||
|
|||
// const safeSetPath = (state, currentPartIndex = 0) => {
|
|||
// const currentKey = pathParts[currentPartIndex]
|
|||
|
|||
// if (pathParts.length - 1 == currentPartIndex) {
|
|||
// state[currentKey] = value
|
|||
// return
|
|||
// }
|
|||
|
|||
// if (
|
|||
// state[currentKey] === null ||
|
|||
// state[currentKey] === undefined ||
|
|||
// !isObject(state[currentKey])
|
|||
// ) {
|
|||
// state[currentKey] = {}
|
|||
// }
|
|||
|
|||
// safeSetPath(state[currentKey], currentPartIndex + 1)
|
|||
// }
|
|||
|
|||
store.update(state => { |
|||
// safeSetPath(state)
|
|||
appStore.update(state => { |
|||
state = set(path, value, state); |
|||
return state |
|||
}) |
|||
} |
|||
|
|||
export const setStateFromBinding = (store, binding, value) => { |
|||
const parsedBinding = parseBinding(binding) |
|||
if (!parsedBinding) return |
|||
return setState(store, parsedBinding.path, value) |
|||
} |
|||
// export const setStateFromBinding = (store, binding, value) => {
|
|||
// const parsedBinding = parseBinding(binding)
|
|||
// if (!parsedBinding) return
|
|||
// return setState(store, parsedBinding.path, value)
|
|||
// }
|
|||
|
|||
@ -0,0 +1,16 @@ |
|||
import { writable } from "svelte/store"; |
|||
|
|||
const appStore = writable({}); |
|||
appStore.actions = { |
|||
|
|||
}; |
|||
|
|||
const routerStore = writable({}); |
|||
routerStore.actions = { |
|||
|
|||
} |
|||
|
|||
export { |
|||
appStore, |
|||
routerStore |
|||
} |
|||
Loading…
Reference in new issue