@aemforms/af-core 0.22.19 → 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 (122) 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} +10 -69
  5. package/lib/{BaseNode.js → esm/BaseNode.js} +84 -184
  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/{Container.d.ts → esm/Container.d.ts} +12 -59
  11. package/lib/{Container.js → esm/Container.js} +54 -116
  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} +39 -104
  15. package/lib/{Field.js → esm/Field.js} +142 -208
  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/{Form.d.ts → esm/Form.d.ts} +19 -85
  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} +11 -54
  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 +15 -34
  42. package/lib/{data → esm/data}/DataValue.d.ts +1 -7
  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 +41 -117
  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 +3 -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 +0 -6
  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/DateField.js +0 -35
  81. package/lib/Fieldset.d.ts +0 -24
  82. package/lib/Fieldset.js +0 -74
  83. package/lib/FileObject.js +0 -39
  84. package/lib/FileUpload.js +0 -155
  85. package/lib/Form.js +0 -252
  86. package/lib/FormInstance.d.ts +0 -38
  87. package/lib/FormInstance.js +0 -127
  88. package/lib/FormMetaData.d.ts +0 -11
  89. package/lib/FormMetaData.js +0 -28
  90. package/lib/InstanceManager.d.ts +0 -16
  91. package/lib/InstanceManager.js +0 -53
  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 -255
  96. package/lib/controller/Controller.js +0 -328
  97. package/lib/controller/index.d.ts +0 -1
  98. package/lib/controller/index.js +0 -24
  99. package/lib/data/DataValue.js +0 -56
  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 -393
  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 -87
  111. package/lib/utils/FormCreationUtils.d.ts +0 -11
  112. package/lib/utils/FormCreationUtils.js +0 -83
  113. package/lib/utils/FormUtils.d.ts +0 -47
  114. package/lib/utils/FormUtils.js +0 -257
  115. package/lib/utils/JsonUtils.d.ts +0 -63
  116. package/lib/utils/JsonUtils.js +0 -157
  117. package/lib/utils/LogUtils.js +0 -17
  118. package/lib/utils/SchemaUtils.d.ts +0 -16
  119. package/lib/utils/SchemaUtils.js +0 -92
  120. package/lib/utils/TranslationUtils.d.ts +0 -41
  121. package/lib/utils/TranslationUtils.js +0 -185
  122. package/lib/utils/ValidationUtils.d.ts +0 -170
