Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/move relevant customizations #6

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/enketo-core/src/js/relevant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {});
},

Expand Down Expand Up @@ -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.
},

/**
Expand Down Expand Up @@ -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;
},
Expand Down
3 changes: 2 additions & 1 deletion packages/enketo-core/test/spec/form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
50 changes: 0 additions & 50 deletions packages/enketo-express/public/js/src/module/relevant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Loading