|
|
@@ -1,57 +1,16 @@
|
|
|
-import {
|
|
|
- APP_INITIALIZER,
|
|
|
- ComponentFactory,
|
|
|
- ComponentFactoryResolver,
|
|
|
- Injectable,
|
|
|
- Injector,
|
|
|
- Provider,
|
|
|
-} from '@angular/core';
|
|
|
-import { FormControl } from '@angular/forms';
|
|
|
+import { ComponentFactoryResolver, Injectable } from '@angular/core';
|
|
|
import { Type } from '@vendure/common/lib/shared-types';
|
|
|
|
|
|
+import { FormInputComponent } from '../../common/component-registry-types';
|
|
|
import { CustomFields, CustomFieldsFragment } from '../../common/generated-types';
|
|
|
+import { ComponentRegistryService } from '../component-registry/component-registry.service';
|
|
|
|
|
|
export type CustomFieldConfigType = CustomFieldsFragment;
|
|
|
|
|
|
-export interface CustomFieldControl {
|
|
|
- formControl: FormControl;
|
|
|
- customFieldConfig: CustomFieldConfigType;
|
|
|
-}
|
|
|
+export interface CustomFieldControl extends FormInputComponent<CustomFieldConfigType> {}
|
|
|
|
|
|
export type CustomFieldEntityName = Exclude<keyof CustomFields, '__typename'>;
|
|
|
|
|
|
-/**
|
|
|
- * @description
|
|
|
- * Registers a custom component to act as the form input control for the given custom field.
|
|
|
- * This should be used in the NgModule `providers` array of your ui extension module.
|
|
|
- *
|
|
|
- * @example
|
|
|
- * ```TypeScript
|
|
|
- * \@NgModule({
|
|
|
- * imports: [SharedModule],
|
|
|
- * declarations: [MyCustomFieldControl],
|
|
|
- * providers: [
|
|
|
- * registerCustomFieldComponent('Product', 'someCustomField', MyCustomFieldControl),
|
|
|
- * ],
|
|
|
- * })
|
|
|
- * export class MyUiExtensionModule {}
|
|
|
- * ```
|
|
|
- */
|
|
|
-export function registerCustomFieldComponent(
|
|
|
- entity: CustomFieldEntityName,
|
|
|
- fieldName: string,
|
|
|
- component: Type<CustomFieldControl>,
|
|
|
-): Provider {
|
|
|
- return {
|
|
|
- provide: APP_INITIALIZER,
|
|
|
- multi: true,
|
|
|
- useFactory: (customFieldComponentService: CustomFieldComponentService) => () => {
|
|
|
- customFieldComponentService.registerCustomFieldComponent(entity, fieldName, component);
|
|
|
- },
|
|
|
- deps: [CustomFieldComponentService],
|
|
|
- };
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* This service allows the registration of custom controls for customFields.
|
|
|
*/
|
|
|
@@ -59,9 +18,10 @@ export function registerCustomFieldComponent(
|
|
|
providedIn: 'root',
|
|
|
})
|
|
|
export class CustomFieldComponentService {
|
|
|
- private registry: { [K in CustomFieldEntityName]?: { [name: string]: Type<CustomFieldControl> } } = {};
|
|
|
-
|
|
|
- constructor(private componentFactoryResolver: ComponentFactoryResolver, private injector: Injector) {}
|
|
|
+ constructor(
|
|
|
+ private componentFactoryResolver: ComponentFactoryResolver,
|
|
|
+ private componentRegistryService: ComponentRegistryService,
|
|
|
+ ) {}
|
|
|
|
|
|
/**
|
|
|
* Register a CustomFieldControl component to be used with the specified customField and entity.
|
|
|
@@ -71,21 +31,25 @@ export class CustomFieldComponentService {
|
|
|
fieldName: string,
|
|
|
component: Type<CustomFieldControl>,
|
|
|
) {
|
|
|
- if (!this.registry[entity]) {
|
|
|
- this.registry[entity] = {};
|
|
|
- }
|
|
|
- Object.assign(this.registry[entity], { [fieldName]: component });
|
|
|
+ const id = this.generateId(entity, fieldName, true);
|
|
|
+ this.componentRegistryService.registerInputComponent(id, component);
|
|
|
}
|
|
|
|
|
|
- getCustomFieldComponent(
|
|
|
- entity: CustomFieldEntityName,
|
|
|
- fieldName: string,
|
|
|
- ): ComponentFactory<CustomFieldControl> | undefined {
|
|
|
- const entityFields = this.registry[entity];
|
|
|
- const componentType = entityFields && entityFields[fieldName];
|
|
|
- if (componentType) {
|
|
|
- const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType);
|
|
|
- return componentFactory;
|
|
|
+ /**
|
|
|
+ * Checks whether a custom component is registered for the given entity custom field,
|
|
|
+ * and if so returns the ID of that component.
|
|
|
+ */
|
|
|
+ customFieldComponentExists(entity: CustomFieldEntityName, fieldName: string): string | undefined {
|
|
|
+ const id = this.generateId(entity, fieldName, true);
|
|
|
+ return this.componentRegistryService.getInputComponent(id) ? id : undefined;
|
|
|
+ }
|
|
|
+
|
|
|
+ private generateId(entity: CustomFieldEntityName, fieldName: string, isCustomField: boolean) {
|
|
|
+ let id = entity;
|
|
|
+ if (isCustomField) {
|
|
|
+ id += '-customFields';
|
|
|
}
|
|
|
+ id += '-' + fieldName;
|
|
|
+ return id;
|
|
|
}
|
|
|
}
|