/
image-preview-ref.ts
87 lines (74 loc) · 2.22 KB
/
image-preview-ref.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
/**
* 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 { ESCAPE, hasModifierKey, LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
import { OverlayRef } from '@angular/cdk/overlay';
import { Subject } from 'rxjs';
import { filter, take, takeUntil } from 'rxjs/operators';
import { NzImagePreviewOptions } from './image-preview-options';
import { NzImagePreviewComponent } from './image-preview.component';
export class NzImagePreviewRef {
private destroy$ = new Subject<void>();
constructor(
public previewInstance: NzImagePreviewComponent,
private config: NzImagePreviewOptions,
private overlayRef: OverlayRef
) {
overlayRef
.keydownEvents()
.pipe(
filter(
event =>
(this.config.nzKeyboard as boolean) &&
(event.keyCode === ESCAPE || event.keyCode === LEFT_ARROW || event.keyCode === RIGHT_ARROW) &&
!hasModifierKey(event)
)
)
.subscribe(event => {
event.preventDefault();
if (event.keyCode === ESCAPE) {
this.close();
}
if (event.keyCode === LEFT_ARROW) {
this.prev();
}
if (event.keyCode === RIGHT_ARROW) {
this.next();
}
});
overlayRef.detachments().subscribe(() => {
this.overlayRef.dispose();
});
previewInstance.containerClick.pipe(take(1), takeUntil(this.destroy$)).subscribe(() => {
this.close();
});
previewInstance.closeClick.pipe(take(1), takeUntil(this.destroy$)).subscribe(() => {
this.close();
});
previewInstance.animationStateChanged
.pipe(
filter(event => event.phaseName === 'done' && event.toState === 'leave'),
take(1)
)
.subscribe(() => {
this.dispose();
});
}
switchTo(index: number): void {
this.previewInstance.switchTo(index);
}
next(): void {
this.previewInstance.next();
}
prev(): void {
this.previewInstance.prev();
}
close(): void {
this.previewInstance.startLeaveAnimation();
}
private dispose(): void {
this.destroy$.next();
this.overlayRef.dispose();
}
}