ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
311 lines (305 loc) • 10.5 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Platform, PlatformModule } from '@angular/cdk/platform';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, ChangeDetectorRef, Renderer2, Input, Output, ViewChild, NgModule } from '@angular/core';
import { NzUpdateHostClassService, NzConfigService, WithConfig } from 'ng-zorro-antd/core';
import { CommonModule } from '@angular/common';
import { NzIconModule } from 'ng-zorro-antd/icon';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var NzAvatarComponent = /** @class */ (function () {
function NzAvatarComponent(nzConfigService, elementRef, cd, updateHostClassService, renderer, platform) {
this.nzConfigService = nzConfigService;
this.elementRef = elementRef;
this.cd = cd;
this.updateHostClassService = updateHostClassService;
this.renderer = renderer;
this.platform = platform;
this.nzError = new EventEmitter();
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}
*/
NzAvatarComponent.prototype.setClass = /**
* @template THIS
* @this {THIS}
* @return {THIS}
*/
function () {
var _a;
/** @type {?} */
var classMap = (_a = {},
_a[(/** @type {?} */ (this)).prefixCls] = true,
_a[(/** @type {?} */ (this)).prefixCls + "-" + (/** @type {?} */ (this)).sizeMap[(/** @type {?} */ (this)).nzSize]] = (/** @type {?} */ (this)).sizeMap[(/** @type {?} */ (this)).nzSize],
_a[(/** @type {?} */ (this)).prefixCls + "-" + (/** @type {?} */ (this)).nzShape] = (/** @type {?} */ (this)).nzShape,
_a[(/** @type {?} */ (this)).prefixCls + "-icon"] = (/** @type {?} */ (this)).nzIcon,
_a[(/** @type {?} */ (this)).prefixCls + "-image"] = (/** @type {?} */ (this)).hasSrc // downgrade after image error
,
_a);
(/** @type {?} */ (this)).updateHostClassService.updateHostClass((/** @type {?} */ (this)).el, classMap);
(/** @type {?} */ (this)).cd.detectChanges();
return (/** @type {?} */ (this));
};
/**
* @param {?} $event
* @return {?}
*/
NzAvatarComponent.prototype.imgError = /**
* @param {?} $event
* @return {?}
*/
function ($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.setClass().notifyCalc();
this.setSizeStyle();
}
};
/**
* @param {?} changes
* @return {?}
*/
NzAvatarComponent.prototype.ngOnChanges = /**
* @param {?} changes
* @return {?}
*/
function (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 {?}
*/
NzAvatarComponent.prototype.calcStringSize = /**
* @private
* @return {?}
*/
function () {
if (!this.hasText) {
return;
}
/** @type {?} */
var childrenWidth = this.textEl.nativeElement.offsetWidth;
/** @type {?} */
var avatarWidth = this.el.getBoundingClientRect().width;
/** @type {?} */
var 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}
*/
NzAvatarComponent.prototype.notifyCalc = /**
* @private
* @template THIS
* @this {THIS}
* @return {THIS}
*/
function () {
var _this = this;
// If use ngAfterViewChecked, always demands more computations, so......
if ((/** @type {?} */ (this)).platform.isBrowser) {
setTimeout((/**
* @return {?}
*/
function () {
(/** @type {?} */ (_this)).calcStringSize();
}));
}
return (/** @type {?} */ (this));
};
/**
* @private
* @return {?}
*/
NzAvatarComponent.prototype.setSizeStyle = /**
* @private
* @return {?}
*/
function () {
/** @type {?} */
var size = typeof this.nzSize === 'string' ? this.nzSize : this.nzSize + "px";
this.renderer.setStyle(this.el, 'width', size);
this.renderer.setStyle(this.el, 'height', size);
this.renderer.setStyle(this.el, 'line-height', size);
if (this.hasIcon) {
this.renderer.setStyle(this.el, 'font-size', "calc(" + size + " / 2)");
}
};
NzAvatarComponent.decorators = [
{ type: Component, args: [{
selector: 'nz-avatar',
exportAs: 'nzAvatar',
template: "<i nz-icon *ngIf=\"nzIcon && hasIcon\" [nzType]=\"!oldAPIIcon && nzIcon\" [ngClass]=\"oldAPIIcon && nzIcon\"></i>\n<img *ngIf=\"nzSrc && hasSrc\" [src]=\"nzSrc\" [attr.srcset]=\"nzSrcSet\" [attr.alt]=\"nzAlt\" (error)=\"imgError($event)\"/>\n<span class=\"ant-avatar-string\" #textEl [ngStyle]=\"textStyles\" *ngIf=\"nzText && hasText\">{{ nzText }}</span>\n",
providers: [NzUpdateHostClassService],
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None
}] }
];
/** @nocollapse */
NzAvatarComponent.ctorParameters = function () { return [
{ type: NzConfigService },
{ type: ElementRef },
{ type: ChangeDetectorRef },
{ type: NzUpdateHostClassService },
{ type: Renderer2 },
{ type: Platform }
]; };
NzAvatarComponent.propDecorators = {
nzShape: [{ type: Input }],
nzSize: [{ 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('circle'),
__metadata("design:type", String)
], NzAvatarComponent.prototype, "nzShape", void 0);
__decorate([
WithConfig('default'),
__metadata("design:type", Object)
], NzAvatarComponent.prototype, "nzSize", void 0);
return NzAvatarComponent;
}());
if (false) {
/** @type {?} */
NzAvatarComponent.prototype.nzShape;
/** @type {?} */
NzAvatarComponent.prototype.nzSize;
/** @type {?} */
NzAvatarComponent.prototype.nzText;
/** @type {?} */
NzAvatarComponent.prototype.nzSrc;
/** @type {?} */
NzAvatarComponent.prototype.nzSrcSet;
/** @type {?} */
NzAvatarComponent.prototype.nzAlt;
/** @type {?} */
NzAvatarComponent.prototype.nzIcon;
/** @type {?} */
NzAvatarComponent.prototype.nzError;
/** @type {?} */
NzAvatarComponent.prototype.oldAPIIcon;
/** @type {?} */
NzAvatarComponent.prototype.hasText;
/** @type {?} */
NzAvatarComponent.prototype.hasSrc;
/** @type {?} */
NzAvatarComponent.prototype.hasIcon;
/** @type {?} */
NzAvatarComponent.prototype.textStyles;
/** @type {?} */
NzAvatarComponent.prototype.textEl;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.el;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.prefixCls;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.sizeMap;
/** @type {?} */
NzAvatarComponent.prototype.nzConfigService;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.elementRef;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.cd;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.updateHostClassService;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.renderer;
/**
* @type {?}
* @private
*/
NzAvatarComponent.prototype.platform;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var NzAvatarModule = /** @class */ (function () {
function NzAvatarModule() {
}
NzAvatarModule.decorators = [
{ type: NgModule, args: [{
declarations: [NzAvatarComponent],
exports: [NzAvatarComponent],
imports: [CommonModule, NzIconModule, PlatformModule]
},] }
];
return NzAvatarModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NzAvatarComponent, NzAvatarModule };
//# sourceMappingURL=ng-zorro-antd-avatar.js.map