This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lazyLoad() { | |
const obs = new IntersectionObserver(entries => { | |
entries.forEach(({ isIntersecting }) => { | |
if (isIntersecting) { | |
this.srcAttr = this.src; | |
obs.unobserve(this.el.nativeElement); | |
} | |
}); | |
}); | |
obs.observe(this.el.nativeElement); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AfterViewInit, Directive, ElementRef, HostBinding, Input } from '@angular/core'; | |
@Directive({ | |
selector: 'img[appLazyLoad]' | |
}) | |
export class LazyLoadDirective implements AfterViewInit { | |
@HostBinding('attr.src') srcAttr = null; | |
@Input() src: string; | |
constructor(private el: ElementRef) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ | |
selector: '[appProgressiveImageFallback]', | |
providers: [...] | |
}) | |
export class ProgressiveImageFallbackDirective implements OnInit, Dimensions { | |
// ... | |
@HostBinding('style.display') display = Display.flex; | |
constructor(private imageState: ImageStateService) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ | |
selector: '[appProgressiveImagePlaceholder]', | |
providers: [...] | |
}) | |
export class ProgressiveImagePlaceholderDirective implements OnInit, Dimensions { | |
// ... | |
@HostBinding('style.display') display = Display.flex; | |
constructor(private imageState: ImageStateService) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ | |
selector: 'img[appProgressiveImage]', | |
providers: [...] | |
}) | |
export class ProgressiveImageDirective implements OnInit, Dimensions { | |
// ... | |
@HostBinding('style.display') display = Display.flex; | |
constructor(private imageState: ImageStateService) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export enum Display { | |
flex = 'flex', | |
none = 'none' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ | |
selector: 'img[appProgressiveImage]', | |
providers: [{ | |
provide: Dimensions, | |
useExisting: forwardRef(() => ProgressiveImageDirective) | |
}] | |
}) | |
export class ProgressiveImageDirective implements OnInit, Dimensions { | |
@HostBinding('style.width.px') | |
width: number; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-progressive-image', | |
template: `<ng-content></ng-content>`, | |
providers: [ImageStateService] | |
}) | |
export class ProgressiveImageComponent implements AfterContentChecked {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum ImageState { | |
loading = 'loading', | |
error = 'error', | |
loaded = 'loaded' | |
} | |
@Injectable() | |
export class ImageStateService { | |
private stateSource = new Subject<ImageState>(); | |
private state$ = this.stateSource; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ | |
selector: 'img[appProgressiveImage]', | |
providers: [{ | |
provide: Dimensions, | |
useExisting: forwardRef(() => ProgressiveImageDirective) | |
}] | |
}) | |
export class ProgressiveImageDirective implements OnInit, Dimensions {} | |
@Directive({ |