@aemforms/af-core 0.22.18 → 0.22.20

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.
Files changed (120) hide show
  1. package/lib/browser/afb-events.js +151 -0
  2. package/lib/browser/afb-runtime.js +3620 -0
  3. package/lib/cjs/index.cjs +8886 -0
  4. package/lib/{BaseNode.d.ts → esm/BaseNode.d.ts} +11 -68
  5. package/lib/{BaseNode.js → esm/BaseNode.js} +86 -182
  6. package/lib/esm/Checkbox.d.ts +79 -0
  7. package/lib/esm/Checkbox.js +27 -0
  8. package/lib/{CheckboxGroup.d.ts → esm/CheckboxGroup.d.ts} +2 -14
  9. package/lib/esm/CheckboxGroup.js +23 -0
  10. package/lib/esm/Container.d.ts +53 -0
  11. package/lib/{Container.js → esm/Container.js} +76 -124
  12. package/lib/{DateField.d.ts → esm/DateField.d.ts} +1 -1
  13. package/lib/esm/DateField.js +21 -0
  14. package/lib/{Field.d.ts → esm/Field.d.ts} +44 -98
  15. package/lib/{Field.js → esm/Field.js} +172 -206
  16. package/lib/esm/Fieldset.d.ts +16 -0
  17. package/lib/esm/Fieldset.js +45 -0
  18. package/lib/{FileObject.d.ts → esm/FileObject.d.ts} +1 -4
  19. package/lib/esm/FileObject.js +26 -0
  20. package/lib/{FileUpload.d.ts → esm/FileUpload.d.ts} +3 -18
  21. package/lib/esm/FileUpload.js +108 -0
  22. package/lib/esm/Form.d.ts +113 -0
  23. package/lib/esm/Form.js +176 -0
  24. package/lib/esm/FormInstance.d.ts +13 -0
  25. package/lib/esm/FormInstance.js +81 -0
  26. package/lib/esm/FormMetaData.d.ts +7 -0
  27. package/lib/esm/FormMetaData.js +10 -0
  28. package/lib/esm/InstanceManager.d.ts +9 -0
  29. package/lib/esm/InstanceManager.js +31 -0
  30. package/lib/esm/Node.d.ts +7 -0
  31. package/lib/esm/Node.js +16 -0
  32. package/lib/esm/Scriptable.d.ts +17 -0
  33. package/lib/{Scriptable.js → esm/Scriptable.js} +12 -55
  34. package/lib/{controller → esm/controller}/EventQueue.d.ts +3 -11
  35. package/lib/{controller → esm/controller}/EventQueue.js +10 -26
  36. package/lib/esm/controller/Events.d.ts +85 -0
  37. package/lib/esm/controller/Events.js +149 -0
  38. package/lib/{controller → esm/controller}/Logger.d.ts +0 -6
  39. package/lib/{controller → esm/controller}/Logger.js +5 -11
  40. package/lib/{data → esm/data}/DataGroup.d.ts +1 -7
  41. package/lib/{data → esm/data}/DataGroup.js +20 -35
  42. package/lib/{data → esm/data}/DataValue.d.ts +2 -8
  43. package/lib/esm/data/DataValue.js +46 -0
  44. package/lib/{data → esm/data}/EmptyDataValue.d.ts +1 -1
  45. package/lib/esm/data/EmptyDataValue.js +29 -0
  46. package/lib/esm/index.d.ts +21 -0
  47. package/lib/esm/index.js +21 -0
  48. package/lib/{rules → esm/rules}/FunctionRuntime.d.ts +2 -24
  49. package/lib/{rules → esm/rules}/FunctionRuntime.js +44 -114
  50. package/lib/esm/rules/RuleEngine.d.ts +12 -0
  51. package/lib/esm/rules/RuleEngine.js +47 -0
  52. package/lib/{types → esm/types}/Json.d.ts +6 -22
  53. package/lib/esm/types/Json.js +7 -0
  54. package/lib/esm/types/Model.d.ts +131 -0
  55. package/lib/esm/types/Model.js +8 -0
  56. package/lib/esm/types/index.d.ts +2 -0
  57. package/lib/esm/types/index.js +2 -0
  58. package/lib/{utils → esm/utils}/DataRefParser.d.ts +2 -5
  59. package/lib/{utils → esm/utils}/DataRefParser.js +22 -49
  60. package/lib/{utils → esm/utils}/Fetch.d.ts +1 -0
  61. package/lib/esm/utils/Fetch.js +61 -0
  62. package/lib/esm/utils/FormCreationUtils.d.ts +9 -0
  63. package/lib/esm/utils/FormCreationUtils.js +74 -0
  64. package/lib/esm/utils/FormUtils.d.ts +12 -0
  65. package/lib/esm/utils/FormUtils.js +187 -0
  66. package/lib/esm/utils/JsonUtils.d.ts +11 -0
  67. package/lib/esm/utils/JsonUtils.js +76 -0
  68. package/lib/{utils → esm/utils}/LogUtils.d.ts +1 -4
  69. package/lib/esm/utils/LogUtils.js +6 -0
  70. package/lib/esm/utils/SchemaUtils.d.ts +3 -0
  71. package/lib/esm/utils/SchemaUtils.js +71 -0
  72. package/lib/esm/utils/TranslationUtils.d.ts +11 -0
  73. package/lib/esm/utils/TranslationUtils.js +115 -0
  74. package/lib/esm/utils/ValidationUtils.d.ts +19 -0
  75. package/lib/{utils → esm/utils}/ValidationUtils.js +29 -187
  76. package/package.json +17 -10
  77. package/lib/Checkbox.d.ts +0 -88
  78. package/lib/Checkbox.js +0 -49
  79. package/lib/CheckboxGroup.js +0 -43
  80. package/lib/Container.d.ts +0 -199
  81. package/lib/DateField.js +0 -35
  82. package/lib/Fieldset.d.ts +0 -34
  83. package/lib/Fieldset.js +0 -113
  84. package/lib/FileObject.js +0 -39
  85. package/lib/FileUpload.js +0 -155
  86. package/lib/Form.d.ts +0 -262
  87. package/lib/Form.js +0 -230
  88. package/lib/FormInstance.d.ts +0 -38
  89. package/lib/FormInstance.js +0 -119
  90. package/lib/FormMetaData.d.ts +0 -11
  91. package/lib/FormMetaData.js +0 -28
  92. package/lib/Node.d.ts +0 -12
  93. package/lib/Node.js +0 -27
  94. package/lib/Scriptable.d.ts +0 -31
  95. package/lib/controller/Controller.d.ts +0 -223
  96. package/lib/controller/Controller.js +0 -287
  97. package/lib/controller/index.d.ts +0 -1
  98. package/lib/controller/index.js +0 -24
  99. package/lib/data/DataValue.js +0 -52
  100. package/lib/data/EmptyDataValue.js +0 -46
  101. package/lib/index.d.ts +0 -28
  102. package/lib/index.js +0 -63
  103. package/lib/rules/RuleEngine.d.ts +0 -23
  104. package/lib/rules/RuleEngine.js +0 -62
  105. package/lib/types/Json.js +0 -19
  106. package/lib/types/Model.d.ts +0 -379
  107. package/lib/types/Model.js +0 -20
  108. package/lib/types/index.d.ts +0 -2
  109. package/lib/types/index.js +0 -25
  110. package/lib/utils/Fetch.js +0 -29
  111. package/lib/utils/FormUtils.d.ts +0 -47
  112. package/lib/utils/FormUtils.js +0 -257
  113. package/lib/utils/JsonUtils.d.ts +0 -62
  114. package/lib/utils/JsonUtils.js +0 -143
  115. package/lib/utils/LogUtils.js +0 -17
  116. package/lib/utils/SchemaUtils.d.ts +0 -16
  117. package/lib/utils/SchemaUtils.js +0 -92
  118. package/lib/utils/TranslationUtils.d.ts +0 -41
  119. package/lib/utils/TranslationUtils.js +0 -185
  120. package/lib/utils/ValidationUtils.d.ts +0 -170
