-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
poc: use decorator to extract property
- Loading branch information
Wendell
committed
Jun 17, 2019
1 parent
e59171b
commit 145593a
Showing
8 changed files
with
165 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
|
||
// tslint:disable no-any | ||
|
||
import { Inject, Injectable, Optional } from '@angular/core'; | ||
import { Subject } from 'rxjs'; | ||
|
||
import { NzConfig, NZ_CONFIG } from './config'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NzConfigService { | ||
configUpdated$ = new Subject<string>(); | ||
config: NzConfig; | ||
|
||
constructor(@Optional() @Inject(NZ_CONFIG) defaultConfig?: NzConfig) { | ||
this.config = defaultConfig || {}; | ||
} | ||
|
||
getDefaultConfig(componentName: keyof NzConfig, propName: string): any { | ||
const componentConfig: any = this.config[componentName] || {}; | ||
return componentConfig[propName] || null; | ||
} | ||
|
||
set(componentName: keyof NzConfig, value?: any): void { | ||
this.config[componentName] = { ...this.config[componentName], ...(value || {}) }; | ||
this.configUpdated$.next(componentName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
|
||
import { InjectionToken } from '@angular/core'; | ||
|
||
export interface NzConfig { | ||
nzButton?: NzButtonConfig; | ||
} | ||
|
||
export interface NzButtonConfig { | ||
nzSize?: 'default' | 'large' | 'small'; | ||
nzShape?: 'circle' | 'round'; | ||
nzBlock?: boolean; | ||
} | ||
|
||
/** | ||
* User should provide an object implements this interface to set global configurations. | ||
*/ | ||
export const NZ_CONFIG = new InjectionToken<NzConfig>('nz-config'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
|
||
export * from './public-api'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
|
||
export * from './config.service'; | ||
export * from './config'; | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
// import { Type } from '@angular/core'; | ||
|
||
// tslint:disable:no-invalid-this | ||
// tslint:disable:no-any | ||
|
||
// export function ComponentWithConfig<T>(type: Type<T>): (t: any) => void { | ||
// return function(target: any): void {}; | ||
// } | ||
|
||
import { NzConfigService } from 'ng-zorro-antd'; | ||
import { NzConfig } from './config'; | ||
|
||
/** | ||
* 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 | ||
export function PropWithConfig(componentName: keyof NzConfig) { | ||
return function<T>(innerDefaultValue: T): (t: any, p: string) => void { | ||
return function(target: any, propName: string): void { | ||
const privatePropName = `$$__assignedValue__${propName}`; | ||
|
||
if (Object.prototype.hasOwnProperty.call(target, privatePropName)) { | ||
console.warn(`The prop "${privatePropName}" is already exist, it will be override by ${name} decorator.`); | ||
} | ||
|
||
Object.defineProperty(target, privatePropName, { | ||
configurable: true, | ||
writable: true | ||
}); | ||
|
||
Object.defineProperty(target, propName, { | ||
get(): T { | ||
const defaultConfig = ((this.nzConfigService as NzConfigService).config[componentName] as any)[propName]; | ||
return this[privatePropName] || defaultConfig || innerDefaultValue; | ||
}, | ||
set(value?: T): void { | ||
this[privatePropName] = value; | ||
} | ||
}); | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters