-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviewer.directive.ts
109 lines (94 loc) · 2.19 KB
/
viewer.directive.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
/**
* Created by giscafer on 2017/09/21.
* 基于viewer.js封装
*/
import {
ContentChild,
ContentChildren,
Directive,
ElementRef, HostListener,
Input,
NgModule,
Renderer2,
NgZone
} from "@angular/core";
import { CommonModule } from "@angular/common";
/* 动态获取接口异步返回的图片*/
@Directive({
selector: '[viewer-content]',
})
export class ViewerContentDirective {
nativeElement: HTMLElement;
constructor(private _el: ElementRef) {
this.nativeElement = this._el.nativeElement;
}
}
@Directive({
selector: '[yzt-viewer]'
})
export class YZTViewerDirective {
@Input() originalAttr: string = "name";
@ContentChild('content') content: ElementRef;
@ContentChildren(ViewerContentDirective)
set _imgContents(value) {
this.imgContents = value;
this.renderContent();
}
viewer: any;
imgContents;
nativeElement: HTMLElement;
constructor(private _elementRef: ElementRef, private ngZone: NgZone) {
this.nativeElement = this._elementRef.nativeElement;
}
ngOnInit() {
this.renderContent();
}
/**
* 如果img动态增加,则动态渲染
*/
renderContent() {
let nativeEl = this.nativeElement;
if (this.content) {
nativeEl = this.content.nativeElement;
}
let imgs = nativeEl.getElementsByTagName('img');
this.ngZone.runOutsideAngular(() => {
if (imgs.length) {
if (this.viewer) {
this.viewer.destroy();
}
this.viewer = new Viewer(this.nativeElement, {
url: this.originalAttr,
});
this.viewer.play();
}
});
// setTimeout(() => {
// let imgs = nativeEl.getElementsByTagName('img');
// if (imgs.length) {
// if (this.viewer) {
// this.viewer.destroy();
// }
// this.viewer = new Viewer(this.nativeElement, {
// url: this.originalAttr,
// });
// }
// }, 500);
}
ngOnDestroy(): void {
if (this.viewer) {
this.viewer.destroy();
}
}
}
@NgModule({
declarations: [
YZTViewerDirective, ViewerContentDirective
],
exports: [YZTViewerDirective, ViewerContentDirective],
imports: [
CommonModule
]
})
export class YZTViewerDirectiveModule {
}