@oneblink/sdk-core 8.11.1 → 9.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/abnService.d.ts +5 -5
- package/dist/abnService.js +9 -17
- package/dist/abnService.js.map +1 -1
- package/dist/conditionalLogicService/conditionallyShowElement.d.ts +1 -1
- package/dist/conditionalLogicService/conditionallyShowElement.js +4 -11
- package/dist/conditionalLogicService/conditionallyShowElement.js.map +1 -1
- package/dist/conditionalLogicService/conditionallyShowOption.d.ts +1 -1
- package/dist/conditionalLogicService/conditionallyShowOption.js +10 -18
- package/dist/conditionalLogicService/conditionallyShowOption.js.map +1 -1
- package/dist/conditionalLogicService/evaluateConditionalOptionsPredicate.js +2 -6
- package/dist/conditionalLogicService/evaluateConditionalOptionsPredicate.js.map +1 -1
- package/dist/conditionalLogicService/evaluateConditionalPredicate.d.ts +1 -1
- package/dist/conditionalLogicService/evaluateConditionalPredicate.js +14 -22
- package/dist/conditionalLogicService/evaluateConditionalPredicate.js.map +1 -1
- package/dist/conditionalLogicService/generateFormElementsConditionallyShown.js +12 -19
- package/dist/conditionalLogicService/generateFormElementsConditionallyShown.js.map +1 -1
- package/dist/conditionalLogicService/types.js +1 -2
- package/dist/conditionalLogicService.d.ts +1 -1
- package/dist/conditionalLogicService.js +6 -26
- package/dist/conditionalLogicService.js.map +1 -1
- package/dist/fileUploadService.js +2 -6
- package/dist/fileUploadService.js.map +1 -1
- package/dist/form-elements-regex.js +10 -14
- package/dist/form-elements-regex.js.map +1 -1
- package/dist/formElementsService.d.ts +2 -3
- package/dist/formElementsService.js +4 -15
- package/dist/formElementsService.js.map +1 -1
- package/dist/freshdeskService.d.ts +1 -1
- package/dist/freshdeskService.js +23 -31
- package/dist/freshdeskService.js.map +1 -1
- package/dist/goodToGoService.d.ts +1 -1
- package/dist/goodToGoService.js +2 -6
- package/dist/goodToGoService.js.map +1 -1
- package/dist/index.d.ts +14 -14
- package/dist/index.js +14 -50
- package/dist/index.js.map +1 -1
- package/dist/paymentService.d.ts +1 -1
- package/dist/paymentService.js +11 -18
- package/dist/paymentService.js.map +1 -1
- package/dist/replaceCustomValues.js +42 -50
- package/dist/replaceCustomValues.js.map +1 -1
- package/dist/schedulingService.js +3 -6
- package/dist/schedulingService.js.map +1 -1
- package/dist/sharepointService.d.ts +1 -1
- package/dist/sharepointService.js +4 -8
- package/dist/sharepointService.js.map +1 -1
- package/dist/submissionService.d.ts +1 -1
- package/dist/submissionService.js +4 -21
- package/dist/submissionService.js.map +1 -1
- package/dist/typeCastService.js +1 -5
- package/dist/typeCastService.js.map +1 -1
- package/dist/typedoc.d.ts +1 -1
- package/dist/typedoc.js +2 -18
- package/dist/typedoc.js.map +1 -1
- package/dist/types/resource-definitions.js +1 -2
- package/dist/userService.js +8 -11
- package/dist/userService.js.map +1 -1
- package/dist/webhookSubscriptionService.js +1 -3
- package/dist/webhookSubscriptionService.js.map +1 -1
- package/package.json +23 -25
|
@@ -1,39 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.generateFormElementsConditionallyShown = generateFormElementsConditionallyShown;
|
|
7
|
-
const formElementsService_1 = require("../formElementsService");
|
|
8
|
-
const conditionallyShowElement_1 = __importDefault(require("./conditionallyShowElement"));
|
|
9
|
-
const conditionallyShowOption_1 = __importDefault(require("./conditionallyShowOption"));
|
|
10
|
-
const __1 = require("..");
|
|
1
|
+
import { flattenFormElements } from '../formElementsService.js';
|
|
2
|
+
import conditionallyShowElement from './conditionallyShowElement.js';
|
|
3
|
+
import conditionallyShowOption from './conditionallyShowOption.js';
|
|
4
|
+
import { typeCastService } from '../index.js';
|
|
11
5
|
const handleConditionallyShowElement = (formElementsCtrl, element, errorCallback) => {
|
|
12
6
|
try {
|
|
13
|
-
return (
|
|
7
|
+
return conditionallyShowElement(formElementsCtrl, element, []);
|
|
14
8
|
}
|
|
15
9
|
catch (error) {
|
|
16
|
-
errorCallback
|
|
10
|
+
errorCallback?.(error);
|
|
17
11
|
return false;
|
|
18
12
|
}
|
|
19
13
|
};
|
|
20
14
|
const handleConditionallyShowOption = (formElementsCtrl, element, option, errorCallback) => {
|
|
21
15
|
try {
|
|
22
|
-
return (
|
|
16
|
+
return conditionallyShowOption(formElementsCtrl, element, option, []);
|
|
23
17
|
}
|
|
24
18
|
catch (error) {
|
|
25
|
-
errorCallback
|
|
19
|
+
errorCallback?.(error);
|
|
26
20
|
return 'HIDE';
|
|
27
21
|
}
|
|
28
22
|
};
|
|
29
23
|
const generateFormElementsConditionallyShownWithParent = ({ formElements, submission, parentFormElementsCtrl, errorCallback, }) => {
|
|
30
24
|
const formElementsCtrl = {
|
|
31
|
-
flattenedElements:
|
|
25
|
+
flattenedElements: flattenFormElements(formElements),
|
|
32
26
|
model: submission,
|
|
33
27
|
parentFormElementsCtrl,
|
|
34
28
|
};
|
|
35
29
|
return formElementsCtrl.flattenedElements.reduce((formElementsConditionallyShown, element) => {
|
|
36
|
-
var _a;
|
|
37
30
|
switch (element.type) {
|
|
38
31
|
case 'section':
|
|
39
32
|
case 'page': {
|
|
@@ -89,7 +82,7 @@ const generateFormElementsConditionallyShownWithParent = ({ formElements, submis
|
|
|
89
82
|
if (formElementsConditionallyShown[element.name]) {
|
|
90
83
|
break;
|
|
91
84
|
}
|
|
92
|
-
const entries =
|
|
85
|
+
const entries = formElementsCtrl.model?.[element.name];
|
|
93
86
|
formElementsConditionallyShown[element.name] = {
|
|
94
87
|
type: 'repeatableSet',
|
|
95
88
|
isHidden: !handleConditionallyShowElement(formElementsCtrl, element, errorCallback),
|
|
@@ -114,7 +107,7 @@ const generateFormElementsConditionallyShownWithParent = ({ formElements, submis
|
|
|
114
107
|
isHidden: !handleConditionallyShowElement(formElementsCtrl, element, errorCallback),
|
|
115
108
|
};
|
|
116
109
|
if (!formElementConditionallyShown.isHidden) {
|
|
117
|
-
const optionsElement =
|
|
110
|
+
const optionsElement = typeCastService.formElements.toOptionsElement(element);
|
|
118
111
|
if (optionsElement &&
|
|
119
112
|
optionsElement.conditionallyShowOptions &&
|
|
120
113
|
Array.isArray(optionsElement.options)) {
|
|
@@ -234,7 +227,7 @@ const generateFormElementsConditionallyShownWithParent = ({ formElements, submis
|
|
|
234
227
|
* @param parameters
|
|
235
228
|
* @returns
|
|
236
229
|
*/
|
|
237
|
-
function generateFormElementsConditionallyShown(parameters) {
|
|
230
|
+
export function generateFormElementsConditionallyShown(parameters) {
|
|
238
231
|
return generateFormElementsConditionallyShownWithParent(parameters);
|
|
239
232
|
}
|
|
240
233
|
//# sourceMappingURL=generateFormElementsConditionallyShown.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateFormElementsConditionallyShown.js","sourceRoot":"","sources":["../../src/conditionalLogicService/generateFormElementsConditionallyShown.ts"],"names":[],"mappings":";;;;;AA+UA,wFAIC;AAjVD,gEAA4D;AAC5D,0FAAiE;AACjE,wFAEkC;AAClC,0BAAoC;AA8BpC,MAAM,8BAA8B,GAAG,CACrC,gBAAkC,EAClC,OAA8B,EAC9B,aAAwC,EACxC,EAAE;IACF,IAAI,CAAC;QACH,OAAO,IAAA,kCAAwB,EAAC,gBAAgB,EAAE,OAAO,EAAE,EAAE,CAAC,CAAA;IAChE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAG,KAAc,CAAC,CAAA;QAC/B,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC,CAAA;AAED,MAAM,6BAA6B,GAAG,CACpC,gBAAkC,EAClC,OAAyC,EACzC,MAAqC,EACrC,aAAwC,EACtB,EAAE;IACpB,IAAI,CAAC;QACH,OAAO,IAAA,iCAAuB,EAAC,gBAAgB,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,CAAA;IACvE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,aA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AAC,MAAM,CAAC,CAAA;oCACvB,6BAA6B,CAAC,mBAAmB,GAAG,IAAI,CAAA;oCACxD,MAAK;gCACP,CAAC;gCACD,KAAK,4BAA4B,CAAC,CAAC,CAAC;oCAClC,6BAA6B,CAAC,mBAAmB,GAAG,IAAI,CAAA;oCACxD,MAAK;gCACP,CAAC;4BACH,CAAC;wBACH,CAAC;wBACD,6BAA6B,CAAC,OAAO,GAAG,UAAU,CAAA;oBACpD,CAAC;gBACH,CAAC;gBAED,8BAA8B,CAAC,OAAO,CAAC,IAAI,CAAC;oBAC1C,6BAA6B,CAAA;YACjC,CAAC;QACH,CAAC;QAED,OAAO,8BAA8B,CAAA;IACvC,CAAC,EACD,EAAE,CACH,CAAA;AACH,CAAC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsFG;AACH,SAAgB,sCAAsC,CACpD,UAAoD;IAEpD,OAAO,gDAAgD,CAAC,UAAU,CAAC,CAAA;AACrE,CAAC","sourcesContent":["import { FormTypes, SubmissionTypes } from '@oneblink/types'\nimport { FormElementsCtrl } from './types'\nimport { flattenFormElements } from '../formElementsService'\nimport conditionallyShowElement from './conditionallyShowElement'\nimport conditionallyShowOption, {\n ShouldShowOption,\n} from './conditionallyShowOption'\nimport { typeCastService } from '..'\n\nexport type FormElementsConditionallyShown = Record<\n string,\n FormElementConditionallyShown\n>\n\nexport type FormElementConditionallyShownElement = {\n type: 'formElement'\n isHidden: boolean\n options?: FormTypes.ChoiceElementOption[]\n dependencyIsLoading?: boolean\n}\n\nexport type FormElementConditionallyShown =\n | undefined\n | FormElementConditionallyShownElement\n | {\n type: 'formElements'\n isHidden: boolean\n formElements: FormElementsConditionallyShown | undefined\n }\n | {\n type: 'repeatableSet'\n isHidden: boolean\n entries: Record<string, FormElementsConditionallyShown | undefined>\n }\n\nexport type ErrorCallback = (error: Error) => void\n\nconst handleConditionallyShowElement = (\n formElementsCtrl: FormElementsCtrl,\n element: FormTypes.FormElement,\n errorCallback: ErrorCallback | undefined,\n) => {\n try {\n return conditionallyShowElement(formElementsCtrl, element, [])\n } catch (error) {\n errorCallback?.(error as Error)\n return false\n }\n}\n\nconst handleConditionallyShowOption = (\n formElementsCtrl: FormElementsCtrl,\n element: FormTypes.FormElementWithOptions,\n option: FormTypes.ChoiceElementOption,\n errorCallback: ErrorCallback | undefined,\n): ShouldShowOption => {\n try {\n return conditionallyShowOption(formElementsCtrl, element, option, [])\n } catch (error) {\n errorCallback?.(error as Error)\n return 'HIDE'\n }\n}\n\nexport type FormElementsConditionallyShownParameters = {\n /** Form elements to evaluate */\n formElements: FormTypes.FormElement[]\n /** Current submission data */\n submission: SubmissionTypes.S3SubmissionData['submission']\n /** Optional callback for handling errors caught during the evaluation */\n errorCallback?: ErrorCallback\n}\n\nconst generateFormElementsConditionallyShownWithParent = ({\n formElements,\n submission,\n parentFormElementsCtrl,\n errorCallback,\n}: FormElementsConditionallyShownParameters & {\n parentFormElementsCtrl?: FormElementsCtrl['parentFormElementsCtrl']\n}): FormElementsConditionallyShown => {\n const formElementsCtrl = {\n flattenedElements: flattenFormElements(formElements),\n model: submission,\n parentFormElementsCtrl,\n }\n return formElementsCtrl.flattenedElements.reduce<FormElementsConditionallyShown>(\n (formElementsConditionallyShown, element) => {\n switch (element.type) {\n case 'section':\n case 'page': {\n const formElementConditionallyShown =\n formElementsConditionallyShown[element.id]\n const isHidden = formElementConditionallyShown\n ? formElementConditionallyShown.isHidden\n : !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n )\n\n formElementsConditionallyShown[element.id] = {\n type: 'formElement',\n isHidden,\n }\n\n // If the parent element is hidden, hide all the child elements\n if (isHidden) {\n element.elements.forEach((childElement) => {\n switch (childElement.type) {\n case 'section':\n case 'page': {\n formElementsConditionallyShown[childElement.id] = {\n type: 'formElement',\n isHidden: true,\n }\n break\n }\n default: {\n formElementsConditionallyShown[childElement.name] = {\n type: 'formElement',\n isHidden: true,\n }\n }\n }\n })\n }\n break\n }\n case 'infoPage':\n case 'form': {\n if (formElementsConditionallyShown[element.name]) {\n break\n }\n const nestedModel = submission[element.name] as\n | SubmissionTypes.S3SubmissionData['submission']\n | undefined\n formElementsConditionallyShown[element.name] = {\n type: 'formElements',\n isHidden: !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n ),\n formElements: generateFormElementsConditionallyShownWithParent({\n formElements: element.elements || [],\n submission: nestedModel || {},\n parentFormElementsCtrl: formElementsCtrl,\n }),\n }\n break\n }\n case 'repeatableSet': {\n if (formElementsConditionallyShown[element.name]) {\n break\n }\n const entries = formElementsCtrl.model?.[element.name] as\n | Array<SubmissionTypes.S3SubmissionData['submission']>\n | undefined\n formElementsConditionallyShown[element.name] = {\n type: 'repeatableSet',\n isHidden: !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n ),\n entries: (entries || []).reduce(\n (\n result: Record<\n string,\n FormElementsConditionallyShown | undefined\n >,\n entry,\n index,\n ) => {\n result[index.toString()] =\n generateFormElementsConditionallyShownWithParent({\n formElements: element.elements,\n submission: entry,\n parentFormElementsCtrl: formElementsCtrl,\n })\n return result\n },\n {},\n ),\n }\n break\n }\n default: {\n if (formElementsConditionallyShown[element.name]) {\n break\n }\n const formElementConditionallyShown: FormElementConditionallyShown = {\n type: 'formElement',\n isHidden: !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n ),\n }\n\n if (!formElementConditionallyShown.isHidden) {\n const optionsElement =\n typeCastService.formElements.toOptionsElement(element)\n if (\n optionsElement &&\n optionsElement.conditionallyShowOptions &&\n Array.isArray(optionsElement.options)\n ) {\n const newOptions = []\n for (const option of optionsElement.options) {\n const optionPredicatesResult = handleConditionallyShowOption(\n formElementsCtrl,\n optionsElement,\n option,\n errorCallback,\n )\n switch (optionPredicatesResult) {\n case 'SHOW': {\n newOptions.push(option)\n break\n }\n case 'LOADING_STATIC_DEPENDENCY': {\n newOptions.push(option)\n formElementConditionallyShown.dependencyIsLoading = true\n break\n }\n case 'LOADING_DYNAMIC_DEPENDENCY': {\n formElementConditionallyShown.dependencyIsLoading = true\n break\n }\n }\n }\n formElementConditionallyShown.options = newOptions\n }\n }\n\n formElementsConditionallyShown[element.name] =\n formElementConditionallyShown\n }\n }\n\n return formElementsConditionallyShown\n },\n {},\n )\n}\n\n/**\n * Given a form element and submission data, evaluate which elements are\n * currently shown. The function also takes an optional errorCallback for\n * handling errors caught during the evaluation.\n *\n * #### Example\n *\n * ```js\n * const formElementsConditionallyShown =\n * conditionalLogicService.generateFormElementsConditionallyShown({\n * submission: {\n * radio: ['hide'],\n * text: 'hidden text',\n * },\n * formElements: [\n * {\n * name: 'radio',\n * label: 'Radio',\n * type: 'radio',\n * required: false,\n * id: '5ef3beb8-8ac8-4c8d-9fd3-d8197113bf54',\n * conditionallyShow: false,\n * options: [\n * {\n * id: '9a44e15a-5929-419d-825e-b3dc0a29591f',\n * label: 'Show',\n * value: 'show',\n * },\n * {\n * id: 'd776ed42-072c-4bf8-9879-874b2bef85d3',\n * label: 'Hide',\n * value: 'Hide',\n * },\n * ],\n * readOnly: false,\n * isDataLookup: false,\n * isElementLookup: false,\n * buttons: false,\n * optionsType: 'CUSTOM',\n * conditionallyShowOptions: false,\n * },\n * {\n * name: 'text',\n * label: 'Text',\n * type: 'text',\n * required: false,\n * id: '8fbddb41-348d-494c-904b-56a2c4361f13',\n * requiresAllConditionallyShowPredicates: false,\n * conditionallyShow: true,\n * conditionallyShowPredicates: [\n * {\n * elementId: '5ef3beb8-8ac8-4c8d-9fd3-d8197113bf54',\n * optionIds: ['9a44e15a-5929-419d-825e-b3dc0a29591f'],\n * type: 'OPTIONS',\n * },\n * ],\n * readOnly: false,\n * isDataLookup: false,\n * isElementLookup: false,\n * },\n * ],\n * parentFormElementsCtrl: undefined,\n * errorCallback: (error) => {\n * //do something with the error here\n * console.error(error)\n * },\n * })\n * ```\n *\n * The above example returns an object like below\n *\n * ```json\n * {\n * \"radio\": {\n * \"isHidden\": false,\n * \"type\": \"formElement\"\n * },\n * \"text\": {\n * \"isHidden\": true,\n * \"type\": \"formElement\"\n * }\n * }\n * ```\n *\n * @param parameters\n * @returns\n */\nexport function generateFormElementsConditionallyShown(\n parameters: FormElementsConditionallyShownParameters,\n): FormElementsConditionallyShown {\n return generateFormElementsConditionallyShownWithParent(parameters)\n}\n"]}
|
|
1
|
+
{"version":3,"file":"generateFormElementsConditionallyShown.js","sourceRoot":"","sources":["../../src/conditionalLogicService/generateFormElementsConditionallyShown.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAA;AAC/D,OAAO,wBAAwB,MAAM,+BAA+B,CAAA;AACpE,OAAO,uBAEN,MAAM,8BAA8B,CAAA;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AA8B7C,MAAM,8BAA8B,GAAG,CACrC,gBAAkC,EAClC,OAA8B,EAC9B,aAAwC,EACxC,EAAE;IACF,IAAI,CAAC;QACH,OAAO,wBAAwB,CAAC,gBAAgB,EAAE,OAAO,EAAE,EAAE,CAAC,CAAA;IAChE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,aAAa,EAAE,CAAC,KAAc,CAAC,CAAA;QAC/B,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC,CAAA;AAED,MAAM,6BAA6B,GAAG,CACpC,gBAAkC,EAClC,OAAyC,EACzC,MAAqC,EACrC,aAAwC,EACtB,EAAE;IACpB,IAAI,CAAC;QACH,OAAO,uBAAuB,CAAC,gBAAgB,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,CAAA;IACvE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,aAAa,EAAE,CAAC,KAAc,CAAC,CAAA;QAC/B,OAAO,MAAM,CAAA;IACf,CAAC;AACH,CAAC,CAAA;AAWD,MAAM,gDAAgD,GAAG,CAAC,E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,CAAC,mBAAmB,GAAG,IAAI,CAAA;oCACxD,MAAK;gCACP,CAAC;4BACH,CAAC;wBACH,CAAC;wBACD,6BAA6B,CAAC,OAAO,GAAG,UAAU,CAAA;oBACpD,CAAC;gBACH,CAAC;gBAED,8BAA8B,CAAC,OAAO,CAAC,IAAI,CAAC;oBAC1C,6BAA6B,CAAA;YACjC,CAAC;QACH,CAAC;QAED,OAAO,8BAA8B,CAAA;IACvC,CAAC,EACD,EAAE,CACH,CAAA;AACH,CAAC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsFG;AACH,MAAM,UAAU,sCAAsC,CACpD,UAAoD;IAEpD,OAAO,gDAAgD,CAAC,UAAU,CAAC,CAAA;AACrE,CAAC","sourcesContent":["import { FormTypes, SubmissionTypes } from '@oneblink/types'\nimport { FormElementsCtrl } from './types.js'\nimport { flattenFormElements } from '../formElementsService.js'\nimport conditionallyShowElement from './conditionallyShowElement.js'\nimport conditionallyShowOption, {\n ShouldShowOption,\n} from './conditionallyShowOption.js'\nimport { typeCastService } from '../index.js'\n\nexport type FormElementsConditionallyShown = Record<\n string,\n FormElementConditionallyShown\n>\n\nexport type FormElementConditionallyShownElement = {\n type: 'formElement'\n isHidden: boolean\n options?: FormTypes.ChoiceElementOption[]\n dependencyIsLoading?: boolean\n}\n\nexport type FormElementConditionallyShown =\n | undefined\n | FormElementConditionallyShownElement\n | {\n type: 'formElements'\n isHidden: boolean\n formElements: FormElementsConditionallyShown | undefined\n }\n | {\n type: 'repeatableSet'\n isHidden: boolean\n entries: Record<string, FormElementsConditionallyShown | undefined>\n }\n\nexport type ErrorCallback = (error: Error) => void\n\nconst handleConditionallyShowElement = (\n formElementsCtrl: FormElementsCtrl,\n element: FormTypes.FormElement,\n errorCallback: ErrorCallback | undefined,\n) => {\n try {\n return conditionallyShowElement(formElementsCtrl, element, [])\n } catch (error) {\n errorCallback?.(error as Error)\n return false\n }\n}\n\nconst handleConditionallyShowOption = (\n formElementsCtrl: FormElementsCtrl,\n element: FormTypes.FormElementWithOptions,\n option: FormTypes.ChoiceElementOption,\n errorCallback: ErrorCallback | undefined,\n): ShouldShowOption => {\n try {\n return conditionallyShowOption(formElementsCtrl, element, option, [])\n } catch (error) {\n errorCallback?.(error as Error)\n return 'HIDE'\n }\n}\n\nexport type FormElementsConditionallyShownParameters = {\n /** Form elements to evaluate */\n formElements: FormTypes.FormElement[]\n /** Current submission data */\n submission: SubmissionTypes.S3SubmissionData['submission']\n /** Optional callback for handling errors caught during the evaluation */\n errorCallback?: ErrorCallback\n}\n\nconst generateFormElementsConditionallyShownWithParent = ({\n formElements,\n submission,\n parentFormElementsCtrl,\n errorCallback,\n}: FormElementsConditionallyShownParameters & {\n parentFormElementsCtrl?: FormElementsCtrl['parentFormElementsCtrl']\n}): FormElementsConditionallyShown => {\n const formElementsCtrl = {\n flattenedElements: flattenFormElements(formElements),\n model: submission,\n parentFormElementsCtrl,\n }\n return formElementsCtrl.flattenedElements.reduce<FormElementsConditionallyShown>(\n (formElementsConditionallyShown, element) => {\n switch (element.type) {\n case 'section':\n case 'page': {\n const formElementConditionallyShown =\n formElementsConditionallyShown[element.id]\n const isHidden = formElementConditionallyShown\n ? formElementConditionallyShown.isHidden\n : !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n )\n\n formElementsConditionallyShown[element.id] = {\n type: 'formElement',\n isHidden,\n }\n\n // If the parent element is hidden, hide all the child elements\n if (isHidden) {\n element.elements.forEach((childElement) => {\n switch (childElement.type) {\n case 'section':\n case 'page': {\n formElementsConditionallyShown[childElement.id] = {\n type: 'formElement',\n isHidden: true,\n }\n break\n }\n default: {\n formElementsConditionallyShown[childElement.name] = {\n type: 'formElement',\n isHidden: true,\n }\n }\n }\n })\n }\n break\n }\n case 'infoPage':\n case 'form': {\n if (formElementsConditionallyShown[element.name]) {\n break\n }\n const nestedModel = submission[element.name] as\n | SubmissionTypes.S3SubmissionData['submission']\n | undefined\n formElementsConditionallyShown[element.name] = {\n type: 'formElements',\n isHidden: !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n ),\n formElements: generateFormElementsConditionallyShownWithParent({\n formElements: element.elements || [],\n submission: nestedModel || {},\n parentFormElementsCtrl: formElementsCtrl,\n }),\n }\n break\n }\n case 'repeatableSet': {\n if (formElementsConditionallyShown[element.name]) {\n break\n }\n const entries = formElementsCtrl.model?.[element.name] as\n | Array<SubmissionTypes.S3SubmissionData['submission']>\n | undefined\n formElementsConditionallyShown[element.name] = {\n type: 'repeatableSet',\n isHidden: !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n ),\n entries: (entries || []).reduce(\n (\n result: Record<\n string,\n FormElementsConditionallyShown | undefined\n >,\n entry,\n index,\n ) => {\n result[index.toString()] =\n generateFormElementsConditionallyShownWithParent({\n formElements: element.elements,\n submission: entry,\n parentFormElementsCtrl: formElementsCtrl,\n })\n return result\n },\n {},\n ),\n }\n break\n }\n default: {\n if (formElementsConditionallyShown[element.name]) {\n break\n }\n const formElementConditionallyShown: FormElementConditionallyShown = {\n type: 'formElement',\n isHidden: !handleConditionallyShowElement(\n formElementsCtrl,\n element,\n errorCallback,\n ),\n }\n\n if (!formElementConditionallyShown.isHidden) {\n const optionsElement =\n typeCastService.formElements.toOptionsElement(element)\n if (\n optionsElement &&\n optionsElement.conditionallyShowOptions &&\n Array.isArray(optionsElement.options)\n ) {\n const newOptions = []\n for (const option of optionsElement.options) {\n const optionPredicatesResult = handleConditionallyShowOption(\n formElementsCtrl,\n optionsElement,\n option,\n errorCallback,\n )\n switch (optionPredicatesResult) {\n case 'SHOW': {\n newOptions.push(option)\n break\n }\n case 'LOADING_STATIC_DEPENDENCY': {\n newOptions.push(option)\n formElementConditionallyShown.dependencyIsLoading = true\n break\n }\n case 'LOADING_DYNAMIC_DEPENDENCY': {\n formElementConditionallyShown.dependencyIsLoading = true\n break\n }\n }\n }\n formElementConditionallyShown.options = newOptions\n }\n }\n\n formElementsConditionallyShown[element.name] =\n formElementConditionallyShown\n }\n }\n\n return formElementsConditionallyShown\n },\n {},\n )\n}\n\n/**\n * Given a form element and submission data, evaluate which elements are\n * currently shown. The function also takes an optional errorCallback for\n * handling errors caught during the evaluation.\n *\n * #### Example\n *\n * ```js\n * const formElementsConditionallyShown =\n * conditionalLogicService.generateFormElementsConditionallyShown({\n * submission: {\n * radio: ['hide'],\n * text: 'hidden text',\n * },\n * formElements: [\n * {\n * name: 'radio',\n * label: 'Radio',\n * type: 'radio',\n * required: false,\n * id: '5ef3beb8-8ac8-4c8d-9fd3-d8197113bf54',\n * conditionallyShow: false,\n * options: [\n * {\n * id: '9a44e15a-5929-419d-825e-b3dc0a29591f',\n * label: 'Show',\n * value: 'show',\n * },\n * {\n * id: 'd776ed42-072c-4bf8-9879-874b2bef85d3',\n * label: 'Hide',\n * value: 'Hide',\n * },\n * ],\n * readOnly: false,\n * isDataLookup: false,\n * isElementLookup: false,\n * buttons: false,\n * optionsType: 'CUSTOM',\n * conditionallyShowOptions: false,\n * },\n * {\n * name: 'text',\n * label: 'Text',\n * type: 'text',\n * required: false,\n * id: '8fbddb41-348d-494c-904b-56a2c4361f13',\n * requiresAllConditionallyShowPredicates: false,\n * conditionallyShow: true,\n * conditionallyShowPredicates: [\n * {\n * elementId: '5ef3beb8-8ac8-4c8d-9fd3-d8197113bf54',\n * optionIds: ['9a44e15a-5929-419d-825e-b3dc0a29591f'],\n * type: 'OPTIONS',\n * },\n * ],\n * readOnly: false,\n * isDataLookup: false,\n * isElementLookup: false,\n * },\n * ],\n * parentFormElementsCtrl: undefined,\n * errorCallback: (error) => {\n * //do something with the error here\n * console.error(error)\n * },\n * })\n * ```\n *\n * The above example returns an object like below\n *\n * ```json\n * {\n * \"radio\": {\n * \"isHidden\": false,\n * \"type\": \"formElement\"\n * },\n * \"text\": {\n * \"isHidden\": true,\n * \"type\": \"formElement\"\n * }\n * }\n * ```\n *\n * @param parameters\n * @returns\n */\nexport function generateFormElementsConditionallyShown(\n parameters: FormElementsConditionallyShownParameters,\n): FormElementsConditionallyShown {\n return generateFormElementsConditionallyShownWithParent(parameters)\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConditionTypes, FormTypes, SubmissionTypes } from '@oneblink/types';
|
|
2
|
-
export * from './conditionalLogicService/generateFormElementsConditionallyShown';
|
|
2
|
+
export * from './conditionalLogicService/generateFormElementsConditionallyShown.js';
|
|
3
3
|
/**
|
|
4
4
|
* Given a set of form elements and submission data, evaluate if predicates are
|
|
5
5
|
* met or not.
|
|
@@ -1,26 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.evaluateConditionalPredicates = evaluateConditionalPredicates;
|
|
21
|
-
const evaluateConditionalPredicate_1 = __importDefault(require("./conditionalLogicService/evaluateConditionalPredicate"));
|
|
22
|
-
const formElementsService_1 = require("./formElementsService");
|
|
23
|
-
__exportStar(require("./conditionalLogicService/generateFormElementsConditionallyShown"), exports);
|
|
1
|
+
import evaluateConditionalPredicate from './conditionalLogicService/evaluateConditionalPredicate.js';
|
|
2
|
+
import { flattenFormElements } from './formElementsService.js';
|
|
3
|
+
export * from './conditionalLogicService/generateFormElementsConditionallyShown.js';
|
|
24
4
|
/**
|
|
25
5
|
* Given a set of form elements and submission data, evaluate if predicates are
|
|
26
6
|
* met or not.
|
|
@@ -83,15 +63,15 @@ __exportStar(require("./conditionalLogicService/generateFormElementsConditionall
|
|
|
83
63
|
* @param options
|
|
84
64
|
* @returns
|
|
85
65
|
*/
|
|
86
|
-
function evaluateConditionalPredicates({ isConditional, requiresAllConditionalPredicates, conditionalPredicates, formElements, submission, }) {
|
|
66
|
+
export function evaluateConditionalPredicates({ isConditional, requiresAllConditionalPredicates, conditionalPredicates, formElements, submission, }) {
|
|
87
67
|
if (!isConditional || !conditionalPredicates.length) {
|
|
88
68
|
return true;
|
|
89
69
|
}
|
|
90
70
|
const formElementsCtrl = {
|
|
91
|
-
flattenedElements:
|
|
71
|
+
flattenedElements: flattenFormElements(formElements),
|
|
92
72
|
model: submission,
|
|
93
73
|
};
|
|
94
|
-
const predicateFn = (predicate) => (
|
|
74
|
+
const predicateFn = (predicate) => evaluateConditionalPredicate({
|
|
95
75
|
predicate,
|
|
96
76
|
formElementsCtrl,
|
|
97
77
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conditionalLogicService.js","sourceRoot":"","sources":["../src/conditionalLogicService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"conditionalLogicService.js","sourceRoot":"","sources":["../src/conditionalLogicService.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,2DAA2D,CAAA;AACpG,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAG9D,cAAc,qEAAqE,CAAA;AAEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AACH,MAAM,UAAU,6BAA6B,CAAC,EAC5C,aAAa,EACb,gCAAgC,EAChC,qBAAqB,EACrB,YAAY,EACZ,UAAU,GAOX;IACC,IAAI,CAAC,aAAa,IAAI,CAAC,qBAAqB,CAAC,MAAM,EAAE,CAAC;QACpD,OAAO,IAAI,CAAA;IACb,CAAC;IACD,MAAM,gBAAgB,GAAG;QACvB,iBAAiB,EAAE,mBAAmB,CAAC,YAAY,CAAC;QACpD,KAAK,EAAE,UAAU;KAClB,CAAA;IAED,MAAM,WAAW,GAAG,CAAC,SAA8C,EAAE,EAAE,CACrE,4BAA4B,CAAC;QAC3B,SAAS;QACT,gBAAgB;KACjB,CAAC,CAAA;IACJ,IAAI,gCAAgC,EAAE,CAAC;QACrC,OAAO,qBAAqB,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;IACjD,CAAC;SAAM,CAAC;QACN,OAAO,qBAAqB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;IAChD,CAAC;AACH,CAAC","sourcesContent":["import evaluateConditionalPredicate from './conditionalLogicService/evaluateConditionalPredicate.js'\nimport { flattenFormElements } from './formElementsService.js'\nimport { ConditionTypes, FormTypes, SubmissionTypes } from '@oneblink/types'\n\nexport * from './conditionalLogicService/generateFormElementsConditionallyShown.js'\n\n/**\n * Given a set of form elements and submission data, evaluate if predicates are\n * met or not.\n *\n * #### Example\n *\n * ```js\n * const evaluation = conditionalLogicService.evaluateConditionalPredicates(\n * {\n * isConditional: true,\n * requiresAllConditionalPredicates: false,\n * conditionalPredicates: [\n * {\n * type: 'OPTIONS',\n * elementId: '3534abe4-b0b5-4ffa-a216-49c223ab6f95',\n * optionIds: ['9ce633dd-22d6-4e0e-a9e0-1aa62d435e72'],\n * },\n * ],\n * submission: {\n * checkboxes: ['a'],\n * },\n * formElements: [\n * {\n * name: 'checkboxes',\n * label: 'Checkboxes',\n * type: 'checkboxes',\n * required: false,\n * id: '3534abe4-b0b5-4ffa-a216-49c223ab6f95',\n * options: [\n * {\n * id: '9ce633dd-22d6-4e0e-a9e0-1aa62d435e72',\n * label: 'First',\n * value: 'a',\n * },\n * {\n * id: '5850b32c-3833-4498-a072-47fcc8122242',\n * label: 'Second',\n * value: 'b',\n * },\n * {\n * id: 'ab363e9f-a63f-4923-ba0d-47892fc26a93',\n * label: 'Third',\n * value: 'c',\n * },\n * ],\n * readOnly: false,\n * buttons: false,\n * optionsType: 'CUSTOM',\n * conditionallyShowOptions: false,\n * isDataLookup: false,\n * isElementLookup: false,\n * conditionallyShow: false,\n * requiresAllConditionallyShowPredicates: false,\n * },\n * ],\n * },\n * )\n * ```\n *\n * @param options\n * @returns\n */\nexport function evaluateConditionalPredicates({\n isConditional,\n requiresAllConditionalPredicates,\n conditionalPredicates,\n formElements,\n submission,\n}: {\n isConditional: boolean\n requiresAllConditionalPredicates: boolean\n conditionalPredicates: ConditionTypes.ConditionalPredicate[]\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n}): boolean {\n if (!isConditional || !conditionalPredicates.length) {\n return true\n }\n const formElementsCtrl = {\n flattenedElements: flattenFormElements(formElements),\n model: submission,\n }\n\n const predicateFn = (predicate: ConditionTypes.ConditionalPredicate) =>\n evaluateConditionalPredicate({\n predicate,\n formElementsCtrl,\n })\n if (requiresAllConditionalPredicates) {\n return conditionalPredicates.every(predicateFn)\n } else {\n return conditionalPredicates.some(predicateFn)\n }\n}\n"]}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.restrictedFileTypes = void 0;
|
|
4
|
-
exports.getContentDisposition = getContentDisposition;
|
|
5
1
|
/**
|
|
6
2
|
* Attempts to get the content disposition for the header by compiling a string
|
|
7
3
|
* of UTF-8 formatting based on the file name.
|
|
@@ -16,13 +12,13 @@ exports.getContentDisposition = getContentDisposition;
|
|
|
16
12
|
* @param filename The name of the file being uploaded
|
|
17
13
|
* @returns
|
|
18
14
|
*/
|
|
19
|
-
function getContentDisposition(filename) {
|
|
15
|
+
export function getContentDisposition(filename) {
|
|
20
16
|
return `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`;
|
|
21
17
|
}
|
|
22
18
|
/**
|
|
23
19
|
* A default list of files types (extensions) that is allowed.
|
|
24
20
|
*/
|
|
25
|
-
|
|
21
|
+
export const restrictedFileTypes = [
|
|
26
22
|
'avif',
|
|
27
23
|
'apng',
|
|
28
24
|
'png',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileUploadService.js","sourceRoot":"","sources":["../src/fileUploadService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fileUploadService.js","sourceRoot":"","sources":["../src/fileUploadService.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAgB;IACpD,OAAO,gCAAgC,kBAAkB,CAAC,QAAQ,CAAC,EAAE,CAAA;AACvE,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,MAAM;IACN,MAAM;IACN,KAAK;IACL,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,KAAK;IACL,KAAK;IACL,KAAK;IACL,MAAM;IACN,MAAM;IACN,KAAK;IACL,MAAM;IACN,KAAK;IACL,MAAM;IACN,KAAK;CACN,CAAA","sourcesContent":["/**\n * Attempts to get the content disposition for the header by compiling a string\n * of UTF-8 formatting based on the file name.\n *\n * #### Example\n *\n * ```js\n * const contentDisposition =\n * fileUploadService.getContentDisposition(fileName)\n * ```\n *\n * @param filename The name of the file being uploaded\n * @returns\n */\nexport function getContentDisposition(filename: string): string {\n return `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`\n}\n\n/**\n * A default list of files types (extensions) that is allowed.\n */\nexport const restrictedFileTypes = [\n 'avif',\n 'apng',\n 'png',\n 'jpg',\n 'jpeg',\n 'jfif',\n 'pjpeg',\n 'pjp',\n 'gif',\n 'pdf',\n 'heic',\n 'heif',\n 'doc',\n 'docx',\n 'xls',\n 'xlsx',\n 'csv',\n]\n"]}
|
|
@@ -1,33 +1,29 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NestedSubmissionValueElementRegex = exports.RootSubmissionValueElementRegex = exports.NestedElementRegex = exports.RootElementRegex = void 0;
|
|
4
|
-
exports.matchElementsTagRegex = matchElementsTagRegex;
|
|
5
1
|
/**
|
|
6
2
|
* The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink
|
|
7
3
|
* platform. Ignores nested elements based on the '|' character. Will match
|
|
8
4
|
* `"{ELEMENT:Parent_Name}"` but will NOT match `"{ELEMENT:Children|Name}"`.
|
|
9
5
|
*/
|
|
10
|
-
|
|
6
|
+
export const RootElementRegex = /({ELEMENT:)([^|}]+)(})/g;
|
|
11
7
|
/**
|
|
12
8
|
* The regex used for matching `{ELEMENT:<elementName>|<nestedElementName>}`
|
|
13
9
|
* tags in the OneBlink platform. Includes nested elements based on the '|'
|
|
14
10
|
* character. {ELEMENT:Children|Name} would be matched. Will match
|
|
15
11
|
* `"{ELEMENT:Parent_Name}"` and `"{ELEMENT:Children|Name}"`.
|
|
16
12
|
*/
|
|
17
|
-
|
|
13
|
+
export const NestedElementRegex = /({ELEMENT:)([^}]+)(})/g;
|
|
18
14
|
/**
|
|
19
15
|
* The regex used for matching `{ELEMENT_VALUE:<elementName>}` tags in the OneBlink
|
|
20
16
|
* platform. Ignores nested elements based on the '|' character. Will match
|
|
21
17
|
* `"{ELEMENT_VALUE:Parent_Name}"` but will NOT match `"{ELEMENT_VALUE:Children|Name}"`.
|
|
22
18
|
*/
|
|
23
|
-
|
|
19
|
+
export const RootSubmissionValueElementRegex = /({ELEMENT_VALUE:)([^|}]+)(})/g;
|
|
24
20
|
/**
|
|
25
21
|
* The regex used for matching `{ELEMENT_VALUE:<elementName>|<nestedElementName>}`
|
|
26
22
|
* tags in the OneBlink platform. Includes nested elements based on the '|'
|
|
27
23
|
* character. {ELEMENT_VALUE:Children|Name} would be matched. Will match
|
|
28
24
|
* `"{ELEMENT_VALUE:Parent_Name}"` and `"{ELEMENT_VALUE:Children|Name}"`.
|
|
29
25
|
*/
|
|
30
|
-
|
|
26
|
+
export const NestedSubmissionValueElementRegex = /({ELEMENT_VALUE:)([^}]+)(})/g;
|
|
31
27
|
/**
|
|
32
28
|
* Takes a string and calls a provided handler function for each found instance
|
|
33
29
|
* of `{ELEMENT:<elementName>}` in the string. Used to replace values in
|
|
@@ -64,20 +60,20 @@ exports.NestedSubmissionValueElementRegex = /({ELEMENT_VALUE:)([^}]+)(})/g;
|
|
|
64
60
|
* @param handler
|
|
65
61
|
* @returns
|
|
66
62
|
*/
|
|
67
|
-
function matchElementsTagRegex(options, matchHandler) {
|
|
63
|
+
export function matchElementsTagRegex(options, matchHandler) {
|
|
68
64
|
const text = typeof options === 'string' ? options : options.text;
|
|
69
65
|
const excludeNestedElements = typeof options !== 'string' && !!options.excludeNestedElements;
|
|
70
66
|
const useSubmissionValue = typeof options !== 'string' && !!options.useSubmissionValue;
|
|
71
67
|
let matches;
|
|
72
68
|
const rootRegex = useSubmissionValue
|
|
73
|
-
?
|
|
74
|
-
:
|
|
69
|
+
? RootSubmissionValueElementRegex
|
|
70
|
+
: RootElementRegex;
|
|
75
71
|
const nestedRegex = useSubmissionValue
|
|
76
|
-
?
|
|
77
|
-
:
|
|
72
|
+
? NestedSubmissionValueElementRegex
|
|
73
|
+
: NestedElementRegex;
|
|
78
74
|
while ((matches = (excludeNestedElements ? rootRegex : nestedRegex).exec(text)) !==
|
|
79
75
|
null) {
|
|
80
|
-
if (
|
|
76
|
+
if (matches?.length < 3)
|
|
81
77
|
continue;
|
|
82
78
|
const elementName = matches[2];
|
|
83
79
|
matchHandler({ elementName, elementMatch: matches[0] });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form-elements-regex.js","sourceRoot":"","sources":["../src/form-elements-regex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"form-elements-regex.js","sourceRoot":"","sources":["../src/form-elements-regex.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,yBAAyB,CAAA;AACzD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,wBAAwB,CAAA;AAE1D;;;;GAIG;AACH,MAAM,CAAC,MAAM,+BAA+B,GAAG,+BAA+B,CAAA;AAC9E;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,8BAA8B,CAAA;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,qBAAqB,CACnC,OAgBK,EACL,YAGU;IAEV,MAAM,IAAI,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAA;IACjE,MAAM,qBAAqB,GACzB,OAAO,OAAO,KAAK,QAAQ,IAAI,CAAC,CAAC,OAAO,CAAC,qBAAqB,CAAA;IAChE,MAAM,kBAAkB,GACtB,OAAO,OAAO,KAAK,QAAQ,IAAI,CAAC,CAAC,OAAO,CAAC,kBAAkB,CAAA;IAC7D,IAAI,OAAO,CAAA;IACX,MAAM,SAAS,GAAG,kBAAkB;QAClC,CAAC,CAAC,+BAA+B;QACjC,CAAC,CAAC,gBAAgB,CAAA;IACpB,MAAM,WAAW,GAAG,kBAAkB;QACpC,CAAC,CAAC,iCAAiC;QACnC,CAAC,CAAC,kBAAkB,CAAA;IACtB,OACE,CAAC,OAAO,GAAG,CAAC,qBAAqB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACxE,IAAI,EACJ,CAAC;QACD,IAAI,OAAO,EAAE,MAAM,GAAG,CAAC;YAAE,SAAQ;QAEjC,MAAM,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;QAC9B,YAAY,CAAC,EAAE,WAAW,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IACzD,CAAC;AACH,CAAC","sourcesContent":["/**\n * The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink\n * platform. Ignores nested elements based on the '|' character. Will match\n * `\"{ELEMENT:Parent_Name}\"` but will NOT match `\"{ELEMENT:Children|Name}\"`.\n */\nexport const RootElementRegex = /({ELEMENT:)([^|}]+)(})/g\n/**\n * The regex used for matching `{ELEMENT:<elementName>|<nestedElementName>}`\n * tags in the OneBlink platform. Includes nested elements based on the '|'\n * character. {ELEMENT:Children|Name} would be matched. Will match\n * `\"{ELEMENT:Parent_Name}\"` and `\"{ELEMENT:Children|Name}\"`.\n */\nexport const NestedElementRegex = /({ELEMENT:)([^}]+)(})/g\n\n/**\n * The regex used for matching `{ELEMENT_VALUE:<elementName>}` tags in the OneBlink\n * platform. Ignores nested elements based on the '|' character. Will match\n * `\"{ELEMENT_VALUE:Parent_Name}\"` but will NOT match `\"{ELEMENT_VALUE:Children|Name}\"`.\n */\nexport const RootSubmissionValueElementRegex = /({ELEMENT_VALUE:)([^|}]+)(})/g\n/**\n * The regex used for matching `{ELEMENT_VALUE:<elementName>|<nestedElementName>}`\n * tags in the OneBlink platform. Includes nested elements based on the '|'\n * character. {ELEMENT_VALUE:Children|Name} would be matched. Will match\n * `\"{ELEMENT_VALUE:Parent_Name}\"` and `\"{ELEMENT_VALUE:Children|Name}\"`.\n */\nexport const NestedSubmissionValueElementRegex = /({ELEMENT_VALUE:)([^}]+)(})/g\n\n/**\n * Takes a string and calls a provided handler function for each found instance\n * of `{ELEMENT:<elementName>}` in the string. Used to replace values in\n * OneBlink calculation and info (HTML) elements.\n *\n * #### Example\n *\n * ```js\n * formElementsService.matchElementsTagRegex(\n * myString,\n * ({ elementName, elementMatch }) => {\n * const v = submission[elementName]\n * myString = myString.replace(elementMatch, v)\n * },\n * )\n * ```\n *\n * Or\n *\n * ```js\n * formElementsService.matchElementsTagRegex(\n * {\n * text: myString,\n * excludeNestedElements: true,\n * },\n * ({ elementName, elementMatch }) => {\n * const v = submission[elementName]\n * myString = myString.replace(elementMatch, v)\n * },\n * )\n * ```\n *\n * @param options\n * @param handler\n * @returns\n */\nexport function matchElementsTagRegex(\n options:\n | string\n | {\n text: string\n /**\n * Determine if only root level elements should be matched.\n *\n * `false` will match `\"{ELEMENT:Parent_Name}\"` and\n * `\"{ELEMENT:Children|Name}\"`.\n *\n * `true` will match `\"{ELEMENT:Parent_Name}\"` but will NOT replace\n * `{ELEMENT:Children|Name}`.\n */\n excludeNestedElements: boolean\n /** Use the submission value instead of labels for elements with optionsets */\n useSubmissionValue?: boolean\n },\n matchHandler: (options: {\n elementName: string\n elementMatch: string\n }) => void,\n) {\n const text = typeof options === 'string' ? options : options.text\n const excludeNestedElements =\n typeof options !== 'string' && !!options.excludeNestedElements\n const useSubmissionValue =\n typeof options !== 'string' && !!options.useSubmissionValue\n let matches\n const rootRegex = useSubmissionValue\n ? RootSubmissionValueElementRegex\n : RootElementRegex\n const nestedRegex = useSubmissionValue\n ? NestedSubmissionValueElementRegex\n : NestedElementRegex\n while (\n (matches = (excludeNestedElements ? rootRegex : nestedRegex).exec(text)) !==\n null\n ) {\n if (matches?.length < 3) continue\n\n const elementName = matches[2]\n matchHandler({ elementName, elementMatch: matches[0] })\n }\n}\n"]}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { FormTypes } from '@oneblink/types';
|
|
2
|
-
|
|
3
|
-
export { matchElementsTagRegex } from './form-elements-regex';
|
|
2
|
+
export { matchElementsTagRegex } from './form-elements-regex.js';
|
|
4
3
|
/**
|
|
5
4
|
* Iterate over all form elements, also iterating over nested form element (e.g.
|
|
6
5
|
* page elements).
|
|
@@ -115,5 +114,5 @@ declare const fixElementName: (elementName: string) => string;
|
|
|
115
114
|
* authentication should be injected, defaults to true
|
|
116
115
|
* @returns
|
|
117
116
|
*/
|
|
118
|
-
declare function injectFormElementsIntoForm(form: Form, forms: Form[], injectAuthenticatedForms?: boolean): FormElement[];
|
|
117
|
+
declare function injectFormElementsIntoForm(form: FormTypes.Form, forms: FormTypes.Form[], injectAuthenticatedForms?: boolean): FormTypes.FormElement[];
|
|
119
118
|
export { forEachFormElement, forEachFormElementWithOptions, findFormElement, parseDynamicFormElementOptions, flattenFormElements, determineIsInfoPage, fixElementName, injectFormElementsIntoForm, };
|
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.fixElementName = exports.matchElementsTagRegex = void 0;
|
|
4
|
-
exports.forEachFormElement = forEachFormElement;
|
|
5
|
-
exports.forEachFormElementWithOptions = forEachFormElementWithOptions;
|
|
6
|
-
exports.findFormElement = findFormElement;
|
|
7
|
-
exports.parseDynamicFormElementOptions = parseDynamicFormElementOptions;
|
|
8
|
-
exports.flattenFormElements = flattenFormElements;
|
|
9
|
-
exports.determineIsInfoPage = determineIsInfoPage;
|
|
10
|
-
exports.injectFormElementsIntoForm = injectFormElementsIntoForm;
|
|
11
|
-
const _1 = require(".");
|
|
12
|
-
var form_elements_regex_1 = require("./form-elements-regex");
|
|
13
|
-
Object.defineProperty(exports, "matchElementsTagRegex", { enumerable: true, get: function () { return form_elements_regex_1.matchElementsTagRegex; } });
|
|
1
|
+
import { typeCastService } from './index.js';
|
|
2
|
+
export { matchElementsTagRegex } from './form-elements-regex.js';
|
|
14
3
|
/**
|
|
15
4
|
* Iterate over all form elements, also iterating over nested form element (e.g.
|
|
16
5
|
* page elements).
|
|
@@ -52,7 +41,7 @@ function forEachFormElement(elements, forEach) {
|
|
|
52
41
|
*/
|
|
53
42
|
function forEachFormElementWithOptions(elements, forEach) {
|
|
54
43
|
findFormElement(elements, (formElement, parentElements) => {
|
|
55
|
-
const optionsFormElement =
|
|
44
|
+
const optionsFormElement = typeCastService.formElements.toOptionsElement(formElement);
|
|
56
45
|
if (optionsFormElement) {
|
|
57
46
|
forEach(optionsFormElement, parentElements);
|
|
58
47
|
}
|
|
@@ -237,7 +226,6 @@ const fixElementName = (elementName) => {
|
|
|
237
226
|
.replace(/\s/g, '_')
|
|
238
227
|
.trim();
|
|
239
228
|
};
|
|
240
|
-
exports.fixElementName = fixElementName;
|
|
241
229
|
/**
|
|
242
230
|
* Injects the elements of any elements with type `FORM` or `INFOPAGE` into the
|
|
243
231
|
* form
|
|
@@ -302,4 +290,5 @@ function injectFormElements(elements, forms, parentIds, injectAuthenticatedForms
|
|
|
302
290
|
return newElements;
|
|
303
291
|
}, []);
|
|
304
292
|
}
|
|
293
|
+
export { forEachFormElement, forEachFormElementWithOptions, findFormElement, parseDynamicFormElementOptions, flattenFormElements, determineIsInfoPage, fixElementName, injectFormElementsIntoForm, };
|
|
305
294
|
//# sourceMappingURL=formElementsService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formElementsService.js","sourceRoot":"","sources":["../src/formElementsService.ts"],"names":[],"mappings":";;;AAuYE,gDAAkB;AAClB,sEAA6B;AAC7B,0CAAe;AACf,wEAA8B;AAC9B,kDAAmB;AACnB,kDAAmB;AAEnB,gEAA0B;AA7Y5B,wBAAmC;AAMnC,6DAA6D;AAApD,4HAAA,qBAAqB,OAAA;AAE9B;;;;;;;;;;;;;;GAcG;AACH,SAAS,kBAAkB,CACzB,QAAiC,EACjC,OAGS;IAET,eAAe,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,cAAc,EAAE,EAAE;QACxD,OAAO,CAAC,WAAW,EAAE,cAAc,CAAC,CAAA;QACpC,OAAO,KAAK,CAAA;IACd,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAS,6BAA6B,CACpC,QAAiC,EACjC,OAGS;IAET,eAAe,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,cAAc,EAAE,EAAE;QACxD,MAAM,kBAAkB,GACtB,kBAAe,CAAC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AAC;gBACxB,OAAO,CAAC,GAAG,CACT,kEAAkE,mBAAmB,wBAAwB,CAC9G,CAAA;gBACD,OAAO,WAAW,CAAA;YACpB,CAAC;YAED,OAAO,CAAC,QAAQ,GAAG,kBAAkB,CACnC,YAAY,CAAC,QAAQ,EACrB,KAAK,EACL,CAAC,GAAG,SAAS,EAAE,YAAY,CAAC,EAAE,CAAC,EAC/B,wBAAwB,CACzB,CAAA;YACD,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC3B,CAAC;aAAM,CAAC;YACN,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC3B,CAAC;QAED,OAAO,WAAW,CAAA;IACpB,CAAC,EAAE,EAAE,CAAC,CAAA;AACR,CAAC","sourcesContent":["import { FormTypes } from '@oneblink/types'\nimport { typeCastService } from '.'\nimport {\n Form,\n FormElement,\n HtmlElement,\n} from '@oneblink/types/typescript/forms'\nexport { matchElementsTagRegex } from './form-elements-regex'\n\n/**\n * Iterate over all form elements, also iterating over nested form element (e.g.\n * page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElement(form.elements, (formElement) => {\n * // do something with formElement\n * })\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element\n */\nfunction forEachFormElement(\n elements: FormTypes.FormElement[],\n forEach: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n forEach(formElement, parentElements)\n return false\n })\n}\n\n/**\n * Iterate over all form elements that have options (e.g. `'select'` type\n * elements), also iterating over nested form element (e.g. page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElementWithOptions(\n * form.elements,\n * (formElementWithOptions) => {\n * // do something with formElementWithOptions\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element with options\n */\nfunction forEachFormElementWithOptions(\n elements: FormTypes.FormElement[],\n forEach: (\n elementWithOptions: FormTypes.FormElementWithOptions,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n const optionsFormElement =\n typeCastService.formElements.toOptionsElement(formElement)\n if (optionsFormElement) {\n forEach(optionsFormElement, parentElements)\n }\n return false\n })\n}\n\n/**\n * Iterate over all form elements and return an element that matches a\n * predicate, also iterating over nested form element (e.g. page elements). Will\n * return `undefined` if no matching element is found.\n *\n * #### Example\n *\n * ```js\n * const formElement = formElementsService.findFormElement(\n * form.elements,\n * (formElement) => {\n * return formElement.id === '123-abc'\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param predicate Predicate function to execute on each form element\n * @param parentElements\n * @returns\n */\nfunction findFormElement(\n elements: FormTypes.FormElement[],\n predicate: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => boolean,\n parentElements: FormTypes.FormElement[] = [],\n): FormTypes.FormElement | undefined {\n for (const element of elements) {\n if (predicate(element, parentElements)) {\n return element\n }\n\n if (\n (element.type === 'repeatableSet' ||\n element.type === 'page' ||\n element.type === 'form' ||\n element.type === 'infoPage' ||\n element.type === 'section') &&\n Array.isArray(element.elements)\n ) {\n const nestedElement = findFormElement(element.elements, predicate, [\n ...parentElements,\n element,\n ])\n\n if (nestedElement) {\n return nestedElement\n }\n }\n }\n}\n\n/**\n * Parse unknown data as valid dynamic options for a forms element. This will\n * always return an Array of valid dynamic options.\n *\n * #### Example\n *\n * ```js\n * const options = formElementsService.parseDynamicFormElementOptions(data)\n * // \"options\" are valid for a form element\n * ```\n *\n * @param data\n * @returns\n */\nfunction parseDynamicFormElementOptions(\n data: unknown,\n): FormTypes.DynamicChoiceElementOption[] {\n if (!Array.isArray(data)) {\n return []\n }\n return data.reduce<FormTypes.DynamicChoiceElementOption[]>(\n (options, record, index) => {\n if (typeof record === 'string') {\n options.push({\n value: record,\n label: record,\n })\n } else if (typeof record === 'object') {\n const option = record as Record<string, unknown>\n const value =\n typeof option.value === 'string' && option.value\n ? option.value\n : index.toString()\n const label =\n typeof option.label === 'string' && option.label\n ? option.label\n : value\n const colour =\n typeof option.colour === 'string' && option.colour\n ? option.colour\n : undefined\n const displayAlways =\n typeof option.displayAlways === 'boolean'\n ? option.displayAlways\n : undefined\n const imageUrl =\n typeof option.imageUrl === 'string' && option.imageUrl\n ? option.imageUrl\n : undefined\n const attributes = Array.isArray(option.attributes)\n ? option.attributes.reduce<\n FormTypes.DynamicChoiceElementOptionAttribute[]\n >((memo, attribute: unknown) => {\n if (\n typeof attribute === 'object' &&\n attribute &&\n 'value' in attribute &&\n typeof attribute.value === 'string' &&\n 'label' in attribute &&\n typeof attribute.label === 'string'\n ) {\n memo.push({\n value: attribute.value,\n label: attribute.label,\n })\n }\n return memo\n }, [])\n : undefined\n options.push({\n value,\n label,\n colour,\n attributes,\n displayAlways,\n imageUrl,\n options: Array.isArray(option.options)\n ? parseDynamicFormElementOptions(option.options)\n : undefined,\n })\n }\n return options\n },\n [],\n )\n}\n\n/**\n * Takes the nested definition structure and returns all form elements as 1d\n * array.\n *\n * #### Example\n *\n * ```js\n * const flattenedElements = formElementsService.flattenFormElements(\n * form.elements,\n * )\n * ```\n *\n * @param elements\n * @returns\n */\nfunction flattenFormElements(\n elements: FormTypes.FormElement[],\n): FormTypes.FormElement[] {\n return elements.reduce<FormTypes.FormElement[]>(\n (flattenedElements, element) => {\n flattenedElements.push(element)\n switch (element.type) {\n case 'section':\n case 'page': {\n flattenedElements.push(...flattenFormElements(element.elements))\n }\n }\n return flattenedElements\n },\n [],\n )\n}\n\nconst infoPageElements: FormTypes.FormElementType[] = [\n 'heading',\n 'html',\n 'image',\n 'section',\n 'page',\n 'infoPage',\n 'form',\n]\n/**\n * Determine a form is considered an info page. This means the form does not\n * allow any user input.\n *\n * @param form\n * @returns\n */\nfunction determineIsInfoPage(form: FormTypes.Form): boolean {\n const foundInputElement = findFormElement(form.elements, (e) => {\n return !infoPageElements.includes(e.type)\n })\n return !foundInputElement\n}\n\n/**\n * Remove invalid characters from a form element name.\n *\n * @param elementName\n * @returns\n */\nconst fixElementName = (elementName: string) => {\n // removes characters that aren't letters, numbers, underscores or dashes\n // replaces empty spaces with _\n return elementName\n .replace(/[^-\\w\\s]/g, '')\n .replace(/\\s/g, '_')\n .trim()\n}\n\n/**\n * Injects the elements of any elements with type `FORM` or `INFOPAGE` into the\n * form\n *\n * @param form The form to inject elements into\n * @param forms The forms that will be used to inject elements from if\n * referenced\n * @param injectAuthenticatedForms Indicates whether forms requiring\n * authentication should be injected, defaults to true\n * @returns\n */\nfunction injectFormElementsIntoForm(\n form: Form,\n forms: Form[],\n injectAuthenticatedForms = true,\n): FormElement[] {\n const elementsWithInjectedForms = injectFormElements(\n form.elements,\n forms,\n [form.id],\n injectAuthenticatedForms,\n )\n form.elements = elementsWithInjectedForms\n return form.elements\n}\n\nfunction injectFormElements(\n elements: FormElement[],\n forms: Form[],\n parentIds: number[],\n injectAuthenticatedForms: boolean,\n): FormElement[] {\n return elements.reduce<FormElement[]>((newElements, element) => {\n if (\n (element.type === 'page' ||\n element.type === 'repeatableSet' ||\n element.type === 'section') &&\n Array.isArray(element.elements)\n ) {\n const childElements = injectFormElements(\n element.elements,\n forms,\n parentIds,\n injectAuthenticatedForms,\n )\n element.elements = childElements\n }\n\n if (element.type === 'form' || element.type === 'infoPage') {\n const formToInject = forms.find((form) => element.formId === form.id)\n\n if (!formToInject) {\n const newElement: HtmlElement = {\n ...element,\n type: 'html',\n name: 'Form_not_found',\n label: 'Form not found.',\n defaultValue:\n 'Unable to display the embedded form for this element, as the form was not found. Please contact your Administrator.',\n }\n newElements.push(newElement)\n return newElements\n }\n\n if (formToInject.isAuthenticated && !injectAuthenticatedForms) {\n console.log(\n `No form elements injected for element id: ${element.id}, as request was unauthenticated and target form (form id: ${formToInject.id}) requires authentication.`,\n )\n\n const newElement: HtmlElement = {\n ...element,\n type: 'html',\n name: 'Form_requires_authenticated',\n label: 'Form Requires Authentication.',\n defaultValue:\n 'Unable to display the embedded form for this element, as the form requires authentication. Please login and refresh to view this embedded form.',\n }\n newElements.push(newElement)\n return newElements\n }\n\n const injectingParentForm =\n parentIds && parentIds.find((id) => element.formId === id)\n\n if (injectingParentForm) {\n console.log(\n `Infinite loop was detected while attempting to inject form id: ${injectingParentForm}. Ignoring elements...`,\n )\n return newElements\n }\n\n element.elements = injectFormElements(\n formToInject.elements,\n forms,\n [...parentIds, formToInject.id],\n injectAuthenticatedForms,\n )\n newElements.push(element)\n } else {\n newElements.push(element)\n }\n\n return newElements\n }, [])\n}\n\nexport {\n forEachFormElement,\n forEachFormElementWithOptions,\n findFormElement,\n parseDynamicFormElementOptions,\n flattenFormElements,\n determineIsInfoPage,\n fixElementName,\n injectFormElementsIntoForm,\n}\n"]}
|
|
1
|
+
{"version":3,"file":"formElementsService.js","sourceRoot":"","sources":["../src/formElementsService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AAEhE;;;;;;;;;;;;;;GAcG;AACH,SAAS,kBAAkB,CACzB,QAAiC,EACjC,OAGS;IAET,eAAe,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,cAAc,EAAE,EAAE;QACxD,OAAO,CAAC,WAAW,EAAE,cAAc,CAAC,CAAA;QACpC,OAAO,KAAK,CAAA;IACd,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAS,6BAA6B,CACpC,QAAiC,EACjC,OAGS;IAET,eAAe,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,cAAc,EAAE,EAAE;QACxD,MAAM,kBAAkB,GACtB,eAAe,CAAC,YAAY,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAA;QAC5D,IAAI,kBAAkB,EAAE,CAAC;YACvB,OAAO,CAAC,kBAAkB,EAAE,cAAc,CAAC,CAAA;QAC7C,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAA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,CAAC,GAAG,SAAS,EAAE,YAAY,CAAC,EAAE,CAAC,EAC/B,wBAAwB,CACzB,CAAA;YACD,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC3B,CAAC;aAAM,CAAC;YACN,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC3B,CAAC;QAED,OAAO,WAAW,CAAA;IACpB,CAAC,EAAE,EAAE,CAAC,CAAA;AACR,CAAC;AAED,OAAO,EACL,kBAAkB,EAClB,6BAA6B,EAC7B,eAAe,EACf,8BAA8B,EAC9B,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,0BAA0B,GAC3B,CAAA","sourcesContent":["import { FormTypes } from '@oneblink/types'\nimport { typeCastService } from './index.js'\nexport { matchElementsTagRegex } from './form-elements-regex.js'\n\n/**\n * Iterate over all form elements, also iterating over nested form element (e.g.\n * page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElement(form.elements, (formElement) => {\n * // do something with formElement\n * })\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element\n */\nfunction forEachFormElement(\n elements: FormTypes.FormElement[],\n forEach: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n forEach(formElement, parentElements)\n return false\n })\n}\n\n/**\n * Iterate over all form elements that have options (e.g. `'select'` type\n * elements), also iterating over nested form element (e.g. page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElementWithOptions(\n * form.elements,\n * (formElementWithOptions) => {\n * // do something with formElementWithOptions\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element with options\n */\nfunction forEachFormElementWithOptions(\n elements: FormTypes.FormElement[],\n forEach: (\n elementWithOptions: FormTypes.FormElementWithOptions,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n const optionsFormElement =\n typeCastService.formElements.toOptionsElement(formElement)\n if (optionsFormElement) {\n forEach(optionsFormElement, parentElements)\n }\n return false\n })\n}\n\n/**\n * Iterate over all form elements and return an element that matches a\n * predicate, also iterating over nested form element (e.g. page elements). Will\n * return `undefined` if no matching element is found.\n *\n * #### Example\n *\n * ```js\n * const formElement = formElementsService.findFormElement(\n * form.elements,\n * (formElement) => {\n * return formElement.id === '123-abc'\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param predicate Predicate function to execute on each form element\n * @param parentElements\n * @returns\n */\nfunction findFormElement(\n elements: FormTypes.FormElement[],\n predicate: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => boolean,\n parentElements: FormTypes.FormElement[] = [],\n): FormTypes.FormElement | undefined {\n for (const element of elements) {\n if (predicate(element, parentElements)) {\n return element\n }\n\n if (\n (element.type === 'repeatableSet' ||\n element.type === 'page' ||\n element.type === 'form' ||\n element.type === 'infoPage' ||\n element.type === 'section') &&\n Array.isArray(element.elements)\n ) {\n const nestedElement = findFormElement(element.elements, predicate, [\n ...parentElements,\n element,\n ])\n\n if (nestedElement) {\n return nestedElement\n }\n }\n }\n}\n\n/**\n * Parse unknown data as valid dynamic options for a forms element. This will\n * always return an Array of valid dynamic options.\n *\n * #### Example\n *\n * ```js\n * const options = formElementsService.parseDynamicFormElementOptions(data)\n * // \"options\" are valid for a form element\n * ```\n *\n * @param data\n * @returns\n */\nfunction parseDynamicFormElementOptions(\n data: unknown,\n): FormTypes.DynamicChoiceElementOption[] {\n if (!Array.isArray(data)) {\n return []\n }\n return data.reduce<FormTypes.DynamicChoiceElementOption[]>(\n (options, record, index) => {\n if (typeof record === 'string') {\n options.push({\n value: record,\n label: record,\n })\n } else if (typeof record === 'object') {\n const option = record as Record<string, unknown>\n const value =\n typeof option.value === 'string' && option.value\n ? option.value\n : index.toString()\n const label =\n typeof option.label === 'string' && option.label\n ? option.label\n : value\n const colour =\n typeof option.colour === 'string' && option.colour\n ? option.colour\n : undefined\n const displayAlways =\n typeof option.displayAlways === 'boolean'\n ? option.displayAlways\n : undefined\n const imageUrl =\n typeof option.imageUrl === 'string' && option.imageUrl\n ? option.imageUrl\n : undefined\n const attributes = Array.isArray(option.attributes)\n ? option.attributes.reduce<\n FormTypes.DynamicChoiceElementOptionAttribute[]\n >((memo, attribute: unknown) => {\n if (\n typeof attribute === 'object' &&\n attribute &&\n 'value' in attribute &&\n typeof attribute.value === 'string' &&\n 'label' in attribute &&\n typeof attribute.label === 'string'\n ) {\n memo.push({\n value: attribute.value,\n label: attribute.label,\n })\n }\n return memo\n }, [])\n : undefined\n options.push({\n value,\n label,\n colour,\n attributes,\n displayAlways,\n imageUrl,\n options: Array.isArray(option.options)\n ? parseDynamicFormElementOptions(option.options)\n : undefined,\n })\n }\n return options\n },\n [],\n )\n}\n\n/**\n * Takes the nested definition structure and returns all form elements as 1d\n * array.\n *\n * #### Example\n *\n * ```js\n * const flattenedElements = formElementsService.flattenFormElements(\n * form.elements,\n * )\n * ```\n *\n * @param elements\n * @returns\n */\nfunction flattenFormElements(\n elements: FormTypes.FormElement[],\n): FormTypes.FormElement[] {\n return elements.reduce<FormTypes.FormElement[]>(\n (flattenedElements, element) => {\n flattenedElements.push(element)\n switch (element.type) {\n case 'section':\n case 'page': {\n flattenedElements.push(...flattenFormElements(element.elements))\n }\n }\n return flattenedElements\n },\n [],\n )\n}\n\nconst infoPageElements: FormTypes.FormElementType[] = [\n 'heading',\n 'html',\n 'image',\n 'section',\n 'page',\n 'infoPage',\n 'form',\n]\n/**\n * Determine a form is considered an info page. This means the form does not\n * allow any user input.\n *\n * @param form\n * @returns\n */\nfunction determineIsInfoPage(form: FormTypes.Form): boolean {\n const foundInputElement = findFormElement(form.elements, (e) => {\n return !infoPageElements.includes(e.type)\n })\n return !foundInputElement\n}\n\n/**\n * Remove invalid characters from a form element name.\n *\n * @param elementName\n * @returns\n */\nconst fixElementName = (elementName: string) => {\n // removes characters that aren't letters, numbers, underscores or dashes\n // replaces empty spaces with _\n return elementName\n .replace(/[^-\\w\\s]/g, '')\n .replace(/\\s/g, '_')\n .trim()\n}\n\n/**\n * Injects the elements of any elements with type `FORM` or `INFOPAGE` into the\n * form\n *\n * @param form The form to inject elements into\n * @param forms The forms that will be used to inject elements from if\n * referenced\n * @param injectAuthenticatedForms Indicates whether forms requiring\n * authentication should be injected, defaults to true\n * @returns\n */\nfunction injectFormElementsIntoForm(\n form: FormTypes.Form,\n forms: FormTypes.Form[],\n injectAuthenticatedForms = true,\n): FormTypes.FormElement[] {\n const elementsWithInjectedForms = injectFormElements(\n form.elements,\n forms,\n [form.id],\n injectAuthenticatedForms,\n )\n form.elements = elementsWithInjectedForms\n return form.elements\n}\n\nfunction injectFormElements(\n elements: FormTypes.FormElement[],\n forms: FormTypes.Form[],\n parentIds: number[],\n injectAuthenticatedForms: boolean,\n): FormTypes.FormElement[] {\n return elements.reduce<FormTypes.FormElement[]>((newElements, element) => {\n if (\n (element.type === 'page' ||\n element.type === 'repeatableSet' ||\n element.type === 'section') &&\n Array.isArray(element.elements)\n ) {\n const childElements = injectFormElements(\n element.elements,\n forms,\n parentIds,\n injectAuthenticatedForms,\n )\n element.elements = childElements\n }\n\n if (element.type === 'form' || element.type === 'infoPage') {\n const formToInject = forms.find((form) => element.formId === form.id)\n\n if (!formToInject) {\n const newElement: FormTypes.HtmlElement = {\n ...element,\n type: 'html',\n name: 'Form_not_found',\n label: 'Form not found.',\n defaultValue:\n 'Unable to display the embedded form for this element, as the form was not found. Please contact your Administrator.',\n }\n newElements.push(newElement)\n return newElements\n }\n\n if (formToInject.isAuthenticated && !injectAuthenticatedForms) {\n console.log(\n `No form elements injected for element id: ${element.id}, as request was unauthenticated and target form (form id: ${formToInject.id}) requires authentication.`,\n )\n\n const newElement: FormTypes.HtmlElement = {\n ...element,\n type: 'html',\n name: 'Form_requires_authenticated',\n label: 'Form Requires Authentication.',\n defaultValue:\n 'Unable to display the embedded form for this element, as the form requires authentication. Please login and refresh to view this embedded form.',\n }\n newElements.push(newElement)\n return newElements\n }\n\n const injectingParentForm =\n parentIds && parentIds.find((id) => element.formId === id)\n\n if (injectingParentForm) {\n console.log(\n `Infinite loop was detected while attempting to inject form id: ${injectingParentForm}. Ignoring elements...`,\n )\n return newElements\n }\n\n element.elements = injectFormElements(\n formToInject.elements,\n forms,\n [...parentIds, formToInject.id],\n injectAuthenticatedForms,\n )\n newElements.push(element)\n } else {\n newElements.push(element)\n }\n\n return newElements\n }, [])\n}\n\nexport {\n forEachFormElement,\n forEachFormElementWithOptions,\n findFormElement,\n parseDynamicFormElementOptions,\n flattenFormElements,\n determineIsInfoPage,\n fixElementName,\n injectFormElementsIntoForm,\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FreshdeskTypes, SubmissionEventTypes } from '@oneblink/types';
|
|
2
|
-
import { ResourceDefinition, ResourceDefinitionChoice } from './types/resource-definitions';
|
|
2
|
+
import { ResourceDefinition, ResourceDefinitionChoice } from './types/resource-definitions.js';
|
|
3
3
|
export declare const multilineFieldType = "custom_paragraph";
|
|
4
4
|
export declare const emailFieldType = "default_requester";
|
|
5
5
|
export declare const textFieldTypes: string[];
|
package/dist/freshdeskService.js
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
exports.setFreshdeskFieldOptions = setFreshdeskFieldOptions;
|
|
6
|
-
exports.multilineFieldType = 'custom_paragraph';
|
|
7
|
-
exports.emailFieldType = 'default_requester';
|
|
8
|
-
exports.textFieldTypes = [
|
|
9
|
-
exports.multilineFieldType,
|
|
1
|
+
export const multilineFieldType = 'custom_paragraph';
|
|
2
|
+
export const emailFieldType = 'default_requester';
|
|
3
|
+
export const textFieldTypes = [
|
|
4
|
+
multilineFieldType,
|
|
10
5
|
'custom_text',
|
|
11
6
|
'default_subject',
|
|
12
|
-
|
|
7
|
+
emailFieldType,
|
|
13
8
|
];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
export const numberFieldTypes = ['custom_number', 'custom_decimal'];
|
|
10
|
+
export const dateFieldType = 'custom_date';
|
|
11
|
+
export const checkboxFieldType = 'custom_checkbox';
|
|
12
|
+
export const choiceFieldTypes = [
|
|
18
13
|
'custom_dropdown',
|
|
19
|
-
|
|
14
|
+
checkboxFieldType,
|
|
20
15
|
'default_priority',
|
|
21
16
|
'default_status',
|
|
22
17
|
'default_source',
|
|
@@ -24,18 +19,17 @@ exports.choiceFieldTypes = [
|
|
|
24
19
|
'default_agent',
|
|
25
20
|
'default_group',
|
|
26
21
|
];
|
|
27
|
-
function generateFreshdeskTicketFieldDefinitions(fields) {
|
|
22
|
+
export function generateFreshdeskTicketFieldDefinitions(fields) {
|
|
28
23
|
return setFreshdeskFieldOptions(fields).reduce((memo, freshdeskField) => {
|
|
29
|
-
|
|
30
|
-
const isCheckboxField = freshdeskField.type === exports.checkboxFieldType;
|
|
24
|
+
const isCheckboxField = freshdeskField.type === checkboxFieldType;
|
|
31
25
|
const isRequired = freshdeskField.required_for_customers ||
|
|
32
26
|
freshdeskField.name === 'ticket_type';
|
|
33
|
-
const isTextField =
|
|
34
|
-
const isMultilineTextField = freshdeskField.type ===
|
|
35
|
-
const isEmailField = freshdeskField.type ===
|
|
36
|
-
const isDateField = freshdeskField.type ===
|
|
37
|
-
const isNumberField =
|
|
38
|
-
const isChoiceField =
|
|
27
|
+
const isTextField = textFieldTypes.includes(freshdeskField.type);
|
|
28
|
+
const isMultilineTextField = freshdeskField.type === multilineFieldType;
|
|
29
|
+
const isEmailField = freshdeskField.type === emailFieldType;
|
|
30
|
+
const isDateField = freshdeskField.type === dateFieldType;
|
|
31
|
+
const isNumberField = numberFieldTypes.includes(freshdeskField.type);
|
|
32
|
+
const isChoiceField = choiceFieldTypes.includes(freshdeskField.type);
|
|
39
33
|
const baseField = {
|
|
40
34
|
id: freshdeskField.id.toString(),
|
|
41
35
|
displayName: freshdeskField.label_for_customers,
|
|
@@ -86,12 +80,10 @@ function generateFreshdeskTicketFieldDefinitions(fields) {
|
|
|
86
80
|
});
|
|
87
81
|
}
|
|
88
82
|
else if (freshdeskField.type === 'nested_field' &&
|
|
89
|
-
|
|
83
|
+
freshdeskField.options?.length) {
|
|
90
84
|
const choices = freshdeskField.options.reduce((categoriesMemo, category) => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
var _a;
|
|
94
|
-
if ((_a = subCategory.options) === null || _a === void 0 ? void 0 : _a.length) {
|
|
85
|
+
const subCategories = category.options?.reduce((subCategoriesMemo, subCategory) => {
|
|
86
|
+
if (subCategory.options?.length) {
|
|
95
87
|
subCategoriesMemo.push({
|
|
96
88
|
value: subCategory.value,
|
|
97
89
|
label: subCategory.label,
|
|
@@ -103,7 +95,7 @@ function generateFreshdeskTicketFieldDefinitions(fields) {
|
|
|
103
95
|
}
|
|
104
96
|
return subCategoriesMemo;
|
|
105
97
|
}, []);
|
|
106
|
-
if (subCategories
|
|
98
|
+
if (subCategories?.length) {
|
|
107
99
|
categoriesMemo.push({
|
|
108
100
|
value: category.value,
|
|
109
101
|
label: category.label,
|
|
@@ -130,7 +122,7 @@ function generateFreshdeskTicketFieldDefinitions(fields) {
|
|
|
130
122
|
* @param fields
|
|
131
123
|
* @returns
|
|
132
124
|
*/
|
|
133
|
-
function setFreshdeskFieldOptions(fields) {
|
|
125
|
+
export function setFreshdeskFieldOptions(fields) {
|
|
134
126
|
for (const field of fields) {
|
|
135
127
|
const choices = field.choices;
|
|
136
128
|
if (!choices) {
|