Skip to content

Commit

Permalink
add when support for visibility condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmars committed Oct 10, 2023
1 parent 62e664e commit 16c3235
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 262 deletions.
22 changes: 11 additions & 11 deletions docs/demos/frgov/pega-govfr.js

Large diffs are not rendered by default.

188 changes: 94 additions & 94 deletions docs/demos/ukgds/pega-govuk.js

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions docs/demos/uswds/pega-govus.js

Large diffs are not rendered by default.

100 changes: 50 additions & 50 deletions docs/pega-mashup-light-webcomponentv2.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/pega-mashup-webcomponent-all.js

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions docs/pega-mashup-webcomponent-light-all.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/core/src/interpreter/v2/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ export default class PegaServices extends PegaElement {
if (el && response.data.caseInfo && response.data.caseInfo.content) {
this.data.data.caseInfo.content = response.data.caseInfo.content;
this.casedata.content = response.data.caseInfo.content;
this.data.uiResources.context_data = response.uiResources.context_data;
render(this.renderMainLayout(this.data.uiResources.resources.views[this.actionID], 'Obj'), el);
}
return;
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/utils/fields-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,31 @@ export const FieldPreProcessing = (data, path, isReadOnly, webcomp, context) =>

/* Visibility condition */
if (typeof data.config.visibility === 'string' && data.config.visibility !== 'true') {
if (!isValidExpression(data.config.visibility, content, context)) return null;
if (data.config.visibility.startsWith('@W ')) {
webcomp.isDeclarativeTarget = true;
}
if (!isValidExpression(data.config.visibility, content, webcomp, context)) return null;
} else if (data.config.visibility === false || data.config.visibility === 'false') {
return null;
}
/* Read-only condition */
data.config.readonlystate = false;
if (typeof data.config.readOnly === 'string' && data.config.readOnly !== 'false') {
data.config.readonlystate = isValidExpression(data.config.readOnly, content, context);
data.config.readonlystate = isValidExpression(data.config.readOnly, content, webcomp, context);
} else if (data.config.readOnly === true || data.config.readOnly === 'true' || isDeclarativeTarget) {
data.config.readonlystate = true;
}
/* Required condition */
data.config.requiredstate = false;
if (typeof data.config.required === 'string' && data.config.required !== 'false') {
data.config.requiredstate = isValidExpression(data.config.required, content, context);
data.config.requiredstate = isValidExpression(data.config.required, content, webcomp, context);
} else if (data.config.required === true || data.config.required === 'true') {
data.config.requiredstate = true;
}
/* Disabled condition */
data.config.disabledstate = false;
if (typeof data.config.disabled === 'string' && data.config.disabled !== 'false') {
data.config.disabledstate = isValidExpression(data.config.disabled, content, context);
data.config.disabledstate = isValidExpression(data.config.disabled, content, webcomp, context);
} else if (data.config.disabled === true || data.config.disabled === 'true') {
data.config.disabledstate = true;
}
Expand Down
52 changes: 27 additions & 25 deletions packages/core/src/utils/form-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,37 +338,39 @@ function compare(post, operator, value) {
* Check if the condition is true - INCOMPLETE - does not handle all use cases like OR and AND
*
*/
export const isValidExpression = (expression, content, context) => {
const exprs = expression.replace('@E ', '').split('&&');
for (const expr in exprs) {
const ops = exprs[expr].trim().match(/[\w.]+|[><=!]+|'[^']+'/g);
if (ops.length === 3) {
const val = context === '' ? content[ops[0].substring(1)] : getValue(content, context + ops[0]);
if (typeof val !== 'undefined') {
if (!compare(val, ops[1], ops[2].replace(/^'|'$/g, ''))) {
return false;
export const isValidExpression = (expression, content, webcomp, context) => {
if (expression.startsWith('@E ')) {
const exprs = expression.replace('@E ', '').split('&&');
for (const expr in exprs) {
const ops = exprs[expr].trim().match(/[\w.]+|[><=!]+|'[^']+'/g);
if (ops.length === 3) {
const val = context === '' ? content[ops[0].substring(1)] : getValue(content, context + ops[0]);
if (typeof val !== 'undefined') {
if (!compare(val, ops[1], ops[2].replace(/^'|'$/g, ''))) {
return false;
}
}
} else if (ops.length === 2 && ops[1] === 'IS_NOT_NULL') {
const val = context === '' ? content[ops[0].substring(1)] : getValue(content, context + ops[0]);
if (typeof val !== 'undefined') {
if (!compare(val, '!=', '')) {
return false;
}
}
}
}
}
return true;
};

/**
* Apply the visibility conditions - Work in progress not used currently - instead do refresh
*/
export const applyVisibleToForm = (form, content) => {
const els = form.querySelectorAll('[data-visibility]');
for (let i = 0; i < els.length; i++) {
const expression = els[i].getAttribute('data-visibility');
if (expression !== null) {
if (isValidExpression(expression, content)) {
els[i].style = 'display:none';
} else {
els[i].style = '';
} else if (expression.startsWith('@W ')) {
const exprs = expression.replace('@W ', '').split('&&');
// eslint-disable-next-line no-underscore-dangle
const listWhen = webcomp?.data?.uiResources?.context_data?.caseInfo?.content?.summary_of_when_conditions__;
if (listWhen) {
for (const expr in exprs) {
const ops = exprs[expr];
if (!listWhen[ops]) return false;
}
}
}
return true;
};

/**
Expand Down

0 comments on commit 16c3235

Please sign in to comment.