@@ -0,0 +1,16 @@
1
+ import Container from './Container.js';
2
+ import { ContainerModel, FieldsetJson, FieldsetModel, FormModel, IFormFieldFactory } from './types/index.js';
3
+ export declare class Fieldset extends Container<FieldsetJson> implements FieldsetModel {
4
+ constructor(params: FieldsetJson, _options: {
5
+ form: FormModel;
6
+ parent: ContainerModel;
7
+ fieldFactory: IFormFieldFactory;
8
+ });
9
+ private _applyDefaults;
10
+ get type(): "array" | "object" | undefined;
11
+ get items(): (import("./types/Model.js").FieldModel | FieldsetModel)[];
12
+ get value(): null;
13
+ get fieldType(): string;
14
+ get enabled(): boolean | undefined;
15
+ set enabled(e: boolean | undefined);
16
+ }
@@ -0,0 +1,45 @@
1
+ import Container from './Container.js';
2
+ import { ExecuteRule, Initialize } from './controller/Events.js';
3
+ const defaults = {
4
+ visible: true
5
+ };
6
+ export class Fieldset extends Container {
7
+ constructor(params, _options) {
8
+ super(params, _options);
9
+ this._applyDefaults();
10
+ this.queueEvent(new Initialize());
11
+ this.queueEvent(new ExecuteRule());
12
+ }
13
+ _applyDefaults() {
14
+ Object.entries(defaults).map(([key, value]) => {
15
+ if (this._jsonModel[key] === undefined) {
16
+ this._jsonModel[key] = value;
17
+ }
18
+ });
19
+ if (this._jsonModel.dataRef && this._jsonModel.type === undefined) {
20
+ this._jsonModel.type = 'object';
21
+ }
22
+ }
23
+ get type() {
24
+ const ret = super.type;
25
+ if (ret === 'array' || ret === 'object') {
26
+ return ret;
27
+ }
28
+ return undefined;
29
+ }
30
+ get items() {
31
+ return super.items;
32
+ }
33
+ get value() {
34
+ return null;
35
+ }
36
+ get fieldType() {
37
+ return 'panel';
38
+ }
39
+ get enabled() {
40
+ return this._jsonModel.enabled;
41
+ }
42
+ set enabled(e) {
43
+ this._setProperty('enabled', e);
44
+ }
45
+ }
@@ -1,7 +1,4 @@
1
- import { IFileObject } from './types';
2
- /**
3
- * Defines a file object which implements the {@link IFileObject | file object interface}
4
- */
1
+ import { IFileObject } from './types/index.js';
5
2
  export declare class FileObject implements IFileObject {
6
3
  data: any;
7
4
  mediaType: string;
@@ -0,0 +1,26 @@
1
+ export class FileObject {
2
+ data;
3
+ mediaType = 'application/octet-stream';
4
+ name = 'unknown';
5
+ size = 0;
6
+ constructor(init) {
7
+ Object.assign(this, init);
8
+ }
9
+ get type() {
10
+ return this.mediaType;
11
+ }
12
+ toJSON() {
13
+ return {
14
+ 'name': this.name,
15
+ 'size': this.size,
16
+ 'mediaType': this.mediaType,
17
+ 'data': this.data.toString()
18
+ };
19
+ }
20
+ equals(obj) {
21
+ return (this.data === obj.data &&
22
+ this.mediaType === obj.mediaType &&
23
+ this.name === obj.name &&
24
+ this.size === obj.size);
25
+ }
26
+ }
@@ -1,9 +1,6 @@
1
- import Field from './Field';
2
- import { FieldModel } from './types';
3
- import DataGroup from './data/DataGroup';
4
- /**
5
- * Implementation of FileUpload runtime model which extends from {@link Field | field}
6
- */
1
+ import Field from './Field.js';
2
+ import { FieldModel } from './types/index.js';
3
+ import DataGroup from './data/DataGroup.js';
7
4
  declare class FileUpload extends Field implements FieldModel {
8
5
  protected _getDefaults(): {
9
6
  accept: string[];
@@ -14,20 +11,8 @@ declare class FileUpload extends Field implements FieldModel {
14
11
  type: string | undefined;
15
12
  };
16
13
  protected _getFallbackType(): string | undefined;
17
- /**
18
- * Returns the max file size in bytes as per IEC specification
19
- */
20
14
  get maxFileSize(): number;
21
- /**
22
- * Returns the list of mime types which file attachment can accept
23
- */
24
15
  get accept(): string[] | undefined;
25
- /**
26
- * Checks whether there are any updates in the properties
27
- * @param propNames
28
- * @param updates
29
- * @private
30
- */
31
16
  protected _applyUpdates(propNames: string[], updates: any): any;
32
17
  protected getInternalType(): "file" | "file[]";
33
18
  protected getDataNodeValue(typedValue: any): any;
@@ -0,0 +1,108 @@
1
+ import { propertyChange } from './controller/Events.js';
2
+ import Field from './Field.js';
3
+ import { getFileSizeInBytes } from './utils/FormUtils.js';
4
+ import { FileObject } from './FileObject.js';
5
+ import { Constraints } from './utils/ValidationUtils.js';
6
+ function addNameToDataURL(dataURL, name) {
7
+ return dataURL.replace(';base64', `;name=${encodeURIComponent(name)};base64`);
8
+ }
9
+ function processFiles(files) {
10
+ return Promise.all([].map.call(files, processFile));
11
+ }
12
+ async function processFile(file) {
13
+ const { name, size, type } = file;
14
+ const fileObj = await new Promise((resolve, reject) => {
15
+ const reader = new FileReader();
16
+ reader.onload = event => {
17
+ resolve(new FileObject({
18
+ data: addNameToDataURL(event.target.result, name),
19
+ type,
20
+ name,
21
+ size
22
+ }));
23
+ };
24
+ reader.readAsDataURL(file.data);
25
+ });
26
+ return fileObj;
27
+ }
28
+ class FileUpload extends Field {
29
+ _getDefaults() {
30
+ return {
31
+ ...super._getDefaults(),
32
+ accept: ['audio/*', 'video/*', 'image/*', 'text/*', 'application/pdf'],
33
+ maxFileSize: '2MB'
34
+ };
35
+ }
36
+ _getFallbackType() {
37
+ return 'file';
38
+ }
39
+ get maxFileSize() {
40
+ return getFileSizeInBytes(this._jsonModel.maxFileSize);
41
+ }
42
+ get accept() {
43
+ return this._jsonModel.accept;
44
+ }
45
+ _applyUpdates(propNames, updates) {
46
+ return propNames.reduce((acc, propertyName) => {
47
+ const prevValue = this._jsonModel[propertyName];
48
+ const currentValue = updates[propertyName];
49
+ if (currentValue !== prevValue) {
50
+ acc[propertyName] = {
51
+ propertyName,
52
+ currentValue,
53
+ prevValue
54
+ };
55
+ if (prevValue instanceof FileObject && typeof currentValue === 'object' && propertyName === 'value') {
56
+ this._jsonModel[propertyName] = new FileObject({ ...prevValue, ...{ 'data': currentValue.data } });
57
+ }
58
+ else {
59
+ this._jsonModel[propertyName] = currentValue;
60
+ }
61
+ }
62
+ return acc;
63
+ }, {});
64
+ }
65
+ getInternalType() {
66
+ return this.type?.endsWith('[]') ? 'file[]' : 'file';
67
+ }
68
+ getDataNodeValue(typedValue) {
69
+ let dataNodeValue = typedValue;
70
+ if (dataNodeValue != null) {
71
+ if (this.type === 'string') {
72
+ dataNodeValue = dataNodeValue.data?.toString();
73
+ }
74
+ else if (this.type === 'string[]') {
75
+ dataNodeValue = dataNodeValue instanceof Array ? dataNodeValue : [dataNodeValue];
76
+ dataNodeValue = dataNodeValue.map((_) => _?.data?.toString());
77
+ }
78
+ }
79
+ return dataNodeValue;
80
+ }
81
+ async _serialize() {
82
+ const val = this._jsonModel.value;
83
+ if (val === undefined) {
84
+ return null;
85
+ }
86
+ const filesInfo = await processFiles(val instanceof Array ? val : [val]);
87
+ return filesInfo;
88
+ }
89
+ importData(dataModel) {
90
+ this._bindToDataModel(dataModel);
91
+ const dataNode = this.getDataNode();
92
+ if (dataNode !== undefined) {
93
+ const value = dataNode?.$value;
94
+ if (value != null) {
95
+ const res = Constraints.type(this.getInternalType(), value);
96
+ if (!res.valid) {
97
+ this.form.logger.error(`unable to bind ${this.name} to data`);
98
+ }
99
+ this.form.getEventQueue().queue(this, propertyChange('value', res.value, this._jsonModel.value));
100
+ this._jsonModel.value = res.value;
101
+ }
102
+ else {
103
+ this._jsonModel.value = null;
104
+ }
105
+ }
106
+ }
107
+ }
108
+ export default FileUpload;
@@ -0,0 +1,113 @@
1
+ import Container from './Container.js';
2
+ import { Action, BaseModel, FieldModel, FieldsetModel, FormJson, FormModel, IFormFieldFactory } from './types/index.js';
3
+ import FormMetaData from './FormMetaData.js';
4
+ import EventQueue from './controller/EventQueue.js';
5
+ import { Logger, LogLevel } from './controller/Logger.js';
6
+ import RuleEngine from './rules/RuleEngine.js';
7
+ declare class Form extends Container<FormJson> implements FormModel {
8
+ private _ruleEngine;
9
+ private _eventQueue;
10
+ private _fields;
11
+ _ids: Generator<string, void, string>;
12
+ private _invalidFields;
13
+ private _logger;
14
+ constructor(n: FormJson, fieldFactory: IFormFieldFactory, _ruleEngine: RuleEngine, _eventQueue?: EventQueue, logLevel?: LogLevel);
15
+ get logger(): Logger;
16
+ private dataRefRegex;
17
+ get metaData(): FormMetaData;
18
+ get action(): string | undefined;
19
+ get lang(): string;
20
+ importData(dataModel: any): void;
21
+ exportData(): any;
22
+ setFocus(field: BaseModel): void;
23
+ getState(): {
24
+ description?: string | undefined;
25
+ } & import("./types/Json.js").RulesJson & {
26
+ enumNames?: string[] | undefined;
27
+ enum?: any[] | undefined;
28
+ } & {
29
+ accept?: string[] | undefined;
30
+ enforceEnum?: boolean | undefined;
31
+ exclusiveMinimum?: number | undefined;
32
+ exclusiveMaximum?: number | undefined;
33
+ format?: string | undefined;
34
+ maxFileSize?: string | number | undefined;
35
+ maxLength?: number | undefined;
36
+ maximum?: number | undefined;
37
+ maxItems?: number | undefined;
38
+ minOccur?: number | undefined;
39
+ maxOccur?: number | undefined;
40
+ minLength?: number | undefined;
41
+ minimum?: number | undefined;
42
+ minItems?: number | undefined;
43
+ pattern?: string | undefined;
44
+ required?: boolean | undefined;
45
+ step?: number | undefined;
46
+ type?: string | undefined;
47
+ validationExpression?: string | undefined;
48
+ uniqueItems?: boolean | undefined;
49
+ } & {
50
+ dataRef?: string | null | undefined;
51
+ ':type'?: string | undefined;
52
+ label?: import("./types/Json.js").Label | undefined;
53
+ enabled?: boolean | undefined;
54
+ visible?: boolean | undefined;
55
+ name?: string | undefined;
56
+ constraintMessages?: import("./types/Json.js").ConstraintsMessages | undefined;
57
+ fieldType?: string | undefined;
58
+ errorMessage?: string | undefined;
59
+ properties?: {
60
+ [key: string]: any;
61
+ } | undefined;
62
+ repeatable?: boolean | undefined;
63
+ screenReaderText?: string | undefined;
64
+ tooltip?: string | undefined;
65
+ altText?: string | undefined;
66
+ viewType?: string | undefined;
67
+ } & {
68
+ items: (import("./types/Json.js").FieldJson | import("./types/Json.js").ContainerJson)[];
69
+ initialItems?: number | undefined;
70
+ activeChild?: string | undefined;
71
+ } & {
72
+ metadata?: import("./types/Json.js").MetaDataJson | undefined;
73
+ data?: any;
74
+ title?: string | undefined;
75
+ action?: string | undefined;
76
+ adaptiveForm?: string | undefined;
77
+ lang?: string | undefined;
78
+ } & {
79
+ items: any[];
80
+ properties: {
81
+ [key: string]: any;
82
+ };
83
+ index: number;
84
+ parent: undefined;
85
+ qualifiedName: any;
86
+ events: {};
87
+ rules: {};
88
+ repeatable: boolean | undefined;
89
+ ':type': string;
90
+ id: string;
91
+ };
92
+ get type(): string;
93
+ isTransparent(): boolean;
94
+ get form(): FormModel;
95
+ get ruleEngine(): RuleEngine;
96
+ getUniqueId(): string;
97
+ fieldAdded(field: FieldModel | FieldsetModel): void;
98
+ visit(callBack: (field: FieldModel | FieldsetModel) => void): void;
99
+ traverseChild(container: Container<any>, callBack: (field: FieldModel | FieldsetModel) => void): void;
100
+ validate(): import("./types/Model.js").ValidationError[];
101
+ isValid(): boolean;
102
+ dispatch(action: Action): void;
103
+ executeAction(action: Action): void;
104
+ submit(action: Action, context: any): void;
105
+ getElement(id: string): FieldModel | FieldsetModel | this;
106
+ get qualifiedName(): string;
107
+ getEventQueue(): EventQueue;
108
+ get name(): string;
109
+ get value(): null;
110
+ get id(): string;
111
+ get title(): string;
112
+ }
113
+ export default Form;
@@ -0,0 +1,176 @@
1
+ import Container from './Container.js';
2
+ import FormMetaData from './FormMetaData.js';
3
+ import EventQueue from './controller/EventQueue.js';
4
+ import { Logger } from './controller/Logger.js';
5
+ import { getAttachments, IdGenerator } from './utils/FormUtils.js';
6
+ import DataGroup from './data/DataGroup.js';
7
+ import { submit } from './rules/FunctionRuntime.js';
8
+ import { ExecuteRule, FieldChanged, FormLoad, Initialize, ValidationComplete } from './controller/Events.js';
9
+ class Form extends Container {
10
+ _ruleEngine;
11
+ _eventQueue;
12
+ _fields = {};
13
+ _ids;
14
+ _invalidFields = [];
15
+ _logger;
16
+ constructor(n, fieldFactory, _ruleEngine, _eventQueue = new EventQueue(), logLevel = 'off') {
17
+ super(n, { fieldFactory: fieldFactory });
18
+ this._ruleEngine = _ruleEngine;
19
+ this._eventQueue = _eventQueue;
20
+ this._logger = new Logger(logLevel);
21
+ this.queueEvent(new Initialize());
22
+ this.queueEvent(new ExecuteRule());
23
+ this._ids = IdGenerator();
24
+ this._bindToDataModel(new DataGroup('$form', {}));
25
+ this._initialize();
26
+ this.queueEvent(new FormLoad());
27
+ }
28
+ get logger() {
29
+ return this._logger;
30
+ }
31
+ dataRefRegex = /("[^"]+?"|[^.]+?)(?:\.|$)/g;
32
+ get metaData() {
33
+ const metaData = this._jsonModel.metadata || {};
34
+ return new FormMetaData(metaData);
35
+ }
36
+ get action() {
37
+ return this._jsonModel.action;
38
+ }
39
+ get lang() {
40
+ return this._jsonModel.lang || 'en';
41
+ }
42
+ importData(dataModel) {
43
+ this._bindToDataModel(new DataGroup('$form', dataModel));
44
+ this.syncDataAndFormModel(this.getDataNode());
45
+ this._eventQueue.runPendingQueue();
46
+ }
47
+ exportData() {
48
+ return this.getDataNode()?.$value;
49
+ }
50
+ setFocus(field) {
51
+ const parent = field.parent;
52
+ const currentField = field;
53
+ while (parent != null && parent.activeChild != currentField) {
54
+ parent.activeChild = currentField;
55
+ }
56
+ }
57
+ getState() {
58
+ const self = this;
59
+ const res = super.getState();
60
+ res.id = '$form';
61
+ Object.defineProperty(res, 'data', {
62
+ get: function () {
63
+ return self.exportData();
64
+ }
65
+ });
66
+ Object.defineProperty(res, 'attachments', {
67
+ get: function () {
68
+ return getAttachments(self);
69
+ }
70
+ });
71
+ return res;
72
+ }
73
+ get type() {
74
+ return 'object';
75
+ }
76
+ isTransparent() {
77
+ return false;
78
+ }
79
+ get form() {
80
+ return this;
81
+ }
82
+ get ruleEngine() {
83
+ return this._ruleEngine;
84
+ }
85
+ getUniqueId() {
86
+ if (this._ids == null) {
87
+ return '';
88
+ }
89
+ return this._ids.next().value;
90
+ }
91
+ fieldAdded(field) {
92
+ this._fields[field.id] = field;
93
+ field.subscribe((action) => {
94
+ if (this._invalidFields.indexOf(action.target.id) === -1) {
95
+ this._invalidFields.push(action.target.id);
96
+ }
97
+ }, 'invalid');
98
+ field.subscribe((action) => {
99
+ const index = this._invalidFields.indexOf(action.target.id);
100
+ if (index > -1) {
101
+ this._invalidFields.splice(index, 1);
102
+ }
103
+ }, 'valid');
104
+ field.subscribe((action) => {
105
+ const field = action.target.getState();
106
+ if (field) {
107
+ const fieldChangedAction = new FieldChanged(action.payload.changes, field);
108
+ this.dispatch(fieldChangedAction);
109
+ }
110
+ });
111
+ }
112
+ visit(callBack) {
113
+ this.traverseChild(this, callBack);
114
+ }
115
+ traverseChild(container, callBack) {
116
+ container.items.forEach((field) => {
117
+ if (field.isContainer) {
118
+ this.traverseChild(field, callBack);
119
+ }
120
+ callBack(field);
121
+ });
122
+ }
123
+ validate() {
124
+ const validationErrors = super.validate();
125
+ this.dispatch(new ValidationComplete(validationErrors));
126
+ return validationErrors;
127
+ }
128
+ isValid() {
129
+ return this._invalidFields.length === 0;
130
+ }
131
+ dispatch(action) {
132
+ if (action.type === 'submit') {
133
+ super.queueEvent(action);
134
+ this._eventQueue.runPendingQueue();
135
+ }
136
+ else {
137
+ super.dispatch(action);
138
+ }
139
+ }
140
+ executeAction(action) {
141
+ if ((action.type !== 'submit') || this._invalidFields.length === 0) {
142
+ super.executeAction(action);
143
+ }
144
+ }
145
+ submit(action, context) {
146
+ if (this.validate().length === 0) {
147
+ const payload = action?.payload || {};
148
+ submit(context, payload?.success, payload?.error, payload?.submit_as, payload?.data);
149
+ }
150
+ }
151
+ getElement(id) {
152
+ if (id == this.id) {
153
+ return this;
154
+ }
155
+ return this._fields[id];
156
+ }
157
+ get qualifiedName() {
158
+ return '$form';
159
+ }
160
+ getEventQueue() {
161
+ return this._eventQueue;
162
+ }
163
+ get name() {
164
+ return '$form';
165
+ }
166
+ get value() {
167
+ return null;
168
+ }
169
+ get id() {
170
+ return '$form';
171
+ }
172
+ get title() {
173
+ return this._jsonModel.title || '';
174
+ }
175
+ }
176
+ export default Form;
@@ -0,0 +1,13 @@
1
+ import { FormModel } from './types/index.js';
2
+ import { LogLevel } from './controller/Logger.js';
3
+ import { CustomFunction, FunctionDefinition } from './rules/FunctionRuntime.js';
4
+ export declare const createFormInstance: (formModel: any, callback?: ((f: FormModel) => any) | undefined, logLevel?: LogLevel, fModel?: any) => FormModel;
5
+ export declare const validateFormInstance: (formModel: any, data: any) => boolean;
6
+ export declare const validateFormData: (formModel: any, data: any) => {
7
+ messages: any[];
8
+ valid: boolean;
9
+ };
10
+ export declare const fetchForm: (url: string, headers?: any) => Promise<string>;
11
+ export declare const registerFunctions: (functions: {
12
+ [key: string]: Function | FunctionDefinition;
13
+ }) => void;
@@ -0,0 +1,81 @@
1
+ import Form from './Form.js';
2
+ import { jsonString } from './utils/JsonUtils.js';
3
+ import { request } from './utils/Fetch.js';
4
+ import RuleEngine from './rules/RuleEngine.js';
5
+ import EventQueue from './controller/EventQueue.js';
6
+ import { Logger } from './controller/Logger.js';
7
+ import { FormFieldFactory } from './utils/FormCreationUtils.js';
8
+ import { FunctionRuntime } from './rules/FunctionRuntime.js';
9
+ export const createFormInstance = (formModel, callback, logLevel = 'error', fModel = undefined) => {
10
+ try {
11
+ let f = fModel;
12
+ if (f == null) {
13
+ f = new Form({ ...formModel }, FormFieldFactory, new RuleEngine(), new EventQueue(new Logger(logLevel)), logLevel);
14
+ }
15
+ const formData = formModel?.data;
16
+ if (formData) {
17
+ f.importData(formData);
18
+ }
19
+ if (typeof callback === 'function') {
20
+ callback(f);
21
+ }
22
+ f.getEventQueue().runPendingQueue();
23
+ return f;
24
+ }
25
+ catch (e) {
26
+ console.error(`Unable to create an instance of the Form ${e}`);
27
+ throw new Error(e);
28
+ }
29
+ };
30
+ export const validateFormInstance = (formModel, data) => {
31
+ try {
32
+ const f = new Form({ ...formModel }, FormFieldFactory, new RuleEngine());
33
+ if (data) {
34
+ f.importData(data);
35
+ }
36
+ return f.validate().length === 0;
37
+ }
38
+ catch (e) {
39
+ throw new Error(e);
40
+ }
41
+ };
42
+ export const validateFormData = (formModel, data) => {
43
+ try {
44
+ const f = new Form({ ...formModel }, FormFieldFactory, new RuleEngine());
45
+ if (data) {
46
+ f.importData(data);
47
+ }
48
+ const res = f.validate();
49
+ return {
50
+ messages: res,
51
+ valid: res.length === 0
52
+ };
53
+ }
54
+ catch (e) {
55
+ throw new Error(e);
56
+ }
57
+ };
58
+ export const fetchForm = (url, headers = {}) => {
59
+ const headerObj = new Headers();
60
+ Object.entries(headers).forEach(([key, value]) => {
61
+ headerObj.append(key, value);
62
+ });
63
+ return new Promise((resolve, reject) => {
64
+ request(`${url}.model.json`, null, { headers }).then((response) => {
65
+ if (response.status !== 200) {
66
+ reject('Not Found');
67
+ }
68
+ else {
69
+ let formObj = response.body;
70
+ if ('model' in formObj) {
71
+ const { model } = formObj;
72
+ formObj = model;
73
+ }
74
+ resolve(jsonString(formObj));
75
+ }
76
+ });
77
+ });
78
+ };
79
+ export const registerFunctions = (functions) => {
80
+ FunctionRuntime.registerFunctions(functions);
81
+ };
@@ -0,0 +1,7 @@
1
+ import { FormMetaDataModel, MetaDataJson } from './types/index.js';
2
+ import Node from './Node.js';
3
+ declare class FormMetaData extends Node<MetaDataJson> implements FormMetaDataModel {
4
+ get version(): string;
5
+ get grammar(): string;
6
+ }
7
+ export default FormMetaData;
@@ -0,0 +1,10 @@
1
+ import Node from './Node.js';
2
+ class FormMetaData extends Node {
3
+ get version() {
4
+ return this.getP('version', '');
5
+ }
6
+ get grammar() {
7
+ return this.getP('grammar', '');
8
+ }
9
+ }
10
+ export default FormMetaData;
@@ -0,0 +1,9 @@
1
+ import { Action, FieldsetModel } from './types/index.js';
2
+ import { Fieldset } from './Fieldset.js';
3
+ export declare class InstanceManager extends Fieldset implements FieldsetModel {
4
+ get maxOccur(): number;
5
+ set maxOccur(m: number);
6
+ get minOccur(): number;
7
+ addInstance(action: Action): void;
8
+ removeInstance(action: Action): void;
9
+ }
@@ -0,0 +1,31 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Fieldset } from './Fieldset.js';
8
+ import { dependencyTracked } from './BaseNode.js';
9
+ export class InstanceManager extends Fieldset {
10
+ get maxOccur() {
11
+ return this._jsonModel.maxItems;
12
+ }
13
+ set maxOccur(m) {
14
+ this.maxItems = m;
15
+ }
16
+ get minOccur() {
17
+ return this.minItems;
18
+ }
19
+ addInstance(action) {
20
+ return this.addItem(action);
21
+ }
22
+ removeInstance(action) {
23
+ return this.removeItem(action);
24
+ }
25
+ }
26
+ __decorate([
27
+ dependencyTracked()
28
+ ], InstanceManager.prototype, "maxOccur", null);
29
+ __decorate([
30
+ dependencyTracked()
31
+ ], InstanceManager.prototype, "minOccur", null);