ng-zorro-antd-rate.mjs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import { __esDecorate, __runInitializers } from 'tslib';
  2. import { RIGHT_ARROW, LEFT_ARROW } from '@angular/cdk/keycodes';
  3. import * as i0 from '@angular/core';
  4. import { EventEmitter, booleanAttribute, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, forwardRef, numberAttribute, ViewChild, NgModule } from '@angular/core';
  5. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  6. import { takeUntil } from 'rxjs/operators';
  7. import * as i1$1 from 'ng-zorro-antd/core/config';
  8. import { WithConfig } from 'ng-zorro-antd/core/config';
  9. import * as i3 from 'ng-zorro-antd/core/services';
  10. import { NzDestroyService } from 'ng-zorro-antd/core/services';
  11. import { fromEventOutsideAngular } from 'ng-zorro-antd/core/util';
  12. import * as i4 from 'ng-zorro-antd/tooltip';
  13. import { NzToolTipModule } from 'ng-zorro-antd/tooltip';
  14. import { NgTemplateOutlet } from '@angular/common';
  15. import * as i1 from 'ng-zorro-antd/icon';
  16. import { NzIconModule } from 'ng-zorro-antd/icon';
  17. import * as i2 from '@angular/cdk/bidi';
  18. /**
  19. * Use of this source code is governed by an MIT-style license that can be
  20. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  21. */
  22. class NzRateItemComponent {
  23. character;
  24. index = 0;
  25. allowHalf = false;
  26. itemHover = new EventEmitter();
  27. itemClick = new EventEmitter();
  28. hoverRate(isHalf) {
  29. this.itemHover.next(isHalf && this.allowHalf);
  30. }
  31. clickRate(isHalf) {
  32. this.itemClick.next(isHalf && this.allowHalf);
  33. }
  34. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzRateItemComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
  35. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "19.2.2", type: NzRateItemComponent, isStandalone: true, selector: "[nz-rate-item]", inputs: { character: "character", index: "index", allowHalf: ["allowHalf", "allowHalf", booleanAttribute] }, outputs: { itemHover: "itemHover", itemClick: "itemClick" }, exportAs: ["nzRateItem"], ngImport: i0, template: `
  36. <div
  37. class="ant-rate-star-second"
  38. (mouseover)="hoverRate(false); $event.stopPropagation()"
  39. (click)="clickRate(false)"
  40. >
  41. <ng-template
  42. [ngTemplateOutlet]="character || defaultCharacter"
  43. [ngTemplateOutletContext]="{ $implicit: index }"
  44. ></ng-template>
  45. </div>
  46. <div class="ant-rate-star-first" (mouseover)="hoverRate(true); $event.stopPropagation()" (click)="clickRate(true)">
  47. <ng-template
  48. [ngTemplateOutlet]="character || defaultCharacter"
  49. [ngTemplateOutletContext]="{ $implicit: index }"
  50. ></ng-template>
  51. </div>
  52. <ng-template #defaultCharacter>
  53. <nz-icon nzType="star" nzTheme="fill" />
  54. </ng-template>
  55. `, isInline: true, dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { 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 });
  56. }
  57. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzRateItemComponent, decorators: [{
  58. type: Component,
  59. args: [{
  60. changeDetection: ChangeDetectionStrategy.OnPush,
  61. encapsulation: ViewEncapsulation.None,
  62. selector: '[nz-rate-item]',
  63. exportAs: 'nzRateItem',
  64. template: `
  65. <div
  66. class="ant-rate-star-second"
  67. (mouseover)="hoverRate(false); $event.stopPropagation()"
  68. (click)="clickRate(false)"
  69. >
  70. <ng-template
  71. [ngTemplateOutlet]="character || defaultCharacter"
  72. [ngTemplateOutletContext]="{ $implicit: index }"
  73. ></ng-template>
  74. </div>
  75. <div class="ant-rate-star-first" (mouseover)="hoverRate(true); $event.stopPropagation()" (click)="clickRate(true)">
  76. <ng-template
  77. [ngTemplateOutlet]="character || defaultCharacter"
  78. [ngTemplateOutletContext]="{ $implicit: index }"
  79. ></ng-template>
  80. </div>
  81. <ng-template #defaultCharacter>
  82. <nz-icon nzType="star" nzTheme="fill" />
  83. </ng-template>
  84. `,
  85. imports: [NgTemplateOutlet, NzIconModule]
  86. }]
  87. }], propDecorators: { character: [{
  88. type: Input
  89. }], index: [{
  90. type: Input
  91. }], allowHalf: [{
  92. type: Input,
  93. args: [{ transform: booleanAttribute }]
  94. }], itemHover: [{
  95. type: Output
  96. }], itemClick: [{
  97. type: Output
  98. }] } });
  99. const NZ_CONFIG_MODULE_NAME = 'rate';
  100. let NzRateComponent = (() => {
  101. let _nzAllowClear_decorators;
  102. let _nzAllowClear_initializers = [];
  103. let _nzAllowClear_extraInitializers = [];
  104. let _nzAllowHalf_decorators;
  105. let _nzAllowHalf_initializers = [];
  106. let _nzAllowHalf_extraInitializers = [];
  107. return class NzRateComponent {
  108. static {
  109. const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
  110. _nzAllowClear_decorators = [WithConfig()];
  111. _nzAllowHalf_decorators = [WithConfig()];
  112. __esDecorate(null, null, _nzAllowClear_decorators, { kind: "field", name: "nzAllowClear", static: false, private: false, access: { has: obj => "nzAllowClear" in obj, get: obj => obj.nzAllowClear, set: (obj, value) => { obj.nzAllowClear = value; } }, metadata: _metadata }, _nzAllowClear_initializers, _nzAllowClear_extraInitializers);
  113. __esDecorate(null, null, _nzAllowHalf_decorators, { kind: "field", name: "nzAllowHalf", static: false, private: false, access: { has: obj => "nzAllowHalf" in obj, get: obj => obj.nzAllowHalf, set: (obj, value) => { obj.nzAllowHalf = value; } }, metadata: _metadata }, _nzAllowHalf_initializers, _nzAllowHalf_extraInitializers);
  114. if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
  115. }
  116. nzConfigService;
  117. ngZone;
  118. renderer;
  119. cdr;
  120. directionality;
  121. destroy$;
  122. _nzModuleName = NZ_CONFIG_MODULE_NAME;
  123. ulElement;
  124. nzAllowClear = __runInitializers(this, _nzAllowClear_initializers, true);
  125. nzAllowHalf = (__runInitializers(this, _nzAllowClear_extraInitializers), __runInitializers(this, _nzAllowHalf_initializers, false));
  126. nzDisabled = (__runInitializers(this, _nzAllowHalf_extraInitializers), false);
  127. nzAutoFocus = false;
  128. nzCharacter;
  129. nzCount = 5;
  130. nzTooltips = [];
  131. nzOnBlur = new EventEmitter();
  132. nzOnFocus = new EventEmitter();
  133. nzOnHoverChange = new EventEmitter();
  134. nzOnKeyDown = new EventEmitter();
  135. classMap = {};
  136. starArray = [];
  137. starStyleArray = [];
  138. dir = 'ltr';
  139. hasHalf = false;
  140. hoverValue = 0;
  141. isFocused = false;
  142. _value = 0;
  143. isNzDisableFirstChange = true;
  144. get nzValue() {
  145. return this._value;
  146. }
  147. set nzValue(input) {
  148. if (this._value === input) {
  149. return;
  150. }
  151. this._value = input;
  152. this.hasHalf = !Number.isInteger(input) && this.nzAllowHalf;
  153. this.hoverValue = Math.ceil(input);
  154. }
  155. constructor(nzConfigService, ngZone, renderer, cdr, directionality, destroy$) {
  156. this.nzConfigService = nzConfigService;
  157. this.ngZone = ngZone;
  158. this.renderer = renderer;
  159. this.cdr = cdr;
  160. this.directionality = directionality;
  161. this.destroy$ = destroy$;
  162. }
  163. ngOnChanges(changes) {
  164. const { nzAutoFocus, nzCount, nzValue } = changes;
  165. if (nzAutoFocus && !nzAutoFocus.isFirstChange()) {
  166. const el = this.ulElement.nativeElement;
  167. if (this.nzAutoFocus && !this.nzDisabled) {
  168. this.renderer.setAttribute(el, 'autofocus', 'autofocus');
  169. }
  170. else {
  171. this.renderer.removeAttribute(el, 'autofocus');
  172. }
  173. }
  174. if (nzCount) {
  175. this.updateStarArray();
  176. }
  177. if (nzValue) {
  178. this.updateStarStyle();
  179. }
  180. }
  181. ngOnInit() {
  182. this.nzConfigService
  183. .getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME)
  184. .pipe(takeUntil(this.destroy$))
  185. .subscribe(() => this.cdr.markForCheck());
  186. this.directionality.change.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
  187. this.dir = direction;
  188. this.cdr.detectChanges();
  189. });
  190. this.dir = this.directionality.value;
  191. fromEventOutsideAngular(this.ulElement.nativeElement, 'focus')
  192. .pipe(takeUntil(this.destroy$))
  193. .subscribe(event => {
  194. this.isFocused = true;
  195. if (this.nzOnFocus.observers.length) {
  196. this.ngZone.run(() => this.nzOnFocus.emit(event));
  197. }
  198. });
  199. fromEventOutsideAngular(this.ulElement.nativeElement, 'blur')
  200. .pipe(takeUntil(this.destroy$))
  201. .subscribe(event => {
  202. this.isFocused = false;
  203. if (this.nzOnBlur.observers.length) {
  204. this.ngZone.run(() => this.nzOnBlur.emit(event));
  205. }
  206. });
  207. }
  208. onItemClick(index, isHalf) {
  209. if (this.nzDisabled) {
  210. return;
  211. }
  212. this.hoverValue = index + 1;
  213. const actualValue = isHalf ? index + 0.5 : index + 1;
  214. if (this.nzValue === actualValue) {
  215. if (this.nzAllowClear) {
  216. this.nzValue = 0;
  217. this.onChange(this.nzValue);
  218. }
  219. }
  220. else {
  221. this.nzValue = actualValue;
  222. this.onChange(this.nzValue);
  223. }
  224. this.updateStarStyle();
  225. }
  226. onItemHover(index, isHalf) {
  227. if (this.nzDisabled) {
  228. return;
  229. }
  230. if (this.hoverValue !== index + 1 || isHalf !== this.hasHalf) {
  231. this.hoverValue = index + 1;
  232. this.hasHalf = isHalf;
  233. this.updateStarStyle();
  234. }
  235. this.nzOnHoverChange.emit(this.hoverValue);
  236. }
  237. onRateLeave() {
  238. this.hasHalf = !Number.isInteger(this.nzValue);
  239. this.hoverValue = Math.ceil(this.nzValue);
  240. this.nzOnHoverChange.emit(this.hoverValue);
  241. this.updateStarStyle();
  242. }
  243. focus() {
  244. this.ulElement.nativeElement.focus();
  245. }
  246. blur() {
  247. this.ulElement.nativeElement.blur();
  248. }
  249. onKeyDown(e) {
  250. const oldVal = this.nzValue;
  251. if (e.keyCode === RIGHT_ARROW && this.nzValue < this.nzCount) {
  252. this.nzValue += this.nzAllowHalf ? 0.5 : 1;
  253. }
  254. else if (e.keyCode === LEFT_ARROW && this.nzValue > 0) {
  255. this.nzValue -= this.nzAllowHalf ? 0.5 : 1;
  256. }
  257. if (oldVal !== this.nzValue) {
  258. this.onChange(this.nzValue);
  259. this.nzOnKeyDown.emit(e);
  260. this.updateStarStyle();
  261. this.cdr.markForCheck();
  262. }
  263. }
  264. updateStarArray() {
  265. this.starArray = Array(this.nzCount)
  266. .fill(0)
  267. .map((_, i) => i);
  268. this.updateStarStyle();
  269. }
  270. updateStarStyle() {
  271. this.starStyleArray = this.starArray.map(i => {
  272. const prefix = 'ant-rate-star';
  273. const value = i + 1;
  274. return {
  275. [`${prefix}-full`]: value < this.hoverValue || (!this.hasHalf && value === this.hoverValue),
  276. [`${prefix}-half`]: this.hasHalf && value === this.hoverValue,
  277. [`${prefix}-active`]: this.hasHalf && value === this.hoverValue,
  278. [`${prefix}-zero`]: value > this.hoverValue,
  279. [`${prefix}-focused`]: this.hasHalf && value === this.hoverValue && this.isFocused
  280. };
  281. });
  282. }
  283. writeValue(value) {
  284. this.nzValue = value || 0;
  285. this.updateStarArray();
  286. this.cdr.markForCheck();
  287. }
  288. setDisabledState(isDisabled) {
  289. this.nzDisabled = (this.isNzDisableFirstChange && this.nzDisabled) || isDisabled;
  290. this.isNzDisableFirstChange = false;
  291. this.cdr.markForCheck();
  292. }
  293. registerOnChange(fn) {
  294. this.onChange = fn;
  295. }
  296. registerOnTouched(fn) {
  297. this.onTouched = fn;
  298. }
  299. onChange = () => null;
  300. onTouched = () => null;
  301. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzRateComponent, deps: [{ token: i1$1.NzConfigService }, { token: i0.NgZone }, { token: i0.Renderer2 }, { token: i0.ChangeDetectorRef }, { token: i2.Directionality }, { token: i3.NzDestroyService }], target: i0.ɵɵFactoryTarget.Component });
  302. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.2", type: NzRateComponent, isStandalone: true, selector: "nz-rate", inputs: { nzAllowClear: ["nzAllowClear", "nzAllowClear", booleanAttribute], nzAllowHalf: ["nzAllowHalf", "nzAllowHalf", booleanAttribute], nzDisabled: ["nzDisabled", "nzDisabled", booleanAttribute], nzAutoFocus: ["nzAutoFocus", "nzAutoFocus", booleanAttribute], nzCharacter: "nzCharacter", nzCount: ["nzCount", "nzCount", numberAttribute], nzTooltips: "nzTooltips" }, outputs: { nzOnBlur: "nzOnBlur", nzOnFocus: "nzOnFocus", nzOnHoverChange: "nzOnHoverChange", nzOnKeyDown: "nzOnKeyDown" }, providers: [
  303. NzDestroyService,
  304. {
  305. provide: NG_VALUE_ACCESSOR,
  306. useExisting: forwardRef(() => NzRateComponent),
  307. multi: true
  308. }
  309. ], viewQueries: [{ propertyName: "ulElement", first: true, predicate: ["ulElement"], descendants: true, static: true }], exportAs: ["nzRate"], usesOnChanges: true, ngImport: i0, template: `
  310. <ul
  311. #ulElement
  312. class="ant-rate"
  313. [class.ant-rate-disabled]="nzDisabled"
  314. [class.ant-rate-rtl]="dir === 'rtl'"
  315. [class]="classMap"
  316. (keydown)="onKeyDown($event); $event.preventDefault()"
  317. (mouseleave)="onRateLeave(); $event.stopPropagation()"
  318. [tabindex]="nzDisabled ? -1 : 1"
  319. >
  320. @for (star of starArray; track star) {
  321. <li
  322. class="ant-rate-star"
  323. [class]="starStyleArray[$index] || ''"
  324. nz-tooltip
  325. [nzTooltipTitle]="nzTooltips[$index]"
  326. >
  327. <div
  328. nz-rate-item
  329. [allowHalf]="nzAllowHalf"
  330. [character]="nzCharacter"
  331. [index]="$index"
  332. (itemHover)="onItemHover($index, $event)"
  333. (itemClick)="onItemClick($index, $event)"
  334. ></div>
  335. </li>
  336. }
  337. </ul>
  338. `, isInline: true, dependencies: [{ kind: "ngmodule", type: NzToolTipModule }, { kind: "directive", type: i4.NzTooltipDirective, selector: "[nz-tooltip]", inputs: ["nzTooltipTitle", "nzTooltipTitleContext", "nz-tooltip", "nzTooltipTrigger", "nzTooltipPlacement", "nzTooltipOrigin", "nzTooltipVisible", "nzTooltipMouseEnterDelay", "nzTooltipMouseLeaveDelay", "nzTooltipOverlayClassName", "nzTooltipOverlayStyle", "nzTooltipArrowPointAtCenter", "cdkConnectedOverlayPush", "nzTooltipColor"], outputs: ["nzTooltipVisibleChange"], exportAs: ["nzTooltip"] }, { kind: "component", type: NzRateItemComponent, selector: "[nz-rate-item]", inputs: ["character", "index", "allowHalf"], outputs: ["itemHover", "itemClick"], exportAs: ["nzRateItem"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  339. };
  340. })();
  341. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzRateComponent, decorators: [{
  342. type: Component,
  343. args: [{
  344. changeDetection: ChangeDetectionStrategy.OnPush,
  345. encapsulation: ViewEncapsulation.None,
  346. selector: 'nz-rate',
  347. exportAs: 'nzRate',
  348. preserveWhitespaces: false,
  349. template: `
  350. <ul
  351. #ulElement
  352. class="ant-rate"
  353. [class.ant-rate-disabled]="nzDisabled"
  354. [class.ant-rate-rtl]="dir === 'rtl'"
  355. [class]="classMap"
  356. (keydown)="onKeyDown($event); $event.preventDefault()"
  357. (mouseleave)="onRateLeave(); $event.stopPropagation()"
  358. [tabindex]="nzDisabled ? -1 : 1"
  359. >
  360. @for (star of starArray; track star) {
  361. <li
  362. class="ant-rate-star"
  363. [class]="starStyleArray[$index] || ''"
  364. nz-tooltip
  365. [nzTooltipTitle]="nzTooltips[$index]"
  366. >
  367. <div
  368. nz-rate-item
  369. [allowHalf]="nzAllowHalf"
  370. [character]="nzCharacter"
  371. [index]="$index"
  372. (itemHover)="onItemHover($index, $event)"
  373. (itemClick)="onItemClick($index, $event)"
  374. ></div>
  375. </li>
  376. }
  377. </ul>
  378. `,
  379. providers: [
  380. NzDestroyService,
  381. {
  382. provide: NG_VALUE_ACCESSOR,
  383. useExisting: forwardRef(() => NzRateComponent),
  384. multi: true
  385. }
  386. ],
  387. imports: [NzToolTipModule, NzRateItemComponent, NzToolTipModule]
  388. }]
  389. }], ctorParameters: () => [{ type: i1$1.NzConfigService }, { type: i0.NgZone }, { type: i0.Renderer2 }, { type: i0.ChangeDetectorRef }, { type: i2.Directionality }, { type: i3.NzDestroyService }], propDecorators: { ulElement: [{
  390. type: ViewChild,
  391. args: ['ulElement', { static: true }]
  392. }], nzAllowClear: [{
  393. type: Input,
  394. args: [{ transform: booleanAttribute }]
  395. }], nzAllowHalf: [{
  396. type: Input,
  397. args: [{ transform: booleanAttribute }]
  398. }], nzDisabled: [{
  399. type: Input,
  400. args: [{ transform: booleanAttribute }]
  401. }], nzAutoFocus: [{
  402. type: Input,
  403. args: [{ transform: booleanAttribute }]
  404. }], nzCharacter: [{
  405. type: Input
  406. }], nzCount: [{
  407. type: Input,
  408. args: [{ transform: numberAttribute }]
  409. }], nzTooltips: [{
  410. type: Input
  411. }], nzOnBlur: [{
  412. type: Output
  413. }], nzOnFocus: [{
  414. type: Output
  415. }], nzOnHoverChange: [{
  416. type: Output
  417. }], nzOnKeyDown: [{
  418. type: Output
  419. }] } });
  420. /**
  421. * Use of this source code is governed by an MIT-style license that can be
  422. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  423. */
  424. class NzRateModule {
  425. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzRateModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  426. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzRateModule, imports: [NzRateComponent, NzRateItemComponent], exports: [NzRateComponent] });
  427. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzRateModule, imports: [NzRateComponent, NzRateItemComponent] });
  428. }
  429. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzRateModule, decorators: [{
  430. type: NgModule,
  431. args: [{
  432. imports: [NzRateComponent, NzRateItemComponent],
  433. exports: [NzRateComponent]
  434. }]
  435. }] });
  436. /**
  437. * Use of this source code is governed by an MIT-style license that can be
  438. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  439. */
  440. /**
  441. * Generated bundle index. Do not edit.
  442. */
  443. export { NzRateComponent, NzRateItemComponent, NzRateModule };
  444. //# sourceMappingURL=ng-zorro-antd-rate.mjs.map