@stemy/ngx-dynamic-form 13.2.4 → 13.2.6

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.
@@ -1,6 +1,6 @@
1
1
  import * as i1 from '@ng-dynamic-forms/core';
2
- import { DynamicInputControlModel, DYNAMIC_FORM_CONTROL_TYPE_EDITOR, serializable, DynamicFormArrayGroupModel as DynamicFormArrayGroupModel$1, DynamicFormArrayModel as DynamicFormArrayModel$1, DynamicFormOption as DynamicFormOption$1, DynamicSelectModel as DynamicSelectModel$1, DynamicCheckboxModel, DynamicDatePickerModel, DynamicFormGroupModel, DynamicInputModel, DynamicTextAreaModel, DynamicFileUploadModel, DynamicFormService as DynamicFormService$1, DynamicFormValueControlModel, DynamicFormComponent, DynamicTemplateDirective, DynamicFormControlContainerComponent, DYNAMIC_FORM_CONTROL_TYPE_ARRAY, DynamicFormArrayComponent, DynamicFormControlComponent, DynamicFormGroupComponent, DYNAMIC_FORM_CONTROL_MAP_FN, DYNAMIC_VALIDATORS } from '@ng-dynamic-forms/core';
3
- export { DYNAMIC_FORM_CONTROL_TYPE_ARRAY, DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX, DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX_GROUP, DYNAMIC_FORM_CONTROL_TYPE_DATEPICKER, DYNAMIC_FORM_CONTROL_TYPE_EDITOR, DYNAMIC_FORM_CONTROL_TYPE_FILE_UPLOAD, DYNAMIC_FORM_CONTROL_TYPE_GROUP, DYNAMIC_FORM_CONTROL_TYPE_INPUT, DYNAMIC_FORM_CONTROL_TYPE_RADIO_GROUP, DYNAMIC_FORM_CONTROL_TYPE_SELECT, DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA, DynamicCheckboxGroupModel, DynamicCheckboxModel, DynamicDatePickerModel, DynamicFileUploadModel, DynamicFormControlComponent, DynamicFormGroupModel, DynamicFormsCoreModule, DynamicInputModel, DynamicListDirective, DynamicRadioGroupModel, DynamicTemplateDirective, DynamicTextAreaModel } from '@ng-dynamic-forms/core';
2
+ import { DynamicInputControlModel, DYNAMIC_FORM_CONTROL_TYPE_EDITOR, serializable, DynamicFormArrayGroupModel as DynamicFormArrayGroupModel$1, DynamicFormArrayModel as DynamicFormArrayModel$1, DynamicFormGroupModel as DynamicFormGroupModel$1, DynamicFormOption as DynamicFormOption$1, DynamicSelectModel as DynamicSelectModel$1, DynamicCheckboxModel, DynamicDatePickerModel, DynamicInputModel, DynamicTextAreaModel, DynamicFileUploadModel, DynamicFormService as DynamicFormService$1, DynamicFormValueControlModel, DynamicFormComponent, DynamicTemplateDirective, DynamicFormControlContainerComponent, DYNAMIC_FORM_CONTROL_TYPE_ARRAY, DynamicFormArrayComponent, DynamicFormControlComponent, DynamicFormGroupComponent, DYNAMIC_FORM_CONTROL_MAP_FN, DYNAMIC_VALIDATORS } from '@ng-dynamic-forms/core';
3
+ export { DYNAMIC_FORM_CONTROL_TYPE_ARRAY, DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX, DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX_GROUP, DYNAMIC_FORM_CONTROL_TYPE_DATEPICKER, DYNAMIC_FORM_CONTROL_TYPE_EDITOR, DYNAMIC_FORM_CONTROL_TYPE_FILE_UPLOAD, DYNAMIC_FORM_CONTROL_TYPE_GROUP, DYNAMIC_FORM_CONTROL_TYPE_INPUT, DYNAMIC_FORM_CONTROL_TYPE_RADIO_GROUP, DYNAMIC_FORM_CONTROL_TYPE_SELECT, DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA, DynamicCheckboxGroupModel, DynamicCheckboxModel, DynamicDatePickerModel, DynamicFileUploadModel, DynamicFormControlComponent, DynamicFormsCoreModule, DynamicInputModel, DynamicListDirective, DynamicRadioGroupModel, DynamicTemplateDirective, DynamicTextAreaModel } from '@ng-dynamic-forms/core';
4
4
  import * as i2 from '@stemy/ngx-utils';
5
5
  import { ObjectUtils, TimerUtils, StringUtils, OpenApiService, TOASTER_SERVICE, ObservableUtils, EventsService, NgxUtilsModule } from '@stemy/ngx-utils';
6
6
  import { __decorate } from 'tslib';
@@ -148,6 +148,31 @@ class DynamicFormArrayModel extends DynamicFormArrayModel$1 {
148
148
  }
149
149
  }
150
150
 
151
+ class DynamicFormGroupModel extends DynamicFormGroupModel$1 {
152
+ constructor(config, layout) {
153
+ super(config, layout);
154
+ const controls = [...config.group];
155
+ const groups = [];
156
+ const sets = config.fieldSets || [];
157
+ for (const fs of sets) {
158
+ const fields = [];
159
+ for (const f of fs.fields) {
160
+ const ix = controls.findIndex(c => c.id === f);
161
+ if (ix < 0)
162
+ continue;
163
+ fields.push(controls.splice(ix, 1)[0]);
164
+ }
165
+ if (fields.length === 0)
166
+ continue;
167
+ groups.push({ id: fs.id, legend: fs.legend, fields });
168
+ }
169
+ if (controls.length > 0) {
170
+ groups.unshift({ id: "root-controls", legend: config.legend, fields: controls });
171
+ }
172
+ this.groups = groups;
173
+ }
174
+ }
175
+
151
176
  const ignoredKeys = ["disabled", "label", "value", "classes"];
152
177
  class DynamicFormOption extends DynamicFormOption$1 {
153
178
  constructor(config) {
@@ -429,11 +454,13 @@ class DynamicFormService extends DynamicFormService$1 {
429
454
  return this.api.language;
430
455
  }
431
456
  patchGroup(value, formModel, formGroup) {
457
+ value = ObjectUtils.copy(value);
432
458
  this.patchValueRecursive(value, formModel, formGroup);
433
- formGroup.patchValue(ObjectUtils.copy(value));
459
+ formGroup.patchValue(value);
434
460
  this.detectChanges();
435
461
  }
436
462
  patchForm(value, component) {
463
+ value = ObjectUtils.copy(value);
437
464
  this.patchValueRecursive(value, component.model, component.group);
438
465
  component.group.patchValue(value);
439
466
  this.detectChanges(component);
@@ -451,11 +478,9 @@ class DynamicFormService extends DynamicFormService$1 {
451
478
  patchValueRecursive(value, formModel, formGroup) {
452
479
  if (!value)
453
480
  return;
454
- Object.keys(value).forEach(key => {
455
- const subModel = this.findModelById(key, formModel);
481
+ formModel?.forEach(subModel => {
482
+ const key = subModel.id;
456
483
  const subValue = value[key];
457
- if (!subModel)
458
- return;
459
484
  const subControl = this.findControlByModel(subModel, formGroup);
460
485
  if (subModel instanceof DynamicSelectModel && ObjectUtils.isObject(subValue)) {
461
486
  value[key] = subValue.id || subValue._id || subValue;
@@ -510,16 +535,16 @@ class DynamicFormService extends DynamicFormService$1 {
510
535
  result[subModel.id] = resArray;
511
536
  continue;
512
537
  }
513
- if (subModel instanceof DynamicFormGroupModel) {
514
- result[subModel.id] = await this.serializeRecursive(subModel.group, subControl);
515
- continue;
516
- }
517
538
  if (subModel instanceof DynamicInputModel && !ObjectUtils.isNullOrUndefined(subControl.value)) {
518
539
  result[subModel.id] = subModel.inputType == "number"
519
540
  ? parseFloat((`${subControl.value}` || "0").replace(/,/gi, ".")) ?? null
520
541
  : subControl.value;
521
542
  continue;
522
543
  }
544
+ if (subModel instanceof DynamicFormGroupModel) {
545
+ result[subModel.id] = await this.serializeRecursive(subModel.group, subControl);
546
+ continue;
547
+ }
523
548
  result[subModel.id] = subControl.value;
524
549
  }
525
550
  return result;
@@ -589,8 +614,12 @@ class DynamicFormService extends DynamicFormService$1 {
589
614
  return isNaN(date) ? new Date() : date;
590
615
  }
591
616
  async getFormModelForSchema(name, customizeModel) {
617
+ return (await this.getFormGroupModelForSchema(name, customizeModel)).group;
618
+ }
619
+ async getFormGroupModelForSchema(name, customizeModel) {
592
620
  this.api.cache = {};
593
621
  this.schemas = await this.openApi.getSchemas();
622
+ const fieldSets = [];
594
623
  const customizeModels = async (property, schema, modelType, config) => {
595
624
  const model = new modelType(config);
596
625
  if (model instanceof DynamicFormValueControlModel) {
@@ -608,13 +637,12 @@ class DynamicFormService extends DynamicFormService$1 {
608
637
  return Array.isArray(res) ? res : [res];
609
638
  };
610
639
  const schema = this.schemas[name];
611
- const controls = await this.getFormModelForSchemaDef(schema, customizeModels);
612
- // Check if we have controls to customize
613
- if (!controls || controls.length == 0) {
614
- return [];
615
- }
616
- // If we have then lets create a root wrapper that contains them
617
- const config = { id: "root", group: [...controls] };
640
+ const controls = await this.getFormModelForSchemaDef(schema, fieldSets, customizeModels);
641
+ const config = {
642
+ id: "root",
643
+ group: [...controls],
644
+ fieldSets
645
+ };
618
646
  const root = await customizeModels({
619
647
  id: "root",
620
648
  type: "object",
@@ -624,23 +652,36 @@ class DynamicFormService extends DynamicFormService$1 {
624
652
  if (Array.isArray(root)) {
625
653
  controls.length = 0;
626
654
  for (const model of root) {
627
- if (model instanceof DynamicFormGroupModel) {
628
- controls.push(...model.group);
655
+ if (model instanceof DynamicFormGroupModel && model.id === "root") {
656
+ return model;
629
657
  }
630
658
  else {
631
659
  controls.push(model);
632
660
  }
633
661
  }
634
662
  }
635
- return controls.filter(t => null !== t);
663
+ return new DynamicFormGroupModel({
664
+ ...config,
665
+ group: controls
666
+ });
636
667
  }
637
- async getFormModelForSchemaDef(schema, customizeModels) {
668
+ async getFormModelForSchemaDef(schema, fieldSets, customizeModels) {
638
669
  if (!schema)
639
670
  return [];
640
671
  const keys = Object.keys(schema.properties || {});
641
672
  const controls = [];
642
673
  for (const p of keys) {
643
674
  const property = schema.properties[p];
675
+ const fsName = property.hidden ? null : String(property.fieldSet || "");
676
+ if (fsName) {
677
+ const fs = fieldSets.find(t => t.id === fsName);
678
+ if (fs) {
679
+ fs.fields.push(p);
680
+ }
681
+ else {
682
+ fieldSets.push({ id: fsName, legend: `legend.${fsName}`, fields: [p] });
683
+ }
684
+ }
644
685
  const models = await this.getFormControlModels(property, schema, customizeModels);
645
686
  controls.push(...models);
646
687
  }
@@ -722,8 +763,9 @@ class DynamicFormService extends DynamicFormService$1 {
722
763
  };
723
764
  }
724
765
  async getFormArrayConfig(property, schema, customizeModels) {
766
+ const fieldSets = [];
725
767
  const subSchemas = findRefs(property).map(ref => this.schemas[ref]);
726
- const subModels = await Promise.all(subSchemas.map(s => this.getFormModelForSchemaDef(s, customizeModels)));
768
+ const subModels = await Promise.all(subSchemas.map(s => this.getFormModelForSchemaDef(s, fieldSets, customizeModels)));
727
769
  return Object.assign(this.getFormControlConfig(property, schema), {
728
770
  groupFactory: () => mergeFormModels(ObjectUtils.copy(subModels)),
729
771
  initialCount: property.initialCount || 0,
@@ -738,9 +780,11 @@ class DynamicFormService extends DynamicFormService$1 {
738
780
  });
739
781
  }
740
782
  async getFormGroupConfig(property, schema, customizeModels) {
783
+ const fieldSets = [];
741
784
  const subSchemas = findRefs(property).map(ref => this.schemas[ref]);
742
- const subModels = await Promise.all(subSchemas.map(s => this.getFormModelForSchemaDef(s, customizeModels)));
785
+ const subModels = await Promise.all(subSchemas.map(s => this.getFormModelForSchemaDef(s, fieldSets, customizeModels)));
743
786
  return Object.assign(this.getFormControlConfig(property, schema), {
787
+ fieldSets,
744
788
  group: mergeFormModels(subModels)
745
789
  });
746
790
  }
@@ -1143,6 +1187,12 @@ class DynamicBaseFormComponent extends DynamicFormComponent {
1143
1187
  this.onValueChange.emit({ ...ev, form: this });
1144
1188
  }));
1145
1189
  }
1190
+ if (changes.groupModel) {
1191
+ this.model = this.groupModel?.group;
1192
+ }
1193
+ if (changes.model) {
1194
+ this.groupModel = new DynamicFormGroupModel({ id: "root", group: this.model });
1195
+ }
1146
1196
  }
1147
1197
  ngAfterViewInit() {
1148
1198
  this.subscription = ObservableUtils.multiSubscription(ObservableUtils.subscribe({
@@ -1221,7 +1271,7 @@ class DynamicBaseFormComponent extends DynamicFormComponent {
1221
1271
  }
1222
1272
  }
1223
1273
  DynamicBaseFormComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.8", ngImport: i0, type: DynamicBaseFormComponent, deps: [{ token: DynamicFormService }, { token: EventsService }, { token: i0.ChangeDetectorRef }, { token: i1.DynamicFormComponentService }], target: i0.ɵɵFactoryTarget.Component });
1224
- DynamicBaseFormComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.8", type: DynamicBaseFormComponent, selector: "dynamic-base-form", inputs: { group: "group", model: "model", layout: "layout", labelPrefix: "labelPrefix", getComponentType: "getComponentType" }, outputs: { blur: "blur", change: "change", focus: "focus", onValueChange: "onValueChange", onStatusChange: "onStatusChange", onSubmit: "onSubmit", onDetectChanges: "onDetectChanges" }, queries: [{ propertyName: "contentTemplates", predicate: DynamicTemplateDirective }], viewQueries: [{ propertyName: "viewTemplates", predicate: DynamicTemplateDirective, descendants: true }], usesInheritance: true, usesOnChanges: true, ngImport: i0, template: "", isInline: true, changeDetection: i0.ChangeDetectionStrategy.Default });
1274
+ DynamicBaseFormComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.8", type: DynamicBaseFormComponent, selector: "dynamic-base-form", inputs: { group: "group", groupModel: "groupModel", model: "model", layout: "layout", labelPrefix: "labelPrefix", getComponentType: "getComponentType" }, outputs: { blur: "blur", change: "change", focus: "focus", onValueChange: "onValueChange", onStatusChange: "onStatusChange", onSubmit: "onSubmit", onDetectChanges: "onDetectChanges" }, queries: [{ propertyName: "contentTemplates", predicate: DynamicTemplateDirective }], viewQueries: [{ propertyName: "viewTemplates", predicate: DynamicTemplateDirective, descendants: true }], usesInheritance: true, usesOnChanges: true, ngImport: i0, template: "", isInline: true, changeDetection: i0.ChangeDetectionStrategy.Default });
1225
1275
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.8", ngImport: i0, type: DynamicBaseFormComponent, decorators: [{
1226
1276
  type: Component,
1227
1277
  args: [{
@@ -1237,6 +1287,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.8", ngImpor
1237
1287
  args: [EventsService]
1238
1288
  }] }, { type: i0.ChangeDetectorRef }, { type: i1.DynamicFormComponentService }]; }, propDecorators: { group: [{
1239
1289
  type: Input
1290
+ }], groupModel: [{
1291
+ type: Input
1240
1292
  }], model: [{
1241
1293
  type: Input
1242
1294
  }], layout: [{
@@ -1429,7 +1481,7 @@ class DynamicBaseFormArrayComponent extends DynamicFormArrayComponent {
1429
1481
  const parts = collectPathAble(model, p => p.id);
1430
1482
  if (parts.length == 0)
1431
1483
  return "";
1432
- if (model instanceof DynamicFormGroupModel) {
1484
+ if (model instanceof DynamicFormGroupModel$1) {
1433
1485
  return `form-group-${parts.join("-")}`;
1434
1486
  }
1435
1487
  return `form-control-${parts.join("-")}`;
@@ -1783,5 +1835,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.8", ngImpor
1783
1835
  * Generated bundle index. Do not edit.
1784
1836
  */
1785
1837
 
1786
- export { AsyncSubmitDirective, DynamicBaseFormArrayComponent, DynamicBaseFormComponent, DynamicBaseFormControlComponent, DynamicBaseFormControlContainerComponent, DynamicBaseFormGroupComponent, DynamicBaseSelectComponent, DynamicEditorModel, DynamicFormArrayGroupModel, DynamicFormArrayModel, DynamicFormOption, DynamicFormService, DynamicSelectModel, EDITOR_FORMATS, FormSelectSubject, FormSubject, MAX_INPUT_NUM, MIN_INPUT_NUM, NgxDynamicFormModule, createFormArray, createFormCheckbox, createFormDate, createFormEditor, createFormFile, createFormGroup, createFormInput, createFormSelect, createFormTextarea, mergeFormModels, replaceSpecialChars, validateItemsMaxLength, validateItemsMaxValue, validateItemsMinLength, validateItemsMinValue, validateJSON, validatePhone, validateRequiredTranslation };
1838
+ export { AsyncSubmitDirective, DynamicBaseFormArrayComponent, DynamicBaseFormComponent, DynamicBaseFormControlComponent, DynamicBaseFormControlContainerComponent, DynamicBaseFormGroupComponent, DynamicBaseSelectComponent, DynamicEditorModel, DynamicFormArrayGroupModel, DynamicFormArrayModel, DynamicFormGroupModel, DynamicFormOption, DynamicFormService, DynamicSelectModel, EDITOR_FORMATS, FormSelectSubject, FormSubject, MAX_INPUT_NUM, MIN_INPUT_NUM, NgxDynamicFormModule, createFormArray, createFormCheckbox, createFormDate, createFormEditor, createFormFile, createFormGroup, createFormInput, createFormSelect, createFormTextarea, mergeFormModels, replaceSpecialChars, validateItemsMaxLength, validateItemsMaxValue, validateItemsMinLength, validateItemsMinValue, validateJSON, validatePhone, validateRequiredTranslation };
1787
1839
  //# sourceMappingURL=stemy-ngx-dynamic-form.mjs.map