@rxap/form-system 14.0.1 → 14.1.0-dev.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,11 +5,11 @@ 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';
11
+ import { getMetadata, setMetadataMapMap, setMetadataMap, setMetadata } from '@rxap/utilities/reflect-metadata';
12
+ import { map, startWith, distinctUntilChanged, shareReplay, tap, catchError, switchMap } from 'rxjs/operators';
13
13
  import { Subscription, of } from 'rxjs';
14
14
  import * as i3 from '@angular/material/form-field';
15
15
  import { MAT_FORM_FIELD } from '@angular/material/form-field';
@@ -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
@@ -484,8 +540,8 @@ let InputSelectOptionsDirective = class InputSelectOptionsDirective {
484
540
  this.options = null;
485
541
  }
486
542
  ngAfterViewInit() {
487
- this.extractControl();
488
- this.extractDatasource();
543
+ var _a;
544
+ this.extractOptionsDatasource();
489
545
  this.viewer = {
490
546
  id: '[rxapInputSelectOptions]' + this.control.controlId,
491
547
  viewChange: this.control.value$.pipe(map(value => {
@@ -496,37 +552,12 @@ let InputSelectOptionsDirective = class InputSelectOptionsDirective {
496
552
  })),
497
553
  control: this.control,
498
554
  };
499
- this.loadOptions();
500
555
  if (this.matAutocomplete) {
501
556
  this.matAutocomplete.displayWith = this.toDisplay.bind(this);
557
+ this.settings ?? (this.settings = {});
558
+ (_a = this.settings).filteredOptions ?? (_a.filteredOptions = true);
502
559
  }
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;
560
+ this.loadOptions();
530
561
  }
531
562
  toDisplay(value) {
532
563
  if (!value) {
@@ -534,25 +565,32 @@ let InputSelectOptionsDirective = class InputSelectOptionsDirective {
534
565
  }
535
566
  return this.options?.find((option) => option.value === value)?.display ?? (isDevMode() ? 'to display error' : '');
536
567
  }
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
568
  ngOnDestroy() {
552
569
  this.subscription?.unsubscribe();
553
570
  }
554
571
  loadOptions(dataSource = this.dataSource) {
555
- this.subscription.add(dataSource.connect(this.viewer).pipe(tap(options => this.renderTemplate(this.options = options))).subscribe());
572
+ this.subscription.add(dataSource.connect(this.viewer).pipe(switchMap(options => {
573
+ if (this.settings?.filteredOptions) {
574
+ return this.control.value$.pipe(map(controlValue => {
575
+ if (typeof controlValue === 'string' && controlValue) {
576
+ controlValue = controlValue.toLowerCase();
577
+ if (Array.isArray(options)) {
578
+ return options.filter(option => option.display.toLowerCase().includes(controlValue));
579
+ }
580
+ else {
581
+ return Object.entries(options)
582
+ .filter(([value, display]) => typeof display === 'string' && display.toLowerCase().includes(controlValue))
583
+ .map(([value, display]) => ({ value, display }));
584
+ }
585
+ }
586
+ return options;
587
+ }));
588
+ }
589
+ return of(options);
590
+ }), tap(options => this.renderTemplate(this.options = options)),
591
+ // trigger a change detection after the options are rendered
592
+ // this is needed to trigger the mat-autocomplete options to display function
593
+ tap(() => this.ngControl?.control?.setValue(this.ngControl?.control?.value))).subscribe());
556
594
  }
557
595
  renderTemplate(options = this.options) {
558
596
  this.viewContainerRef.clear();
@@ -587,7 +625,7 @@ __decorate([
587
625
  __metadata("design:type", BaseDataSource)
588
626
  ], InputSelectOptionsDirective.prototype, "dataSource", void 0);
589
627
  InputSelectOptionsDirective = __decorate([
590
- Mixin(ExtractFormDefinitionMixin, ExtractDataSourcesMixin),
628
+ Mixin(ExtractOptionsDataSourceMixin),
591
629
  __metadata("design:paramtypes", [TemplateRef,
592
630
  DataSourceLoader,
593
631
  ViewContainerRef,
@@ -602,7 +640,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImpor
602
640
  }], ctorParameters: function () { return [{ type: i0.TemplateRef, decorators: [{
603
641
  type: Inject,
604
642
  args: [TemplateRef]
605
- }] }, { type: i2.DataSourceLoader, decorators: [{
643
+ }] }, { type: i1$1.DataSourceLoader, decorators: [{
606
644
  type: Inject,
607
645
  args: [DataSourceLoader]
608
646
  }] }, { type: i0.ViewContainerRef, decorators: [{
@@ -729,11 +767,92 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImpor
729
767
  }]
730
768
  }] });
731
769
 
770
+ var IsSelectableValueDirective_1;
771
+ let IsSelectableValueDirective = IsSelectableValueDirective_1 = class IsSelectableValueDirective {
772
+ constructor(dataSourceLoader, injector) {
773
+ this.dataSourceLoader = dataSourceLoader;
774
+ this.injector = injector;
775
+ this.viewer = { id: '[rxapIsSelectableValue]' };
776
+ }
777
+ validate(control) {
778
+ if (control.value) {
779
+ this.extractOptionsDatasource(control);
780
+ const data = this.useDataSourceValue?.lastValue;
781
+ if (data) {
782
+ let options;
783
+ if (Array.isArray(data)) {
784
+ options = data;
785
+ }
786
+ else {
787
+ options = Object.entries(data).map(([value, display]) => ({ value, display }));
788
+ }
789
+ if (!options.some(option => option.value === control.value)) {
790
+ return {
791
+ isSelectableValue: {
792
+ expected: 'Value should be from the list of provided options',
793
+ }
794
+ };
795
+ }
796
+ }
797
+ else if (isDevMode()) {
798
+ console.warn('The last value from the OptionsDataSource is empty', this);
799
+ }
800
+ }
801
+ return null;
802
+ }
803
+ };
804
+ 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 });
805
+ IsSelectableValueDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.2", type: IsSelectableValueDirective, selector: "[rxapIsSelectableValue]", inputs: { viewer: "viewer" }, providers: [
806
+ {
807
+ provide: NG_VALIDATORS,
808
+ multi: true,
809
+ useExisting: forwardRef(() => IsSelectableValueDirective_1)
810
+ }
811
+ ], ngImport: i0 });
812
+ IsSelectableValueDirective = IsSelectableValueDirective_1 = __decorate([
813
+ Mixin(ExtractOptionsDataSourceMixin),
814
+ __metadata("design:paramtypes", [DataSourceLoader,
815
+ Injector])
816
+ ], IsSelectableValueDirective);
817
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirective, decorators: [{
818
+ type: Directive,
819
+ args: [{
820
+ selector: '[rxapIsSelectableValue]',
821
+ providers: [
822
+ {
823
+ provide: NG_VALIDATORS,
824
+ multi: true,
825
+ useExisting: forwardRef(() => IsSelectableValueDirective)
826
+ }
827
+ ]
828
+ }]
829
+ }], ctorParameters: function () { return [{ type: i1$1.DataSourceLoader, decorators: [{
830
+ type: Inject,
831
+ args: [DataSourceLoader]
832
+ }] }, { type: i0.Injector, decorators: [{
833
+ type: Inject,
834
+ args: [INJECTOR]
835
+ }] }]; }, propDecorators: { viewer: [{
836
+ type: Input
837
+ }] } });
838
+ class IsSelectableValueDirectiveModule {
839
+ }
840
+ IsSelectableValueDirectiveModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
841
+ IsSelectableValueDirectiveModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule, declarations: [IsSelectableValueDirective], exports: [IsSelectableValueDirective] });
842
+ IsSelectableValueDirectiveModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule });
843
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: IsSelectableValueDirectiveModule, decorators: [{
844
+ type: NgModule,
845
+ args: [{
846
+ declarations: [IsSelectableValueDirective],
847
+ exports: [IsSelectableValueDirective]
848
+ }]
849
+ }] });
850
+
732
851
  // endregion
733
852
 
734
853
  /**
735
854
  * Generated bundle index. Do not edit.
736
855
  */
737
856
 
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 };
857
+ 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
858
  //# sourceMappingURL=rxap-form-system.mjs.map