Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #870 from eyelidlessness/feature/849-non-relevant-…
Browse files Browse the repository at this point in the history
…exclude-from-calc

Optionally clear non-relevant values/remove them from calculations
  • Loading branch information
eyelidlessness authored May 17, 2022
2 parents 5e1ec1b + 0868644 commit 842041b
Show file tree
Hide file tree
Showing 22 changed files with 2,480 additions and 59 deletions.
7 changes: 6 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ module.exports = (grunt) => {
grunt.registerTask('test:watch', ['transforms', 'concurrent:test']);
grunt.registerTask('css', ['sass']);
grunt.registerTask('server', ['connect:server:keepalive']);
grunt.registerTask('develop', ['css', 'compile', 'concurrent:develop']);
grunt.registerTask('develop', [
'css',
'compile',
'transforms',
'concurrent:develop',
]);
grunt.registerTask('default', ['css', 'compile']);
};
6 changes: 5 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export default /** @type {const} */ ({
computeAsync: window.location.search.includes('&computeAsync'),
},

forceClearNonRelevant: window.location.search.includes('&clearNonRelevant'),
/**
* When set to `true`, non-relevant values will be treated as blank. This behavior
* is largely consistent with JavaRosa.
*/
excludeNonRelevant: window.location.search.includes('&excludeNonRelevant'),

maps: [
{
Expand Down
2 changes: 1 addition & 1 deletion jsdoc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
readme: 'README.md',
template: 'node_modules/docdash',
},
plugins: ['plugins/markdown'],
plugins: ['plugins/markdown', 'jsdoc-ts-utils'],
source: {
include: ['src/', './README.md'],
},
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"grunt-sass": "^3.1.0",
"grunt-shell": "^3.0.1",
"jsdoc": "^3.6.10",
"jsdoc-ts-utils": "^2.0.1",
"karma": "^6.3.19",
"karma-chrome-launcher": "^3.1.1",
"karma-coverage": "^2.2.0",
Expand Down
29 changes: 26 additions & 3 deletions src/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ import './plugins';
import './extend';
import { callOnIdle } from './timers';

/**
* @typedef FormOptions
* @property {string} [language] Overrides the default languages rules of the XForm itself. Pass any valid and present-in-the-form IANA subtag string, e.g. `ar`.
* @property {boolean} [printRelevantOnly] If `printRelevantOnly` is set to `true`
* or not set at all, printing the form only includes what is visible, ie. all the
* groups and questions that do not have a `relevant` expression or for which the
* expression evaluates to `true`.
*/

/**
* Class: Form
*
* Most methods are prototype method to facilitate customizations outside of enketo-core.
*
* @param {Element} formEl - HTML form element (a product of Enketo Transformer after transforming a valid ODK XForm)
* @param {FormDataObj} data - Data object containing XML model, (partial) XML instance-to-load, external data and flag about whether instance-to-load has already been submitted before.
* @param {object} [options] - form options
* @param {boolean} [options.printRelevantOnly] - If `printRelevantOnly` is set to `true` or not set at all, printing the form only includes what is visible, ie. all the groups and questions that do not have a `relevant` expression or for which the expression evaluates to `true`.
* @param {string} [options.language] - Overrides the default languages rules of the XForm itself. Pass any valid and present-in-the-form IANA subtag string, e.g. `ar`.
* @param {FormOptions} [options]
* @class
*/
function Form(formEl, data, options) {
Expand Down Expand Up @@ -79,6 +86,12 @@ function Form(formEl, data, options) {
* Getter and setter functions
*/
Form.prototype = {
/** @type {string[] | null} */
repeatPathPrefixes: null,

/** @type {Record<string, string | null>} */
nodePathToRepeatPath: {},

/**
* @type {Array}
*/
Expand Down Expand Up @@ -356,6 +369,16 @@ Form.prototype.init = function () {
});

this.all = {};

// Builds a cache of known repeat path prefixes `repeat.init`.
// The cache is sorted by length, longest to shortest, to ensure
// that lookups using this cache find the deepest nested repeat
// for a given path.
this.repeatPathPrefixes = Array.from(
this.view.html.querySelectorAll('.or-repeat-info')
)
.map((element) => `${element.dataset.name}/`)
.sort((a, b) => b.length - a.length);
}

this.view.html.removeEventListener(
Expand Down
14 changes: 11 additions & 3 deletions src/js/nodeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import $ from 'jquery';
import types from './types';
import event from './event';
import { getXPath } from './dom-utils';
import { isNodeRelevant, setNonRelevantValue } from './relevant';

/**
* @typedef NodesetFilter
Expand Down Expand Up @@ -119,12 +120,19 @@ Nodeset.prototype.setVal = function (newVals, xmlDataType) {
}

newVal = this.convert(newVal, xmlDataType);

const strVal = String(newVal);
const targets = this.getElements();

if (targets.length === 1 && newVal.toString() !== curVal.toString()) {
if (targets.length === 1 && strVal !== curVal.toString()) {
const target = targets[0];
// first change the value so that it can be evaluated in XPath (validated)
target.textContent = newVal.toString();
// First change the value so that it can be evaluated in XPath (validated).
if (isNodeRelevant(target)) {
target.textContent = strVal;
} else {
setNonRelevantValue(target, strVal);
}

// then return validation result
updated = this.getClosestRepeat();
updated.nodes = [target.nodeName];
Expand Down
Loading

0 comments on commit 842041b

Please sign in to comment.