UNPKG

ohayolibs

Version:

Ohayo is a set of essential modules for ohayojp.

35 lines (27 loc) 1.42 kB
import { Inject, Injectable, Optional } from '@angular/core'; import { NzSafeAny } from 'ng-zorro-antd/core/types'; import { deepMergeKey } from '../other/deep'; import { OhayoConfig, OhayoConfigKey, OHAYO_CONFIG } from './config.types'; @Injectable({ providedIn: 'root' }) export class OhayoConfigService { private config: OhayoConfig; constructor(@Optional() @Inject(OHAYO_CONFIG) defaultConfig?: OhayoConfig) { this.config = { ...defaultConfig }; } get<T extends OhayoConfigKey>(componentName: T, key?: string): OhayoConfig[T] { const res = ((this.config[componentName] as { [key: string]: NzSafeAny }) || {}) as NzSafeAny; return key ? { [key]: res[key] } : res; } merge<T extends OhayoConfigKey>(componentName: T, ...defaultValues: OhayoConfig[T][]): OhayoConfig[T] { return deepMergeKey({}, true, ...defaultValues, this.get(componentName)); } attach<T extends OhayoConfigKey>(componentThis: NzSafeAny, componentName: T, defaultValues: OhayoConfig[T]): void { Object.assign(componentThis, this.merge(componentName, defaultValues)); } attachKey<T extends OhayoConfigKey>(componentThis: NzSafeAny, componentName: T, key: string): void { Object.assign(componentThis, this.get(componentName, key)); } set<T extends OhayoConfigKey>(componentName: T, value: OhayoConfig[T]): void { this.config[componentName] = { ...this.config[componentName], ...value }; } }