UNPKG

ng-zorro-antd

Version:

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

200 lines (193 loc) 7.24 kB
import { __decorate, __metadata } from 'tslib'; import { Platform, PlatformModule } from '@angular/cdk/platform'; import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, ChangeDetectorRef, Input, Output, ViewChild, NgModule } from '@angular/core'; import { NzConfigService, WithConfig } from 'ng-zorro-antd/core/config'; import { InputNumber } from 'ng-zorro-antd/core/util'; import { BidiModule } from '@angular/cdk/bidi'; import { CommonModule } from '@angular/common'; import { NzIconModule } from 'ng-zorro-antd/icon'; /** * 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 NZ_CONFIG_MODULE_NAME = 'avatar'; class NzAvatarComponent { constructor(nzConfigService, elementRef, cdr, platform) { this.nzConfigService = nzConfigService; this.elementRef = elementRef; this.cdr = cdr; this.platform = platform; this._nzModuleName = NZ_CONFIG_MODULE_NAME; this.nzShape = 'circle'; this.nzSize = 'default'; this.nzGap = 4; this.nzError = new EventEmitter(); this.hasText = false; this.hasSrc = true; this.hasIcon = false; this.textStyles = {}; this.classMap = {}; this.customSize = null; this.el = this.elementRef.nativeElement; // TODO: move to host after View Engine deprecation this.elementRef.nativeElement.classList.add('ant-avatar'); } imgError($event) { this.nzError.emit($event); if (!$event.defaultPrevented) { this.hasSrc = false; this.hasIcon = false; this.hasText = false; if (this.nzIcon) { this.hasIcon = true; } else if (this.nzText) { this.hasText = true; } this.cdr.detectChanges(); this.setSizeStyle(); this.notifyCalc(); } } ngOnChanges() { this.hasText = !this.nzSrc && !!this.nzText; this.hasIcon = !this.nzSrc && !!this.nzIcon; this.hasSrc = !!this.nzSrc; this.setSizeStyle(); this.notifyCalc(); } calcStringSize() { if (!this.hasText) { return; } const childrenWidth = this.textEl.nativeElement.offsetWidth; const avatarWidth = this.el.getBoundingClientRect().width; const offset = this.nzGap * 2 < avatarWidth ? this.nzGap * 2 : 8; const scale = avatarWidth - offset < childrenWidth ? (avatarWidth - offset) / childrenWidth : 1; this.textStyles = { transform: `scale(${scale}) translateX(-50%)` }; if (this.customSize) { Object.assign(this.textStyles, { lineHeight: this.customSize }); } this.cdr.detectChanges(); } notifyCalc() { // If use ngAfterViewChecked, always demands more computations, so...... if (this.platform.isBrowser) { setTimeout(() => { this.calcStringSize(); }); } } setSizeStyle() { if (typeof this.nzSize === 'number') { this.customSize = `${this.nzSize}px`; } else { this.customSize = null; } this.cdr.markForCheck(); } } NzAvatarComponent.decorators = [ { type: Component, args: [{ selector: 'nz-avatar', exportAs: 'nzAvatar', template: ` <i nz-icon *ngIf="nzIcon && hasIcon" [nzType]="nzIcon"></i> <img *ngIf="nzSrc && hasSrc" [src]="nzSrc" [attr.srcset]="nzSrcSet" [attr.alt]="nzAlt" (error)="imgError($event)" /> <span class="ant-avatar-string" #textEl [ngStyle]="textStyles" *ngIf="nzText && hasText">{{ nzText }}</span> `, host: { '[class.ant-avatar-lg]': `nzSize === 'large'`, '[class.ant-avatar-sm]': `nzSize === 'small'`, '[class.ant-avatar-square]': `nzShape === 'square'`, '[class.ant-avatar-circle]': `nzShape === 'circle'`, '[class.ant-avatar-icon]': `nzIcon`, '[class.ant-avatar-image]': `hasSrc `, '[style.width]': 'customSize', '[style.height]': 'customSize', '[style.line-height]': 'customSize', // nzSize type is number when customSize is true '[style.font-size.px]': '(hasIcon && customSize) ? $any(nzSize) / 2 : null' }, preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None },] } ]; NzAvatarComponent.ctorParameters = () => [ { type: NzConfigService }, { type: ElementRef }, { type: ChangeDetectorRef }, { type: Platform } ]; NzAvatarComponent.propDecorators = { nzShape: [{ type: Input }], nzSize: [{ type: Input }], nzGap: [{ type: Input }], nzText: [{ type: Input }], nzSrc: [{ type: Input }], nzSrcSet: [{ type: Input }], nzAlt: [{ type: Input }], nzIcon: [{ type: Input }], nzError: [{ type: Output }], textEl: [{ type: ViewChild, args: ['textEl', { static: false },] }] }; __decorate([ WithConfig(), __metadata("design:type", String) ], NzAvatarComponent.prototype, "nzShape", void 0); __decorate([ WithConfig(), __metadata("design:type", Object) ], NzAvatarComponent.prototype, "nzSize", void 0); __decorate([ WithConfig(), InputNumber(), __metadata("design:type", Object) ], NzAvatarComponent.prototype, "nzGap", void 0); /** * 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 */ class NzAvatarGroupComponent { } NzAvatarGroupComponent.decorators = [ { type: Component, args: [{ selector: 'nz-avatar-group', exportAs: 'nzAvatarGroup', template: ` <ng-content></ng-content> `, changeDetection: ChangeDetectionStrategy.OnPush, host: { class: 'ant-avatar-group' } },] } ]; /** * 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 */ class NzAvatarModule { } NzAvatarModule.decorators = [ { type: NgModule, args: [{ declarations: [NzAvatarComponent, NzAvatarGroupComponent], exports: [NzAvatarComponent, NzAvatarGroupComponent], imports: [BidiModule, CommonModule, NzIconModule, PlatformModule] },] } ]; /** * 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 { NzAvatarComponent, NzAvatarModule, NzAvatarGroupComponent as ɵa }; //# sourceMappingURL=ng-zorro-antd-avatar.js.map