-
Notifications
You must be signed in to change notification settings - Fork 1
/
lazy-image.ts
153 lines (126 loc) · 4.23 KB
/
lazy-image.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
import {Directive, ElementRef, EventEmitter, Input, NgZone, Output} from "@angular/core";
import {Observable} from "rxjs/Observable";
import {Subject} from "rxjs/Subject";
/**
* Usage
*
directive that allows you to load images which are deferred (not available right away)
so image will be polled. While image is polled we can show default image as well as
as loading image.
usage:
<img lazyImage class="center-block" style="width: 229px; height: 130px"
[url]="['https://secure.digitalsignage.com/studioweb/assets/some_lazy1.png','https://secure.digitalsignage.com/studioweb/assets/some_lazy2.png']"
[loadingImage]="'https://secure.digitalsignage.com/studioweb/assets/screen_loading.png'"
[defaultImage]="'https://secure.digitalsignage.com/studioweb/assets/screen.png'"
[errorImage]="'https://secure.digitalsignage.com/studioweb/assets/screen_error.png'"
[retry]="5"
[delay]="1500"
(loaded)="_onLoaded()"
(error)="_onError()"
(completed)="_onCompleted()">
or to use via API
...
@ViewChild(LazyImage)
lazyImage: LazyImage;
_lazyLoad() {
this.lazyImage.setUrl(['https://secure.digitalsignage.com/studioweb/assets/some_lazy.png']);
}
_resetSnapshotSelection() {
if (this.lazyImage)
this.lazyImage.resetToDefault();
}
_takeSnapshot() {
this.lazyImage.url = 'http://example.com/foo.png;
}
...
*/
@Directive({
selector: '[lazyImage]'
})
export class LazyImage {
private m_urls: Array<string> = [];
private m_index = 0;
private cancel$ = new Subject();
constructor(private el: ElementRef, private ngZone: NgZone) {
}
// @Input('lazyImage') lazyImage; // to change support to directive of: <img lazyImage="'http://www...'" ...
@Output() loaded: EventEmitter<any> = new EventEmitter<any>();
@Output() completed: EventEmitter<any> = new EventEmitter<any>();
@Output() errored: EventEmitter<any> = new EventEmitter<any>();
@Input() defaultImage: string;
@Input() loadingImage: string;
@Input() errorImage: string;
@Input() retry: number = 5;
@Input() delay: number = 1000;
@Input()
set urls(i_urls: Array<string>) {
this.setUrls(i_urls)
}
setUrls(i_urls: Array<string>) {
this.m_index = 0;
this.m_urls = i_urls;
this.loadImage(i_urls);
}
public resetToDefault() {
this.setImage(this.el.nativeElement, this.defaultImage);
this.cancel$.next({})
}
ngAfterViewInit() {
this.setImage(this.el.nativeElement, this.defaultImage);
}
ngOnInit() {
}
setImage(element: HTMLElement, i_url) {
// const isImgNode = element.nodeName.toLowerCase() === 'img';
// if (isImgNode) {
// } else {
// element.style.backgroundImage = `url('${imagePath}')`;
// }
(<HTMLImageElement>element).src = i_url;
return element;
}
loadImage(i_urls) {
const pollAPI$ = Observable.defer(() => {
return new Promise((resolve, reject) => {
const img = new Image();
var url;
if (i_urls[this.m_index]){
url = i_urls[this.m_index];
this.m_index++;
} else {
this.m_index = 0;
url = i_urls[this.m_index];
}
img.src = url;
img.onload = () => {
resolve(url);
};
img.onerror = err => {
this.setImage(this.el.nativeElement, this.loadingImage);
reject(err)
};
})
}).retryWhen(err => {
return err.scan((errorCount, err) => {
if (errorCount >= this.retry) {
throw err;
}
return errorCount + 1;
}, 0)
.delay(this.delay);
})
.takeUntil(this.cancel$)
pollAPI$.subscribe((v) => {
this.setImage(this.el.nativeElement, v)
this.loaded.emit();
}, (e) => {
this.setImage(this.el.nativeElement, this.errorImage);
this.errored.emit();
// console.error(e)
}, () => {
this.completed.emit();
})
}
destroy() {
}
}