@@ -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;
@@ -1,57 +1,28 @@
1
- import Container from './Container';
2
- import { Action, BaseModel, FieldJson, FieldModel, FieldsetJson, FieldsetModel, FormJson, FormModel } from './types';
3
- import FormMetaData from './FormMetaData';
4
- import EventQueue from './controller/EventQueue';
5
- import { Logger, LogLevel } from './controller/Logger';
6
- import RuleEngine from './rules/RuleEngine';
7
- /**
8
- * Defines `form model` which implements {@link FormModel | form model}
9
- */
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';
10
7
  declare class Form extends Container<FormJson> implements FormModel {
11
8
  private _ruleEngine;
12
9
  private _eventQueue;
13
- /**
14
- * @private
15
- */
16
10
  private _fields;
17
- /**
18
- * @private
19
- */
20
11
  _ids: Generator<string, void, string>;
21
- /**
22
- * @private
23
- */
24
12
  private _invalidFields;
25
13
  private _logger;
26
- /**
27
- * @param n
28
- * @param _ruleEngine
29
- * @param _eventQueue
30
- * @param logLevel
31
- * @private
32
- */
33
- constructor(n: FormJson, _ruleEngine: RuleEngine, _eventQueue?: EventQueue, logLevel?: LogLevel);
14
+ constructor(n: FormJson, fieldFactory: IFormFieldFactory, _ruleEngine: RuleEngine, _eventQueue?: EventQueue, logLevel?: LogLevel);
34
15
  get logger(): Logger;
35
16
  private dataRefRegex;
36
17
  get metaData(): FormMetaData;
37
18
  get action(): string | undefined;
38
- protected _createChild(child: FieldsetJson | FieldJson): FieldModel | FieldsetModel;
19
+ get lang(): string;
39
20
  importData(dataModel: any): void;
40
21
  exportData(): any;
41
22
  setFocus(field: BaseModel): void;
42
- /**
43
- * Returns the current state of the form
44
- *
45
- * To access the form data and attachments, one needs to use the `data` and `attachments` property.
46
- * For example,
47
- * ```
48
- * const data = form.getState().data
49
- * const attachments = form.getState().attachments
50
- * ```
51
- */
52
23
  getState(): {
53
24
  description?: string | undefined;
54
- } & import("./types").RulesJson & {
25
+ } & import("./types/Json.js").RulesJson & {
55
26
  enumNames?: string[] | undefined;
56
27
  enum?: any[] | undefined;
57
28
  } & {
@@ -63,9 +34,7 @@ declare class Form extends Container<FormJson> implements FormModel {
63
34
  maxFileSize?: string | number | undefined;
64
35
  maxLength?: number | undefined;
65
36
  maximum?: number | undefined;
66
- maxItems?: number | undefined; /**
67
- * @private
68
- */
37
+ maxItems?: number | undefined;
69
38
  minOccur?: number | undefined;
70
39
  maxOccur?: number | undefined;
71
40
  minLength?: number | undefined;
@@ -80,11 +49,11 @@ declare class Form extends Container<FormJson> implements FormModel {
80
49
  } & {
81
50
  dataRef?: string | null | undefined;
82
51
  ':type'?: string | undefined;
83
- label?: import("./types").Label | undefined;
52
+ label?: import("./types/Json.js").Label | undefined;
84
53
  enabled?: boolean | undefined;
85
54
  visible?: boolean | undefined;
86
55
  name?: string | undefined;
87
- constraintMessages?: import("./types").ConstraintsMessages | undefined;
56
+ constraintMessages?: import("./types/Json.js").ConstraintsMessages | undefined;
88
57
  fieldType?: string | undefined;
89
58
  errorMessage?: string | undefined;
90
59
  properties?: {
@@ -94,19 +63,18 @@ declare class Form extends Container<FormJson> implements FormModel {
94
63
  screenReaderText?: string | undefined;
95
64
  tooltip?: string | undefined;
96
65
  altText?: string | undefined;
66
+ viewType?: string | undefined;
97
67
  } & {
98
- items: (FieldJson | import("./types").ContainerJson)[];
68
+ items: (import("./types/Json.js").FieldJson | import("./types/Json.js").ContainerJson)[];
99
69
  initialItems?: number | undefined;
100
70
  activeChild?: string | undefined;
101
71
  } & {
102
- metadata?: import("./types").MetaDataJson | undefined;
72
+ metadata?: import("./types/Json.js").MetaDataJson | undefined;
103
73
  data?: any;
104
74
  title?: string | undefined;
105
75
  action?: string | undefined;
106
- adaptiveForm?: string | undefined; /**
107
- * @param field
108
- * @private
109
- */
76
+ adaptiveForm?: string | undefined;
77
+ lang?: string | undefined;
110
78
  } & {
111
79
  items: any[];
112
80
  properties: {
@@ -117,6 +85,7 @@ declare class Form extends Container<FormJson> implements FormModel {
117
85
  qualifiedName: any;
118
86
  events: {};
119
87
  rules: {};
88
+ repeatable: boolean | undefined;
120
89
  ':type': string;
121
90
  id: string;
122
91
  };
@@ -125,51 +94,16 @@ declare class Form extends Container<FormJson> implements FormModel {
125
94
  get form(): FormModel;
126
95
  get ruleEngine(): RuleEngine;
127
96
  getUniqueId(): string;
128
- /**
129
- * @param field
130
- * @private
131
- */
132
97
  fieldAdded(field: FieldModel | FieldsetModel): void;
133
- /**
134
- * visits each element in the form
135
- * @param callBack a function which is invoked on each form element
136
- * (including container type elements) visited
137
- */
138
98
  visit(callBack: (field: FieldModel | FieldsetModel) => void): void;
139
- /**
140
- *
141
- * @param container
142
- * @param callBack
143
- * @private
144
- */
145
99
  traverseChild(container: Container<any>, callBack: (field: FieldModel | FieldsetModel) => void): void;
146
- validate(): import("./types").ValidationError[];
147
- /**
148
- * Checks if the given form is valid or not
149
- * @returns `true`, if form is valid, `false` otherwise
150
- */
100
+ validate(): import("./types/Model.js").ValidationError[];
151
101
  isValid(): boolean;
152
- /**
153
- * @param field
154
- * @private
155
- */
156
102
  dispatch(action: Action): void;
157
- /**
158
- * @param action
159
- * @private
160
- */
161
103
  executeAction(action: Action): void;
162
- /**
163
- * @param action
164
- * @param context
165
- * @private
166
- */
167
104
  submit(action: Action, context: any): void;
168
105
  getElement(id: string): FieldModel | FieldsetModel | this;
169
106
  get qualifiedName(): string;
170
- /**
171
- * @private
172
- */
173
107
  getEventQueue(): EventQueue;
174
108
  get name(): string;
175
109
  get value(): null;
@@ -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;