@rxap/form-system 18.1.3 → 18.2.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.
@@ -1,21 +1,23 @@
1
1
  import * as i0 from '@angular/core';
2
- import { forwardRef, Directive, Input, isDevMode, INJECTOR, Injector, Inject, runInInjectionContext, SkipSelf, Optional, HostBinding, HostListener, Renderer2, ElementRef, Self, TemplateRef, ViewContainerRef, ChangeDetectorRef, NgModule, inject, InjectionToken, Injectable, ComponentFactoryResolver } from '@angular/core';
2
+ import { forwardRef, Directive, Input, isDevMode, INJECTOR, Injector, Inject, runInInjectionContext, SkipSelf, Optional, HostBinding, HostListener, inject, ViewContainerRef, ChangeDetectorRef, TemplateRef, Renderer2, ElementRef, Self, NgModule, InjectionToken, Injectable, ComponentFactoryResolver } from '@angular/core';
3
3
  import * as i1$1 from '@angular/forms';
4
- import { NG_VALIDATORS, UntypedFormGroup, UntypedFormArray, ControlContainer, NgControl } from '@angular/forms';
4
+ import { NG_VALIDATORS, UntypedFormGroup, UntypedFormArray, NgControl, ControlContainer } from '@angular/forms';
5
5
  import { __decorate, __metadata } from 'tslib';
6
6
  import * as i1 from '@rxap/data-source';
7
7
  import { PipeDataSource, DataSourceLoader, BaseDataSource, RXAP_DATA_SOURCE_METADATA } from '@rxap/data-source';
8
8
  import { Mixin } from '@rxap/mixin';
9
- import { RxapFormControl, RxapFormGroup, RxapFormArray, RXAP_FORM_CONTEXT } from '@rxap/forms';
9
+ import { RxapFormControl, RxapFormGroup, RxapFormArray, controlValueChanges$, RXAP_FORM_CONTEXT } from '@rxap/forms';
10
10
  import { map, tap, startWith as startWith$1, catchError, throttleTime, distinctUntilChanged as distinctUntilChanged$1, switchMap, shareReplay } from 'rxjs/operators';
11
11
  import { setMetadataMapMap, getMetadata, setMetadataMap, setMetadata } from '@rxap/reflect-metadata';
12
12
  import { RxapError, isPromiseLike, isPromise, equals } from '@rxap/utilities';
13
13
  import { ToMethod } from '@rxap/pattern';
14
14
  import { debounceTime, map as map$1, distinctUntilChanged, startWith, pairwise, tap as tap$1, Subscription, of, EMPTY } from 'rxjs';
15
15
  import * as i2 from '@angular/material/button';
16
- import * as i3 from '@angular/material/form-field';
17
- import { MAT_FORM_FIELD, MatFormField } from '@angular/material/form-field';
18
16
  import '@angular/material/autocomplete';
17
+ import { isUUID } from '@rxap/validator';
18
+ import { isDefined } from '@rxap/rxjs';
19
+ import * as i3 from '@angular/material/form-field';
20
+ import { MatFormField, MAT_FORM_FIELD } from '@angular/material/form-field';
19
21
  import * as i2$1 from '@rxap/remote-method';
20
22
  import { RemoteMethodLoader, ProxyRemoteMethod } from '@rxap/remote-method';
21
23
 
@@ -706,6 +708,237 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0", ngImpor
706
708
  type: Optional
707
709
  }] }] });
708
710
 
