/
back-top.component.ts
161 lines (141 loc) · 5 KB
/
back-top.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* 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
*/
import { Direction, Directionality } from '@angular/cdk/bidi';
import { normalizePassiveListenerOptions, Platform } from '@angular/cdk/platform';
import { DOCUMENT } from '@angular/common';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Inject,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Optional,
Output,
SimpleChanges,
TemplateRef,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { fromEvent, Subject, Subscription } from 'rxjs';
import { debounceTime, takeUntil } from 'rxjs/operators';
import { fadeMotion } from 'ng-zorro-antd/core/animation';
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { NzDestroyService, NzScrollService } from 'ng-zorro-antd/core/services';
import { NumberInput, NzSafeAny } from 'ng-zorro-antd/core/types';
import { InputNumber } from 'ng-zorro-antd/core/util';
const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'backTop';
const passiveEventListenerOptions = normalizePassiveListenerOptions({ passive: true });
@Component({
selector: 'nz-back-top',
exportAs: 'nzBackTop',
animations: [fadeMotion],
template: `
<div #backTop class="ant-back-top" [class.ant-back-top-rtl]="dir === 'rtl'" @fadeMotion *ngIf="visible">
<ng-template #defaultContent>
<div class="ant-back-top-content">
<div class="ant-back-top-icon">
<i nz-icon nzType="vertical-align-top"></i>
</div>
</div>
</ng-template>
<ng-template [ngTemplateOutlet]="nzTemplate || defaultContent"></ng-template>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
providers: [NzDestroyService]
})
export class NzBackTopComponent implements OnInit, OnDestroy, OnChanges {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
static ngAcceptInputType_nzVisibilityHeight: NumberInput;
static ngAcceptInputType_nzDuration: NumberInput;
private scrollListenerDestroy$ = new Subject();
private target: HTMLElement | null = null;
visible: boolean = false;
dir: Direction = 'ltr';
@Input() nzTemplate?: TemplateRef<void>;
@Input() @WithConfig() @InputNumber() nzVisibilityHeight: number = 400;
@Input() nzTarget?: string | HTMLElement;
@Input() @InputNumber() nzDuration: number = 450;
@Output() readonly nzClick: EventEmitter<boolean> = new EventEmitter();
@ViewChild('backTop', { static: false })
set backTop(backTop: ElementRef<HTMLElement> | undefined) {
if (backTop) {
this.backTopClickSubscription.unsubscribe();
this.backTopClickSubscription = this.zone.runOutsideAngular(() =>
fromEvent(backTop.nativeElement, 'click')
.pipe(takeUntil(this.destroy$))
.subscribe(() => {
this.scrollSrv.scrollTo(this.getTarget(), 0, { duration: this.nzDuration });
if (this.nzClick.observers.length) {
this.zone.run(() => this.nzClick.emit(true));
}
})
);
}
}
private backTopClickSubscription = Subscription.EMPTY;
constructor(
@Inject(DOCUMENT) private doc: NzSafeAny,
public nzConfigService: NzConfigService,
private scrollSrv: NzScrollService,
private platform: Platform,
private cd: ChangeDetectorRef,
private zone: NgZone,
private cdr: ChangeDetectorRef,
private destroy$: NzDestroyService,
@Optional() private directionality: Directionality
) {
this.dir = this.directionality.value;
}
ngOnInit(): void {
this.registerScrollEvent();
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
private getTarget(): HTMLElement | Window {
return this.target || window;
}
private handleScroll(): void {
if (this.visible === this.scrollSrv.getScroll(this.getTarget()) > this.nzVisibilityHeight) {
return;
}
this.visible = !this.visible;
this.cd.detectChanges();
}
private registerScrollEvent(): void {
if (!this.platform.isBrowser) {
return;
}
this.scrollListenerDestroy$.next();
this.handleScroll();
this.zone.runOutsideAngular(() => {
fromEvent(this.getTarget(), 'scroll', <AddEventListenerOptions>passiveEventListenerOptions)
.pipe(debounceTime(50), takeUntil(this.scrollListenerDestroy$))
.subscribe(() => this.handleScroll());
});
}
ngOnDestroy(): void {
this.scrollListenerDestroy$.next();
this.scrollListenerDestroy$.complete();
}
ngOnChanges(changes: SimpleChanges): void {
const { nzTarget } = changes;
if (nzTarget) {
this.target = typeof this.nzTarget === 'string' ? this.doc.querySelector(this.nzTarget) : this.nzTarget;
this.registerScrollEvent();
}
}
}