@rxap/form-system 14.0.2 → 14.1.0

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.
@@ -5,12 +5,12 @@ import { RxapError, Required, equals } from '@rxap/utilities';
5
5
  import * as i1 from '@angular/forms';
6
6
  import { ControlContainer, NgControl, NG_VALIDATORS } from '@angular/forms';
7
7
  import { RxapFormControl, RxapFormGroup, RxapFormArray } from '@rxap/forms';
8
- import * as i2 from '@rxap/data-source';
8
+ import * as i1$1 from '@rxap/data-source';
9
9
  import { PipeDataSource, DataSourceLoader, BaseDataSource, RXAP_DATA_SOURCE_METADATA } from '@rxap/data-source';
10
10
  import { Mixin } from '@rxap/mixin';
11
- import { getMetadata, setMetadataMap, setMetadataMapMap, setMetadata } from '@rxap/utilities/reflect-metadata';
12
- import { map, startWith, distinctUntilChanged, shareReplay, tap, catchError } from 'rxjs/operators';
13
- import { Subscription, of } from 'rxjs';
11
+ import { getMetadata, setMetadataMapMap, setMetadataMap, setMetadata } from '@rxap/utilities/reflect-metadata';
12
+ import { map, startWith, distinctUntilChanged, shareReplay, tap, catchError, throttleTime, switchMap } from 'rxjs/operators';
13
+ import { Subscription, of, EMPTY } from 'rxjs';
14
14
  import * as i3 from '@angular/material/form-field';
15
15
  import { MAT_FORM_FIELD } from '@angular/material/form-field';
16
16
  import '@angular/material/autocomplete';
@@ -174,7 +174,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImpor
174
174
  }] }, { type: i0.ViewContainerRef, decorators: [{
175
175
  type: Inject,
176
176
  args: [ViewContainerRef]
177
- }] }, { type: i2.DataSourceLoader, decorators: [{
177
+ }] }, { type: i1$1.DataSourceLoader, decorators: [{
178
178
  type: Inject,
179
179
  args: [DataSourceLoader]
180
180
  }] }, { type: i0.Injector, decorators: [{
@@ -233,8 +233,8 @@ function controlDataSource(control) {
233
233
  return new ControlDataSource(control, { id: control.controlId });
234
234
  }
235
235
 
236
- class ExtractControlMixin {
237
- extractControl(parent, controlPath) {
236
+ class ExtractControlFromParentMixin {
237
+ extractControlFromParent(parent, controlPath) {
238
238
  if (!controlPath) {
239
239
  throw new RxapFormSystemError('The control path is empty', '');
240
240
  }
@@ -263,17 +263,82 @@ class ExtractFormDefinitionMixin {
263
263
  }
264
264
  }
265
265
 
266
- function UseComponent(component, setProperties = {}) {
266
+ function UseDataSource(dataSource, name, settings) {
267
267
  return function (target, propertyKey) {
268
- setMetadataMap(propertyKey, component, FormSystemMetadataKeys.COMPONENTS, target);
269
- setMetadataMap(propertyKey, setProperties, FormSystemMetadataKeys.COMPONENTS_PROPERTIES, target);
268
+ const value = { dataSource, settings };
269
+ setMetadataMapMap(propertyKey, name, value, FormSystemMetadataKeys.DATA_SOURCES, target);
270
270
  };
271
271
  }
272
272
 
273
- function UseDataSource(dataSource, name, settings) {
273
+ class ExtractControlMixin {
274
+ extractControl(ngControl = this.ngControl ?? this.matFormField?._control?.ngControl ?? null) {
275
+ if (!ngControl) {
276
+ throw new Error('The ngControl is not defined!');
277
+ }
278
+ if (!(ngControl instanceof NgControl)) {
279
+ throw new Error('The AbstractControlDirective is not a NgControl!');
280
+ }
281
+ this.ngControl = ngControl;
282
+ const control = this.ngControl.control;
283
+ if (!(control instanceof RxapFormControl)) {
284
+ throw new Error('Control is not a RxapFormControl!');
285
+ }
286
+ return this.control = control;
287
+ }
288
+ }
289
+
290
+ const DATA_SOURCE_NAME = 'options';
291
+ function ComposeOptionsTransformers(...fnc) {
292
+ return base => fnc.reduce((source, transform) => transform(source), base);
293
+ }
294
+ function UseOptionsDataSource(dataSource, settings) {
274
295
  return function (target, propertyKey) {
275
- const value = { dataSource, settings };
276
- setMetadataMapMap(propertyKey, name, value, FormSystemMetadataKeys.DATA_SOURCES, target);
296
+ UseDataSource(dataSource, DATA_SOURCE_NAME, settings)(target, propertyKey);
297
+ };
298
+ }
299
+ let ExtractOptionsDataSourceMixin = class ExtractOptionsDataSourceMixin {
300
+ extractOptionsDatasource(control) {
301
+ control ?? (control = this.extractControl());
302
+ if (!(control instanceof RxapFormControl)) {
303
+ throw new Error('Ensure to use a RxapFromControl');
304
+ }
305
+ const formDefinition = this.extractFormDefinition(control);
306
+ const useDataSourceValueMap = this.extractDataSources(formDefinition, control.controlId);
307
+ if (!useDataSourceValueMap.has('options')) {
308
+ throw new Error(`The data source with the name 'options' is not defined`);
309
+ }
310
+ const useDataSourceValue = this.useDataSourceValue = useDataSourceValueMap.get('options');
311
+ this.settings = this.useDataSourceValue.settings ?? {};
312
+ let dataSource;
313
+ try {
314
+ dataSource = this.dataSourceLoader.load(useDataSourceValue.dataSource, this.metadata, this.injector);
315
+ }
316
+ catch (e) {
317
+ if (e.name && e.name === 'NullInjectorError') {
318
+ if (isDevMode()) {
319
+ console.error('Cloud not inject the options data source', this.useDataSourceValue);
320
+ }
321
+ throw new Error('Cloud not inject the options data source:\n' + e.message);
322
+ }
323
+ throw e;
324
+ }
325
+ if (this.settings?.transformer) {
326
+ dataSource = new PipeDataSource(dataSource, map(this.settings.transformer), tap(options => useDataSourceValue.lastValue = options));
327
+ }
328
+ else {
329
+ dataSource = new PipeDataSource(dataSource, tap(options => useDataSourceValue.lastValue = options));
330
+ }
331
+ return this.dataSource = dataSource;
332
+ }
333
+ };
334
+ ExtractOptionsDataSourceMixin = __decorate([
335
+ Mixin(ExtractControlMixin, ExtractFormDefinitionMixin, ExtractDataSourcesMixin)
336
+ ], ExtractOptionsDataSourceMixin);
337
+
338
+ function UseComponent(component, setProperties = {}) {
339
+ return function (target, propertyKey) {
340
+ setMetadataMap(propertyKey, component, FormSystemMetadataKeys.COMPONENTS, target);
341
+ setMetadataMap(propertyKey, setProperties, FormSystemMetadataKeys.COMPONENTS_PROPERTIES, target);
277
342
  };
278
343
  }
279
344
 
@@ -322,7 +387,7 @@ let ControlHideShowDirective = class ControlHideShowDirective {
322
387
  if (!this.ngControl && !this.controlId) {
323
388
  throw new Error('If the NgControl is not injectable then the control id must be defined!');
324
389
  }
325
- const control = this.ngControl?.control ?? this.extractControl(this.parent, this.controlId);
390
+ const control = this.ngControl?.control ?? this.extractControlFromParent(this.parent, this.controlId);
326
391
  if (!(control instanceof RxapFormControl)) {
327
392
  throw new Error('The control is not an instance of RxapFormControl');
328
393
  }
@@ -414,7 +479,7 @@ let ControlHideShowDirective = class ControlHideShowDirective {
414
479
  ControlHideShowDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: ControlHideShowDirective, deps: [{ token: Renderer2 }, { token: ElementRef }, { token: ControlContainer, optional: true }, { token: NgControl, optional: true, self: true }], target: i0.ɵɵFactoryTarget.Directive });
415
480
  ControlHideShowDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.2", type: ControlHideShowDirective, selector: "[rxapControlHideShow]", inputs: { controlId: ["rxapControlHideShow", "controlId"] }, ngImport: i0 });
416
481
  ControlHideShowDirective = __decorate([
417
- Mixin(ExtractControlMixin, ExtractFormDefinitionMixin),
482
+ Mixin(ExtractControlFromParentMixin, ExtractFormDefinitionMixin),
418
483
  __metadata("design:paramtypes", [Renderer2,
419
484
  ElementRef, Object, Object])
420
485
  ], ControlHideShowDirective);
@@ -458,15 +523,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImpor
458
523
  }]
459
524
  }] });
460
525
 
461
- const DATA_SOURCE_NAME = 'options';
462
- function ComposeOptionsTransformers(...fnc) {
463
- return base => fnc.reduce((source, transform) => transform(source), base);
464
- }
465
- function UseOptionsDataSource(dataSource, settings) {
466
- return function (target, propertyKey) {
467
- UseDataSource(dataSource, DATA_SOURCE_NAME, settings)(target, propertyKey);
468
- };
469
- }
470
526
  let InputSelectOptionsDirective = class InputSelectOptionsDirective {
471
527
  constructor(template, dataSourceLoader, viewContainerRef, injector, cdr,
472
528
  // The ngControl could be unknown on construction
@@ -482,13 +538,21 @@ let InputSelectOptionsDirective = class InputSelectOptionsDirective {
482
538
  this.viewer = { id: '[rxapInputSelectOptions]' };
483
539
  this.subscription = new Subscription();
484
540
  this.options = null;
541
+ /**
542
+ * This flag is used to prevent the setValue is called for each refresh
543
+ * of the options list. This is needed because the setValue method will
544
+ * trigger a new refresh of the options list. This results in an endless
545
+ * call stack. This flag is set to true if the setValue method is called
546
+ * once.
547
+ */
548
+ this.isAutocompleteToDisplayTriggered = false;
485
549
  }
486
550
  ngAfterViewInit() {
487
- this.extractControl();
488
- this.extractDatasource();
551
+ var _a;
552
+ this.extractOptionsDatasource();
489
553
  this.viewer = {
490
554
  id: '[rxapInputSelectOptions]' + this.control.controlId,
491
- viewChange: this.control.value$.pipe(map(value => {
555
+ viewChange: this.settings?.loadOnce ? EMPTY : this.control.value$.pipe(throttleTime(1000), distinctUntilChanged((a, b) => equals(a, b)), map(value => {
492
556
  if (this.settings?.ignoreSelectedValue) {
493
557
  return {};
494
558
  }
@@ -496,37 +560,12 @@ let InputSelectOptionsDirective = class InputSelectOptionsDirective {
496
560
  })),
497
561
  control: this.control,
498
562
  };
499
- this.loadOptions();
500
563
  if (this.matAutocomplete) {
501
564
  this.matAutocomplete.displayWith = this.toDisplay.bind(this);
565
+ this.settings ?? (this.settings = {});
566
+ (_a = this.settings).filteredOptions ?? (_a.filteredOptions = true);
502
567
  }
503
- }
504
- extractDatasource(control = this.control) {
505
- const formDefinition = this.extractFormDefinition(control);
506
- const useDataSourceValueMap = this.extractDataSources(formDefinition, control.controlId);
507
- if (!useDataSourceValueMap.has(DATA_SOURCE_NAME)) {
508
- throw new Error(`The data source with the name 'options' is not defined`);
509
- }
510
- const useDataSourceValue = useDataSourceValueMap.get(DATA_SOURCE_NAME);
511
- this.settings = useDataSourceValue.settings;
512
- let dataSource;
513
- try {
514
- dataSource = this.dataSourceLoader.load(useDataSourceValue.dataSource, this.metadata, this.injector);
515
- }
516
- catch (e) {
517
- if (e.name && e.name === 'NullInjectorError') {
518
- if (isDevMode()) {
519
- console.error('Cloud not inject the options data source', useDataSourceValue);
520
- }
521
- throw new Error('Cloud not inject the options data source:\n' + e.message);
522
- }
523
- throw e;
524
- }
525
- const settings = useDataSourceValue.settings;
526
- if (settings?.transformer) {
527
- dataSource = new PipeDataSource(dataSource, map(settings.transformer));
528
- }
529
- return this.dataSource = dataSource;
568
+ this.loadOptions();
530
569
  }
531
570
  toDisplay(value) {
532
571
  if (!value) {
@@ -534,25 +573,36 @@ let InputSelectOptionsDirective = class InputSelectOptionsDirective {
534
573
  }
535
574
  return this.options?.find((option) => option.value === value)?.display ?? (isDevMode() ? 'to display error' : '');
536
575
  }
537
- extractControl(ngControl = this.ngControl ?? this.matFormField?._control?.ngControl ?? null) {
538
- if (!ngControl) {
539
- throw new Error('The ngControl is not defined!');
540
- }
541
- if (!(ngControl instanceof NgControl)) {
542
- throw new Error(`The control is not an instance of ngControl`);
543
- }
544
- this.ngControl = ngControl;
545
- const control = this.ngControl.control;
546
- if (!(control instanceof RxapFormControl)) {
547
- throw new Error('Control is not a RxapFormControl!');
548
- }
549
- return this.control = control;
550
- }
551
576
  ngOnDestroy() {
552
577
  this.subscription?.unsubscribe();
553
578
  }
554
579
  loadOptions(dataSource = this.dataSource) {
555
- this.subscription.add(dataSource.connect(this.viewer).pipe(tap(options => this.renderTemplate(this.options = options))).subscribe());
580
+ this.subscription.add(dataSource.connect(this.viewer).pipe(switchMap(options => {
581
+ if (this.settings?.filteredOptions) {
582
+ return this.control.value$.pipe(map(controlValue => {
583
+ if (typeof controlValue === 'string' && controlValue) {
584
+ controlValue = controlValue.toLowerCase();
585
+ if (Array.isArray(options)) {
586
+ return options.filter(option => option.display.toLowerCase().includes(controlValue));
587
+ }
588
+ else {
589
+ return Object.entries(options)
590
+ .filter(([value, display]) => typeof display === 'string' && display.toLowerCase().includes(controlValue))
591
+ .map(([value, display]) => ({ value, display }));
592
+ }
593
+ }
594
+ return options;
595
+ }));
596
+ }
597
+ return of(options);
598
+ }), tap(options => this.renderTemplate(this.options = options)), tap(() => {
599
+ if (this.matAutocomplete && !this.isAutocompleteToDisplayTriggered) {
600
+ this.isAutocompleteToDisplayTriggered = true;
601
+ // trigger a change detection after the options are rendered
602
+ // this is needed to trigger the mat-autocomplete options to display function
603
+ this.ngControl?.control?.setValue(this.ngControl?.control?.value);
604
+ }
605
+ })).subscribe());
556
606
  }
557
607
  renderTemplate(options = this.options) {
558
608
  this.viewContainerRef.clear();
@@ -587,7 +637,7 @@ __decorate([
587
637
  __metadata("design:type", BaseDataSource)
588
638
  ], InputSelectOptionsDirective.prototype, "dataSource", void 0);
589
639
  InputSelectOptionsDirective = __decorate([
590
- Mixin(ExtractFormDefinitionMixin, ExtractDataSourcesMixin),
640
+ Mixin(ExtractOptionsDataSourceMixin),
591
641
  __metadata("design:paramtypes", [TemplateRef,
592
642
  DataSourceLoader,
593
643
  ViewContainerRef,
@@ -602,7 +652,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImpor
602
652
  }], ctorParameters: function () { return [{ type: i0.TemplateRef, decorators: [{
603
653
  type: Inject,
604
654
  args: [TemplateRef]
605
- }] }, { type: i2.DataSourceLoader, decorators: [{
655
+ }] }, { type: i1$1.DataSourceLoader, decorators: [{
606
656
  type: Inject,
607
657
  args: [DataSourceLoader]
608
658
  }] }, { type: i0.ViewContainerRef, decorators: [{
@@ -729,11 +779,92 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImpor
729
779
  }]
730
780
  }] });
731
781
 
782
+ var IsSelectableValueDirective_1;
783
+ let IsSelectableValueDirective = IsSelectableValueDirective_1 = class IsSelectableValueDirective {
784
+ constructor(dataSourceLoader, injector) {
785
+ this.dataSourceLoader = dataSourceLoader;
786
+ this.injector = injector;
787
+ this.viewer = { id: '[rxapIsSelectableValue]' };
788
+ }
789
+ validate(control) {
790
+ if (control.value) {
791
+ this.extractOptionsDatasource(control);
792
+ const data = this.useDataSourceValue?.lastValue;
793
+ if (data) {
794
+ let options;
795
+ if (Array.isArray(data)) {
796
+ options = data;
797
+ }
798
+ else {
799
+ options = Object.entries(data).map(([value, display]) => ({ value, display }));
800
+ }
801
+ if (!options.some(option => option.value === control.value)) {
802
+ return {
803
+ isSelectableValue: {
804
+ expected: 'Value should be from the list of provided options',
805
+ }
806
+ };
807
+ }
808
+ }
809
+ else if (isDevMode()) {
810
+ console.warn('The last value from the OptionsDataSource is empty', this);
811
+ }
812
+ }
813
+ return null;
814
+ }
815
+ };
816
+ IsSelectableValueDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirective, deps: [{ token: DataSourceLoader }, { token: INJECTOR }], target: i0.ɵɵFactoryTarget.Directive });
817
+ IsSelectableValueDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.2", type: IsSelectableValueDirective, selector: "[rxapIsSelectableValue]", inputs: { viewer: "viewer" }, providers: [
818
+ {
819
+ provide: NG_VALIDATORS,
820
+ multi: true,
821
+ useExisting: forwardRef(() => IsSelectableValueDirective_1)
822
+ }
823
+ ], ngImport: i0 });
824
+ IsSelectableValueDirective = IsSelectableValueDirective_1 = __decorate([
825
+ Mixin(ExtractOptionsDataSourceMixin),
826
+ __metadata("design:paramtypes", [DataSourceLoader,
827
+ Injector])
828
+ ], IsSelectableValueDirective);
829
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirective, decorators: [{
830
+ type: Directive,
831
+ args: [{
832
+ selector: '[rxapIsSelectableValue]',
833
+ providers: [
834
+ {
835
+ provide: NG_VALIDATORS,
836
+ multi: true,
837
+ useExisting: forwardRef(() => IsSelectableValueDirective)
838
+ }
839
+ ]
840
+ }]
841
+ }], ctorParameters: function () { return [{ type: i1$1.DataSourceLoader, decorators: [{
842
+ type: Inject,
843
+ args: [DataSourceLoader]
844
+ }] }, { type: i0.Injector, decorators: [{
845
+ type: Inject,
846
+ args: [INJECTOR]
847
+ }] }]; }, propDecorators: { viewer: [{
848
+ type: Input
849
+ }] } });
850
+ class IsSelectableValueDirectiveModule {
851
+ }
852
+ IsSelectableValueDirectiveModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
853
+ IsSelectableValueDirectiveModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule, declarations: [IsSelectableValueDirective], exports: [IsSelectableValueDirective] });
854
+ IsSelectableValueDirectiveModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule });
855
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule, decorators: [{
856
+ type: NgModule,
857
+ args: [{
858
+ declarations: [IsSelectableValueDirective],
859
+ exports: [IsSelectableValueDirective]
860
+ }]
861
+ }] });
862
+
732
863
  // endregion
