ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
106 lines (100 loc) • 4.29 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, 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 || {};
}
getConfig() {
return this.config;
}
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 = i0.ɵɵdefineInjectable({ factory: function NzConfigService_Factory() { return new NzConfigService(i0.ɵɵ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,] }] }
];
/* eslint-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.
*/
// eslint-disable-next-line
function WithConfig() {
return function ConfigDecorator(target, propName, originalDescriptor) {
const privatePropName = `$$__zorroConfigDecorator__${propName}`;
Object.defineProperty(target, privatePropName, {
configurable: true,
writable: true,
enumerable: false
});
return {
get() {
var _a, _b;
const originalValue = (originalDescriptor === null || originalDescriptor === void 0 ? void 0 : originalDescriptor.get) ? originalDescriptor.get.bind(this)() : this[privatePropName];
const assignedByUser = (((_a = this.propertyAssignCounter) === null || _a === void 0 ? void 0 : _a[propName]) || 0) > 1;
const configValue = (_b = this.nzConfigService.getConfigForComponent(this._nzModuleName)) === null || _b === void 0 ? void 0 : _b[propName];
if (assignedByUser && isDefined(originalValue)) {
return originalValue;
}
else {
return isDefined(configValue) ? configValue : originalValue;
}
},
set(value) {
// If the value is assigned, we consider the newly assigned value as 'assigned by user'.
this.propertyAssignCounter = this.propertyAssignCounter || {};
this.propertyAssignCounter[propName] = (this.propertyAssignCounter[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