Skip to content

Commit

Permalink
feat(core/forms): evaluate formula for defaultValue only when field i…
Browse files Browse the repository at this point in the history
…s visible
  • Loading branch information
sara-gnucoop committed Dec 23, 2022
1 parent a7295b8 commit 2a3eddb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {AjfContext, evaluateExpression} from '@ajf/core/models';
import {EventEmitter} from '@angular/core';

import {AjfFieldInstance} from '../../interface/fields-instances/field-instance';
import {AjfNode} from '../../interface/nodes/node';
import {AjfNodeInstanceCreate, createNodeInstance} from '../nodes-instances/create-node-instance';
import {nodeInstanceCompleteName} from '../nodes-instances/node-instance-complete-name';

Expand All @@ -44,6 +45,7 @@ export type AjfFieldInstanceCreate = AjfNodeInstanceCreate & Partial<AjfFieldIns
export function createFieldInstance(
instance: AjfFieldInstanceCreate,
context: AjfContext,
containerNode?: AjfNode | null,
): AjfFieldInstance {
const nodeInstance = createNodeInstance(instance);
let value: any = null;
Expand All @@ -55,7 +57,13 @@ export function createFieldInstance(
value = context[completeName];
} else if (instance.node.defaultValue != null) {
if (instance.node.defaultValue.formula != null) {
context[completeName] = evaluateExpression(instance.node.defaultValue.formula, context);
let visibility: boolean = nodeInstance.visible ? nodeInstance.visible : false;
if (visibility && containerNode && containerNode.visibility) {
visibility = evaluateExpression(containerNode.visibility.condition, context);
}
if (visibility) {
context[completeName] = evaluateExpression(instance.node.defaultValue.formula, context);
}
} else {
context[completeName] = instance.node.defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {AjfNodeInstance} from '../../interface/nodes-instances/node-instance';
import {AjfNode} from '../../interface/nodes/node';
import {isContainerNode} from '../nodes/is-container-node';

/**
* It returns the container node of the node.
*/
export function getContainerNode(
allNodes: (AjfNode | AjfNodeInstance)[],
node: AjfNode,
): AjfNode | null {
let parentNode: AjfNode | null = null;
let curParent: number | null = node.parent;
while (curParent != null && parentNode == null) {
const curNode = allNodes
.map((n: AjfNode | AjfNodeInstance) => (n as AjfNodeInstance).node || (n as AjfNode))
.find(n => n.id == curParent);
if (curNode) {
if (isContainerNode(curNode)) {
parentNode = curNode;
}
}
curParent = curNode != null ? curNode.parent : null;
}
return parentNode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {createValidationGroup} from '../validation/create-validation-group';
import {createWarningGroup} from '../warning/create-warning-group';

import {getAncestorRepeatingNodesNames} from './get-ancestor-repeating-nodes-names';
import {getContainerNode} from './get-container-node';
import {getInstanceCondition} from './get-instance-condition';
import {getInstanceConditions} from './get-instance-conditions';
import {getInstanceFormula} from './get-instance-formula';
Expand Down Expand Up @@ -93,7 +94,8 @@ export function nodeToNodeInstance(
instance = createTableFieldInstance({node: field, prefix}, context);
break;
default:
instance = createFieldInstance({node: field, prefix}, context);
const containerNode = getContainerNode(allNodes, node);
instance = createFieldInstance({node: field, prefix}, context, containerNode);
break;
}
}
Expand Down

0 comments on commit 2a3eddb

Please sign in to comment.