Skip to content

Commit

Permalink
[compile-contact-summary] separate templated source into proper .js file
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jul 5, 2018
1 parent 4c2b5fe commit 5b82f0d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 88 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.17.6
* [compile-contact-summary] separate templated source into proper .js file
* Fix unit tests for compile-contact-summary

## v1.17.5
* Move bundled nools code to a proper .js file

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-conf",
"version": "1.17.5",
"version": "1.17.6",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/contact-summary/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"esversion": 5,
"globals": {
"cards": false,
"contact": false,
"context": false,
"fields": false,
"reports": false
},
"undef": true,
"unused": true
}
86 changes: 86 additions & 0 deletions src/contact-summary/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function isReportValid(report) {
// valid XForms won't have .errors field
// valid JSON forms will have empty array errors:[]
return report && !(report.errors && report.errors.length);
}

var result = {
cards: [],
fields: fields.filter(function(f) {
if(f.appliesToType === contact.type ||
(f.appliesToType.charAt(0) === '!' && f.appliesToType.slice(1) !== contact.type)) {
if(!f.appliesIf || f.appliesIf()) {
delete f.appliesToType;
delete f.appliesIf;
return true;
}
}
}),
};


function addCard(card, r) {
if(!card.appliesIf(r)) return;

function addValue(src, dst, prop) {
switch(typeof src[prop]) {
case 'undefined': return;
case 'function': dst[prop] = src[prop](r); break;
default: dst[prop] = src[prop];
}
}

var fields = typeof card.fields === 'function' ?
card.fields(r) :
card.fields
.filter(function(f) {
switch(typeof f.appliesIf) {
case 'undefined': return true;
case 'function': return f.appliesIf(r);
default: return f.appliesIf;
}
})
.map(function(f) {
var ret = {};
addValue(f, ret, 'label');
addValue(f, ret, 'value');
addValue(f, ret, 'translate');
addValue(f, ret, 'filter');
addValue(f, ret, 'width');
addValue(f, ret, 'icon');
if(f.context) {
ret.context = {};
addValue(f.context, ret.context, 'count');
addValue(f.context, ret.context, 'total');
}
return ret;
});

result.cards.push({
label: card.label,
fields: fields,
});

if(card.modifyContext) card.modifyContext(context, r);
}

cards.forEach(function(card) {
var idx1, r;
switch(card.appliesToType) {
case 'report':
for(idx1=0; idx1<reports.length; ++idx1) {
r = reports[idx1];
if(!isReportValid(r)) continue;
addCard(card, r);
}
break;
default:
if(contact.type !== card.appliesToType) return;
addCard(card);
}
});

result.context = context;

// return the result for 2.13+ as per #2635
return result;
88 changes: 2 additions & 86 deletions src/lib/compile-contact-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,98 +17,14 @@ module.exports = projectDir => {
if(fs.exists(freeformPath)) {
code = templatedJs.fromFile(projectDir, freeformPath);
} else if(fs.exists(structuredPath)) {
const contactSummaryLib = fs.read(`${__dirname}/../contact-summary/lib.js`);
code = templatedJs.fromString(projectDir, `
var context, fields, cards;
__include_inline__('contact-summary-extras.js');
__include_inline__('contact-summary.templated.js');
function isReportValid(report) {
// valid XForms won't have .errors field
// valid JSON forms will have empty array errors:[]
return report && !(report.errors && report.errors.length);
}
var result = {
cards: [],
fields: fields.filter(function(f) {
if(f.appliesToType === contact.type ||
(f.appliesToType.charAt(0) === '!' && f.appliesToType.slice(1) !== contact.type)) {
if(!f.appliesIf || f.appliesIf()) {
delete f.appliesToType;
delete f.appliesIf;
return true;
}
}
}),
};
function addCard(card, r) {
if(!card.appliesIf(r)) return;
function addValue(src, dst, prop) {
switch(typeof src[prop]) {
case 'undefined': return;
case 'function': dst[prop] = src[prop](r); break;
default: dst[prop] = src[prop];
}
}
var fields = typeof card.fields === 'function' ?
card.fields(r) :
card.fields
.filter(function(f) {
switch(typeof f.appliesIf) {
case 'undefined': return true;
case 'function': return f.appliesIf(r);
default: return f.appliesIf;
}
})
.map(function(f) {
var ret = {};
addValue(f, ret, 'label');
addValue(f, ret, 'value');
addValue(f, ret, 'translate');
addValue(f, ret, 'filter');
addValue(f, ret, 'width');
addValue(f, ret, 'icon');
if(f.context) {
ret.context = {};
addValue(f.context, ret.context, 'count');
addValue(f.context, ret.context, 'total');
}
return ret;
});
result.cards.push({
label: card.label,
fields: fields,
});
if(card.modifyContext) card.modifyContext(context, r);
}
cards.forEach(function(card) {
var idx1, r;
switch(card.appliesToType) {
case 'report':
for(idx1=0; idx1<reports.length; ++idx1) {
r = reports[idx1];
if(!isReportValid(r)) continue;
addCard(card, r);
}
break;
default:
if(contact.type !== card.appliesToType) return;
addCard(card);
}
});
result.context = context;
// return the result for 2.13+ as per #2635
return result;
${contactSummaryLib}
`);
} else throw new Error(`Could not find contact-summary javascript at either of ${freeformPath} or ${structuredPath}. Please create one xor other of these files.`);

Expand Down

0 comments on commit 5b82f0d

Please sign in to comment.