Skip to content

Commit

Permalink
lazy load images
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzizub committed May 14, 2019
1 parent 96321b4 commit 37453e8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/app/home/home.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {TopPartnerLogoComponent} from './top-partner-logo/top-partner-logo.component';
import {TopPartnerPanelComponent} from './top-partner-panel/top-partner-panel.component';
import {PhotoPanelComponent} from './photo-panel/photo-panel.component';
import { LazyDirective } from './lazy.directive';

@NgModule({
imports: [
Expand All @@ -40,7 +41,8 @@ import {PhotoPanelComponent} from './photo-panel/photo-panel.component';
TicketsComponent,
TopPartnerLogoComponent,
TopPartnerPanelComponent,
PhotoPanelComponent
PhotoPanelComponent,
LazyDirective
],
entryComponents: [
TicketAdditionalInfoComponent,
Expand Down
39 changes: 39 additions & 0 deletions src/app/home/lazy.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Directive, AfterViewInit, Output, EventEmitter, ElementRef } from '@angular/core';

@Directive({
selector: '[lazyLoad]'
})
export class LazyDirective implements AfterViewInit {

@Output() public lazyLoad: EventEmitter<any> = new EventEmitter();

private _intersectionObserver?: IntersectionObserver;

constructor(
private _element: ElementRef
) {
}

public ngAfterViewInit() {
this._intersectionObserver = new IntersectionObserver(entries => {
this.checkForIntersection(entries);
}, {});
this._intersectionObserver.observe((this._element.nativeElement) as Element);
}

private checkForIntersection = (entries: Array<IntersectionObserverEntry>) => {
entries.forEach((entry: IntersectionObserverEntry) => {
if (this.checkIfIntersecting(entry)) {
this.lazyLoad.emit();
this._intersectionObserver.unobserve((this._element.nativeElement) as Element);
this._intersectionObserver.disconnect();
}
});
}

private checkIfIntersecting(entry: IntersectionObserverEntry) {
return (entry as any).isIntersecting && entry.target === this._element.nativeElement;
}


}
4 changes: 2 additions & 2 deletions src/app/home/photo-panel/photo-panel.component.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="panel-holder">
<div class="panel-holder" (lazyLoad)="loadImages()">
<div class="photos-holder">
<div (click)="goToGallery()" class="more-photos-info">
<h1>Click to see more photos</h1>
</div>
<div class="mover">
<div class="mover" >
<img *ngFor="let image of images; let i = index"
[ngClass]="i == 0 ? 'first': 'second'"
(load)="setVisibilityOfImage(i)"
Expand Down
6 changes: 1 addition & 5 deletions src/app/home/photo-panel/photo-panel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum PhotoState {
transition('* <=> *', animate(500))
])]
})
export class PhotoPanelComponent implements OnInit {
export class PhotoPanelComponent{

images: Observable<string>[];
imagesColRef: Observable<Images[]>;
Expand All @@ -31,10 +31,6 @@ export class PhotoPanelComponent implements OnInit {

}

ngOnInit() {
this.loadImages();
}

loadImages() {
this.imagesColRef = this.afStore
.collection<Images>('images', ref => ref.where('name', '==', 'past-images').limit(1))
Expand Down

0 comments on commit 37453e8

Please sign in to comment.