711
+ let OptionsFromMethodDirective = class OptionsFromMethodDirective {
712
+ constructor() {
713
+ this.options = null;
714
+ this.ngControl = null;
715
+ this.matFormField = null;
716
+ this.settings = {};
717
+ this.viewContainerRef = inject(ViewContainerRef);
718
+ this.injector = inject(INJECTOR);
719
+ this.cdr = inject(ChangeDetectorRef);
720
+ this.template = inject(TemplateRef);
721
+ }
722
+ static ngTemplateContextGuard(dir, ctx) {
723
+ return true;
724
+ }
725
+ async ngAfterViewInit() {
726
+ this.matFormField = this.injector.get(MatFormField, null);
727
+ this.ngControl = this.injector.get(NgControl, this.matFormField?._control.ngControl);
728
+ this.control = this.ngControl?.control ?? undefined;
729
+ this.method ??= this.extractOptionsMethod();
730
+ // ensure that the options are loaded. It is possible that the ngOnChange is triggered before the ngAfterViewInit
731
+ // then the options are not loaded.
732
+ if (!this.options) {
733
+ this.setOptions(await this.loadOptions(this.parameters));
734
+ }
735
+ }
736
+ async ngOnChanges(changes) {
737
+ // only try to load the options if the method is defined
738
+ // else the initial load will be triggered in the ngAfterViewInit
739
+ if (!this.method) {
740
+ console.debug('The method is not yet defined');
741
+ return;
742
+ }
743
+ const parametersChanges = changes['parameters'];
744
+ if (parametersChanges) {
745
+ this.setOptions(await this.loadOptions(parametersChanges.currentValue));
746
+ }
747
+ }
748
+ async loadOptions(parameters) {
749
+ return this.method.call(parameters);
750
+ }
751
+ renderTemplate() {
752
+ if (!this.options) {
753
+ throw new Error('The options are not yet loaded');
754
+ }
755
+ this.viewContainerRef.clear();
756
+ for (const option of this.options) {
757
+ this.viewContainerRef.createEmbeddedView(this.template, { $implicit: option });
758
+ }
759
+ if (this.resetOnChange !== undefined) {
760
+ this.control?.reset(this.resetOnChange);
761
+ }
762
+ this.cdr.detectChanges();
763
+ }
764
+ setOptions(options) {
765
+ if (!options) {
766
+ this.options = null;
767
+ this.viewContainerRef.clear();
768
+ }
769
+ else {
770
+ this.options = options.slice();
771
+ this.renderTemplate();
772
+ }
773
+ }
774
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: OptionsFromMethodDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
775
+ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.0", type: OptionsFromMethodDirective, isStandalone: true, selector: "[rxapOptionsFromMethod]", inputs: { parameters: ["rxapOptionsFromMethodParameters", "parameters"], resetOnChange: ["rxapOptionsFromMethodResetOnChange", "resetOnChange"], method: ["rxapOptionsFromMethodCall", "method"] }, usesOnChanges: true, ngImport: i0 }); }
776
+ };
777
+ OptionsFromMethodDirective = __decorate([
778
+ Mixin(ExtractOptionsMethodMixin)
779
+ ], OptionsFromMethodDirective);
780
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: OptionsFromMethodDirective, decorators: [{
781
+ type: Directive,
782
+ args: [{
783
+ // eslint-disable-next-line @angular-eslint/directive-selector
784
+ selector: '[rxapOptionsFromMethod]',
785
+ standalone: true,
786
+ }]
787
+ }], propDecorators: { parameters: [{
788
+ type: Input,
789
+ args: ['rxapOptionsFromMethodParameters']
790
+ }], resetOnChange: [{
791
+ type: Input,
792
+ args: ['rxapOptionsFromMethodResetOnChange']
793
+ }], method: [{
794
+ type: Input,
795
+ args: ['rxapOptionsFromMethodCall']
796
+ }] } });
797
+
798
+ function UseAutocompleteOptionsMethod(method, config = {}) {
799
+ config.adapter ??= {};
800
+ config.adapter.parameter ??= (parameters) => ({ parameters });
801
+ return UseOptionsMethod(method, config);
802
+ }
803
+ function UseAutocompleteResolveMethod(method, config = {}) {
804
+ config.adapter ??= {};
805
+ config.adapter.parameter ??= (parameters) => ({ parameters });
806
+ return UseResolveMethod(method, config);
807
+ }
808
+ let AutocompleteOptionsFromMethodDirective = class AutocompleteOptionsFromMethodDirective extends OptionsFromMethodDirective {
809
+ constructor() {
810
+ super(...arguments);
811
+ this.ngControl = null;
812
+ this.matFormField = null;
813
+ this.injector = inject(INJECTOR);
814
+ this.settings = {};
815
+ /**
816
+ * This flag is used to prevent the setValue is called for each refresh
817
+ * of the options list. This is needed because the setValue method will
818
+ * trigger a new refresh of the options list. This results in an endless
819
+ * call stack. This flag is set to true if the setValue method is called
820
+ * once.
821
+ */
822
+ this.isAutocompleteToDisplayTriggered = false;
823
+ }
824
+ static ngTemplateContextGuard(dir, ctx) {
825
+ return true;
826
+ }
827
+ ngOnDestroy() {
828
+ this._subscription?.unsubscribe();
829
+ }
830
+ async ngAfterViewInit() {
831
+ if (this.matAutocomplete) {
832
+ this.matAutocomplete.displayWith = this.toDisplay.bind(this);
833
+ this.settings ??= {};
834
+ this.settings.filteredOptions ??= true;
835
+ }
836
+ await super.ngAfterViewInit();
837
+ this.resolveMethod ??= this.extractResolveMethod();
838
+ if (!this.control) {
839
+ throw new Error('The control is not yet defined');
840
+ }
841
+ const value$ = controlValueChanges$(this.control);
842
+ this._subscription = value$.pipe(isDefined(),
843
+ // only trigger the load options or resolve value if the value is changed
844
+ // this is required because in the resolveValue method the control value is set
845
+ // to trigger the toDisplay function in the mat-autocomplete
846
+ distinctUntilChanged(), tap$1(async (value) => {
847
+ this.setOptions(await this.loadOptions(this.parameters));
848
+ if (this.isValue(value)) {
849
+ this.triggerAutocompleteToDisplay();
850
+ }
851
+ })).subscribe();
852
+ }
853
+ toDisplay(value) {
854
+ if (!value) {
855
+ return '';
856
+ }
857
+ const option = this.findOptionByValue(value);
858
+ return option?.display ?? (isDevMode() ? 'to display error' : '...');
859
+ }
860
+ isValue(value) {
861
+ return typeof value === 'string' && isUUID(value);
862
+ }
863
+ loadOptions(parameters = {}) {
864
+ if (!this.control) {
865
+ throw new Error('The control is not yet defined');
866
+ }
867
+ const value = this.control?.value;
868
+ const c_parameters = { ...parameters };
869
+ if (this.isValue(value)) {
870
+ return this.resolveValue(value, c_parameters);
871
+ }
872
+ else {
873
+ c_parameters.search ??= value;
874
+ if (!c_parameters.search) {
875
+ return Promise.resolve([]);
876
+ }
877
+ return super.loadOptions(c_parameters);
878
+ }
879
+ }
880
+ renderTemplate() {
881
+ super.renderTemplate();
882
+ if (this.matAutocomplete && !this.isAutocompleteToDisplayTriggered) {
883
+ this.isAutocompleteToDisplayTriggered = true;
884
+ this.triggerAutocompleteToDisplay();
885
+ }
886
+ }
887
+ async resolveValue(value, parameters = {}) {
888
+ if (!this.resolveMethod) {
889
+ if (isDevMode()) {
890
+ console.warn('The resolve method is not yet defined');
891
+ }
892
+ return null;
893
+ }
894
+ // only resolve the value if the option is not already loaded
895
+ if (!this.findOptionByValue(value)) {
896
+ const option = await this.resolveMethod.call({ ...parameters, value });
897
+ if (option.value !== value) {
898
+ throw new Error('The resolved value is not the same as the input value');
899
+ }
900
+ return [option];
901
+ }
902
+ return this.options;
903
+ }
904
+ triggerAutocompleteToDisplay() {
905
+ // trigger a change detection after the options are rendered
906
+ // this is needed to trigger the mat-autocomplete options to display function
907
+ this.ngControl?.control?.setValue(this.ngControl?.control?.value);
908
+ }
909
+ findOptionByValue(value) {
910
+ return this.options?.find((option) => option.value === value) ?? null;
911
+ }
912
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: AutocompleteOptionsFromMethodDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive }); }
913
+ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.0", type: AutocompleteOptionsFromMethodDirective, isStandalone: true, selector: "[rxapAutocompleteOptionsFromMethod]", inputs: { parameters: ["rxapAutocompleteOptionsFromMethodParameters", "parameters"], resetOnChange: ["rxapAutocompleteOptionsFromMethodResetOnChange", "resetOnChange"], matAutocomplete: ["rxapAutocompleteOptionsFromMethodMatAutocomplete", "matAutocomplete"], method: ["rxapAutocompleteOptionsFromMethodCall", "method"], resolveMethod: ["rxapAutocompleteOptionsFromMethodResolve", "resolveMethod"] }, usesInheritance: true, ngImport: i0 }); }
914
+ };
915
+ AutocompleteOptionsFromMethodDirective = __decorate([
916
+ Mixin(ExtractResolveMethodMixin)
917
+ ], AutocompleteOptionsFromMethodDirective);
918
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: AutocompleteOptionsFromMethodDirective, decorators: [{
919
+ type: Directive,
920
+ args: [{
921
+ // eslint-disable-next-line @angular-eslint/directive-selector
922
+ selector: '[rxapAutocompleteOptionsFromMethod]',
923
+ standalone: true,
924
+ }]
925
+ }], propDecorators: { parameters: [{
926
+ type: Input,
927
+ args: ['rxapAutocompleteOptionsFromMethodParameters']
928
+ }], resetOnChange: [{
929
+ type: Input,
930
+ args: ['rxapAutocompleteOptionsFromMethodResetOnChange']
931
+ }], matAutocomplete: [{
932
+ type: Input,
933
+ args: ['rxapAutocompleteOptionsFromMethodMatAutocomplete']
934
+ }], method: [{
935
+ type: Input,
936
+ args: ['rxapAutocompleteOptionsFromMethodCall']
937
+ }], resolveMethod: [{
938
+ type: Input,
939
+ args: ['rxapAutocompleteOptionsFromMethodResolve']
940
+ }] } });
941
+
709
942
  const RXAP_FORM_SYSTEM_SHOW_METADATA = 'rxap/form-system/show-function';
