@oneblink/sdk-core 0.4.6 → 1.0.0-beta.2

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.
@@ -60,8 +60,7 @@ function conditionallyShowElement(formElementsCtrl, elementToEvaluate, elementsE
60
60
  else {
61
61
  elementsEvaluated.push({
62
62
  id: elementToEvaluate.id,
63
- label: elementToEvaluate.type === 'form' ||
64
- elementToEvaluate.type === 'infoPage'
63
+ label: 'name' in elementToEvaluate
65
64
  ? elementToEvaluate.name
66
65
  : elementToEvaluate.label,
67
66
  });
@@ -1 +1 @@
1
- {"version":3,"file":"conditionallyShowElement.js","sourceRoot":"","sources":["../../src/conditionalLogicService/conditionallyShowElement.ts"],"names":[],"mappings":";;;;;AAEA,kGAAyE;AAEzE,MAAM,qBAAqB,GAAG,CAC5B,gBAAkC,EAClC,YAAmC,EACsB,EAAE;IAC3D,MAAM,aAAa,GAAG,gBAAgB,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;QACxE,OAAO,CACL,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,CAAC;YACvD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,KAAK,YAAY,CAAC,EAAE,CAAC,CAC1D,CAAA;IACH,CAAC,CAAC,CAAA;IACF,IACE,aAAa;QACb,CAAC,aAAa,CAAC,IAAI,KAAK,MAAM,IAAI,aAAa,CAAC,IAAI,KAAK,SAAS,CAAC,EACnE;QACA,OAAO;YACL,aAAa;YACb,GAAG,qBAAqB,CAAC,gBAAgB,EAAE,aAAa,CAAC;SAC1D,CAAA;KACF;IACD,OAAO,EAAE,CAAA;AACX,CAAC,CAAA;AAED,MAAM,4BAA4B,GAAG,CACnC,gBAAkC,EAClC,SAA8C,EAC9C,iBAAuD,EACtB,EAAE;IACnC,MAAM,gBA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;YACrB,EAAE,EAAE,iBAAiB,CAAC,EAAE;YACxB,KAAK,EACH,iBAAiB,CAAC,IAAI,KAAK,MAAM;gBACjC,iBAAiB,CAAC,IAAI,KAAK,UAAU;gBACnC,CAAC,CAAC,iBAAiB,CAAC,IAAI;gBACxB,CAAC,CAAC,iBAAiB,CAAC,KAAK;SAC9B,CAAC,CAAA;KACH;IAED,MAAM,iBAAiB,GAAG,CACxB,SAA8C,EAC9C,EAAE;QACF,oEAAoE;QACpE,iEAAiE;QACjE,yBAAyB;QACzB,OAAO,4BAA4B,CAAC,gBAAgB,EAAE,SAAS,EAAE;YAC/D,GAAG,iBAAiB;SACrB,CAAC,CAAA;IACJ,CAAC,CAAA;IAED,IAAI,iBAAiB,CAAC,sCAAsC,EAAE;QAC5D,OAAO,2BAA2B,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAA;KAC5D;SAAM;QACL,OAAO,2BAA2B,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAA;KAC3D;AACH,CAAC;AAtDD,2CAsDC","sourcesContent":["import { FormTypes, ConditionTypes } from '@oneblink/types'\nimport { FormElementsCtrl } from '../types'\nimport evaluateConditionalPredicate from './evaluateConditionalPredicate'\n\nconst getParentFormElements = (\n formElementsCtrl: FormElementsCtrl,\n childElement: FormTypes.FormElement,\n): Array<FormTypes.SectionElement | FormTypes.PageElement> => {\n const parentElement = formElementsCtrl.flattenedElements.find((element) => {\n return (\n (element.type === 'page' || element.type === 'section') &&\n element.elements.some(({ id }) => id === childElement.id)\n )\n })\n if (\n parentElement &&\n (parentElement.type === 'page' || parentElement.type === 'section')\n ) {\n return [\n parentElement,\n ...getParentFormElements(formElementsCtrl, parentElement),\n ]\n }\n return []\n}\n\nconst conditionallyShowByPredicate = (\n formElementsCtrl: FormElementsCtrl,\n predicate: ConditionTypes.ConditionalPredicate,\n elementsEvaluated: Array<{ id: string; label: string }>,\n): FormTypes.FormElement | boolean => {\n const predicateElement = evaluateConditionalPredicate({\n predicate,\n formElementsCtrl,\n })\n\n if (!predicateElement) {\n return false\n }\n\n // Here we will also need to check if the predicate element\n // is on a page/section element and the page/section element\n // is also hidden. If it is hidden we will treat this\n // predicate element as hidden as well.\n const parentFormElements = getParentFormElements(\n formElementsCtrl,\n predicateElement,\n )\n for (const parentFormElement of parentFormElements) {\n if (\n !conditionallyShowElement(formElementsCtrl, parentFormElement, [\n ...elementsEvaluated,\n ])\n ) {\n return false\n }\n }\n\n // Check to see if the model has one of the valid values to show the element\n return conditionallyShowElement(\n formElementsCtrl,\n predicateElement,\n elementsEvaluated,\n )\n}\n\nexport default function conditionallyShowElement(\n formElementsCtrl: FormElementsCtrl,\n elementToEvaluate: FormTypes.FormElement,\n elementsEvaluated: Array<{ id: string; label: string }>,\n): boolean {\n // If the element does not have the `conditionallyShow` flag set,\n // we can always show the element.\n if (\n !elementToEvaluate ||\n !elementToEvaluate.conditionallyShow ||\n !Array.isArray(elementToEvaluate.conditionallyShowPredicates) ||\n !elementToEvaluate.conditionallyShowPredicates.length\n ) {\n return true\n }\n const conditionallyShowPredicates =\n elementToEvaluate.conditionallyShowPredicates\n\n // Check to see if this element has already been used to evaluate\n // if the element should be shown based on parent element conditional logic\n const elementAlreadyEvaluated = elementsEvaluated.find(\n ({ id }) => id === elementToEvaluate.id,\n )\n if (elementAlreadyEvaluated) {\n throw new Error(\n `Your conditional logic has caused an infinite loop. Check the \"${elementAlreadyEvaluated.label}\" form element to ensure element A does not rely on element B if element B also relies on element A.`,\n )\n } else {\n elementsEvaluated.push({\n id: elementToEvaluate.id,\n label:\n elementToEvaluate.type === 'form' ||\n elementToEvaluate.type === 'infoPage'\n ? elementToEvaluate.name\n : elementToEvaluate.label,\n })\n }\n\n const predicateFunction = (\n predicate: ConditionTypes.ConditionalPredicate,\n ) => {\n // Spread the array of elements evaluated so that each predicate can\n // evaluate the tree without causing false positives for infinite\n // loop conditional logic\n return conditionallyShowByPredicate(formElementsCtrl, predicate, [\n ...elementsEvaluated,\n ])\n }\n\n if (elementToEvaluate.requiresAllConditionallyShowPredicates) {\n return conditionallyShowPredicates.every(predicateFunction)\n } else {\n return conditionallyShowPredicates.some(predicateFunction)\n }\n}\n"]}
1
+ {"version":3,"file":"conditionallyShowElement.js","sourceRoot":"","sources":["../../src/conditionalLogicService/conditionallyShowElement.ts"],"names":[],"mappings":";;;;;AAEA,kGAAyE;AAEzE,MAAM,qBAAqB,GAAG,CAC5B,gBAAkC,EAClC,YAAmC,EACsB,EAAE;IAC3D,MAAM,aAAa,GAAG,gBAAgB,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;QACxE,OAAO,CACL,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,CAAC;YACvD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,KAAK,YAAY,CAAC,EAAE,CAAC,CAC1D,CAAA;IACH,CAAC,CAAC,CAAA;IACF,IACE,aAAa;QACb,CAAC,aAAa,CAAC,IAAI,KAAK,MAAM,IAAI,aAAa,CAAC,IAAI,KAAK,SAAS,CAAC,EACnE;QACA,OAAO;YACL,aAAa;YACb,GAAG,qBAAqB,CAAC,gBAAgB,EAAE,aAAa,CAAC;SAC1D,CAAA;KACF;IACD,OAAO,EAAE,CAAA;AACX,CAAC,CAAA;AAED,MAAM,4BAA4B,GAAG,CACnC,gBAAkC,EAClC,SAA8C,EAC9C,iBAAuD,EACtB,EAAE;IACnC,MAAM,gBAAgB,GAAG,IAAA,sCAA4B,EAAC;QACpD,SAAS;QACT,gBAAgB;KACjB,CAAC,CAAA;IAEF,IAAI,CAAC,gBAAgB,EAAE;QACrB,OAAO,KAAK,CAAA;KACb;IAED,2DAA2D;IAC3D,4DAA4D;IAC5D,qDAAqD;IACrD,uCAAuC;IACvC,MAAM,kBAAkB,GAAG,qBAAqB,CAC9C,gBAAgB,EAChB,gBAAgB,CACjB,CA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AAyB;QACzB,OAAO,4BAA4B,CAAC,gBAAgB,EAAE,SAAS,EAAE;YAC/D,GAAG,iBAAiB;SACrB,CAAC,CAAA;IACJ,CAAC,CAAA;IAED,IAAI,iBAAiB,CAAC,sCAAsC,EAAE;QAC5D,OAAO,2BAA2B,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAA;KAC5D;SAAM;QACL,OAAO,2BAA2B,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAA;KAC3D;AACH,CAAC;AArDD,2CAqDC","sourcesContent":["import { FormTypes, ConditionTypes } from '@oneblink/types'\nimport { FormElementsCtrl } from '../types'\nimport evaluateConditionalPredicate from './evaluateConditionalPredicate'\n\nconst getParentFormElements = (\n formElementsCtrl: FormElementsCtrl,\n childElement: FormTypes.FormElement,\n): Array<FormTypes.SectionElement | FormTypes.PageElement> => {\n const parentElement = formElementsCtrl.flattenedElements.find((element) => {\n return (\n (element.type === 'page' || element.type === 'section') &&\n element.elements.some(({ id }) => id === childElement.id)\n )\n })\n if (\n parentElement &&\n (parentElement.type === 'page' || parentElement.type === 'section')\n ) {\n return [\n parentElement,\n ...getParentFormElements(formElementsCtrl, parentElement),\n ]\n }\n return []\n}\n\nconst conditionallyShowByPredicate = (\n formElementsCtrl: FormElementsCtrl,\n predicate: ConditionTypes.ConditionalPredicate,\n elementsEvaluated: Array<{ id: string; label: string }>,\n): FormTypes.FormElement | boolean => {\n const predicateElement = evaluateConditionalPredicate({\n predicate,\n formElementsCtrl,\n })\n\n if (!predicateElement) {\n return false\n }\n\n // Here we will also need to check if the predicate element\n // is on a page/section element and the page/section element\n // is also hidden. If it is hidden we will treat this\n // predicate element as hidden as well.\n const parentFormElements = getParentFormElements(\n formElementsCtrl,\n predicateElement,\n )\n for (const parentFormElement of parentFormElements) {\n if (\n !conditionallyShowElement(formElementsCtrl, parentFormElement, [\n ...elementsEvaluated,\n ])\n ) {\n return false\n }\n }\n\n // Check to see if the model has one of the valid values to show the element\n return conditionallyShowElement(\n formElementsCtrl,\n predicateElement,\n elementsEvaluated,\n )\n}\n\nexport default function conditionallyShowElement(\n formElementsCtrl: FormElementsCtrl,\n elementToEvaluate: FormTypes.FormElement,\n elementsEvaluated: Array<{ id: string; label: string }>,\n): boolean {\n // If the element does not have the `conditionallyShow` flag set,\n // we can always show the element.\n if (\n !elementToEvaluate ||\n !elementToEvaluate.conditionallyShow ||\n !Array.isArray(elementToEvaluate.conditionallyShowPredicates) ||\n !elementToEvaluate.conditionallyShowPredicates.length\n ) {\n return true\n }\n const conditionallyShowPredicates =\n elementToEvaluate.conditionallyShowPredicates\n\n // Check to see if this element has already been used to evaluate\n // if the element should be shown based on parent element conditional logic\n const elementAlreadyEvaluated = elementsEvaluated.find(\n ({ id }) => id === elementToEvaluate.id,\n )\n if (elementAlreadyEvaluated) {\n throw new Error(\n `Your conditional logic has caused an infinite loop. Check the \"${elementAlreadyEvaluated.label}\" form element to ensure element A does not rely on element B if element B also relies on element A.`,\n )\n } else {\n elementsEvaluated.push({\n id: elementToEvaluate.id,\n label:\n 'name' in elementToEvaluate\n ? elementToEvaluate.name\n : elementToEvaluate.label,\n })\n }\n\n const predicateFunction = (\n predicate: ConditionTypes.ConditionalPredicate,\n ) => {\n // Spread the array of elements evaluated so that each predicate can\n // evaluate the tree without causing false positives for infinite\n // loop conditional logic\n return conditionallyShowByPredicate(formElementsCtrl, predicate, [\n ...elementsEvaluated,\n ])\n }\n\n if (elementToEvaluate.requiresAllConditionallyShowPredicates) {\n return conditionallyShowPredicates.every(predicateFunction)\n } else {\n return conditionallyShowPredicates.some(predicateFunction)\n }\n}\n"]}
@@ -1,4 +1,4 @@
1
1
  import { FormTypes } from '@oneblink/types';
2
2
  import { FormElementsCtrl } from '../types';
3
- export declare type ShouldShowOption = 'SHOW' | 'HIDE' | 'LOADING_DYNAMIC_DEPENDENCY' | 'LOADING_STATIC_DEPENDENCY';
3
+ export type ShouldShowOption = 'SHOW' | 'HIDE' | 'LOADING_DYNAMIC_DEPENDENCY' | 'LOADING_STATIC_DEPENDENCY';
4
4
  export default function conditionallyShowOption(formElementsCtrl: FormElementsCtrl, elementToEvaluate: FormTypes.FormElementWithOptions, optionToEvaluate: FormTypes.ChoiceElementOption, optionsEvaluated: string[]): ShouldShowOption;
@@ -1,11 +1,12 @@
1
1
  import { FormTypes, SubmissionTypes } from '@oneblink/types';
2
- export declare type FormElementsConditionallyShown = Record<string, FormElementConditionallyShown>;
3
- export declare type FormElementConditionallyShown = undefined | {
2
+ export type FormElementsConditionallyShown = Record<string, FormElementConditionallyShown>;
3
+ export type FormElementConditionallyShownElement = {
4
4
  type: 'formElement';
5
5
  isHidden: boolean;
6
- options?: import('@oneblink/types').FormTypes.ChoiceElementOption[];
6
+ options?: FormTypes.ChoiceElementOption[];
7
7
  dependencyIsLoading?: boolean;
8
- } | {
8
+ };
9
+ export type FormElementConditionallyShown = undefined | FormElementConditionallyShownElement | {
9
10
  type: 'formElements';
10
11
  isHidden: boolean;
11
12
  formElements: FormElementsConditionallyShown | undefined;
@@ -14,8 +15,8 @@ export declare type FormElementConditionallyShown = undefined | {
14
15
  isHidden: boolean;
15
16
  entries: Record<string, FormElementsConditionallyShown | undefined>;
16
17
  };
17
- export declare type ErrorCallback = (error: Error) => void;
18
- export declare type FormElementsConditionallyShownParameters = {
18
+ export type ErrorCallback = (error: Error) => void;
19
+ export type FormElementsConditionallyShownParameters = {
19
20
  /** Form elements to evaluate */
20
21
  formElements: FormTypes.FormElement[];
21
22
  /** Current submission data */
@@ -1 +1 @@
1
- {"version":3,"file":"generateFormElementsConditionallyShown.js","sourceRoot":"","sources":["../../src/conditionalLogicService/generateFormElementsConditionallyShown.ts"],"names":[],"mappings":";;;;;;AAEA,gEAA4D;AAC5D,0FAAiE;AACjE,wFAEkC;AAClC,0BAAoC;AA4BpC,MAAM,8BAA8B,GAAG,CACrC,gBAAkC,EAClC,OAA8B,EAC9B,aAAwC,EACxC,EAAE;IACF,IAAI;QACF,OAAO,IAAA,kCAAwB,EAAC,gBAAgB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ACD,QAAQ,sBAAsB,EAAE;gCAC9B,KAAK,MAAM,CAAC,CAAC;oCACX,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;oCACvB,MAAK;iCACN;gCACD,KAAK,2BAA2B,CAAC,CAAC;oCAChC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;oCACvB,6BAA6B,CAAC,mBAAmB,GAAG,IAAI,CAAA;oCACxD,MAAK;iCACN;gCACD,KAAK,4BAA4B,CAAC,CAAC;oCACjC,6BAA6B,CAAC,mBAAmB,GAAG,IAAI,CAAA;oCACxD,MAAK;iCACN;6BACF;yBACF;wBACD,6BAA6B,CAAC,OAAO,GAAG,UAAU,CAAA;qBACnD;iBACF;gBAED,8BAA8B,CAAC,OAAO,CAAC,IAAI,CAAC;oBAC1C,6BAA6B,CAAA;aAChC;SACF;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;AAJD,wFAIC","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 FormElementConditionallyShown =\n | undefined\n | {\n type: 'formElement'\n isHidden: boolean\n options?: import('@oneblink/types').FormTypes.ChoiceElementOption[]\n dependencyIsLoading?: boolean\n }\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 && 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 && 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,gEAA4D;AAC5D,0FAAiE;AACjE,wFAEkC;AAClC,0BAAoC;AA8BpC,MAAM,8BAA8B,GAAG,CACrC,gBAAkC,EAClC,OAA8B,EAC9B,aAAwC,EACxC,EAAE;IACF,IAAI;QACF,OAAO,IAAA,kCAAwB,EAAC,gBAAgB,EAAE,OAAO,EAAE,EAAE,CAAC,CAAA;KAC/D;IAAC,OAAO,KAAK,EAAE;QACd,aAAa,IAAI,aAAa,CAAC,KAAc,CAAC,CAAA;QAC9C,OAAO,KAAK,CAAA;KACb;AACH,CAAC,CAAA;AAED,MAAM,6BAA6B,GAAG,CACpC,gBAAkC,EAClC,OAAyC,EACzC,MAAqC,EACrC,aAAwC,EACtB,EAAE;IACpB,IAAI;QACF,OAAO,IAAA,iCAAuB,EAAC,gBAAgB,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,CAAA;KACtE;IAAC,OAAO,KAAK,EAAE;QACd,aAAa,IAAI,aAAa,CAAC,KAAc,CAAC,CAAA;QAC9C,OAAO,MAAM,CAAA;KACd;AACH,CAAC,CAAA;AAWD,MAAM,gDAAgD,GAAG,CAAC,EACxD,YAAY,EACZ,UAAU,EAC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A8B,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;AAJD,wFAIC","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 && 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 && 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 { FormTypes, SubmissionTypes } from '@oneblink/types';
2
- export declare type CustomValuesOptions = {
2
+ export type CustomValuesOptions = {
3
3
  form: FormTypes.Form;
4
4
  externalId?: string;
5
5
  submissionId: string;
@@ -1,6 +1,6 @@
1
1
  import { FormTypes, SubmissionEventTypes } from '@oneblink/types';
2
2
  declare function toNamedElement(e: FormTypes.FormElement): FormTypes.FormElementWithName | undefined;
3
- export declare type NonNestedElementsFormElement = FormTypes.NonNestedElementsElement | FormTypes.FormElementWithForm;
3
+ export type NonNestedElementsFormElement = FormTypes.NonNestedElementsElement | FormTypes.FormElementWithForm;
4
4
  declare const formElements: {
5
5
  toOptionsElement: (e: FormTypes.FormElement) => FormTypes.FormElementWithOptions | undefined;
6
6
  toFormElementWithForm: (e: FormTypes.FormElement) => FormTypes.FormElementWithForm | undefined;
@@ -1,5 +1,5 @@
1
1
  import { FormTypes, SubmissionTypes } from '@oneblink/types';
2
- export declare type FormElementsCtrl = {
2
+ export type FormElementsCtrl = {
3
3
  model: SubmissionTypes.S3SubmissionData['submission'];
4
4
  flattenedElements: FormTypes.FormElement[];
5
5
  parentFormElementsCtrl?: FormElementsCtrl;
package/package.json CHANGED
@@ -1,34 +1,34 @@
1
1
  {
2
2
  "name": "@oneblink/sdk-core",
3
3
  "description": "OneBlink SDK for JavaScript (works in Browsers and NodeJS)",
4
- "version": "0.4.6",
4
+ "version": "1.0.0-beta.2",
5
5
  "author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/oneblink/sdk-core-js/issues"
8
8
  },
9
9
  "devDependencies": {
10
- "@oneblink/release-cli": "^1.2.0",
10
+ "@oneblink/release-cli": "^2.0.2",
11
11
  "@oneblink/types": "github:oneblink/types",
12
- "@types/jest": "^27.4.1",
13
- "@typescript-eslint/eslint-plugin": "^5.19.0",
14
- "@typescript-eslint/parser": "^5.19.0",
15
- "eslint": "^8.13.0",
16
- "eslint-config-prettier": "^8.5.0",
17
- "eslint-plugin-prettier": "^4.0.0",
12
+ "@types/jest": "^29.5.0",
13
+ "@typescript-eslint/eslint-plugin": "^5.57.1",
14
+ "@typescript-eslint/parser": "^5.57.1",
15
+ "eslint": "^8.37.0",
16
+ "eslint-config-prettier": "^8.8.0",
17
+ "eslint-plugin-prettier": "^4.2.1",
18
18
  "fixpack": "^4.0.0",
19
- "jest": "^27.5.1",
20
- "prettier": "^2.6.2",
21
- "prettier-plugin-jsdoc": "^0.3.38",
22
- "ts-jest": "^27.1.4",
23
- "tsc-watch": "^5.0.3",
24
- "typedoc": "^0.22.15",
25
- "typescript": "^4.6.3"
19
+ "jest": "^29.5.0",
20
+ "prettier": "^2.8.7",
21
+ "prettier-plugin-jsdoc": "^0.4.2",
22
+ "ts-jest": "^29.1.0",
23
+ "tsc-watch": "^6.0.0",
24
+ "typedoc": "^0.23.28",
25
+ "typescript": "^5.0.3"
26
26
  },
27
27
  "directories": {
28
28
  "doc": "docs"
29
29
  },
30
30
  "engines": {
31
- "node": ">=12",
31
+ "node": ">=14",
32
32
  "npm": ">=8"
33
33
  },
34
34
  "files": [
@@ -54,7 +54,7 @@
54
54
  "format": "prettier --write .",
55
55
  "prepare": "npm run build",
56
56
  "pretest": "npm run fixpack && npm run eslint",
57
- "release": "typedoc && oneblink-release",
57
+ "release": "typedoc && oneblink-release --no-name",
58
58
  "start": "tsc-watch",
59
59
  "test": "jest",
60
60
  "types": "npm install --save-dev github:oneblink/types",