ng-zorro-antd-back-top.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { __esDecorate, __runInitializers } from 'tslib';
  2. import * as i3 from '@angular/cdk/platform';
  3. import { normalizePassiveListenerOptions } from '@angular/cdk/platform';
  4. import { DOCUMENT, NgTemplateOutlet } from '@angular/common';
  5. import * as i0 from '@angular/core';
  6. import { EventEmitter, inject, numberAttribute, ViewChild, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
  7. import { Subject, Subscription } from 'rxjs';
  8. import { takeUntil, debounceTime } from 'rxjs/operators';
  9. import { fadeMotion } from 'ng-zorro-antd/core/animation';
  10. import * as i1 from 'ng-zorro-antd/core/config';
  11. import { WithConfig } from 'ng-zorro-antd/core/config';
  12. import * as i2 from 'ng-zorro-antd/core/services';
  13. import { NzDestroyService } from 'ng-zorro-antd/core/services';
  14. import { fromEventOutsideAngular } from 'ng-zorro-antd/core/util';
  15. import * as i5 from 'ng-zorro-antd/icon';
  16. import { NzIconModule } from 'ng-zorro-antd/icon';
  17. import * as i4 from '@angular/cdk/bidi';
  18. const NZ_CONFIG_MODULE_NAME = 'backTop';
  19. const passiveEventListenerOptions = normalizePassiveListenerOptions({ passive: true });
  20. let NzBackTopComponent = (() => {
  21. let _nzVisibilityHeight_decorators;
  22. let _nzVisibilityHeight_initializers = [];
  23. let _nzVisibilityHeight_extraInitializers = [];
  24. return class NzBackTopComponent {
  25. static {
  26. const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
  27. _nzVisibilityHeight_decorators = [WithConfig()];
  28. __esDecorate(null, null, _nzVisibilityHeight_decorators, { kind: "field", name: "nzVisibilityHeight", static: false, private: false, access: { has: obj => "nzVisibilityHeight" in obj, get: obj => obj.nzVisibilityHeight, set: (obj, value) => { obj.nzVisibilityHeight = value; } }, metadata: _metadata }, _nzVisibilityHeight_initializers, _nzVisibilityHeight_extraInitializers);
  29. if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
  30. }
  31. nzConfigService;
  32. scrollSrv;
  33. platform;
  34. zone;
  35. cdr;
  36. destroy$;
  37. directionality;
  38. _nzModuleName = NZ_CONFIG_MODULE_NAME;
  39. scrollListenerDestroy$ = new Subject();
  40. target = null;
  41. visible = false;
  42. dir = 'ltr';
  43. nzTemplate;
  44. nzVisibilityHeight = __runInitializers(this, _nzVisibilityHeight_initializers, 400);
  45. nzTarget = __runInitializers(this, _nzVisibilityHeight_extraInitializers);
  46. nzDuration = 450;
  47. nzClick = new EventEmitter();
  48. set backTop(backTop) {
  49. if (backTop) {
  50. this.backTopClickSubscription.unsubscribe();
  51. this.backTopClickSubscription = fromEventOutsideAngular(backTop.nativeElement, 'click')
  52. .pipe(takeUntil(this.destroy$))
  53. .subscribe(() => {
  54. this.scrollSrv.scrollTo(this.getTarget(), 0, { duration: this.nzDuration });
  55. if (this.nzClick.observers.length) {
  56. this.zone.run(() => this.nzClick.emit(true));
  57. }
  58. });
  59. }
  60. }
  61. backTopClickSubscription = Subscription.EMPTY;
  62. doc = inject(DOCUMENT);
  63. constructor(nzConfigService, scrollSrv, platform, zone, cdr, destroy$, directionality) {
  64. this.nzConfigService = nzConfigService;
  65. this.scrollSrv = scrollSrv;
  66. this.platform = platform;
  67. this.zone = zone;
  68. this.cdr = cdr;
  69. this.destroy$ = destroy$;
  70. this.directionality = directionality;
  71. this.dir = this.directionality.value;
  72. }
  73. ngOnInit() {
  74. this.registerScrollEvent();
  75. this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
  76. this.dir = direction;
  77. this.cdr.detectChanges();
  78. });
  79. this.dir = this.directionality.value;
  80. }
  81. getTarget() {
  82. return this.target || window;
  83. }
  84. handleScroll() {
  85. if (this.visible === this.scrollSrv.getScroll(this.getTarget()) > this.nzVisibilityHeight) {
  86. return;
  87. }
  88. this.visible = !this.visible;
  89. this.cdr.detectChanges();
  90. }
  91. registerScrollEvent() {
  92. if (!this.platform.isBrowser) {
  93. return;
  94. }
  95. this.scrollListenerDestroy$.next(true);
  96. this.handleScroll();
  97. fromEventOutsideAngular(this.getTarget(), 'scroll', passiveEventListenerOptions)
  98. .pipe(debounceTime(50), takeUntil(this.scrollListenerDestroy$))
  99. .subscribe(() => this.handleScroll());
  100. }
  101. ngOnDestroy() {
  102. this.scrollListenerDestroy$.next(true);
  103. this.scrollListenerDestroy$.complete();
  104. }
  105. ngOnChanges(changes) {
  106. const { nzTarget } = changes;
  107. if (nzTarget) {
  108. this.target = typeof this.nzTarget === 'string' ? this.doc.querySelector(this.nzTarget) : this.nzTarget;
  109. this.registerScrollEvent();
  110. }
  111. }
  112. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBackTopComponent, deps: [{ token: i1.NzConfigService }, { token: i2.NzScrollService }, { token: i3.Platform }, { token: i0.NgZone }, { token: i0.ChangeDetectorRef }, { token: i2.NzDestroyService }, { token: i4.Directionality }], target: i0.ɵɵFactoryTarget.Component });
  113. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.2", type: NzBackTopComponent, isStandalone: true, selector: "nz-back-top", inputs: { nzTemplate: "nzTemplate", nzVisibilityHeight: ["nzVisibilityHeight", "nzVisibilityHeight", numberAttribute], nzTarget: "nzTarget", nzDuration: ["nzDuration", "nzDuration", numberAttribute] }, outputs: { nzClick: "nzClick" }, providers: [NzDestroyService], viewQueries: [{ propertyName: "backTop", first: true, predicate: ["backTop"], descendants: true }], exportAs: ["nzBackTop"], usesOnChanges: true, ngImport: i0, template: `
  114. @if (visible) {
  115. <div #backTop class="ant-back-top" [class.ant-back-top-rtl]="dir === 'rtl'" @fadeMotion>
  116. <ng-template #defaultContent>
  117. <div class="ant-back-top-content">
  118. <div class="ant-back-top-icon">
  119. <nz-icon nzType="vertical-align-top" />
  120. </div>
  121. </div>
  122. </ng-template>
  123. <ng-template [ngTemplateOutlet]="nzTemplate || defaultContent"></ng-template>
  124. </div>
  125. }
  126. `, isInline: true, dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i5.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: ["nzSpin", "nzRotate", "nzType", "nzTheme", "nzTwotoneColor", "nzIconfont"], exportAs: ["nzIcon"] }], animations: [fadeMotion], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  127. };
  128. })();
  129. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBackTopComponent, decorators: [{
  130. type: Component,
  131. args: [{
  132. selector: 'nz-back-top',
  133. exportAs: 'nzBackTop',
  134. animations: [fadeMotion],
  135. imports: [NgTemplateOutlet, NzIconModule],
  136. template: `
  137. @if (visible) {
  138. <div #backTop class="ant-back-top" [class.ant-back-top-rtl]="dir === 'rtl'" @fadeMotion>
  139. <ng-template #defaultContent>
  140. <div class="ant-back-top-content">
  141. <div class="ant-back-top-icon">
  142. <nz-icon nzType="vertical-align-top" />
  143. </div>
  144. </div>
  145. </ng-template>
  146. <ng-template [ngTemplateOutlet]="nzTemplate || defaultContent"></ng-template>
  147. </div>
  148. }
  149. `,
  150. changeDetection: ChangeDetectionStrategy.OnPush,
  151. encapsulation: ViewEncapsulation.None,
  152. preserveWhitespaces: false,
  153. providers: [NzDestroyService]
  154. }]
  155. }], ctorParameters: () => [{ type: i1.NzConfigService }, { type: i2.NzScrollService }, { type: i3.Platform }, { type: i0.NgZone }, { type: i0.ChangeDetectorRef }, { type: i2.NzDestroyService }, { type: i4.Directionality }], propDecorators: { nzTemplate: [{
  156. type: Input
  157. }], nzVisibilityHeight: [{
  158. type: Input,
  159. args: [{ transform: numberAttribute }]
  160. }], nzTarget: [{
  161. type: Input
  162. }], nzDuration: [{
  163. type: Input,
  164. args: [{ transform: numberAttribute }]
  165. }], nzClick: [{
  166. type: Output
  167. }], backTop: [{
  168. type: ViewChild,
  169. args: ['backTop', { static: false }]
  170. }] } });
  171. /**
  172. * Use of this source code is governed by an MIT-style license that can be
  173. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  174. */
  175. class NzBackTopModule {
  176. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBackTopModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  177. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzBackTopModule, imports: [NzBackTopComponent], exports: [NzBackTopComponent] });
  178. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBackTopModule, imports: [NzBackTopComponent] });
  179. }
  180. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBackTopModule, decorators: [{
  181. type: NgModule,
  182. args: [{
  183. exports: [NzBackTopComponent],
  184. imports: [NzBackTopComponent]
  185. }]
  186. }] });
  187. /**
  188. * Use of this source code is governed by an MIT-style license that can be
  189. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  190. */
  191. /**
  192. * Generated bundle index. Do not edit.
  193. */
  194. export { NzBackTopComponent, NzBackTopModule };
  195. //# sourceMappingURL=ng-zorro-antd-back-top.mjs.map