-
Notifications
You must be signed in to change notification settings - Fork 143
/
lazyload-image.directive.ts
68 lines (62 loc) 路 2.68 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
import { AfterContentInit, Directive, ElementRef, EventEmitter, Inject, Input, NgZone, OnChanges, OnDestroy, Optional, Output } from '@angular/core';
import { ReplaySubject, Observable, Subscription } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { cretateHooks } from './hooks-factory';
import { lazyLoadImage } from './lazyload-image';
import { Attributes, HookSet, ModuleOptions } from './types';
import { isWindowDefined } 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() scrollObservable?: Observable<any>; // Pass your own scroll 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
@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>;
constructor(el: ElementRef, ngZone: NgZone, @Optional() @Inject('options') options?: ModuleOptions) {
this.elementRef = el;
this.ngZone = ngZone;
this.propertyChanges$ = new ReplaySubject();
this.hooks = cretateHooks(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,
scrollObservable: this.scrollObservable
});
}
ngAfterContentInit() {
// Disable lazy load image in server side
if (!isWindowDefined()) {
return null;
}
this.ngZone.runOutsideAngular(() => {
this.scrollSubscription = this.propertyChanges$
.pipe(
tap(attributes => this.hooks.setup(attributes)),
switchMap(attributes => this.hooks.getObservable(attributes).pipe(lazyLoadImage(this.hooks, attributes)))
)
.subscribe(success => this.onLoad.emit(success));
});
}
ngOnDestroy() {
if (this.scrollSubscription) {
this.scrollSubscription.unsubscribe();
}
}
}