From 3df486008c7ca2cf6c4b3685b6a49c7a4b6f0a27 Mon Sep 17 00:00:00 2001 From: Martijn van de Rijdt Date: Wed, 17 Apr 2024 16:58:40 -0400 Subject: [PATCH 1/2] moved some relevant.js customizations --- packages/enketo-core/src/js/relevant.js | 17 ++++++- .../public/js/src/module/relevant.js | 50 ------------------- 2 files changed, 15 insertions(+), 52 deletions(-) diff --git a/packages/enketo-core/src/js/relevant.js b/packages/enketo-core/src/js/relevant.js index e26caad1..6760aca9 100644 --- a/packages/enketo-core/src/js/relevant.js +++ b/packages/enketo-core/src/js/relevant.js @@ -98,8 +98,14 @@ export default { update(updated, forceClearNonRelevant = config.forceClearNonRelevant) { const nodes = this.form .getRelatedNodes('data-relevant', '', updated) + // the OC customization: **always** add + // .or-group.invalid-relevant and .or-group-data.invalid-relevant + .add( + this.form.getRelatedNodes('data-relevant', '.invalid-relevant') + ) .get(); - + // OC sets forceClearNonRelevant to false always + forceClearNonRelevant = false; this.updateNodes(nodes, forceClearNonRelevant, updated ?? {}); }, @@ -331,8 +337,12 @@ export default { selfRelevant(branchNode) { return ( !branchNode.classList.contains('disabled') && - !branchNode.classList.contains('pre-init') + !branchNode.classList.contains('pre-init') && + !branchNode.classList.contains('invalid-relevant') ); + // The third clause is an OC customization that ensures + // that a branch will not be disabled if it has a question with + // a value. }, /** @@ -526,6 +536,9 @@ export default { this.form.widgets.enable(branchNode); this.activate(branchNode); } + // OC customization to remove any shown irrelevant errors on the group + // (and perhaps question as well?) once it becomes relevant again. + branchNode.classList.remove('invalid-relevant'); return change; }, diff --git a/packages/enketo-express/public/js/src/module/relevant.js b/packages/enketo-express/public/js/src/module/relevant.js index 1d5885ac..dbc2388f 100644 --- a/packages/enketo-express/public/js/src/module/relevant.js +++ b/packages/enketo-express/public/js/src/module/relevant.js @@ -4,56 +4,6 @@ import branchModule from 'enketo-core/src/js/relevant'; import { getXPath } from 'enketo-core/src/js/dom-utils'; import events from 'enketo-core/src/js/event'; -/** - * Overwrite core functionality by **always** adding - * .or-group.invalid-relevant and .or-group-data.invalid-relevant. - * - * @param updated - */ -branchModule.update = function (updated) { - if (!this.form) { - throw new Error( - 'Branch module not correctly instantiated with form property.' - ); - } - - const nodes = this.form - .getRelatedNodes('data-relevant', '', updated) - // the OC customization: - .add(this.form.getRelatedNodes('data-relevant', '.invalid-relevant')) - .get(); - - this.updateNodes(nodes); -}; - -branchModule.originalSelfRelevant = branchModule.selfRelevant; - -// Overwrite in order to add the && !branchNode.classList.contains('invalid-relevant') clause because an irrelevant branch in OC, -// would not be disabled if it is a question with a value! -branchModule.selfRelevant = function (branchNode) { - return ( - this.originalSelfRelevant(branchNode) && - !branchNode.classList.contains('invalid-relevant') - ); -}; - -branchModule.originalEnable = branchModule.enable; - -/** - * Overwrite core functionality. - * The reason for this customization is to remove any shown irrelevant errors on the group (and perhaps question as well?) - * once it becomes relevant again. - * - * @param branchNode - * @param path - */ -branchModule.enable = function (branchNode, path) { - const change = this.originalEnable(branchNode, path); - branchNode.classList.remove('invalid-relevant'); - - return change; -}; - /* * Overwrite to always call this.clear if ever enabled and currently relevant. */ From 67a1f333ce4050f9d3a7d6cf9fc5badccf880283 Mon Sep 17 00:00:00 2001 From: Martijn van de Rijdt Date: Mon, 13 May 2024 11:29:32 -0400 Subject: [PATCH 2/2] disabled test due to customized behavior for OC --- packages/enketo-core/test/spec/form.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/enketo-core/test/spec/form.spec.js b/packages/enketo-core/test/spec/form.spec.js index 28ca210b..59f97a3e 100755 --- a/packages/enketo-core/test/spec/form.spec.js +++ b/packages/enketo-core/test/spec/form.spec.js @@ -730,7 +730,8 @@ describe('branching functionality', () => { expect(form.model.node(three).getVal()).to.equal('three'); }); - it('by clearing values of non-relevant questions when form.clearNonRelevant() is called', () => { + // OC disabled forceClearing of non-relevant values + xit('by clearing values of non-relevant questions when form.clearNonRelevant() is called', () => { const form = loadForm(name); form.init(); expect(form.model.node(two).getVal()).to.equal('two');