733
864
 
734
865
  /**
735
866
  * Generated bundle index. Do not edit.
736
867
  */
737
868
 
738
- export { ComposeOptionsTransformers, ControlDataSource, ControlHideShowDirective, ControlHideShowDirectiveModule, DATA_SOURCE_NAME, ExtractControlMixin, ExtractDataSourcesMixin, ExtractFormDefinitionMixin, FormSystemControlDirective, FormSystemDirectivesModule, FormSystemMetadataKeys, FormSystemModule, InputSelectOptionsDirective, InputSelectOptionsDirectiveModule, IsEqualToDirective, IsEqualToDirectiveModule, RXAP_FORM_CONTROL_SOURCE, RXAP_FORM_SYSTEM_HIDE_METADATA, RXAP_FORM_SYSTEM_HIDE_SHOW_OPTIONS, RXAP_FORM_SYSTEM_SHOW_METADATA, RxapFormSystemError, UseComponent, UseDataSource, UseFormComponent, UseHideFunction, UseOptionsDataSource, UseShowFunction, controlDataSource };
869
+ export { ComposeOptionsTransformers, ControlDataSource, ControlHideShowDirective, ControlHideShowDirectiveModule, DATA_SOURCE_NAME, ExtractControlFromParentMixin, ExtractControlMixin, ExtractDataSourcesMixin, ExtractFormDefinitionMixin, ExtractOptionsDataSourceMixin, FormSystemControlDirective, FormSystemDirectivesModule, FormSystemMetadataKeys, FormSystemModule, InputSelectOptionsDirective, InputSelectOptionsDirectiveModule, IsEqualToDirective, IsEqualToDirectiveModule, IsSelectableValueDirective, IsSelectableValueDirectiveModule, RXAP_FORM_CONTROL_SOURCE, RXAP_FORM_SYSTEM_HIDE_METADATA, RXAP_FORM_SYSTEM_HIDE_SHOW_OPTIONS, RXAP_FORM_SYSTEM_SHOW_METADATA, RxapFormSystemError, UseComponent, UseDataSource, UseFormComponent, UseHideFunction, UseOptionsDataSource, UseShowFunction, controlDataSource };
739
870
  //# sourceMappingURL=rxap-form-system.mjs.map