tuain-form-manager 1.3.0 → 1.4.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.
Files changed (2) hide show
  1. package/lib/form.js +231 -119
  2. package/package.json +1 -1
package/lib/form.js CHANGED
@@ -29,6 +29,11 @@ const FIELD_ASSIGN_ATTRIBUTES = [
29
29
  'options', 'editable',
30
30
  ];
31
31
 
32
+ const TABLE_CONSTRAINTS = [
33
+ 'visible', 'currentPage', 'requestedPage', 'recordsPerPage', 'sortingColumn', 'sortingDirection',
34
+ 'totalRecordsNumber', 'recordsNumber', 'currentFilter', 'tableRecords',
35
+ ];
36
+
32
37
  const SESSION_ATTRIBUTES = {
33
38
  sessionCode: 'sessionCode',
34
39
  profileCode: 'profileCode',
@@ -194,6 +199,9 @@ class Form {
194
199
  return (this.state === newState);
195
200
  }
196
201
 
202
+ /**
203
+ * @deprecated Use changeState
204
+ */
197
205
  changeFormMode(newState) { return this.changeState(newState); }
198
206
 
199
207
  getActionDefinition(code) { return this._formDefinition?.actions?.find(act => act.actionCode === code) ?? null; }
@@ -334,16 +342,16 @@ class Form {
334
342
  }
335
343
 
336
344
  showTable(code) {
337
- const tblDef = this.getTableDefinition(code);
338
- if (tblDef) {
339
- tblDef.constraints.visible = true;
345
+ const responseTable = this.getResponseTable(code);
346
+ if (responseTable) {
347
+ responseTable.visible = true;
340
348
  }
341
349
  }
342
350
 
343
351
  hideTable(code) {
344
- const tblDef = this.getTableDefinition(code);
345
- if (tblDef) {
346
- tblDef.constraints.visible = false;
352
+ const responseTable = this.getResponseTable(code);
353
+ if (responseTable) {
354
+ responseTable.visible = false;
347
355
  }
348
356
  }
349
357
 
@@ -358,7 +366,6 @@ class Form {
358
366
  }
359
367
 
360
368
  getTableDefinition(code) { return this._formDefinition?.tables.find(tbl => tbl.tableCode === code) ?? null; }
361
- getTableConstraints(tableCode) { return this.getTableDefinition(tableCode)?.constraints ?? null; }
362
369
  onTableRowSelection(code, callback) { this.onTableAction(code, ROWSELECTION, callback); }
363
370
  onTablePopulate(code, callback) { this._tablePopulate[code] = callback; }
364
371
  onTableAction(tableCode, actionCode, callback) {
@@ -477,85 +484,106 @@ class Form {
477
484
  }
478
485
 
479
486
  async executeFieldValidation() {
487
+ let message;
480
488
  const fieldCode = this.actionSubject;
481
489
  const fieldValue = this.getFieldValue(fieldCode);
482
490
  const callback = this._fieldValidations[fieldCode];
483
491
  if (!callback) {
484
- this.logger.log({
485
- level: 'debug',
486
- label: 'fieldValidation',
487
- action: fieldCode,
488
- message: `No existe función de validación de ${fieldCode}`,
489
- });
492
+ message = `No existe función de validación de ${fieldCode}`;
493
+ this.logger.log({ level: 'debug', label: 'fieldValidation', action: fieldCode, message });
490
494
  return this.errMgr.get(modErrs.fieldValidation.NoValidationCallback);
491
495
  }
492
496
  let errorObj = null;
493
497
  try {
494
498
  errorObj = await callback(fieldCode, fieldValue);
495
499
  } catch (err) {
496
- const errorDetail = `Excepción en validación de ${fieldCode}: ${err.message} / ${err.stack}`;
497
- this.logger.log({
498
- level: 'error',
499
- label: 'fieldValidation',
500
- action: fieldCode,
501
- message: errorDetail,
502
- });
503
- errorObj = this.errMgr.get(modErrs.fieldValidation.exceptionNotHandled, errorDetail);
500
+ message = `Excepción en validación de ${fieldCode}: ${err.message} / ${err.stack}`;
501
+ this.logger.log({ level: 'error', label: 'fieldValidation', action: fieldCode, message });
502
+ errorObj = this.errMgr.get(modErrs.fieldValidation.exceptionNotHandled, message);
504
503
  }
505
504
  return errorObj;
506
505
  }
507
506
 
508
507
  async executeFormAction(actionCode) {
509
508
  let errorObj = null;
509
+ let message;
510
510
  const callback = this._formActions[actionCode];
511
511
  if (!callback) {
512
- const errorDetail = `No existe función de atención de ${actionCode} para ${this.formCode}`;
513
- this.logger.log({
514
- level: 'debug',
515
- label: 'executeFormAction',
516
- message: errorDetail,
517
- });
518
- errorObj = this.errMgr.get(modErrs.formActionExec.actionNotDefined, errorDetail);
512
+ message = `No existe función de atención de ${actionCode} para ${this.formCode}`;
513
+ this.logger.log({ level: 'debug', label: 'executeFormAction', message });
514
+ errorObj = this.errMgr.get(modErrs.formActionExec.actionNotDefined, message);
519
515
  return [errorObj, this.getExportData()];
520
516
  }
521
517
  if (!this.actionAllowed(actionCode)) {
522
- const errorDetail = `Usuario sin privilegios para la acción ${actionCode} en ${this.formCode}`;
523
- this.logger.log({
524
- level: 'debug',
525
- label: 'executeFormAction',
526
- message: errorDetail,
527
- });
528
- errorObj = this.errMgr.get(modErrs.formActionExec.actionNotDefined, errorDetail);
518
+ message = `Usuario sin privilegios para la acción ${actionCode} en ${this.formCode}`;
519
+ this.logger.log({ level: 'debug', label: 'executeFormAction', message });
520
+ errorObj = this.errMgr.get(modErrs.formActionExec.actionNotDefined, message);
529
521
  return [errorObj, this.getExportData()];
530
522
  }
531
- this.logger.log({
532
- level: 'debug',
533
- label: 'executeFormAction',
534
- message: `START FORM ACTION ${actionCode} / ${this.formCode}/${this.formSubject ?? ''}`,
535
- });
523
+ message = `START FORM ACTION ${actionCode} / ${this.formCode}/${this.formSubject ?? ''}`;
524
+ this.logger.log({ level: 'debug', label: 'executeFormAction', message });
536
525
  try {
537
526
  errorObj = await callback(actionCode);
538
527
  } catch (err) {
539
- const errorDetail = `Excepción en acción de ${actionCode}: ${err.message} / ${err.stack}`;
540
- this.logger.log({
541
- level: 'error',
542
- label: 'executeFormAction',
543
- action: actionCode,
544
- message: errorDetail,
545
- });
546
- errorObj = this.errMgr.get(modErrs.formActionExec.exceptionNotHandled, errorDetail);
528
+ message = `Excepción en acción de ${actionCode}: ${err.message} / ${err.stack}`;
529
+ this.logger.log({ level: 'error', label: 'executeFormAction', action: actionCode, message });
530
+ errorObj = this.errMgr.get(modErrs.formActionExec.exceptionNotHandled, message);
547
531
  }
548
532
  return [errorObj, this.getExportData()];
549
533
  }
550
534
 
535
+ getResponseTable(tableCode) {
536
+ let message;
537
+ let responseTable = this._responseData.recordTables[tableCode];
538
+ if (!responseTable) {
539
+ const tableDefinition = this.getTableDefinition(tableCode);
540
+ if (!tableDefinition) {
541
+ message = `La tabla a ${tableCode} no está en la definición del formulario`;
542
+ this.logger.log({ level: 'error', message });
543
+ return null;
544
+ }
545
+ const {
546
+ visible = true, currentPage = 1, requestedPage = 1, totalRecordsNumber = 0, recordsNumber = 0,
547
+ recordsPerPage = 10, sortingColumn = null, sortingDirection = null,
548
+ } = tableDefinition?.constraints ?? {};
549
+ responseTable = {
550
+ tableCode,
551
+ visible,
552
+ currentPage,
553
+ requestedPage,
554
+ totalRecordsNumber,
555
+ recordsNumber,
556
+ recordsPerPage,
557
+ sortingColumn,
558
+ sortingDirection,
559
+ };
560
+ this._responseData.recordTables[tableCode] = responseTable;
561
+ }
562
+ return responseTable;
563
+ }
564
+
565
+ getResponseTableAction(tableCode, actionCode) {
566
+ const responseTable = this.getResponseTable(tableCode);
567
+ if (!responseTable) { return null; }
568
+ if (!responseTable?.actions) {
569
+ responseTable.actions = { [actionCode]: {} };
570
+ }
571
+ return responseTable.actions[actionCode];
572
+ }
573
+
574
+ getResponseTableField(tableCode, fieldCode) {
575
+ const responseTable = this.getResponseTable(tableCode);
576
+ if (!responseTable) { return null; }
577
+ if (!responseTable?.fields) {
578
+ responseTable.fields = { [fieldCode]: {} };
579
+ }
580
+ return responseTable.fields[fieldCode];
581
+ }
582
+
551
583
  fillTable(records, mapping) {
552
584
  const { tableCode, recordIdField, recordColumnsNames, recordItemAttributes } = mapping;
553
- const tableDefinition = this.getTableDefinition(tableCode);
554
- if (!tableDefinition) {
555
- this.logger.log({
556
- level: 'error',
557
- message: `La tabla a ${tableCode} no está en la definición del formulario`,
558
- });
585
+ const responseTable = this.getResponseTable(tableCode);
586
+ if (!responseTable) {
559
587
  return;
560
588
  }
561
589
  const tableRecords = [];
@@ -573,57 +601,159 @@ class Form {
573
601
  }
574
602
  tableRecords.push(tableRecord);
575
603
  }
576
- const { constraints } = tableDefinition;
577
- this._responseData.recordTables[tableCode] = {
578
- tableCode,
579
- tableRecords,
580
- visible: constraints?.visible ?? true,
581
- currentPage: constraints?.currentPage ?? 1,
582
- requestedPage: constraints?.requestedPage ?? 1,
583
- totalRecordsNumber: constraints?.totalRecordsNumber ?? 0,
584
- recordsNumber: constraints?.recordsNumber ?? 0,
585
- recordsPerPage: constraints?.recordsPerPage ?? 10,
586
- sortingColumn: constraints?.sortingColumn ?? null,
587
- sortingDirection: constraints?.sortingDirection ?? null,
588
- };
604
+ Object.assign(responseTable, { tableRecords });
605
+ }
606
+
607
+ showTableField(tableCode, fieldCode) {
608
+ const tableField = this.getResponseTableField(tableCode, fieldCode);
609
+ if (tableField) {
610
+ Object.assign(tableField, { visible: true });
611
+ }
612
+ }
613
+
614
+ hideTableField(tableCode, fieldCode) {
615
+ const tableField = this.getResponseTableField(tableCode, fieldCode);
616
+ if (tableField) {
617
+ Object.assign(tableField, { visible: false });
618
+ }
619
+ }
620
+
621
+ enableTableAction(tableCode, actionCode) {
622
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
623
+ if (tableAction) {
624
+ Object.assign(tableAction, { enabled: true });
625
+ }
626
+ }
627
+
628
+ disableTableAction(tableCode, actionCode) {
629
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
630
+ if (tableAction) {
631
+ Object.assign(tableAction, { enabled: false });
632
+ }
633
+ }
634
+
635
+ showTableAction(tableCode, actionCode) {
636
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
637
+ if (tableAction) {
638
+ Object.assign(tableAction, { visible: true });
639
+ }
640
+ }
641
+
642
+ hideTableAction(tableCode, actionCode) {
643
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
644
+ if (tableAction) {
645
+ Object.assign(tableAction, { visible: false });
646
+ }
647
+ }
648
+
649
+ showOnStateTableAction(tableCode, actionCode, state) {
650
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
651
+ if (tableAction) {
652
+ if (!tableAction.showOnStates) {
653
+ tableAction.showOnStates = [];
654
+ }
655
+ const currentlyVisible = tableAction.showOnStates.findIndex(item => item === state);
656
+ const correntlyHidden = tableAction.hideOnStates?.findIndex(item => item === state);
657
+ if (currentlyVisible < 0) {
658
+ tableAction.showOnStates.push(state);
659
+ }
660
+ if (correntlyHidden >= 0) {
661
+ tableAction.hideOnStates.splice(correntlyHidden, 1);
662
+ if (tableAction.hideOnStates.length === 0) {
663
+ delete tableAction.hideOnStates;
664
+ }
665
+ }
666
+ }
667
+ }
668
+
669
+ hideOnStateTableAction(tableCode, actionCode, state) {
670
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
671
+ if (tableAction) {
672
+ if (!tableAction.hideOnStates) {
673
+ tableAction.hideOnStates = [];
674
+ }
675
+ const currentlyVisible = tableAction.showOnStates.findIndex(item => item === state);
676
+ const correntlyHidden = tableAction.hideOnStates?.findIndex(item => item === state);
677
+ if (correntlyHidden < 0) {
678
+ tableAction.hideOnStates.push(state);
679
+ }
680
+ if (currentlyVisible >= 0) {
681
+ tableAction.showOnStates.splice(currentlyVisible, 1);
682
+ if (tableAction.showOnStates.length === 0) {
683
+ delete tableAction.showOnStates;
684
+ }
685
+ }
686
+ }
687
+ }
688
+
689
+ enableOnStateTableAction(tableCode, actionCode, state) {
690
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
691
+ if (tableAction) {
692
+ if (!tableAction.enableOnStates) {
693
+ tableAction.enableOnStates = [];
694
+ }
695
+ const currentlyEnabled = tableAction.enableOnStates.findIndex(item => item === state);
696
+ const correntlyDisabled = tableAction.disableOnStates?.findIndex(item => item === state);
697
+ if (currentlyEnabled < 0) {
698
+ tableAction.enableOnStates.push(state);
699
+ }
700
+ if (correntlyDisabled >= 0) {
701
+ tableAction.disableOnStates.splice(correntlyDisabled, 1);
702
+ if (tableAction.disableOnStates.length === 0) {
703
+ delete tableAction.disableOnStates;
704
+ }
705
+ }
706
+ }
707
+ }
708
+
709
+ disableOnStateTableAction(tableCode, actionCode, state) {
710
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
711
+ if (tableAction) {
712
+ if (!tableAction.disableOnStates) {
713
+ tableAction.disableOnStates = [];
714
+ }
715
+ const currentlyEnabled = tableAction.enableOnStates.findIndex(item => item === state);
716
+ const correntlyDisabled = tableAction.disableOnStates?.findIndex(item => item === state);
717
+ if (correntlyDisabled < 0) {
718
+ tableAction.disableOnStates.push(state);
719
+ }
720
+ if (currentlyEnabled >= 0) {
721
+ tableAction.enableOnStates.splice(currentlyEnabled, 1);
722
+ if (tableAction.enableOnStates.length === 0) {
723
+ delete tableAction.enableOnStates;
724
+ }
725
+ }
726
+ }
589
727
  }
590
728
 
591
729
  async executeTableAction() {
592
- let errorDetail = '';
730
+ let message = '';
593
731
  if (!this.actionSubject || typeof this.actionSubject !== 'object') {
594
- errorDetail = 'Acción de tabla requiere actionSubject';
595
- this.logger.log({
596
- level: 'error',
597
- label: 'executeTableAction',
598
- message: errorDetail,
599
- });
600
- return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, errorDetail);
732
+ message = 'Acción de tabla requiere actionSubject';
733
+ this.logger.log({ level: 'error', label: 'executeTableAction', message });
734
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
601
735
  }
602
736
  const { tableCode, actionType = INLINE, tableRecordId, tableRecordData } = this.actionSubject;
603
737
  if (!tableCode) {
604
- errorDetail = 'Acción de tabla requiere tableCode';
605
- this.logger.log({
606
- level: 'error',
607
- label: 'executeTableAction',
608
- message: errorDetail,
609
- });
610
- return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, errorDetail);
738
+ message = 'Acción de tabla requiere tableCode';
739
+ this.logger.log({ level: 'error', label: 'executeTableAction', message });
740
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
611
741
  }
612
742
  let { actionCode } = this.actionSubject;
613
743
  if (actionType === INLINE) {
614
744
  if (!actionCode) {
615
- errorDetail = 'Acción inline requiere actionCode';
616
- return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, errorDetail);
745
+ message = 'Acción inline requiere actionCode';
746
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
617
747
  }
618
748
  if (!tableRecordId) {
619
- errorDetail = 'Acción inline requiere identificador del registro tableRecordId';
620
- return this.errMgr.get(modErrs.tableActionExec.noRecordId, errorDetail);
749
+ message = 'Acción inline requiere identificador del registro tableRecordId';
750
+ return this.errMgr.get(modErrs.tableActionExec.noRecordId, message);
621
751
  }
622
752
  } else if (actionType === ROWSELECTION) {
623
753
  actionCode = ROWSELECTION;
624
754
  if (!tableRecordId) {
625
- errorDetail = 'Acción sobre tabla requiere identificador del registro tableRecordId';
626
- return this.errMgr.get(modErrs.tableActionExec.noRecordId, errorDetail);
755
+ message = 'Acción sobre tabla requiere identificador del registro tableRecordId';
756
+ return this.errMgr.get(modErrs.tableActionExec.noRecordId, message);
627
757
  }
628
758
  } else {
629
759
  // Cualquier otro tipo de acción sobre tabla no involucra tableRecordId!!!
@@ -649,38 +779,25 @@ class Form {
649
779
  });
650
780
  errorObject = await tableActionCallback(tableCode, actionCode, tableRecordId, tableRecordData ?? {});
651
781
  } catch (err) {
652
- errorDetail = `Excepción no soportada en acción ${actionCode} sobre la tabla ${tableCode} : ${err.message} / ${err.stack}`;
653
- this.logger.log({
654
- level: 'error',
655
- label: 'tableAction',
656
- action: actionCode,
657
- message: errorDetail,
658
- });
659
- errorObject = this.errMgr.get(modErrs.tableActionExec.noCallback, errorDetail);
782
+ message = `Excepción no soportada en acción ${actionCode} sobre la tabla ${tableCode} : ${err.message} / ${err.stack}`;
783
+ this.logger.log({ level: 'error', label: 'tableAction', action: actionCode, message });
784
+ errorObject = this.errMgr.get(modErrs.tableActionExec.noCallback, message);
660
785
  }
661
786
  return errorObject;
662
787
  }
