tuain-ng-forms-lib 14.0.0 → 14.0.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.
Files changed (99) hide show
  1. package/.browserslistrc +16 -0
  2. package/.yarn/cache/nanoid-npm-4.0.0-924f5c6312-7d5946df5c.zip +0 -0
  3. package/.yarn/cache/tslib-npm-2.4.0-9cb6dc5030-8c4aa6a3c5.zip +0 -0
  4. package/.yarn/cache/yn-npm-5.0.0-b001dab23c-f0ec7710d3.zip +0 -0
  5. package/.yarn/install-state.gz +0 -0
  6. package/karma.conf.js +44 -0
  7. package/ng-package.json +11 -0
  8. package/package.json +2 -22
  9. package/src/lib/classes/forms/action.ts +55 -0
  10. package/src/lib/classes/forms/element.ts +98 -0
  11. package/src/lib/classes/forms/field.ts +408 -0
  12. package/src/lib/classes/forms/form.constants.ts +28 -0
  13. package/src/lib/classes/forms/form.ts +495 -0
  14. package/src/lib/classes/forms/section.ts +154 -0
  15. package/src/lib/classes/forms/subsection.ts +91 -0
  16. package/src/lib/classes/forms/table/action.ts +41 -0
  17. package/src/lib/classes/forms/table/column.ts +91 -0
  18. package/src/lib/classes/forms/table/row-data.ts +118 -0
  19. package/src/lib/classes/forms/table/table.ts +438 -0
  20. package/src/lib/components/elements/action.component.ts +53 -0
  21. package/src/lib/components/elements/field.component.ts +118 -0
  22. package/src/lib/components/elements/layout/element.component.ts +28 -0
  23. package/src/lib/components/elements/layout/form-error.component.ts +11 -0
  24. package/src/lib/components/elements/layout/form-header.component.ts +17 -0
  25. package/src/lib/components/elements/layout/section.component.ts +16 -0
  26. package/src/lib/components/elements/layout/sub-section.component.ts +17 -0
  27. package/src/lib/components/elements/tables/table-record-action.component.ts +37 -0
  28. package/src/lib/components/elements/tables/table-record-field.component.ts +19 -0
  29. package/src/lib/components/elements/tables/table.component.ts +102 -0
  30. package/src/lib/components/forms/basic-form.ts +1496 -0
  31. package/src/lib/services/event-manager.service.ts +21 -0
  32. package/src/lib/services/file-manager.service.ts +6 -0
  33. package/src/lib/services/form-manager.service.ts +89 -0
  34. package/src/lib/services/icon-dictionary.service.ts +159 -0
  35. package/src/lib/tuain-ng-forms-lib.module.ts +40 -0
  36. package/{public-api.d.ts → src/public-api.ts} +5 -0
  37. package/src/test.ts +27 -0
  38. package/tsconfig.lib.json +15 -0
  39. package/tsconfig.lib.prod.json +10 -0
  40. package/tsconfig.spec.json +17 -0
  41. package/esm2020/lib/classes/forms/action.mjs +0 -35
  42. package/esm2020/lib/classes/forms/element.mjs +0 -76
  43. package/esm2020/lib/classes/forms/field.mjs +0 -321
  44. package/esm2020/lib/classes/forms/form.constants.mjs +0 -26
  45. package/esm2020/lib/classes/forms/form.mjs +0 -422
  46. package/esm2020/lib/classes/forms/section.mjs +0 -132
  47. package/esm2020/lib/classes/forms/subsection.mjs +0 -70
  48. package/esm2020/lib/classes/forms/table/action.mjs +0 -22
  49. package/esm2020/lib/classes/forms/table/column.mjs +0 -61
  50. package/esm2020/lib/classes/forms/table/row-data.mjs +0 -111
  51. package/esm2020/lib/classes/forms/table/table.mjs +0 -372
  52. package/esm2020/lib/components/elements/action.component.mjs +0 -58
  53. package/esm2020/lib/components/elements/field.component.mjs +0 -90
  54. package/esm2020/lib/components/elements/layout/element.component.mjs +0 -31
  55. package/esm2020/lib/components/elements/layout/form-error.component.mjs +0 -20
  56. package/esm2020/lib/components/elements/layout/form-header.component.mjs +0 -30
  57. package/esm2020/lib/components/elements/layout/section.component.mjs +0 -22
  58. package/esm2020/lib/components/elements/layout/sub-section.component.mjs +0 -24
  59. package/esm2020/lib/components/elements/tables/table-record-action.component.mjs +0 -40
  60. package/esm2020/lib/components/elements/tables/table-record-field.component.mjs +0 -25
  61. package/esm2020/lib/components/elements/tables/table.component.mjs +0 -95
  62. package/esm2020/lib/components/forms/basic-form.mjs +0 -1425
  63. package/esm2020/lib/services/event-manager.service.mjs +0 -18
  64. package/esm2020/lib/services/file-manager.service.mjs +0 -6
  65. package/esm2020/lib/services/form-manager.service.mjs +0 -80
  66. package/esm2020/lib/tuain-ng-forms-lib.module.mjs +0 -71
  67. package/esm2020/public-api.mjs +0 -19
  68. package/esm2020/tuain-ng-forms-lib.mjs +0 -5
  69. package/fesm2015/tuain-ng-forms-lib.mjs +0 -3803
  70. package/fesm2015/tuain-ng-forms-lib.mjs.map +0 -1
  71. package/fesm2020/tuain-ng-forms-lib.mjs +0 -3634
  72. package/fesm2020/tuain-ng-forms-lib.mjs.map +0 -1
  73. package/lib/classes/forms/action.d.ts +0 -22
  74. package/lib/classes/forms/element.d.ts +0 -41
  75. package/lib/classes/forms/field.d.ts +0 -109
  76. package/lib/classes/forms/form.constants.d.ts +0 -25
  77. package/lib/classes/forms/form.d.ts +0 -134
  78. package/lib/classes/forms/section.d.ts +0 -39
  79. package/lib/classes/forms/subsection.d.ts +0 -26
  80. package/lib/classes/forms/table/action.d.ts +0 -20
  81. package/lib/classes/forms/table/column.d.ts +0 -33
  82. package/lib/classes/forms/table/row-data.d.ts +0 -14
  83. package/lib/classes/forms/table/table.d.ts +0 -100
  84. package/lib/components/elements/action.component.d.ts +0 -21
  85. package/lib/components/elements/field.component.d.ts +0 -43
  86. package/lib/components/elements/layout/element.component.d.ts +0 -14
  87. package/lib/components/elements/layout/form-error.component.d.ts +0 -8
  88. package/lib/components/elements/layout/form-header.component.d.ts +0 -12
  89. package/lib/components/elements/layout/section.component.d.ts +0 -10
  90. package/lib/components/elements/layout/sub-section.component.d.ts +0 -11
  91. package/lib/components/elements/tables/table-record-action.component.d.ts +0 -15
  92. package/lib/components/elements/tables/table-record-field.component.d.ts +0 -11
  93. package/lib/components/elements/tables/table.component.d.ts +0 -47
  94. package/lib/components/forms/basic-form.d.ts +0 -307
  95. package/lib/services/event-manager.service.d.ts +0 -9
  96. package/lib/services/file-manager.service.d.ts +0 -5
  97. package/lib/services/form-manager.service.d.ts +0 -28
  98. package/lib/tuain-ng-forms-lib.module.d.ts +0 -20
  99. package/tuain-ng-forms-lib.d.ts +0 -5
@@ -0,0 +1,28 @@
1
+ export const NO_ERROR = '00';
2
+ export const HEADER = 'HEADER';
3
+
4
+ export const elementTypes = {
5
+ action: 'ACTION',
6
+ field: 'FIELD',
7
+ table: 'TABLE',
8
+ };
9
+
10
+ export const formActions = {
11
+ tableAction: 'TABLEACTION',
12
+ getData: 'GETDATA',
13
+ getTableData: 'GETTABLEDATA',
14
+ validate: 'VALIDATE',
15
+ };
16
+
17
+ export const operators = {
18
+ G: 'G',
19
+ L: 'L',
20
+ GE: 'GE',
21
+ LE: 'LE',
22
+ EQ: 'EQ',
23
+ NEQ: 'NEQ',
24
+ HAS: 'HAS',
25
+ NOTHAS: 'NOTHAS',
26
+ BETWEEN: 'BETWEEN',
27
+ IN: 'IN',
28
+ };
@@ -0,0 +1,495 @@
1
+ import { FormAction } from './action';
2
+ import { FieldDescriptor, FieldOption } from './field';
3
+ import { RecordTable } from './table/table';
4
+ import { RecordFormSection } from './section';
5
+ import { RecordFormSubSection } from './subsection';
6
+ import { NO_ERROR, HEADER } from './form.constants';
7
+ import { TableRecordData } from './table/row-data';
8
+
9
+ const ACTIVE = 'active';
10
+ const SHOW = 'show';
11
+ const HIDE = 'hide';
12
+ const ENABLE = 'enable';
13
+ const DISABLE = 'disable';
14
+ const CLEAN = 'clean';
15
+ interface Transition {
16
+ name: string;
17
+ source: string;
18
+ destination: string;
19
+ }
20
+
21
+ interface StateFlow {
22
+ defaultState: string;
23
+ states: string[];
24
+ transitions: Transition[];
25
+ }
26
+
27
+ export interface FieldPayload {
28
+ fieldCode: string;
29
+ fieldValue: any;
30
+ editable: boolean;
31
+ visible: boolean;
32
+ required: boolean;
33
+ fieldOptions: string,
34
+ }
35
+
36
+ export interface TablePayload {
37
+ tableCode: string;
38
+ visible: boolean;
39
+ currentPage: number;
40
+ requestedPage: number;
41
+ recordsPerPage: number;
42
+ currentFilter: any,
43
+ sortingColumn: string;
44
+ sortingDirection: string;
45
+ }
46
+
47
+ interface FormPayload {
48
+ fields: FieldPayload[],
49
+ tables: TablePayload[],
50
+ }
51
+
52
+ export class FormStructureAndData {
53
+ state: string;
54
+ _name: string = '';
55
+ _formConfig: any;
56
+ private _title: string = '';
57
+ private _stateFlow: StateFlow;
58
+ private _fieldArray: FieldDescriptor[];
59
+ private _fields = {};
60
+ private _actionArray: FormAction[];
61
+ private _actions = {};
62
+ private _tableArray: RecordTable[];
63
+ private _tables = {};
64
+ private _sectionArray: RecordFormSection[];
65
+ private _sections = {};
66
+ private _immutableData = {};
67
+ private _extraInfo = {};
68
+ private _exclusiveSectionsByAttr = {};
69
+
70
+ constructor(definitionReceived, formConfig) {
71
+ this._formConfig = formConfig;
72
+ this.state = '';
73
+ this._actionArray = [];
74
+ this._fieldArray = [];
75
+ this._tableArray = [];
76
+ this._sectionArray = [];
77
+ this._stateFlow = {
78
+ defaultState: '',
79
+ states: [],
80
+ transitions: [],
81
+ };
82
+ if (!definitionReceived) { return; }
83
+ this._name = (definitionReceived.form && definitionReceived.form.formCode)
84
+ ? definitionReceived.form.formCode : this._name;
85
+ this._title = (definitionReceived.form && definitionReceived.form.formTitle)
86
+ ? definitionReceived.form.formTitle : '';
87
+ this.setStateFlow(definitionReceived?.states, definitionReceived?.transitions,
88
+ definitionReceived?.defaultState);
89
+ this.immutableData = definitionReceived.immutableData;
90
+ this.extraInfo = definitionReceived.extraInfo;
91
+
92
+ if (definitionReceived.actions) {
93
+ const formActions = definitionReceived.actions.map(objDef => {
94
+ let visibleStates = objDef.visibleStates;
95
+ let enabledStates = objDef.enabledStates;
96
+ if (!visibleStates) {
97
+ visibleStates = (objDef.actionModes || '').split(',')
98
+ .map(state => state.trim())
99
+ .filter(state => state.length > 0) || [];
100
+ enabledStates = [...visibleStates];
101
+ }
102
+ return { ...objDef, visibleStates, enabledStates };
103
+ });
104
+ for (const actionReceived of formActions) {
105
+ const globalAction = new FormAction(actionReceived, this._formConfig);
106
+ const globalActionCode = globalAction.actionCode;
107
+ if (globalActionCode) {
108
+ this._actionArray.push(globalAction);
109
+ this._actions[globalActionCode] = globalAction;
110
+ }
111
+ }
112
+ }
113
+
114
+ if (definitionReceived.fields) {
115
+ const formFields = definitionReceived.fields.map(objDef => {
116
+ let visibleStates = objDef.visibleStates;
117
+ let enabledStates = objDef.enabledStates || objDef.editableStates;
118
+ if (!visibleStates) {
119
+ visibleStates = (objDef.fieldModes || '').split(',')
120
+ .map(state => state.trim())
121
+ .filter(state => state.length > 0) || [];
122
+ enabledStates = [...visibleStates];
123
+ }
124
+ return { ...objDef, visibleStates, enabledStates };
125
+ });
126
+ for (const fieldReceived of formFields) {
127
+ const fieldToAdd = new FieldDescriptor(fieldReceived, this._formConfig);
128
+ const fieldCode = fieldToAdd.fieldCode;
129
+ if (fieldCode) {
130
+ this._fieldArray.push(fieldToAdd);
131
+ this._fields[fieldCode] = fieldToAdd;
132
+ }
133
+ }
134
+ }
135
+
136
+ if (definitionReceived.tables) {
137
+ const tables = definitionReceived.tables.map(objDef => {
138
+ let visibleStates = objDef.visibleStates;
139
+ let enabledStates = objDef.enabledStates || objDef.editableStates;
140
+ if (!visibleStates) {
141
+ visibleStates = (objDef.tableModes || '').split(',')
142
+ .map(state => state.trim())
143
+ .filter(state => state.length > 0) || [];
144
+ enabledStates = [...visibleStates];
145
+ }
146
+ if (!visibleStates || visibleStates.length === 0) {
147
+ visibleStates = this._formConfig.defaultStateFlow.states;
148
+ }
149
+ return { ...objDef, visibleStates, enabledStates };
150
+ });
151
+ for (const tableReceived of tables) {
152
+ const tableToAdd = new RecordTable(tableReceived, this._formConfig);
153
+ const tableCode = tableToAdd.tableCode;
154
+ if (tableCode) {
155
+ this._tableArray.push(tableToAdd);
156
+ this._tables[tableCode] = tableToAdd;
157
+ }
158
+ }
159
+ }
160
+
161
+ if (definitionReceived.sections) {
162
+ const formSections = definitionReceived.sections.map(objDef => {
163
+ let visibleStates = objDef.visibleStates;
164
+ if (!visibleStates) {
165
+ visibleStates = (objDef.sectionModes || '').split(',')
166
+ .map(state => state.trim())
167
+ .filter(state => state.length > 0) || [];
168
+ }
169
+ return { ...objDef, visibleStates };
170
+ });
171
+ for (const sectionReceived of formSections) {
172
+ const sectionToAdd = new RecordFormSection(sectionReceived, this);
173
+ const sectionCode = sectionToAdd.sectionCode;
174
+ if (sectionCode) {
175
+ this._sectionArray.push(sectionToAdd);
176
+ this._sections[sectionCode] = sectionToAdd;
177
+ }
178
+ }
179
+ }
180
+ }
181
+
182
+ getTitle(): string { return this._title; }
183
+ setTitle(title: string): void { this._title = title; }
184
+ get name(): string { return this._name; }
185
+ set name(name) { this._name = name; }
186
+
187
+ // Estados
188
+ get defaultState(): string { return this._stateFlow.defaultState; }
189
+ supportState(state: string): boolean { return this._stateFlow.states?.includes(state); }
190
+ supportMode(state: string): boolean { return this.supportState(state); }
191
+ get states(): any[] { return this._stateFlow.states; }
192
+
193
+ getCurrentState(): string { return this.state; }
194
+
195
+ changeState(newState: string): boolean {
196
+ if (!newState || !this.supportState(newState)) {
197
+ return false;
198
+ }
199
+ if (!this.state) {
200
+ this.state = newState;
201
+ } else {
202
+ const transitionToChange = this._stateFlow.transitions.find(trns =>
203
+ trns.source === this.state && trns.destination === newState);
204
+ if (transitionToChange) {
205
+ this.state = newState;
206
+ }
207
+ }
208
+ return (this.state === newState);
209
+ }
210
+
211
+ setStateFlow(states?, transitions?, defaultState?: string): void {
212
+ if (!states || !transitions) {
213
+ this._stateFlow = JSON.parse(JSON.stringify(this._formConfig.defaultStateFlow));
214
+ return;
215
+ }
216
+ this._stateFlow.states = states;
217
+ this._stateFlow.defaultState = defaultState || this._stateFlow.states[0];
218
+ this._stateFlow.transitions = transitions.map(transition => {
219
+ const name = transition.name;
220
+ const source = (this._stateFlow.states.includes(transition.source)) ? transition.source : '';
221
+ const destination = (this._stateFlow.states.includes(transition.destination)) ? transition.destination : '';
222
+ return { name, source, destination };
223
+ }).filter(item => item.name && item.source && item.destination);
224
+ }
225
+
226
+ // immutable Data
227
+ getImmutableElement(name: string): any { return this._immutableData?.[name]?.value ?? null; }
228
+ set immutableData(immutableData) { Object.assign(this._immutableData, immutableData); }
229
+ get immutableData(): any { return JSON.parse(JSON.stringify(this._immutableData)); }
230
+
231
+ // extra Info
232
+ getExtraInfo(name: string): any { return this._extraInfo?.[name]?.value ?? null; }
233
+ set extraInfo(extraInfo) { Object.assign(this._extraInfo, extraInfo); }
234
+ get extraInfo(): any { return JSON.parse(JSON.stringify(this._extraInfo)); }
235
+
236
+ // Fields
237
+ get fields(): any { return this._fields; }
238
+ get fieldNames(): string[] { return this.getFieldNames(); }
239
+ getFields(): FieldDescriptor[] { return this._fieldArray; }
240
+ getFieldNames(): string[] { return this._fieldArray.map(field => field.fieldCode); }
241
+ getField(code: string): FieldDescriptor { return (code && this._fields?.[code]) ? this._fields[code] : null; }
242
+ enableField(code: string): void { this.getField(code)?.enable(); }
243
+ disableField(code: string): void { this.getField(code)?.disable(); }
244
+ getFieldValue(code: string): any { return this.getField(code)?.getValue(); }
245
+ getFieldOptionText(code: string): any { return this.getField(code)?.getOptionText(); }
246
+ getFieldOptions(code: string): FieldOption[] { return this.getField(code)?.getFieldOptions() ?? null; }
247
+ setFieldValue(code: string, value: any): any { this.getField(code)?.setValue(value); }
248
+ setFieldError(code: string, errorCode: string, message: string, type: string = 'error'): any { this.getField(code)?.setError(errorCode, message, type); }
249
+ setFieldIntrinsicErrorMessage(code: string, message: string): any { this.getField(code)?.setIntrinsicErrorMessage(message); }
250
+ setFieldRequired(code: string, required: boolean): any { this.getField(code)?.setRequired(required); }
251
+ setFieldErrorMessage(code: string, message: string): any { this.getField(code)?.setErrorMessage(message); }
252
+
253
+ setFieldOptions(code: string, optionsArray: any[], idAttribute: string, nameAttribute: string): any {
254
+ const field = this.getField(code);
255
+ if (field && optionsArray?.length > 0) {
256
+ const fieldOptions = optionsArray.map(item => ({
257
+ fieldOptionId: item[idAttribute],
258
+ fieldOptionValue: item[nameAttribute]
259
+ }));
260
+ field.setFieldOptions(fieldOptions);
261
+ }
262
+ }
263
+
264
+ getFieldSet(filter, inputCodes: string[] | string | null, secCode?: string | null, subCode?: string | null): string[] {
265
+ let codes: any[] = [];
266
+ if (typeof inputCodes === 'string' && inputCodes) {
267
+ codes = [inputCodes];
268
+ } else if (Array.isArray(inputCodes) && inputCodes.length > 0) {
269
+ codes = inputCodes ?? [];
270
+ } else if (secCode && !subCode) {
271
+ codes = this.getSection(secCode)?.getFieldNames() ?? [];
272
+ } else if (secCode && subCode) {
273
+ codes = this.getSubSection(secCode, subCode)?.getFieldNames() ?? [];
274
+ } else {
275
+ codes = this.getFieldNames() ?? [];
276
+ }
277
+ return (filter) ? codes.filter(fld => filter(this.getField(fld))) : codes;
278
+ }
279
+
280
+ applyOnFields(processFunc, inputCodes?: string[] | string | null, secCode?: string, subCode?: string): number {
281
+ if (!processFunc) { return 0; }
282
+ const codes = this.getFieldSet(null, inputCodes ?? null, secCode, subCode);
283
+ let processedFields = 0;
284
+ for (const code of codes) {
285
+ const field = this.getField(code);
286
+ if (field) {
287
+ try {
288
+ processFunc(field);
289
+ processedFields += 1;
290
+ } catch (e) {
291
+ console.log(`Error procesando funcion en campo ${field}: ${e}`);
292
+ }
293
+ }
294
+ }
295
+ return processedFields;
296
+ }
297
+
298
+ enableFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
299
+ return this.applyOnFields(fld => fld.enable(), codes, secCode, subCode);
300
+ }
301
+
302
+ showFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
303
+ return this.applyOnFields(fld => fld.show(), codes, secCode, subCode);
304
+ }
305
+
306
+ hideFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
307
+ return this.applyOnFields(fld => fld.hide(), codes, secCode, subCode);
308
+ }
309
+
310
+ showLabelFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
311
+ return this.applyOnFields(fld => fld.showLablel(), codes, secCode, subCode);
312
+ }
313
+
314
+ hideLabelFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
315
+ return this.applyOnFields(fld => fld.hideLabel(), codes, secCode, subCode);
316
+ }
317
+
318
+ disableFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
319
+ return this.applyOnFields(fld => fld.disable(), codes, secCode, subCode);
320
+ }
321
+
322
+ cleanFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
323
+ return this.applyOnFields(fld => fld.clean(), codes, secCode, subCode);
324
+ }
325
+
326
+ tagFieldsWithError(message: string, codes?: string[] | string | null, secCode?: string, subCode?: string): number {
327
+ return this.applyOnFields(fld => fld.setErrorMessage(message), codes, secCode, subCode);
328
+ }
329
+
330
+ cleanErrorFields(codes?: string[] | string | null, secCode?: string, subCode?: string): number {
331
+ return this.tagFieldsWithError('', codes, secCode, subCode);
332
+ }
333
+
334
+ tagEmptyRequiredFields(message: string, codes = null, secCode?: string, subCode?: string): boolean {
335
+ return this.tagFieldsWithError(message, this.getRequiredEmptyFields(codes, secCode, subCode)) > 0;
336
+ }
337
+
338
+ getRequiredFields(codes?: string[] | string | null, secCode?: string, subCode?: string): string[] {
339
+ return this.getFieldSet(fld => fld.required, codes ?? null, secCode, subCode);
340
+ }
341
+
342
+ getRequiredEmptyFields(codes?: string[] | string | null, secCode?: string, subCode?: string): string[] {
343
+ return this.getFieldSet(fld => fld.required && fld.empty, codes ?? null, secCode, subCode);
344
+ }
345
+
346
+ getChangedFields(codes?: string[] | string | null, secCode?: string, subCode?: string): string[] {
347
+ return this.getFieldSet(fld => !fld.outputOnly && fld.hasChanged(), codes ?? null, secCode, subCode);
348
+ }
349
+
350
+ getFieldsWithValidationIssues(codes?: string[] | string | null, secCode?: string, subCode?: string): string[] {
351
+ return this.getFieldSet(fld => fld.errorCode !== NO_ERROR, codes ?? null, secCode, subCode);
352
+ }
353
+
354
+ getFieldsValues(inputCodes?: string[] | string | null, secCode?: string, subCode?: string): any {
355
+ const codes = this.getFieldSet(null, inputCodes ?? null, secCode, subCode);
356
+ const resultObject = {};
357
+ for (let index = 0; index < codes.length; index++) {
358
+ const code = codes[index];
359
+ if (code) {
360
+ resultObject[code] = this._fields?.[code]?.getValue() ?? null;
361
+ }
362
+ }
363
+ return resultObject;
364
+ }
365
+
366
+ // Acciones
367
+ get actions() { return this._actions; }
368
+
369
+ getActionsByAttribute(name: string, value): FormAction[] { return this._actionArray.filter(actionItem => actionItem.matchAttribute(name, value)); }
370
+ getActions(): FormAction[] { return this._actionArray; }
371
+ getAction(code: string): FormAction { return (code && this._actions?.[code]) ? this._actions[code] : null; }
372
+ showActions(codes: string[] | string): void { return this.execOnActions(codes, SHOW); }
373
+ hideActions(codes: string[] | string): void { return this.execOnActions(codes, HIDE); }
374
+ enableActions(codes: string[] | string): void { return this.execOnActions(codes, ENABLE); }
375
+ disableActions(codes: string[] | string): void { return this.execOnActions(codes, DISABLE); }
376
+
377
+ execOnActions(codes: string[] | string | null, functionName: string): void {
378
+ const actionCodes = (Array.isArray(codes)) ? codes : (codes ? [codes] : []);
379
+ if (!functionName || actionCodes.length === 0) { return; }
380
+ actionCodes.forEach(code => {
381
+ const action = this.getAction(code);
382
+ action?.[functionName]?.();
383
+ });
384
+ }
385
+
386
+ // Tablas
387
+ get tables(): any { return this.tables; }
388
+ getTables(): RecordTable[] { return this._tableArray; }
389
+ getTable(code: string): RecordTable { return (code && this._tables?.[code]) ? this._tables[code] : null; }
390
+ getTableRecord(code: string, id: string): TableRecordData { return this.getTable(code)?.getTableRecord(id) }
391
+
392
+ enableTables(codes: string[] | string): void { return this.execOnTables(codes, ENABLE); }
393
+ disableTables(codes: string[] | string): void { return this.execOnTables(codes, DISABLE); }
394
+ showTables(codes: string[] | string): void { return this.execOnTables(codes, SHOW); }
395
+ hideTables(codes: string[] | string): void { return this.execOnTables(codes, HIDE); }
396
+ cleanTables(codes: string[] | string): void { return this.execOnTables(codes, CLEAN); }
397
+
398
+ execOnTables(codes: string[] | string | null, functionName: string) {
399
+ const tableCodes = (Array.isArray(codes)) ? codes : (codes ? [codes] : []);
400
+ if (!functionName || tableCodes.length === 0) { return; }
401
+ tableCodes.forEach(code => {
402
+ const table = this.getTable(code);
403
+ table?.[functionName]?.();
404
+ });
405
+ }
406
+
407
+ // Secciones
408
+ get sections(): any { return this._sections; }
409
+ getSections(): RecordFormSection[] { return this._sectionArray; }
410
+ getSectionsByAttribute(name, value): RecordFormSection[] { return this._sectionArray.filter(item => item.matchAttribute(name, value)); }
411
+ get sectionTitles(): string[] { return this._sectionArray.map(formSection => formSection.sectionTitle); }
412
+ get visibleSections(): RecordFormSection[] { return this._sectionArray.filter(sec => sec.visible); }
413
+ getSection(code: string): RecordFormSection { return (code && this._sections?.[code]) ? this._sections[code] : null; }
414
+ showSections(codes: string[] | string): void { this.execOnSections(codes, SHOW); }
415
+ hideSections(codes: string[] | string): void { this.execOnSections(codes, HIDE); }
416
+ activeSection(): string { return this._exclusiveSectionsByAttr[ACTIVE]; }
417
+ getSubSection(code: string, subCode): RecordFormSubSection { return this.getSection(code)?.getSubsection(subCode) ?? null; }
418
+ showSubSections(code: string, subCodes: string[] | string): void { return this.execOnSubSections(code, subCodes, SHOW); }
419
+ hideSubSections(code: string, subCodes: string[] | string): void { return this.execOnSubSections(code, subCodes, HIDE); }
420
+ getSectionActions(code): FormAction[] { return this.getSection(code)?.getActions() ?? null; }
421
+ getSectionActionNames(code): string[] { return this.getSection(code)?.getActionNames() ?? null; }
422
+
423
+ activateSection(code: string): void {
424
+ if (code === this._exclusiveSectionsByAttr[ACTIVE]) { return; }
425
+ this.getSection(this.activeSection())?.inactivate();
426
+ this.getSection(code)?.activate();
427
+ this._exclusiveSectionsByAttr[ACTIVE] = code;
428
+ }
429
+
430
+ execOnSections(codes: string[] | string | null, functionName: string): void {
431
+ const sectionCodes = (Array.isArray(codes)) ? codes : (codes ? [codes] : []);
432
+ if (!functionName || sectionCodes.length === 0) { return; }
433
+ sectionCodes.forEach(code => {
434
+ const section = this.getSection(code);
435
+ section?.[functionName]?.();
436
+ });
437
+ }
438
+
439
+ execOnSubSections(code: string, subNames: string[] | string | null, functionName: string): void {
440
+ const subCodes = (Array.isArray(subNames)) ? subNames : (subNames ? [subNames] : []);
441
+ const section = this.getSection(code);
442
+ if (!functionName || !section || subCodes.length === 0) { return; }
443
+ for (const subCode of subCodes) {
444
+ const subSection = this.getSubSection(code, subCode);
445
+ subSection?.[functionName]?.();
446
+ }
447
+ }
448
+
449
+ /**
450
+ * Métodos propios de gestión del formulario
451
+ */
452
+
453
+ cleanData(): void {
454
+ for (const field of this._fieldArray) {
455
+ field.setValue(field.defaultValue);
456
+ }
457
+ for (const table of this._tableArray) {
458
+ table.clean();
459
+ }
460
+ }
461
+
462
+ getPayload(): any {
463
+ const formData: FormPayload = {
464
+ fields: [],
465
+ tables: [],
466
+ };
467
+ formData.fields = this.getFields().filter(fld => !fld.outputOnly)
468
+ .map(fld => {
469
+ const fieldPayload: FieldPayload = {
470
+ fieldCode: fld.fieldCode,
471
+ fieldValue: fld.getValue(),
472
+ editable: !fld.disabled,
473
+ visible: fld.visible,
474
+ required: fld.required,
475
+ fieldOptions: '',
476
+ }
477
+ return fieldPayload;
478
+ });
479
+
480
+ formData.tables = this.getTables().map(tbl => {
481
+ const tablePayload: TablePayload = {
482
+ tableCode: tbl.tableCode,
483
+ visible: tbl.visible,
484
+ currentPage: tbl.currentPage,
485
+ requestedPage: tbl.requestedPage,
486
+ recordsPerPage: tbl.recordsPerPage,
487
+ currentFilter: tbl.currentFilter,
488
+ sortingColumn: tbl.sorting.columnName,
489
+ sortingDirection: tbl.sorting.direction,
490
+ };
491
+ return tablePayload
492
+ })
493
+ return formData;
494
+ }
495
+ }
@@ -0,0 +1,154 @@
1
+ import { Subject } from 'rxjs';
2
+ import { FormAction } from './action';
3
+ import { RecordFormSubSection } from './subsection';
4
+
5
+ export class RecordFormSection {
6
+ private readonly _activation = new Subject<string>();
7
+ private readonly _inactivation = new Subject<string>();
8
+ visible: boolean;
9
+ visibleStates: string[];
10
+
11
+ active = false;
12
+ sectionId: string;
13
+ sectionCode: string;
14
+ sectionTitle: string;
15
+ customAttributes: any = {};
16
+
17
+ subSections: RecordFormSubSection[];
18
+ subSectionsObj: any;
19
+
20
+ constructor(sectionReceived, formObject) {
21
+ if (!sectionReceived) { return; }
22
+ this.visible = true;
23
+ this.sectionId = (sectionReceived.sectionId) ? sectionReceived.sectionId.toString() : '';
24
+ this.sectionCode = (sectionReceived.sectionCode) ? sectionReceived.sectionCode : '';
25
+ this.sectionTitle = (sectionReceived.sectionTitle) ? sectionReceived.sectionTitle : '';
26
+ this.visibleStates = sectionReceived.visibleStates || [];
27
+ this.subSections = [];
28
+ this.subSectionsObj = {};
29
+ if (sectionReceived.subsections) {
30
+ const subsections = sectionReceived.subsections.map(objDef => {
31
+ let visibleStates = objDef.visibleStates;
32
+ if (!visibleStates) {
33
+ visibleStates = (objDef.subsectionModes || '').split(',')
34
+ .map(state => state.trim())
35
+ .filter(state => state.length > 0);
36
+ }
37
+ if (!visibleStates || visibleStates.length === 0) {
38
+ visibleStates = this.visibleStates;
39
+ }
40
+ if (objDef.elements && Array.isArray(objDef.elements)) {
41
+ objDef.elements = objDef.elements.map(elm => ({ code: elm.elementCode, type: elm.elementTypeName }));
42
+ }
43
+ return { ...objDef, visibleStates };
44
+ });
45
+ for (const subsectionReceived of subsections) {
46
+ const subSectionToAdd = new RecordFormSubSection(subsectionReceived, formObject);
47
+ const subsectionCode = subSectionToAdd.subsectionCode;
48
+ if (subsectionCode) {
49
+ this.subSections.push(subSectionToAdd);
50
+ this.subSectionsObj[subsectionCode] = subSectionToAdd;
51
+ }
52
+ }
53
+ }
54
+ if (sectionReceived?.customAttributes) {
55
+ Object.keys(sectionReceived?.customAttributes)
56
+ ?.forEach(attr => this.customAttributes[attr] = sectionReceived?.customAttributes[attr])
57
+ }
58
+ }
59
+
60
+ getCustomAttribute(name) { return this.customAttributes?.[name] ?? null; }
61
+ setCustomAttribute(name, value) { return name && (this.customAttributes[name] = value); }
62
+ matchAttribute(name, value) { return this.customAttributes?.[name] === value; }
63
+
64
+ get code() { return this.sectionCode; }
65
+ get activation() { return this._activation; }
66
+ get inactivation() { return this._inactivation; }
67
+
68
+ activate() {
69
+ if (!this.active) {
70
+ this.active = true;
71
+ this._activation.next(this.sectionCode);
72
+ }
73
+ }
74
+
75
+ inactivate() {
76
+ if (this.active) {
77
+ this.active = false;
78
+ this._inactivation.next(this.sectionCode);
79
+ }
80
+ }
81
+
82
+ show() { this.visible = true; }
83
+ hide() { this.visible = false; }
84
+ get title() { return this.sectionTitle; }
85
+ set title(title) { this.sectionTitle = title; }
86
+
87
+ getVisibleSubsections(state) {
88
+ return this.subSections.filter(subSection => {
89
+ return subSection.visible && subSection.viewOnState(state);
90
+ });
91
+ }
92
+
93
+ getSubsection(subSectionCode) {
94
+ return (this.subSectionsObj && this.subSectionsObj[subSectionCode])
95
+ ? this.subSectionsObj[subSectionCode] : null;
96
+ }
97
+
98
+ getFields() {
99
+ let fieldsArray = [];
100
+ if (this.subSections && this.subSections.length > 0) {
101
+ for (const subSection of this.subSections) {
102
+ fieldsArray = fieldsArray.concat(subSection.getFields());
103
+ }
104
+ }
105
+ return fieldsArray;
106
+ }
107
+
108
+ getActions(): FormAction[] {
109
+ let actionArray = [];
110
+ if (this.subSections && this.subSections.length > 0) {
111
+ for (const subSection of this.subSections) {
112
+ actionArray = actionArray.concat(subSection.getActions());
113
+ }
114
+ }
115
+ return actionArray;
116
+ }
117
+
118
+ getActionNames(): string[] {
119
+ let actionArray = [];
120
+ if (this.subSections && this.subSections.length > 0) {
121
+ for (const subSection of this.subSections) {
122
+ actionArray = actionArray.concat(subSection.getActionNames());
123
+ }
124
+ }
125
+ return actionArray;
126
+ }
127
+
128
+ getFieldNames() {
129
+ let fieldsArray = [];
130
+ if (this.subSections && this.subSections.length > 0) {
131
+ for (const subSection of this.subSections) {
132
+ fieldsArray = fieldsArray.concat(subSection.getFieldNames());
133
+ }
134
+ }
135
+ return fieldsArray;
136
+ }
137
+
138
+ getField(name) {
139
+ let field = null;
140
+ if (this.subSections && this.subSections.length > 0) {
141
+ for (const subSection of this.subSections) {
142
+ field = subSection.getField(name);
143
+ if (field) {
144
+ return field;
145
+ }
146
+ }
147
+ }
148
+ return null;
149
+ }
150
+
151
+ supportState(state) { return this.viewOnState(state); }
152
+ viewOnState(state) { return this.visibleStates.includes(state); }
153
+ supportMode(state) { return this.viewOnState(state); }
154
+ }