forked from tsai/budibase
23 changed files with 156 additions and 97 deletions
@ -0,0 +1,45 @@ |
|||||
|
import { enrichDataBinding } from "./enrichDataBinding" |
||||
|
import { routeStore } from "../store" |
||||
|
import { saveRow, deleteRow } from "../api" |
||||
|
|
||||
|
const saveRowHandler = async (action, context) => { |
||||
|
let draft = context[`${action.parameters.contextPath}_draft`] |
||||
|
if (action.parameters.fields) { |
||||
|
Object.entries(action.parameters.fields).forEach(([key, entry]) => { |
||||
|
draft[key] = enrichDataBinding(entry.value, context) |
||||
|
}) |
||||
|
} |
||||
|
await saveRow(draft) |
||||
|
} |
||||
|
|
||||
|
const deleteRowHandler = async (action, context) => { |
||||
|
const { tableId, revId, rowId } = action.parameters |
||||
|
await deleteRow({ |
||||
|
tableId: enrichDataBinding(tableId, context), |
||||
|
rowId: enrichDataBinding(rowId, context), |
||||
|
revId: enrichDataBinding(revId, context), |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
const navigationHandler = action => { |
||||
|
routeStore.actions.navigate(action.parameters.url) |
||||
|
} |
||||
|
|
||||
|
const handlerMap = { |
||||
|
["Save Row"]: saveRowHandler, |
||||
|
["Delete Row"]: deleteRowHandler, |
||||
|
["Navigate To"]: navigationHandler, |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Parses an array of actions and returns a function which will execute the |
||||
|
* actions in the current context. |
||||
|
*/ |
||||
|
export const enrichButtonActions = (actions, context) => { |
||||
|
const handlers = actions.map(def => handlerMap[def["##eventHandlerType"]]) |
||||
|
return async () => { |
||||
|
for (let i = 0; i < handlers.length; i++) { |
||||
|
await handlers[i](actions[i], context) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
import { enrichDataBindings } from "./enrichDataBinding" |
||||
|
import { enrichButtonActions } from "./buttonActions" |
||||
|
|
||||
|
/** |
||||
|
* Enriches component props. |
||||
|
* Data bindings are enriched, and button actions are enriched. |
||||
|
*/ |
||||
|
export const enrichProps = (props, dataContexts, dataBindings) => { |
||||
|
// Exclude all private props that start with an underscore
|
||||
|
let validProps = {} |
||||
|
Object.entries(props) |
||||
|
.filter(([name]) => !name.startsWith("_")) |
||||
|
.forEach(([key, value]) => { |
||||
|
validProps[key] = value |
||||
|
}) |
||||
|
|
||||
|
// Create context of all bindings and data contexts
|
||||
|
// Duplicate the closest context as "data" which the builder requires
|
||||
|
const context = { |
||||
|
...dataContexts, |
||||
|
...dataBindings, |
||||
|
data: dataContexts[dataContexts.closestComponentId], |
||||
|
data_draft: dataContexts[`${dataContexts.closestComponentId}_draft`], |
||||
|
} |
||||
|
|
||||
|
// Enrich all data bindings in top level props
|
||||
|
let enrichedProps = enrichDataBindings(validProps, context) |
||||
|
|
||||
|
// Enrich button actions if they exist
|
||||
|
if (props._component.endsWith("/button") && enrichedProps.onClick) { |
||||
|
enrichedProps.onClick = enrichButtonActions(enrichedProps.onClick, context) |
||||
|
} |
||||
|
|
||||
|
return enrichedProps |
||||
|
} |
||||
@ -1,3 +0,0 @@ |
|||||
export { getAppId } from "./getAppId" |
|
||||
export { styleable } from "./styleable" |
|
||||
export { enrichDataBinding } from "./enrichDataBinding" |
|
||||
Loading…
Reference in new issue