UNPKG

ng-zorro-antd

Version:

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

196 lines (190 loc) 7.01 kB
import { Platform } from '@angular/cdk/platform'; import { NzUpdateHostClassService } from 'ng-zorro-antd/core'; import { CommonModule } from '@angular/common'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Input, Renderer2, ViewChild, ViewEncapsulation, NgModule } from '@angular/core'; import { NzIconModule } from 'ng-zorro-antd/icon'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class NzAvatarComponent { /** * @param {?} elementRef * @param {?} cd * @param {?} updateHostClassService * @param {?} renderer * @param {?} platform */ constructor(elementRef, cd, updateHostClassService, renderer, platform) { this.elementRef = elementRef; this.cd = cd; this.updateHostClassService = updateHostClassService; this.renderer = renderer; this.platform = platform; this.nzShape = 'circle'; this.nzSize = 'default'; this.oldAPIIcon = true; // Make the user defined icon compatible to old API. Should be removed in 2.0. // Make the user defined icon compatible to old API. Should be removed in 2.0. this.hasText = false; this.hasSrc = true; this.hasIcon = false; this.el = this.elementRef.nativeElement; this.prefixCls = 'ant-avatar'; this.sizeMap = { large: 'lg', small: 'sm' }; } /** * @template THIS * @this {THIS} * @return {THIS} */ setClass() { /** @type {?} */ const classMap = { [(/** @type {?} */ (this)).prefixCls]: true, [`${(/** @type {?} */ (this)).prefixCls}-${(/** @type {?} */ (this)).sizeMap[(/** @type {?} */ (this)).nzSize]}`]: (/** @type {?} */ (this)).sizeMap[(/** @type {?} */ (this)).nzSize], [`${(/** @type {?} */ (this)).prefixCls}-${(/** @type {?} */ (this)).nzShape}`]: (/** @type {?} */ (this)).nzShape, [`${(/** @type {?} */ (this)).prefixCls}-icon`]: (/** @type {?} */ (this)).nzIcon, [`${(/** @type {?} */ (this)).prefixCls}-image`]: (/** @type {?} */ (this)).hasSrc // downgrade after image error }; (/** @type {?} */ (this)).updateHostClassService.updateHostClass((/** @type {?} */ (this)).el, classMap); (/** @type {?} */ (this)).cd.detectChanges(); return (/** @type {?} */ (this)); } /** * @return {?} */ imgError() { this.hasSrc = false; this.hasIcon = false; this.hasText = false; if (this.nzIcon) { this.hasIcon = true; } else if (this.nzText) { this.hasText = true; } this.setClass().notifyCalc(); this.setSizeStyle(); } /** * @param {?} changes * @return {?} */ ngOnChanges(changes) { if (changes.hasOwnProperty('nzIcon') && changes.nzIcon.currentValue) { this.oldAPIIcon = changes.nzIcon.currentValue.indexOf('anticon') > -1; } this.hasText = !this.nzSrc && !!this.nzText; this.hasIcon = !this.nzSrc && !!this.nzIcon; this.hasSrc = !!this.nzSrc; this.setClass().notifyCalc(); this.setSizeStyle(); } /** * @private * @return {?} */ calcStringSize() { if (!this.hasText) { return; } /** @type {?} */ const childrenWidth = this.textEl.nativeElement.offsetWidth; /** @type {?} */ const avatarWidth = this.el.getBoundingClientRect().width; /** @type {?} */ const scale = avatarWidth - 8 < childrenWidth ? (avatarWidth - 8) / childrenWidth : 1; this.textStyles = { transform: `scale(${scale}) translateX(-50%)` }; if (typeof this.nzSize === 'number') { Object.assign(this.textStyles, { lineHeight: `${this.nzSize}px` }); } this.cd.detectChanges(); } /** * @private * @template THIS * @this {THIS} * @return {THIS} */ notifyCalc() { // If use ngAfterViewChecked, always demands more computations, so...... if ((/** @type {?} */ (this)).platform.isBrowser) { setTimeout((/** * @return {?} */ () => { (/** @type {?} */ (this)).calcStringSize(); })); } return (/** @type {?} */ (this)); } /** * @private * @return {?} */ setSizeStyle() { if (typeof this.nzSize === 'string') { return; } this.renderer.setStyle(this.el, 'width', `${this.nzSize}px`); this.renderer.setStyle(this.el, 'height', `${this.nzSize}px`); this.renderer.setStyle(this.el, 'line-height', `${this.nzSize}px`); if (this.hasIcon) { this.renderer.setStyle(this.el, 'font-size', `${this.nzSize / 2}px`); } } } NzAvatarComponent.decorators = [ { type: Component, args: [{ selector: 'nz-avatar', exportAs: 'nzAvatar', template: "<i nz-icon *ngIf=\"nzIcon && hasIcon\" [type]=\"!oldAPIIcon && nzIcon\" [ngClass]=\"oldAPIIcon && nzIcon\"></i>\n<img [src]=\"nzSrc\" *ngIf=\"nzSrc && hasSrc\" (error)=\"imgError()\"/>\n<span class=\"ant-avatar-string\" #textEl [ngStyle]=\"textStyles\" *ngIf=\"nzText && hasText\">{{ nzText }}</span>", providers: [NzUpdateHostClassService], preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None }] } ]; /** @nocollapse */ NzAvatarComponent.ctorParameters = () => [ { type: ElementRef }, { type: ChangeDetectorRef }, { type: NzUpdateHostClassService }, { type: Renderer2 }, { type: Platform } ]; NzAvatarComponent.propDecorators = { nzShape: [{ type: Input }], nzSize: [{ type: Input }], nzText: [{ type: Input }], nzSrc: [{ type: Input }], nzIcon: [{ type: Input }], textEl: [{ type: ViewChild, args: ['textEl',] }] }; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class NzAvatarModule { } NzAvatarModule.decorators = [ { type: NgModule, args: [{ declarations: [NzAvatarComponent], exports: [NzAvatarComponent], imports: [CommonModule, NzIconModule] },] } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { NzAvatarComponent, NzAvatarModule }; //# sourceMappingURL=ng-zorro-antd-avatar.js.map