-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathlazyload-image.directive.ts
78 lines (72 loc) · 3.16 KB
/
lazyload-image.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
import { isPlatformServer } from '@angular/common';
import { AfterContentInit, Directive, ElementRef, EventEmitter, Inject, Input, NgZone, OnChanges, OnDestroy, Optional, Output, PLATFORM_ID } from '@angular/core';
import { Observable, ReplaySubject, Subscription, never } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { createHooks } from './hooks-factory';
import { lazyLoadImage } from './lazyload-image';
import { Attributes, HookSet, ModuleOptions } from './types';
import { getNavigator } from './util';
@Directive({
selector: '[lazyLoad]'
})
export class LazyLoadImageDirective implements OnChanges, AfterContentInit, OnDestroy {
@Input('lazyLoad') lazyImage!: string; // The image to be lazy loaded
@Input() defaultImage?: string; // The image to be displayed before lazyImage is loaded
@Input() errorImage?: string; // The image to be displayed if lazyImage load fails
@Input() scrollTarget?: any; // Scroll container that contains the image and emits scoll events
@Input() customObservable?: Observable<any>; // Pass your own event emitter
@Input() offset?: number; // The number of px a image should be loaded before it is in view port
@Input() useSrcset?: boolean; // Whether srcset attribute should be used instead of src
@Input() decode?: boolean; // Decode the image before appending to the DOM
@Output() onLoad: EventEmitter<boolean> = new EventEmitter(); // Callback when an image is loaded
private propertyChanges$: ReplaySubject<Attributes>;
private elementRef: ElementRef;
private ngZone: NgZone;
private scrollSubscription?: Subscription;
private hooks: HookSet<any>;
private platformId: Object;
constructor(el: ElementRef, ngZone: NgZone, @Inject(PLATFORM_ID) platformId: Object, @Optional() @Inject('options') options?: ModuleOptions) {
this.elementRef = el;
this.ngZone = ngZone;
this.propertyChanges$ = new ReplaySubject();
this.platformId = platformId;
this.hooks = createHooks(platformId, options);
}
ngOnChanges() {
this.propertyChanges$.next({
element: this.elementRef.nativeElement,
imagePath: this.lazyImage,
defaultImagePath: this.defaultImage,
errorImagePath: this.errorImage,
useSrcset: this.useSrcset,
offset: this.offset ? this.offset | 0 : 0,
scrollContainer: this.scrollTarget,
customObservable: this.customObservable,
decode: this.decode
});
}
ngAfterContentInit() {
// Don't do anything if SSR and the user isn't a bot
if (isPlatformServer(this.platformId) && !this.hooks.isBot(getNavigator(), this.platformId)) {
return null;
}
this.ngZone.runOutsideAngular(() => {
this.scrollSubscription = this.propertyChanges$
.pipe(
tap(attributes => this.hooks.setup(attributes)),
switchMap(attributes => {
if (!attributes.imagePath) {
return never()
}
return this.hooks.getObservable(attributes).pipe(lazyLoadImage(this.hooks, attributes))
})
)
.subscribe(success => this.onLoad.emit(success));
});
}
ngOnDestroy() {
if (this.scrollSubscription) {
this.scrollSubscription.unsubscribe();
}
}
}