-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
62 lines (54 loc) · 2.68 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { convertFromFhir } from './fhirConversionTools.js';
export { responser } from './convertResponses.js';
/**
* Pass in four SurveyJS objects and return a function that will convert FHIR
* Questionnaires to SurveyJS models.
* @param {*} FunctionFactory
* @param {*} Model
* @param {*} Serializer
* @param {*} StylesManager
*/
export default function(FunctionFactory, Model, Serializer, StylesManager, resolver=()=>{}) {
/**
* Generates a SurveyJS Model using a FHIR Questionnaire resource.
* @param {object} fhirJson - Object resulting from parsing the JSON of a FHIR Questionnaire resource.
* @param {function} evaluateExpression - A function for asynchronously evaluating CQL expressions.
* Takes as its input the name of a CQL expression and returns a dummy boolean value. SurveyJS assumes
* the result of the expression is returned via a call to this.returnResult(), which it attaches to
* this function when it is registered.
* @param {string} styleTheme - The name of the SurveyJS theme to use in the model.
* @returns {object} model - The SurveyJS model resulting from the conversion.
*/
return function (fhirJson, evaluateExpression = null, styleTheme = null) {
// Need to add ordinalValue as a custom value on each answer item
Serializer.addProperty('itemvalue', 'ordinalValue:number');
Serializer.addProperty('itemvalue', 'valueCodingCode:string');
Serializer.addProperty('itemvalue', 'valueCodingSystem:string');
Serializer.addProperty('itemvalue', 'valueCodingDisplay:string');
Serializer.addProperty('itemvalue', 'valueType:string');
Serializer.addProperty('survey', {
name: 'questionnaireUrl:string',
default: fhirJson?.url,
category: 'general'
});
// Add a CQL Processor for evaluating expressions
if (evaluateExpression) FunctionFactory.Instance.register('evaluateExpression', evaluateExpression, true);
// Convert the FHIR Questionnaire JSON to JSON formatted for SurveyJS
let surveyJson = convertFromFhir(fhirJson, resolver);
// Check to ensure that evaluateExpression() isn't null if it is being referenced by any calculatedValues
let expRegExp = /evaluateExpression\(/;
surveyJson.calculatedValues.forEach(calc => {
if (evaluateExpression == null && expRegExp.test(calc.expression)) {
throw new Error('Null-valued evaluateExpression() is referenced by at least one calculatedValue.');
}
});
// Styling
if (styleTheme) {
StylesManager.applyTheme(styleTheme);
}
// Create a new SurveyJS Model based upon the converted JSON
var model = new Model(surveyJson);
// Return the model so it can (presumably) be rendered
return model;
}
}