From 9a749017454c83164b5c08a02c854c8555fc69c2 Mon Sep 17 00:00:00 2001 From: Alex Anderson Date: Tue, 19 Sep 2017 06:25:08 +0200 Subject: [PATCH] Recalculate form fields when page is turned (#3920) Attempt to work around jr:choice-name() performance issues by reclalculating fields one more time on form load, and on every page change. If choice-name()s only reference fields on different pages, or vice versa, then maybe they will become usable. Issue: #3870 --- Gruntfile.js | 4 ---- patches/3804_jr-choice-name.patch | 20 -------------------- static/js/services/enketo.js | 7 +++++++ tests/karma/unit/services/enketo.js | 3 ++- 4 files changed, 9 insertions(+), 25 deletions(-) delete mode 100644 patches/3804_jr-choice-name.patch diff --git a/Gruntfile.js b/Gruntfile.js index 89a54f17a07..78b0206c80e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -252,10 +252,6 @@ module.exports = function(grunt) { // https://github.com/medic/medic-webapp/issues/2797 'patch node_modules/pouchdb-adapter-idb/lib/index.js < patches/pouchdb-ignore-safari-check.patch', - // patch enketo to support cascading of jr:choice-name() update results - // https://github.com/medic/medic-webapp/issues/3804 - 'patch node_modules/enketo-core/src/js/output.js < patches/3804_jr-choice-name.patch', - // patch enketo's bootstrap-datepicker dependency so that sass compiles properly 'patch node_modules/enketo-core/src/widget/date/datepicker-extended.scss < patches/enketo-datepicker-extended-sass-import.patch', ]; diff --git a/patches/3804_jr-choice-name.patch b/patches/3804_jr-choice-name.patch deleted file mode 100644 index c9e8e15901a..00000000000 --- a/patches/3804_jr-choice-name.patch +++ /dev/null @@ -1,20 +0,0 @@ -*** node_modules/enketo-core/src/js/output.js 2017-04-20 00:28:05.000000000 +0200 ---- static/js/enketo/core/output.js 2017-08-23 15:35:19.000000000 +0200 -*************** -*** 64,69 **** ---- 64,78 ---- - } - if ( $output.text() !== val ) { - $output.text( val ); -+ -+ // MEDIC CHANGES : BEGIN -+ // For s that are children of choice-groups, trigger -+ // dependent calculations in case one is using a jr:choice-name. -+ var owName = $output.closest( '.option-wrapper' ).find( 'input' ).attr( 'name' ); -+ if ( owName ) { -+ that.form.calc.update( owName ); -+ } -+ // MEDIC CHANGES : END - } - } ); - } diff --git a/static/js/services/enketo.js b/static/js/services/enketo.js index 368e5c088af..a51e6284dff 100644 --- a/static/js/services/enketo.js +++ b/static/js/services/enketo.js @@ -258,6 +258,7 @@ angular.module('inboxServices').service('Enketo', window.history.replaceState({ enketo_page_number: 0 }, ''); overrideNavigationButtons(form, wrapper); addPopStateHandler(form, wrapper); + forceRecalculate(form); return form; }); @@ -272,6 +273,7 @@ angular.module('inboxServices').service('Enketo', if(typeof newPageIndex === 'number') { window.history.pushState({ enketo_page_number: newPageIndex }, ''); } + forceRecalculate(form); }); return false; }); @@ -280,6 +282,7 @@ angular.module('inboxServices').service('Enketo', .off('.pagemode') .on('click.pagemode', function() { window.history.back(); + forceRecalculate(form); return false; }); }; @@ -464,6 +467,10 @@ angular.module('inboxServices').service('Enketo', }); }; + var forceRecalculate = function(form) { + form.calc.update(); + }; + this.save = function(formInternalId, form, docId) { return $q.resolve(form.validate()) .then(function(valid) { diff --git a/tests/karma/unit/services/enketo.js b/tests/karma/unit/services/enketo.js index 1b0365695f3..1a3b4006194 100644 --- a/tests/karma/unit/services/enketo.js +++ b/tests/karma/unit/services/enketo.js @@ -91,7 +91,8 @@ describe('Enketo service', function() { window.EnketoForm = EnketoForm; EnketoForm.returns({ - init: enketoInit + init: enketoInit, + calc: { update: function() {} } }); XmlForm.returns(KarmaUtils.mockPromise(null, { id: 'abc' }));