mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
fix context evaluation for simple forms (not configpanels)
This commit is contained in:
parent
51c9f1a03f
commit
10f4fb86b5
1 changed files with 16 additions and 13 deletions
|
@ -79,16 +79,19 @@ export function adressToFormValue (address) {
|
||||||
* @param {Object} forms - A nested form used in config panels.
|
* @param {Object} forms - A nested form used in config panels.
|
||||||
* @return {Boolean} - expression evaluation result.
|
* @return {Boolean} - expression evaluation result.
|
||||||
*/
|
*/
|
||||||
export function evaluateExpression (expression, forms) {
|
export function evaluateExpression (expression, form, nested = true) {
|
||||||
if (!expression) return true
|
if (!expression) return true
|
||||||
if (expression === '"false"') return false
|
if (expression === '"false"') return false
|
||||||
|
|
||||||
const context = Object.values(forms).reduce((ctx, args) => {
|
const context = nested
|
||||||
Object.entries(args).forEach(([id, value]) => {
|
? Object.values(form).reduce((merged, next) => ({ ...merged, ...next }))
|
||||||
ctx[id] = isObjectLiteral(value) && 'file' in value ? value.content : value
|
: form
|
||||||
})
|
|
||||||
return ctx
|
for (const key in context) {
|
||||||
}, {})
|
if (isObjectLiteral(context[key]) && 'file' in context[key]) {
|
||||||
|
context[key] = context[key].content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Allow to use match(var,regexp) function
|
// Allow to use match(var,regexp) function
|
||||||
const matchRe = /match(\s*(\w+)\s*,\s*"([^"]+)"\s*)/g
|
const matchRe = /match(\s*(\w+)\s*,\s*"([^"]+)"\s*)/g
|
||||||
|
@ -107,9 +110,9 @@ export function evaluateExpression (expression, forms) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a property to an Object that will dynamically returns a expression evaluation result.
|
// Adds a property to an Object that will dynamically returns a expression evaluation result.
|
||||||
function addEvaluationGetter (prop, obj, expr, ctx) {
|
function addEvaluationGetter (prop, obj, expr, ctx, nested) {
|
||||||
Object.defineProperty(obj, prop, {
|
Object.defineProperty(obj, prop, {
|
||||||
get: () => evaluateExpression(expr, ctx)
|
get: () => evaluateExpression(expr, ctx, nested)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,12 +360,12 @@ export function formatYunoHostArguments (args, forms) {
|
||||||
if (validation) validations[arg.id] = validation
|
if (validation) validations[arg.id] = validation
|
||||||
errors[arg.id] = error
|
errors[arg.id] = error
|
||||||
|
|
||||||
if ('visible' in arg && ![false, '"false"'].includes(arg.visible)) {
|
if ('visible' in arg && typeof arg.visible === 'string') {
|
||||||
addEvaluationGetter('visible', field, arg.visible, forms)
|
addEvaluationGetter('visible', field, arg.visible, forms || form, forms !== undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('enabled' in arg) {
|
if ('enabled' in arg && typeof arg.enabled === 'string') {
|
||||||
addEvaluationGetter('enabled', field.props, arg.enabled, forms)
|
addEvaluationGetter('enabled', field.props, arg.enabled, forms || form, forms !== undefined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue