@sumaris-net/ngx-components 2.0.9 → 2.1.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.
@@ -4486,7 +4486,7 @@ class FormArrayHelper {
4486
4486
  this.isEmpty = isEmpty;
4487
4487
  this._validators = options && options.validators;
4488
4488
  // empty array not allow by default
4489
- this.setAllowEmptyArray(toBoolean(options && options.allowEmptyArray, false));
4489
+ this.setAllowEmptyArray(toBoolean(options?.allowEmptyArray, false));
4490
4490
  }
4491
4491
  static getOrCreateArray(formBuilder, form, arrayName) {
4492
4492
  let arrayControl = form.get(arrayName);
@@ -4496,9 +4496,6 @@ class FormArrayHelper {
4496
4496
  }
4497
4497
  return arrayControl;
4498
4498
  }
4499
- static hasHelper(formArray) {
4500
- return formArray && !!formArray[AppFormArray.HELPER_PROPERTY];
4501
- }
4502
4499
  get allowEmptyArray() {
4503
4500
  return this._allowEmptyArray;
4504
4501
  }
@@ -4588,16 +4585,19 @@ class FormArrayHelper {
4588
4585
  }
4589
4586
  }
4590
4587
  class AppFormArray extends UntypedFormArray {
4591
- constructor(controls, validatorOrOpts, asyncValidator) {
4592
- super(controls, validatorOrOpts, asyncValidator);
4588
+ constructor(createControl, equals, isEmpty, options) {
4589
+ super([], options); // empty array not allow by default
4590
+ this.createControl = createControl;
4591
+ this.equals = equals;
4592
+ this.isEmpty = isEmpty;
4593
+ this.options = options;
4594
+ this.setAllowEmptyArray(toBoolean(options && options.allowEmptyArray, false));
4593
4595
  }
4594
- init(createControl, equals, isEmpty, options) {
4595
- this._helper = new FormArrayHelper(this, createControl, equals, isEmpty, options);
4596
- return this;
4596
+ get allowEmptyArray() {
4597
+ return this._allowEmptyArray;
4597
4598
  }
4598
- setHelper(helper) {
4599
- this._helper = helper;
4600
- return this;
4599
+ set allowEmptyArray(value) {
4600
+ this.setAllowEmptyArray(value);
4601
4601
  }
4602
4602
  /**
4603
4603
  * WIll rebuild the array, using the given values
@@ -4605,19 +4605,73 @@ class AppFormArray extends UntypedFormArray {
4605
4605
  * @param options
4606
4606
  */
4607
4607
  setValue(values, options) {
4608
- if (!this._helper) {
4609
- throw new Error('Cannot execute setValue() without an helper. Please call addHelper() before');
4608
+ setValueInArray(this, this.createControl, values);
4609
+ }
4610
+ enable(opts) {
4611
+ this.controls.forEach(c => c.enable(opts));
4612
+ }
4613
+ forEach(ite) {
4614
+ const size = this.length;
4615
+ for (let i = 0; i < size; i++) {
4616
+ const control = this.at(i);
4617
+ if (control)
4618
+ ite(control);
4610
4619
  }
4611
- this._helper.setValue(values, options);
4612
4620
  }
4613
4621
  resize(length) {
4614
- if (!this._helper) {
4615
- throw new Error('Cannot execute resize() without an helper. Please call addHelper() before');
4622
+ return resizeArray(this, this.createControl, length);
4623
+ }
4624
+ /**
4625
+ * @param value
4626
+ * @param options
4627
+ * @deprecated Use push() instead
4628
+ */
4629
+ add(value, options) {
4630
+ return pushValueInArray(this, this.createControl, this.equals, this.isEmpty, value, options);
4631
+ }
4632
+ push(value, options) {
4633
+ return pushValueInArray(this, () => this.createControl(value), this.equals, this.isEmpty, value, options);
4634
+ }
4635
+ removeAt(index) {
4636
+ // Do not remove if last criterion
4637
+ if (!this._allowEmptyArray && this.length === 1) {
4638
+ return clearValueInArray(this, this.isEmpty, index);
4639
+ }
4640
+ else {
4641
+ return removeValueInArray(this, this.isEmpty, index);
4642
+ }
4643
+ }
4644
+ clearAt(index) {
4645
+ return clearValueInArray(this, this.isEmpty, index);
4646
+ }
4647
+ isLast(index) {
4648
+ return (this.length - 1) === index;
4649
+ }
4650
+ removeAllEmpty() {
4651
+ let index = this.controls.findIndex(c => this.isEmpty(c.value));
4652
+ while (index !== -1) {
4653
+ this.removeAt(index);
4654
+ index = this.controls.findIndex(c => this.isEmpty(c.value));
4655
+ }
4656
+ }
4657
+ disable(opts) {
4658
+ this.controls.forEach(c => c.disable(opts));
4659
+ }
4660
+ /* -- internal -- */
4661
+ setAllowEmptyArray(value) {
4662
+ if (this._allowEmptyArray === value)
4663
+ return; // Skip if same
4664
+ this._allowEmptyArray = value;
4665
+ // Set required (or reste) min length validator
4666
+ if (this._allowEmptyArray) {
4667
+ this.setValidators(this.options.validators || null);
4668
+ }
4669
+ else {
4670
+ const validators = Array.isArray(this.options?.validators) ? this.options.validators : (this.options.validators ? [this.options.validators] : []);
4671
+ this.setValidators(validators.concat(SharedFormArrayValidators.requiredArrayMinLength(1)));
4616
4672
  }
4617
- this._helper.resize(length);
4618
4673
  }
4619
4674
  }
4620
- AppFormArray.HELPER_PROPERTY = '_helper';
4621
4675
  // TODO continue to use this kind of declaration ?
4622
4676
  class AppFormUtils {
4623
4677
  static isForm(object) {