tuain-form-manager 1.3.0 → 1.4.3

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 -118
  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
 
@@ -477,85 +485,106 @@ class Form {
477
485
  }
478
486
 
479
487
  async executeFieldValidation() {
488
+ let message;
480
489
  const fieldCode = this.actionSubject;
481
490
  const fieldValue = this.getFieldValue(fieldCode);
482
491
  const callback = this._fieldValidations[fieldCode];
483
492
  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
- });
493
+ message = `No existe función de validación de ${fieldCode}`;
494
+ this.logger.log({ level: 'debug', label: 'fieldValidation', action: fieldCode, message });
490
495
  return this.errMgr.get(modErrs.fieldValidation.NoValidationCallback);
491
496
  }
492
497
  let errorObj = null;
493
498
  try {
494
499
  errorObj = await callback(fieldCode, fieldValue);
495
500
  } 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);
501
+ message = `Excepción en validación de ${fieldCode}: ${err.message} / ${err.stack}`;
502
+ this.logger.log({ level: 'error', label: 'fieldValidation', action: fieldCode, message });
503
+ errorObj = this.errMgr.get(modErrs.fieldValidation.exceptionNotHandled, message);
504
504
  }
505
505
  return errorObj;
506
506
  }
507
507
 
508
508
  async executeFormAction(actionCode) {
509
509
  let errorObj = null;
510
+ let message;
510
511
  const callback = this._formActions[actionCode];
511
512
  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);
513
+ message = `No existe función de atención de ${actionCode} para ${this.formCode}`;
514
+ this.logger.log({ level: 'debug', label: 'executeFormAction', message });
515
+ errorObj = this.errMgr.get(modErrs.formActionExec.actionNotDefined, message);
519
516
  return [errorObj, this.getExportData()];
520
517
  }
521
518
  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);
519
+ message = `Usuario sin privilegios para la acción ${actionCode} en ${this.formCode}`;
520
+ this.logger.log({ level: 'debug', label: 'executeFormAction', message });
521
+ errorObj = this.errMgr.get(modErrs.formActionExec.actionNotDefined, message);
529
522
  return [errorObj, this.getExportData()];
530
523
  }
531
- this.logger.log({
532
- level: 'debug',
533
- label: 'executeFormAction',
534
- message: `START FORM ACTION ${actionCode} / ${this.formCode}/${this.formSubject ?? ''}`,
535
- });
524
+ message = `START FORM ACTION ${actionCode} / ${this.formCode}/${this.formSubject ?? ''}`;
525
+ this.logger.log({ level: 'debug', label: 'executeFormAction', message });
536
526
  try {
537
527
  errorObj = await callback(actionCode);
538
528
  } 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);
529
+ message = `Excepción en acción de ${actionCode}: ${err.message} / ${err.stack}`;
530
+ this.logger.log({ level: 'error', label: 'executeFormAction', action: actionCode, message });
531
+ errorObj = this.errMgr.get(modErrs.formActionExec.exceptionNotHandled, message);
547
532
  }
548
533
  return [errorObj, this.getExportData()];
549
534
  }
550
535
 
536
+ getResponseTable(tableCode) {
537
+ let message;
538
+ let responseTable = this._responseData.recordTables[tableCode];
539
+ if (!responseTable) {
540
+ const tableDefinition = this.getTableDefinition(tableCode);
541
+ if (!tableDefinition) {
542
+ message = `La tabla a ${tableCode} no está en la definición del formulario`;
543
+ this.logger.log({ level: 'error', message });
544
+ return null;
545
+ }
546
+ const {
547
+ visible = true, currentPage = 1, requestedPage = 1, totalRecordsNumber = 0, recordsNumber = 0,
548
+ recordsPerPage = 10, sortingColumn = null, sortingDirection = null,
549
+ } = tableDefinition?.constraints ?? {};
550
+ responseTable = {
551
+ tableCode,
552
+ visible,
553
+ currentPage,
554
+ requestedPage,
555
+ totalRecordsNumber,
556
+ recordsNumber,
557
+ recordsPerPage,
558
+ sortingColumn,
559
+ sortingDirection,
560
+ };
561
+ this._responseData.recordTables[tableCode] = responseTable;
562
+ }
563
+ return responseTable;
564
+ }
565
+
566
+ getResponseTableAction(tableCode, actionCode) {
567
+ const responseTable = this.getResponseTable(tableCode);
568
+ if (!responseTable) { return null; }
569
+ if (!responseTable?.actions) {
570
+ responseTable.actions = { [actionCode]: {} };
571
+ }
572
+ return responseTable.actions[actionCode];
573
+ }
574
+
575
+ getResponseTableField(tableCode, fieldCode) {
576
+ const responseTable = this.getResponseTable(tableCode);
577
+ if (!responseTable) { return null; }
578
+ if (!responseTable?.fields) {
579
+ responseTable.fields = { [fieldCode]: {} };
580
+ }
581
+ return responseTable.fields[fieldCode];
582
+ }
583
+
551
584
  fillTable(records, mapping) {
552
585
  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
- });
586
+ const responseTable = this.getResponseTable(tableCode);
587
+ if (!responseTable) {
559
588
  return;
560
589
  }
561
590
  const tableRecords = [];
@@ -573,57 +602,159 @@ class Form {
573
602
  }
574
603
  tableRecords.push(tableRecord);
575
604
  }
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
- };
605
+ Object.assign(responseTable, { tableRecords });
606
+ }
607
+
608
+ showTableField(tableCode, fieldCode) {
609
+ const tableField = this.getResponseTableField(tableCode, fieldCode);
610
+ if (tableField) {
611
+ Object.assign(tableField, { visible: true });
612
+ }
613
+ }
614
+
615
+ hideTableField(tableCode, fieldCode) {
616
+ const tableField = this.getResponseTableField(tableCode, fieldCode);
617
+ if (tableField) {
618
+ Object.assign(tableField, { visible: false });
619
+ }
620
+ }
621
+
622
+ enableTableAction(tableCode, actionCode) {
623
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
624
+ if (tableAction) {
625
+ Object.assign(tableAction, { enabled: true });
626
+ }
627
+ }
628
+
629
+ disableTableAction(tableCode, actionCode) {
630
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
631
+ if (tableAction) {
632
+ Object.assign(tableAction, { enabled: false });
633
+ }
634
+ }
635
+
636
+ showTableAction(tableCode, actionCode) {
637
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
638
+ if (tableAction) {
639
+ Object.assign(tableAction, { visible: true });
640
+ }
641
+ }
642
+
643
+ hideTableAction(tableCode, actionCode) {
644
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
645
+ if (tableAction) {
646
+ Object.assign(tableAction, { visible: false });
647
+ }
648
+ }
649
+
650
+ showOnStateTableAction(tableCode, actionCode, state) {
651
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
652
+ if (tableAction) {
653
+ if (!tableAction.showOnStates) {
654
+ tableAction.showOnStates = [];
655
+ }
656
+ const currentlyVisible = tableAction.showOnStates.findIndex(item => item === state);
657
+ const correntlyHidden = tableAction.hideOnStates?.findIndex(item => item === state);
658
+ if (currentlyVisible < 0) {
659
+ tableAction.showOnStates.push(state);
660
+ }
661
+ if (correntlyHidden >= 0) {
662
+ tableAction.hideOnStates.splice(correntlyHidden, 1);
663
+ if (tableAction.hideOnStates.length === 0) {
664
+ delete tableAction.hideOnStates;
665
+ }
666
+ }
667
+ }
668
+ }
669
+
670
+ hideOnStateTableAction(tableCode, actionCode, state) {
671
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
672
+ if (tableAction) {
673
+ if (!tableAction.hideOnStates) {
674
+ tableAction.hideOnStates = [];
675
+ }
676
+ const currentlyVisible = tableAction.showOnStates.findIndex(item => item === state);
677
+ const correntlyHidden = tableAction.hideOnStates?.findIndex(item => item === state);
678
+ if (correntlyHidden < 0) {
679
+ tableAction.hideOnStates.push(state);
680
+ }
681
+ if (currentlyVisible >= 0) {
682
+ tableAction.showOnStates.splice(currentlyVisible, 1);
683
+ if (tableAction.showOnStates.length === 0) {
684
+ delete tableAction.showOnStates;
685
+ }
686
+ }
687
+ }
688
+ }
689
+
690
+ enableOnStateTableAction(tableCode, actionCode, state) {
691
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
692
+ if (tableAction) {
693
+ if (!tableAction.enableOnStates) {
694
+ tableAction.enableOnStates = [];
695
+ }
696
+ const currentlyEnabled = tableAction.enableOnStates.findIndex(item => item === state);
697
+ const correntlyDisabled = tableAction.disableOnStates?.findIndex(item => item === state);
698
+ if (currentlyEnabled < 0) {
699
+ tableAction.enableOnStates.push(state);
700
+ }
701
+ if (correntlyDisabled >= 0) {
702
+ tableAction.disableOnStates.splice(correntlyDisabled, 1);
703
+ if (tableAction.disableOnStates.length === 0) {
704
+ delete tableAction.disableOnStates;
705
+ }
706
+ }
707
+ }
708
+ }
709
+
710
+ disableOnStateTableAction(tableCode, actionCode, state) {
711
+ const tableAction = this.getResponseTableAction(tableCode, actionCode);
712
+ if (tableAction) {
713
+ if (!tableAction.disableOnStates) {
714
+ tableAction.disableOnStates = [];
715
+ }
716
+ const currentlyEnabled = tableAction.enableOnStates.findIndex(item => item === state);
717
+ const correntlyDisabled = tableAction.disableOnStates?.findIndex(item => item === state);
718
+ if (correntlyDisabled < 0) {
719
+ tableAction.disableOnStates.push(state);
720
+ }
721
+ if (currentlyEnabled >= 0) {
722
+ tableAction.enableOnStates.splice(currentlyEnabled, 1);
723
+ if (tableAction.enableOnStates.length === 0) {
724
+ delete tableAction.enableOnStates;
725
+ }
726
+ }
727
+ }
589
728
  }
590
729
 
591
730
  async executeTableAction() {
592
- let errorDetail = '';
731
+ let message = '';
593
732
  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);
733
+ message = 'Acción de tabla requiere actionSubject';
734
+ this.logger.log({ level: 'error', label: 'executeTableAction', message });
735
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
601
736
  }
602
737
  const { tableCode, actionType = INLINE, tableRecordId, tableRecordData } = this.actionSubject;
603
738
  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);
739
+ message = 'Acción de tabla requiere tableCode';
740
+ this.logger.log({ level: 'error', label: 'executeTableAction', message });
741
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
611
742
  }
612
743
  let { actionCode } = this.actionSubject;
613
744
  if (actionType === INLINE) {
614
745
  if (!actionCode) {
615
- errorDetail = 'Acción inline requiere actionCode';
616
- return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, errorDetail);
746
+ message = 'Acción inline requiere actionCode';
747
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
617
748
  }
618
749
  if (!tableRecordId) {
619
- errorDetail = 'Acción inline requiere identificador del registro tableRecordId';
620
- return this.errMgr.get(modErrs.tableActionExec.noRecordId, errorDetail);
750
+ message = 'Acción inline requiere identificador del registro tableRecordId';
751
+ return this.errMgr.get(modErrs.tableActionExec.noRecordId, message);
621
752
  }
622
753
  } else if (actionType === ROWSELECTION) {
623
754
  actionCode = ROWSELECTION;
624
755
  if (!tableRecordId) {
625
- errorDetail = 'Acción sobre tabla requiere identificador del registro tableRecordId';
626
- return this.errMgr.get(modErrs.tableActionExec.noRecordId, errorDetail);
756
+ message = 'Acción sobre tabla requiere identificador del registro tableRecordId';
757
+ return this.errMgr.get(modErrs.tableActionExec.noRecordId, message);
627
758
  }
628
759
  } else {
629
760
  // Cualquier otro tipo de acción sobre tabla no involucra tableRecordId!!!
@@ -649,38 +780,25 @@ class Form {
649
780
  });
650
781
  errorObject = await tableActionCallback(tableCode, actionCode, tableRecordId, tableRecordData ?? {});
651
782
  } 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);
783
+ message = `Excepción no soportada en acción ${actionCode} sobre la tabla ${tableCode} : ${err.message} / ${err.stack}`;
784
+ this.logger.log({ level: 'error', label: 'tableAction', action: actionCode, message });
785
+ errorObject = this.errMgr.get(modErrs.tableActionExec.noCallback, message);
660
786
  }
661
787
  return errorObject;
662
788
  }
663
789
 
664
790
  async executeTablePopulate() {
665
- let errorDetail = '';
791
+ let message = '';
666
792
  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);
793
+ message = 'Acción de poblamiento de tabla requiere actionSubject';
794
+ this.logger.log({ level: 'error', label: 'executeTablePopulate', message });
795
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
674
796
  }
675
797
  const { tableCode } = this.actionSubject;
676
798
  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);
799
+ message = 'Acción de poblamiento de tabla requiere tableCode';
800
+ this.logger.log({ level: 'error', label: 'executeTablePopulate', message });
801
+ return this.errMgr.get(modErrs.tableActionExec.subjectIncomplete, message);
684
802
  }
685
803
  let errorObject = null;
686
804
  try {
@@ -695,14 +813,9 @@ class Form {
695
813
  }
696
814
  errorObject = await tablePopulateCallback(tableCode, this.actionSubject);
697
815
  } 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);
816
+ message = `Excepción no soportada en el poblamiento de la tabla ${tableCode} en ${this.formCode}: ${err} / ${err.stack}`;
817
+ this.logger.log({ level: 'error', label: 'executeTablePopulate', action: tableCode, message });
818
+ errorObject = this.errMgr.get(modErrs.tablePopulate.exceptionNotHandled, message);
706
819
  }
707
820
  return errorObject;
708
821
  }
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.3",
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": {