710
943
  const RXAP_FORM_SYSTEM_HIDE_METADATA = 'rxap/form-system/hide-function';
711
944
  const RXAP_FORM_SYSTEM_HIDE_SHOW_OPTIONS = 'rxap/form-system/hide-show-options';
@@ -1051,83 +1284,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0", ngImpor
1051
1284
  }]
1052
1285
  }] });
1053
1286
 
1054
- let OptionsFromMethodDirective = class OptionsFromMethodDirective {
1055
- constructor() {
1056
- this.options = null;
1057
- this.ngControl = null;
1058
- this.matFormField = null;
1059
- this.settings = {};
1060
- this.viewContainerRef = inject(ViewContainerRef);
1061
- this.injector = inject(INJECTOR);
1062
- this.cdr = inject(ChangeDetectorRef);
1063
- this.template = inject(TemplateRef);
1064
- }
1065
- static ngTemplateContextGuard(dir, ctx) {
1066
- return true;
1067
- }
1068
- async ngAfterViewInit() {
1069
- this.ngControl = this.injector.get(NgControl, null);
1070
- this.matFormField = this.injector.get(MatFormField, null);
1071
- this.method = this.extractOptionsMethod();
1072
- // ensure that the options are loaded. It is possible that the ngOnChange is triggered before the ngAfterViewInit
1073
- // then the options are not loaded.
1074
- if (!this.options) {
1075
- this.setOptions(await this.loadOptions(this.parameters));
1076
- }
1077
- }
1078
- async ngOnChanges(changes) {
1079
- // only try to load the options if the method is defined
1080
- // else the initial load will be triggered in the ngAfterViewInit
1081
- if (!this.method) {
1082
- console.debug('The method is not yet defined');
1083
- return;
1084
- }
1085
- const parametersChanges = changes['parameters'];
1086
- if (parametersChanges) {
1087
- this.setOptions(await this.loadOptions(parametersChanges.currentValue));
1088
- }
1089
- }
1090
- async loadOptions(parameters) {
1091
- return this.method.call(parameters);
1092
- }
1093
- renderTemplate() {
1094
- if (!this.options) {
1095
- throw new Error('The options are not yet loaded');
1096
- }
1097
- this.viewContainerRef.clear();
1098
- for (const option of this.options) {
1099
- this.viewContainerRef.createEmbeddedView(this.template, { $implicit: option });
1100
- }
1101
- if (this.resetOnChange !== undefined) {
1102
- this.control?.reset(this.resetOnChange);
1103
- }
1104
- this.cdr.detectChanges();
1105
- }
1106
- setOptions(options) {
1107
- this.options = options.slice();
1108
- this.renderTemplate();
1109
- }
1110
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: OptionsFromMethodDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
1111
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.0", type: OptionsFromMethodDirective, isStandalone: true, selector: "[rxapOptionsFromMethod]", inputs: { parameters: ["rxapOptionsFromMethodParameters", "parameters"], resetOnChange: ["rxapOptionsFromMethodResetOnChange", "resetOnChange"] }, usesOnChanges: true, ngImport: i0 }); }
1112
- };
1113
- OptionsFromMethodDirective = __decorate([
1114
- Mixin(ExtractOptionsMethodMixin)
1115
- ], OptionsFromMethodDirective);
1116
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: OptionsFromMethodDirective, decorators: [{
1117
- type: Directive,
1118
- args: [{
1119
- // eslint-disable-next-line @angular-eslint/directive-selector
1120
- selector: '[rxapOptionsFromMethod]',
1121
- standalone: true,
1122
- }]
1123
- }], propDecorators: { parameters: [{
1124
- type: Input,
1125
- args: ['rxapOptionsFromMethodParameters']
1126
- }], resetOnChange: [{
1127
- type: Input,
1128
- args: ['rxapOptionsFromMethodResetOnChange']
1129
- }] } });
1130
-
1131
1287
  const REMOTE_METHOD_NAME = 'options';
