From 401d47606ca597f117b017789e8efcd3d49b550b Mon Sep 17 00:00:00 2001 From: Sinan997 Date: Tue, 14 May 2024 12:57:28 +0300 Subject: [PATCH] add with methods to theme shared provider function --- .../providers/theme-shared-config.provider.ts | 125 ++++++++++++++---- .../src/lib/theme-shared.module.ts | 19 ++- 2 files changed, 118 insertions(+), 26 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/providers/theme-shared-config.provider.ts b/npm/ng-packs/packages/theme-shared/src/lib/providers/theme-shared-config.provider.ts index 0ca68316c7..8efe332d37 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/providers/theme-shared-config.provider.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/providers/theme-shared-config.provider.ts @@ -1,4 +1,4 @@ -import { APP_INITIALIZER, ErrorHandler, makeEnvironmentProviders } from '@angular/core'; +import { APP_INITIALIZER, Provider, makeEnvironmentProviders } from '@angular/core'; import { noop } from '@abp/ng.core'; import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap'; import { @@ -8,19 +8,104 @@ import { VALIDATION_VALIDATE_ON_SUBMIT, } from '@ngx-validate/core'; import { DEFAULT_VALIDATION_BLUEPRINTS } from '../constants'; -import { DocumentDirHandlerService } from '../handlers'; -import { RootParams } from '../models'; +import { DocumentDirHandlerService, ErrorHandler } from '../handlers'; +import { HttpErrorConfig } from '../models'; import { THEME_SHARED_APPEND_CONTENT, HTTP_ERROR_CONFIG } from '../tokens'; -import { CONFIRMATION_ICONS, DEFAULT_CONFIRMATION_ICONS } from '../tokens/confirmation-icons.token'; +import { + CONFIRMATION_ICONS, + ConfirmationIcons, + DEFAULT_CONFIRMATION_ICONS, +} from '../tokens/confirmation-icons.token'; import { DateParserFormatter } from '../utils'; import { DEFAULT_HANDLERS_PROVIDERS } from './error-handlers.provider'; import { NG_BOOTSTRAP_CONFIG_PROVIDERS } from './ng-bootstrap-config.provider'; import { THEME_SHARED_ROUTE_PROVIDERS } from './route.provider'; import { tenantNotFoundProvider } from './tenant-not-found.provider'; +import { Validation } from '@ngx-validate/core'; + +export enum ThemeSharedFeatureKind { + HttpErrorConfig, + ValidationBluePrint, + ValidationErrorsFn, + ValidateOnSubmit, + Validation, + ConfirmationIcons, +} + +export interface ThemeSharedFeature { + ɵkind: KindT; + ɵproviders: Provider[]; +} + +function makeThemeSharedFeature( + kind: KindT, + providers: Provider[], +): ThemeSharedFeature { + return { + ɵkind: kind, + ɵproviders: providers, + }; +} + +export function withHttpErrorConfig( + httpErrorConfig: HttpErrorConfig, +): ThemeSharedFeature { + return makeThemeSharedFeature(ThemeSharedFeatureKind.HttpErrorConfig, [ + { + provide: HTTP_ERROR_CONFIG, + useValue: httpErrorConfig, + }, + ]); +} -export function provideAbpThemeShared( - { httpErrorConfig, validation = {}, confirmationIcons = {} } = {} as RootParams, -) { +export function withValidationBluePrint( + bluePrints: Validation.Blueprints, +): ThemeSharedFeature { + return makeThemeSharedFeature(ThemeSharedFeatureKind.ValidationBluePrint, [ + { + provide: VALIDATION_BLUEPRINTS, + useValue: { + ...DEFAULT_VALIDATION_BLUEPRINTS, + ...(bluePrints || {}), + }, + }, + ]); +} + +export function withValidationMapErrorsFn( + mapErrorsFn: Validation.MapErrorsFn, +): ThemeSharedFeature { + return makeThemeSharedFeature(ThemeSharedFeatureKind.ValidationErrorsFn, [ + { + provide: VALIDATION_MAP_ERRORS_FN, + useValue: mapErrorsFn || defaultMapErrorsFn, + }, + ]); +} + +export function withValidateOnSubmit( + validateOnSubmit: boolean, +): ThemeSharedFeature { + return makeThemeSharedFeature(ThemeSharedFeatureKind.ValidateOnSubmit, [ + { + provide: VALIDATION_VALIDATE_ON_SUBMIT, + useValue: validateOnSubmit, + }, + ]); +} + +export function withConfirmationIcon( + confirmationIcons: Partial, +): ThemeSharedFeature { + return makeThemeSharedFeature(ThemeSharedFeatureKind.HttpErrorConfig, [ + { + provide: CONFIRMATION_ICONS, + useValue: { ...DEFAULT_CONFIRMATION_ICONS, ...(confirmationIcons || {}) }, + }, + ]); +} + +export function provideAbpThemeShared(...features: ThemeSharedFeature[]) { const providers = [ { provide: APP_INITIALIZER, @@ -35,41 +120,33 @@ export function provideAbpThemeShared( deps: [THEME_SHARED_APPEND_CONTENT], useFactory: noop, }, - { provide: HTTP_ERROR_CONFIG, useValue: httpErrorConfig }, + { provide: HTTP_ERROR_CONFIG, useValue: undefined }, { provide: NgbDateParserFormatter, useClass: DateParserFormatter }, NG_BOOTSTRAP_CONFIG_PROVIDERS, { provide: VALIDATION_BLUEPRINTS, - useValue: { - ...DEFAULT_VALIDATION_BLUEPRINTS, - ...(validation.blueprints || {}), - }, + useValue: { ...DEFAULT_VALIDATION_BLUEPRINTS }, }, { provide: VALIDATION_MAP_ERRORS_FN, - useValue: validation.mapErrorsFn || defaultMapErrorsFn, + useValue: defaultMapErrorsFn, }, { provide: VALIDATION_VALIDATE_ON_SUBMIT, - useValue: validation.validateOnSubmit, + useValue: undefined, }, DocumentDirHandlerService, - { - provide: APP_INITIALIZER, - useFactory: noop, - multi: true, - deps: [DocumentDirHandlerService], - }, { provide: CONFIRMATION_ICONS, - useValue: { - ...DEFAULT_CONFIRMATION_ICONS, - ...(confirmationIcons || {}), - }, + useValue: { ...DEFAULT_CONFIRMATION_ICONS }, }, tenantNotFoundProvider, DEFAULT_HANDLERS_PROVIDERS, ]; + for (const feature of features) { + providers.push(...feature.ɵproviders); + } + return makeEnvironmentProviders(providers); } diff --git a/npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts b/npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts index 8b81c14df2..c26c95807a 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts @@ -20,7 +20,14 @@ import { LoadingDirective } from './directives/loading.directive'; import { NgxDatatableDefaultDirective } from './directives/ngx-datatable-default.directive'; import { NgxDatatableListDirective } from './directives/ngx-datatable-list.directive'; import { RootParams } from './models/common'; -import { provideAbpThemeShared } from './providers'; +import { + provideAbpThemeShared, + withConfirmationIcon, + withHttpErrorConfig, + withValidateOnSubmit, + withValidationBluePrint, + withValidationMapErrorsFn, +} from './providers'; import { PasswordComponent } from './components/password/password.component'; import { CardModule } from './components/card/card.module'; import { AbpVisibleDirective, DisabledDirective } from './directives'; @@ -86,7 +93,15 @@ export class ThemeSharedModule { ): ModuleWithProviders { return { ngModule: ThemeSharedModule, - providers: [provideAbpThemeShared({ httpErrorConfig, validation, confirmationIcons })], + providers: [ + provideAbpThemeShared( + withHttpErrorConfig(httpErrorConfig), + withValidationBluePrint(validation.blueprints), + withValidationMapErrorsFn(validation.mapErrorsFn), + withValidateOnSubmit(validation.validateOnSubmit), + withConfirmationIcon(confirmationIcons), + ), + ], }; } }