UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

101 lines (95 loc) 4.13 kB
import { InjectionToken, ɵɵdefineInjectable, ɵɵinject, Injectable, Optional, Inject } from '@angular/core'; import { Subject } from 'rxjs'; import { filter, mapTo } from 'rxjs/operators'; /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ /** * User should provide an object implements this interface to set global configurations. */ const NZ_CONFIG = new InjectionToken('nz-config'); /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ const isDefined = function (value) { return value !== undefined; }; const ɵ0 = isDefined; class NzConfigService { constructor(defaultConfig) { this.configUpdated$ = new Subject(); this.config = defaultConfig || {}; } getConfigForComponent(componentName) { return this.config[componentName]; } getConfigChangeEventForComponent(componentName) { return this.configUpdated$.pipe(filter(n => n === componentName), mapTo(undefined)); } set(componentName, value) { this.config[componentName] = Object.assign(Object.assign({}, this.config[componentName]), value); this.configUpdated$.next(componentName); } } NzConfigService.ɵprov = ɵɵdefineInjectable({ factory: function NzConfigService_Factory() { return new NzConfigService(ɵɵinject(NZ_CONFIG, 8)); }, token: NzConfigService, providedIn: "root" }); NzConfigService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; NzConfigService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [NZ_CONFIG,] }] } ]; // tslint:disable:no-invalid-this /** * This decorator is used to decorate properties. If a property is decorated, it would try to load default value from * config. */ // tslint:disable-next-line:typedef function WithConfig() { return function ConfigDecorator(target, propName, originalDescriptor) { const privatePropName = `$$__assignedValue__${propName}`; Object.defineProperty(target, privatePropName, { configurable: true, writable: true, enumerable: false }); return { get() { const originalValue = (originalDescriptor === null || originalDescriptor === void 0 ? void 0 : originalDescriptor.get) ? originalDescriptor.get.bind(this)() : this[privatePropName]; const assignedByUser = ((this.assignmentCount || {})[propName] || 0) > 1; if (assignedByUser && isDefined(originalValue)) { return originalValue; } const componentConfig = this.nzConfigService.getConfigForComponent(this._nzModuleName) || {}; const configValue = componentConfig[propName]; const ret = isDefined(configValue) ? configValue : originalValue; return ret; }, set(value) { // If the value is assigned, we consider the newly assigned value as 'assigned by user'. this.assignmentCount = this.assignmentCount || {}; this.assignmentCount[propName] = (this.assignmentCount[propName] || 0) + 1; if (originalDescriptor === null || originalDescriptor === void 0 ? void 0 : originalDescriptor.set) { originalDescriptor.set.bind(this)(value); } else { this[privatePropName] = value; } }, configurable: true, enumerable: true }; }; } /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ /** * Generated bundle index. Do not edit. */ export { NZ_CONFIG, NzConfigService, WithConfig, ɵ0 }; //# sourceMappingURL=ng-zorro-antd-core-config.js.map