1132
1288
  const RXAP_USE_REMOTE_METHOD = 'rxap/form-system/remote-method/use';
1133
1289
  function UseRemoteMethod(remoteMethod, name) {
@@ -1484,5 +1640,5 @@ function SubmitContextFormAdapterFactory(remoteMethod, context) {
1484
1640
  * Generated bundle index. Do not edit.
1485
1641
  */
1486
1642
 
1487
- export { ApplyUseMethodAdapters, ComposeOptionsTransformers, ControlDataSource, ControlHideShowDirective, DATA_SOURCE_NAME, ExtractControlFromParentMixin, ExtractControlMixin, ExtractDataSourcesMixin, ExtractFormDefinitionMixin, ExtractMethodMixin, ExtractMethodsMixin, ExtractOptionsDataSourceMixin, ExtractOptionsMethodMixin, ExtractRemoteMethodsMixin, ExtractResolveMethodMixin, ForFormArrayItemsDirective, FormArrayAddItemButtonDirective, FormArrayItemButton, FormArrayItemRemoveButtonDirective, FormArrayItemRestoreButtonDirective, FormArrayRemovableDirective, FormContextFromActivatedRouteFactory, FormSystemControlDirective, FormSystemDirectivesModule, FormSystemMetadataKeys, InputSelectOptionsDirective, IsEqualToDirective, IsSelectableValueDirective, OPTIONS_METHOD_NAME, OptionsFromMethodDirective, OptionsFromRemoteMethodDirective, REMOTE_METHOD_NAME, RESOLVE_METHOD_NAME, RXAP_FORM_CONTROL_SOURCE, RXAP_FORM_SYSTEM_HIDE_METADATA, RXAP_FORM_SYSTEM_HIDE_SHOW_OPTIONS, RXAP_FORM_SYSTEM_SHOW_METADATA, RXAP_USE_METHOD, RXAP_USE_REMOTE_METHOD, ResolveByUuidParameter, ResolveByUuidResultFactory, RxapFormSystemError, SUBMIT_CONTEXT_FORM_ADAPTER_METHOD, SubmitContextFormAdapter, SubmitContextFormAdapterFactory, TestNgControl, UseComponent, UseDataSource, UseFormComponent, UseHideFunction, UseMethod, UseOptionsDataSource, UseOptionsMethod, UseOptionsRemoteMethod, UseRemoteMethod, UseResolveMethod, UseShowFunction, controlDataSource };
1643
+ export { ApplyUseMethodAdapters, AutocompleteOptionsFromMethodDirective, ComposeOptionsTransformers, ControlDataSource, ControlHideShowDirective, DATA_SOURCE_NAME, ExtractControlFromParentMixin, ExtractControlMixin, ExtractDataSourcesMixin, ExtractFormDefinitionMixin, ExtractMethodMixin, ExtractMethodsMixin, ExtractOptionsDataSourceMixin, ExtractOptionsMethodMixin, ExtractRemoteMethodsMixin, ExtractResolveMethodMixin, ForFormArrayItemsDirective, FormArrayAddItemButtonDirective, FormArrayItemButton, FormArrayItemRemoveButtonDirective, FormArrayItemRestoreButtonDirective, FormArrayRemovableDirective, FormContextFromActivatedRouteFactory, FormSystemControlDirective, FormSystemDirectivesModule, FormSystemMetadataKeys, InputSelectOptionsDirective, IsEqualToDirective, IsSelectableValueDirective, OPTIONS_METHOD_NAME, OptionsFromMethodDirective, OptionsFromRemoteMethodDirective, REMOTE_METHOD_NAME, RESOLVE_METHOD_NAME, RXAP_FORM_CONTROL_SOURCE, RXAP_FORM_SYSTEM_HIDE_METADATA, RXAP_FORM_SYSTEM_HIDE_SHOW_OPTIONS, RXAP_FORM_SYSTEM_SHOW_METADATA, RXAP_USE_METHOD, RXAP_USE_REMOTE_METHOD, ResolveByUuidParameter, ResolveByUuidResultFactory, RxapFormSystemError, SUBMIT_CONTEXT_FORM_ADAPTER_METHOD, SubmitContextFormAdapter, SubmitContextFormAdapterFactory, TestNgControl, UseAutocompleteOptionsMethod, UseAutocompleteResolveMethod, UseComponent, UseDataSource, UseFormComponent, UseHideFunction, UseMethod, UseOptionsDataSource, UseOptionsMethod, UseOptionsRemoteMethod, UseRemoteMethod, UseResolveMethod, UseShowFunction, controlDataSource };
1488
1644
  //# sourceMappingURL=rxap-form-system.mjs.map