663
788
 
664
789
  async executeTablePopulate() {
665
- let errorDetail = '';
790
+ let message = '';
666
791
  if (!this.actionSubject || typeof this.actionSubject !== 'object') {
667
- errorDetail = 'Acción de poblamiento de tabla requiere actionSubject';
668
- this.logger.log({
669
- level: 'error',
670
- label: 'executeTablePopulate',
671
- message: errorDetail,
672
- });
673
- return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, errorDetail);
792
+ message = 'Acción de poblamiento de tabla requiere actionSubject';
793
+ this.logger.log({ level: 'error', label: 'executeTablePopulate', message });
794
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
674
795
  }
675
796
  const { tableCode } = this.actionSubject;
676
797
  if (!tableCode) {
677
- errorDetail = 'Acción de poblamiento de tabla requiere tableCode';
678
- this.logger.log({
679
- level: 'error',
680
- label: 'executeTablePopulate',
681
- message: errorDetail,
682
- });
683
- return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, errorDetail);
798
+ message = 'Acción de poblamiento de tabla requiere tableCode';
799
+ this.logger.log({ level: 'error', label: 'executeTablePopulate', message });
800
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
684
801
  }
685
802
  let errorObject = null;
686
803
  try {
@@ -695,14 +812,9 @@ class Form {
695
812
  }
696
813
  errorObject = await tablePopulateCallback(tableCode, this.actionSubject);
697
814
  } catch (err) {
698
- errorDetail = `Excepción no soportada en el poblamiento de la tabla ${tableCode} en ${this.formCode}: ${err} / ${err.stack}`;
699
- this.logger.log({
700
- level: 'error',
701
- label: 'executeTablePopulate',
702
- action: tableCode,
703
- message: errorDetail,
704
- });
705
- errorObject = this.errMgr.get(modErrs.tablePopulate.exceptionNotHandled, errorDetail);
815
+ message = `Excepción no soportada en el poblamiento de la tabla ${tableCode} en ${this.formCode}: ${err} / ${err.stack}`;
816
+ this.logger.log({ level: 'error', label: 'executeTablePopulate', action: tableCode, message });
817
+ errorObject = this.errMgr.get(modErrs.tablePopulate.exceptionNotHandled, message);
706
818
  }
707
819
  return errorObject;
708
820
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tuain-form-manager",
3
- "version": "1.3.0",
3
+ "version": "1.4.2",
4
4
  "description": "Component library to perform operations on Tuain Development Framework forms to interchange information on web or mobile applications based on the data interchange of abstract forms making trnasformation on the data upon actions required on both sides (front and back)",
5
5
  "main": "index.js",
6
6
  "scripts": {