src/comps/lazy-image/lazy-image.ts
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:
[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
...
selector | [lazyImage] |
defaultImage
|
Type: |
Defined in src/comps/lazy-image/lazy-image.ts:67
|
delay
|
Type:
Default value: |
Defined in src/comps/lazy-image/lazy-image.ts:71
|
errorImage
|
Type: |
Defined in src/comps/lazy-image/lazy-image.ts:69
|
loadingImage
|
Type: |
Defined in src/comps/lazy-image/lazy-image.ts:68
|
retry
|
Type:
Default value: |
Defined in src/comps/lazy-image/lazy-image.ts:70
|
urls
|
|
Defined in src/comps/lazy-image/lazy-image.ts:74
|
completed
|
$event type: EventEmitter<any>
|
Defined in src/comps/lazy-image/lazy-image.ts:65
|
errored
|
$event type: EventEmitter<any>
|
Defined in src/comps/lazy-image/lazy-image.ts:66
|
loaded
|
$event type: EventEmitter<any>
|
Defined in src/comps/lazy-image/lazy-image.ts:64
|
constructor(el: ElementRef, ngZone: NgZone)
|
Defined in src/comps/lazy-image/lazy-image.ts:57
|
setUrls |
setUrls(i_urls: string[])
|
Defined in src/comps/lazy-image/lazy-image.ts:78
|
Returns :
void
|
Static resetToDefault |
resetToDefault()
|
Defined in src/comps/lazy-image/lazy-image.ts:84
|
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Defined in src/comps/lazy-image/lazy-image.ts:89
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Defined in src/comps/lazy-image/lazy-image.ts:93
|
Returns :
void
|
setImage |
setImage(element: HTMLElement, i_url: any)
|
Defined in src/comps/lazy-image/lazy-image.ts:96
|
Returns :
void
|
loadImage |
loadImage(i_urls: any)
|
Defined in src/comps/lazy-image/lazy-image.ts:106
|
Returns :
void
|
destroy |
destroy()
|
Defined in src/comps/lazy-image/lazy-image.ts:151
|
Returns :
void
|
Protected cancel$ |
cancel$: |
Defined in src/comps/lazy-image/lazy-image.ts:57
|
Protected m_index |
m_index: |
Default value : 0
|
Defined in src/comps/lazy-image/lazy-image.ts:56
|
Protected m_urls |
m_urls: |
Defined in src/comps/lazy-image/lazy-image.ts:55
|
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() {
}
}