ng-zorro-antd-avatar.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { __esDecorate, __runInitializers } from 'tslib';
  2. import * as i0 from '@angular/core';
  3. import { EventEmitter, inject, ElementRef, ChangeDetectorRef, afterRender, numberAttribute, ViewChild, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
  4. import { WithConfig } from 'ng-zorro-antd/core/config';
  5. import { toCssPixel } from 'ng-zorro-antd/core/util';
  6. import * as i1 from 'ng-zorro-antd/icon';
  7. import { NzIconModule } from 'ng-zorro-antd/icon';
  8. const NZ_CONFIG_MODULE_NAME = 'avatar';
  9. let NzAvatarComponent = (() => {
  10. let _nzShape_decorators;
  11. let _nzShape_initializers = [];
  12. let _nzShape_extraInitializers = [];
  13. let _nzSize_decorators;
  14. let _nzSize_initializers = [];
  15. let _nzSize_extraInitializers = [];
  16. let _nzGap_decorators;
  17. let _nzGap_initializers = [];
  18. let _nzGap_extraInitializers = [];
  19. return class NzAvatarComponent {
  20. static {
  21. const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
  22. _nzShape_decorators = [WithConfig()];
  23. _nzSize_decorators = [WithConfig()];
  24. _nzGap_decorators = [WithConfig()];
  25. __esDecorate(null, null, _nzShape_decorators, { kind: "field", name: "nzShape", static: false, private: false, access: { has: obj => "nzShape" in obj, get: obj => obj.nzShape, set: (obj, value) => { obj.nzShape = value; } }, metadata: _metadata }, _nzShape_initializers, _nzShape_extraInitializers);
  26. __esDecorate(null, null, _nzSize_decorators, { kind: "field", name: "nzSize", static: false, private: false, access: { has: obj => "nzSize" in obj, get: obj => obj.nzSize, set: (obj, value) => { obj.nzSize = value; } }, metadata: _metadata }, _nzSize_initializers, _nzSize_extraInitializers);
  27. __esDecorate(null, null, _nzGap_decorators, { kind: "field", name: "nzGap", static: false, private: false, access: { has: obj => "nzGap" in obj, get: obj => obj.nzGap, set: (obj, value) => { obj.nzGap = value; } }, metadata: _metadata }, _nzGap_initializers, _nzGap_extraInitializers);
  28. if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
  29. }
  30. _nzModuleName = NZ_CONFIG_MODULE_NAME;
  31. nzShape = __runInitializers(this, _nzShape_initializers, 'circle');
  32. nzSize = (__runInitializers(this, _nzShape_extraInitializers), __runInitializers(this, _nzSize_initializers, 'default'));
  33. nzGap = (__runInitializers(this, _nzSize_extraInitializers), __runInitializers(this, _nzGap_initializers, 4));
  34. nzText = __runInitializers(this, _nzGap_extraInitializers);
  35. nzSrc;
  36. nzSrcSet;
  37. nzAlt;
  38. nzIcon;
  39. nzError = new EventEmitter();
  40. hasText = false;
  41. hasSrc = true;
  42. hasIcon = false;
  43. customSize = null;
  44. textEl;
  45. el = inject(ElementRef).nativeElement;
  46. cdr = inject(ChangeDetectorRef);
  47. constructor() {
  48. afterRender(() => this.calcStringSize());
  49. }
  50. imgError(event) {
  51. this.nzError.emit(event);
  52. if (!event.defaultPrevented) {
  53. this.hasSrc = false;
  54. this.hasIcon = false;
  55. this.hasText = false;
  56. if (this.nzIcon) {
  57. this.hasIcon = true;
  58. }
  59. else if (this.nzText) {
  60. this.hasText = true;
  61. }
  62. this.cdr.detectChanges();
  63. this.setSizeStyle();
  64. this.calcStringSize();
  65. }
  66. }
  67. ngOnChanges() {
  68. this.hasText = !this.nzSrc && !!this.nzText;
  69. this.hasIcon = !this.nzSrc && !!this.nzIcon;
  70. this.hasSrc = !!this.nzSrc;
  71. this.setSizeStyle();
  72. this.calcStringSize();
  73. }
  74. calcStringSize() {
  75. if (!this.hasText || !this.textEl) {
  76. return;
  77. }
  78. const textEl = this.textEl.nativeElement;
  79. const childrenWidth = textEl.offsetWidth;
  80. const avatarWidth = this.el.getBoundingClientRect?.().width ?? 0;
  81. const offset = this.nzGap * 2 < avatarWidth ? this.nzGap * 2 : 8;
  82. const scale = avatarWidth - offset < childrenWidth ? (avatarWidth - offset) / childrenWidth : 1;
  83. textEl.style.transform = `scale(${scale}) translateX(-50%)`;
  84. textEl.style.lineHeight = this.customSize || '';
  85. }
  86. setSizeStyle() {
  87. if (typeof this.nzSize === 'number') {
  88. this.customSize = toCssPixel(this.nzSize);
  89. }
  90. else {
  91. this.customSize = null;
  92. }
  93. this.cdr.markForCheck();
  94. }
  95. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
  96. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.2", type: NzAvatarComponent, isStandalone: true, selector: "nz-avatar", inputs: { nzShape: "nzShape", nzSize: "nzSize", nzGap: ["nzGap", "nzGap", numberAttribute], nzText: "nzText", nzSrc: "nzSrc", nzSrcSet: "nzSrcSet", nzAlt: "nzAlt", nzIcon: "nzIcon" }, outputs: { nzError: "nzError" }, host: { properties: { "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", "style.font-size.px": "(hasIcon && customSize) ? $any(nzSize) / 2 : null" }, classAttribute: "ant-avatar" }, viewQueries: [{ propertyName: "textEl", first: true, predicate: ["textEl"], descendants: true }], exportAs: ["nzAvatar"], usesOnChanges: true, ngImport: i0, template: `
  97. @if (nzIcon && hasIcon) {
  98. <nz-icon [nzType]="nzIcon" />
  99. } @else if (nzSrc && hasSrc) {
  100. <img [src]="nzSrc" [attr.srcset]="nzSrcSet" [attr.alt]="nzAlt" (error)="imgError($event)" />
  101. } @else if (nzText && hasText) {
  102. <span class="ant-avatar-string" #textEl>{{ nzText }}</span>
  103. }
  104. <ng-content></ng-content>
  105. `, isInline: true, dependencies: [{ kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i1.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: ["nzSpin", "nzRotate", "nzType", "nzTheme", "nzTwotoneColor", "nzIconfont"], exportAs: ["nzIcon"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  106. };
  107. })();
  108. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarComponent, decorators: [{
  109. type: Component,
  110. args: [{
  111. selector: 'nz-avatar',
  112. exportAs: 'nzAvatar',
  113. imports: [NzIconModule],
  114. template: `
  115. @if (nzIcon && hasIcon) {
  116. <nz-icon [nzType]="nzIcon" />
  117. } @else if (nzSrc && hasSrc) {
  118. <img [src]="nzSrc" [attr.srcset]="nzSrcSet" [attr.alt]="nzAlt" (error)="imgError($event)" />
  119. } @else if (nzText && hasText) {
  120. <span class="ant-avatar-string" #textEl>{{ nzText }}</span>
  121. }
  122. <ng-content></ng-content>
  123. `,
  124. host: {
  125. class: 'ant-avatar',
  126. '[class.ant-avatar-lg]': `nzSize === 'large'`,
  127. '[class.ant-avatar-sm]': `nzSize === 'small'`,
  128. '[class.ant-avatar-square]': `nzShape === 'square'`,
  129. '[class.ant-avatar-circle]': `nzShape === 'circle'`,
  130. '[class.ant-avatar-icon]': `nzIcon`,
  131. '[class.ant-avatar-image]': `hasSrc `,
  132. '[style.width]': 'customSize',
  133. '[style.height]': 'customSize',
  134. '[style.line-height]': 'customSize',
  135. // nzSize type is number when customSize is true
  136. '[style.font-size.px]': '(hasIcon && customSize) ? $any(nzSize) / 2 : null'
  137. },
  138. preserveWhitespaces: false,
  139. changeDetection: ChangeDetectionStrategy.OnPush,
  140. encapsulation: ViewEncapsulation.None
  141. }]
  142. }], ctorParameters: () => [], propDecorators: { nzShape: [{
  143. type: Input
  144. }], nzSize: [{
  145. type: Input
  146. }], nzGap: [{
  147. type: Input,
  148. args: [{ transform: numberAttribute }]
  149. }], nzText: [{
  150. type: Input
  151. }], nzSrc: [{
  152. type: Input
  153. }], nzSrcSet: [{
  154. type: Input
  155. }], nzAlt: [{
  156. type: Input
  157. }], nzIcon: [{
  158. type: Input
  159. }], nzError: [{
  160. type: Output
  161. }], textEl: [{
  162. type: ViewChild,
  163. args: ['textEl', { static: false }]
  164. }] } });
  165. /**
  166. * Use of this source code is governed by an MIT-style license that can be
  167. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  168. */
  169. class NzAvatarGroupComponent {
  170. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarGroupComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
  171. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.2.2", type: NzAvatarGroupComponent, isStandalone: true, selector: "nz-avatar-group", host: { classAttribute: "ant-avatar-group" }, exportAs: ["nzAvatarGroup"], ngImport: i0, template: `<ng-content></ng-content>`, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush });
  172. }
  173. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarGroupComponent, decorators: [{
  174. type: Component,
  175. args: [{
  176. selector: 'nz-avatar-group',
  177. exportAs: 'nzAvatarGroup',
  178. template: `<ng-content></ng-content>`,
  179. changeDetection: ChangeDetectionStrategy.OnPush,
  180. host: {
  181. class: 'ant-avatar-group'
  182. }
  183. }]
  184. }] });
  185. /**
  186. * Use of this source code is governed by an MIT-style license that can be
  187. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  188. */
  189. class NzAvatarModule {
  190. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  191. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarModule, imports: [NzAvatarComponent, NzAvatarGroupComponent], exports: [NzAvatarComponent, NzAvatarGroupComponent] });
  192. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarModule, imports: [NzAvatarComponent] });
  193. }
  194. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAvatarModule, decorators: [{
  195. type: NgModule,
  196. args: [{
  197. exports: [NzAvatarComponent, NzAvatarGroupComponent],
  198. imports: [NzAvatarComponent, NzAvatarGroupComponent]
  199. }]
  200. }] });
  201. /**
  202. * Use of this source code is governed by an MIT-style license that can be
  203. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  204. */
  205. /**
  206. * Use of this source code is governed by an MIT-style license that can be
  207. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  208. */
  209. /**
  210. * Generated bundle index. Do not edit.
  211. */
  212. export { NzAvatarComponent, NzAvatarGroupComponent, NzAvatarModule };
  213. //# sourceMappingURL=ng-zorro-antd-avatar.mjs.map