Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #274

Merged
merged 5 commits into from Jan 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/constants.ts
@@ -0,0 +1,5 @@
export const cssClassNames = {
loaded: 'ng-lazyloaded',
loading: 'ng-lazyloading',
failed: 'ng-failed-lazyloaded',
}
9 changes: 3 additions & 6 deletions src/lazyload-image.directive.ts
Expand Up @@ -17,8 +17,7 @@ import {
} from '@angular/core';
import { getScrollListener } from './scroll-listener';
import { lazyLoadImage } from './lazyload-image';

const windowTarget = typeof window !== 'undefined' ? window : undefined;
import { isWindowDefined } from './utils';

interface LazyLoadImageDirectiveProps {
lazyImage: string;
Expand All @@ -45,7 +44,6 @@ export class LazyLoadImageDirective implements OnChanges, AfterContentInit, OnDe
private propertyChanges$: ReplaySubject<LazyLoadImageDirectiveProps>;
private elementRef: ElementRef;
private ngZone: NgZone;
private platformId: string;
private scrollSubscription;

constructor(el: ElementRef, ngZone: NgZone) {
Expand All @@ -69,10 +67,8 @@ export class LazyLoadImageDirective implements OnChanges, AfterContentInit, OnDe
ngAfterContentInit() {
/**
* Disable lazy load image in server side
* @see https://github.com/tjoskar/ng-lazyload-image/issues/178
* @see https://github.com/tjoskar/ng-lazyload-image/issues/183
*/
if (typeof window === 'undefined') {
if (!isWindowDefined()) {
return null;
}

Expand All @@ -81,6 +77,7 @@ export class LazyLoadImageDirective implements OnChanges, AfterContentInit, OnDe
if (this.scrollObservable) {
scrollObservable = this.scrollObservable.startWith('');
} else {
const windowTarget = isWindowDefined() ? window : undefined;
scrollObservable = getScrollListener(this.scrollTarget || windowTarget);
}
this.scrollSubscription = this.propertyChanges$
Expand Down
11 changes: 6 additions & 5 deletions src/lazyload-image.ts
Expand Up @@ -8,6 +8,7 @@ import 'rxjs/add/observable/of';
import { Observable } from 'rxjs/Observable';
import { getScrollListener } from './scroll-listener';
import { Rect } from './rect';
import { cssClassNames } from './constants';

export function isVisible(element: HTMLElement, threshold = 0, _window: Window, scrollContainer?: HTMLElement) {
const elementBounds = Rect.fromElement(element);
Expand Down Expand Up @@ -110,16 +111,16 @@ function setLoadedStyle(element: HTMLImageElement | HTMLDivElement) {
const styles = element.className
.split(' ')
.filter(s => !!s)
.filter(s => s !== 'ng-lazyloading');
styles.push('ng-lazyloaded');
.filter(s => s !== cssClassNames.loading);
styles.push(cssClassNames.loaded);
element.className = styles.join(' ');
return element;
}

export function lazyLoadImage(element: HTMLImageElement | HTMLDivElement, imagePath: string, defaultImagePath: string, errorImgPath: string, offset: number, useSrcset: boolean = false, scrollContainer?: HTMLElement) {
setImageAndSourcesToDefault(element, defaultImagePath, useSrcset);
if (element.className && element.className.includes('ng-lazyloaded')) {
element.className = element.className.replace('ng-lazyloaded', '');
if (element.className && element.className.includes(cssClassNames.loaded)) {
element.className = element.className.replace(cssClassNames.loaded, '');
Copy link
Owner

@tjoskar tjoskar Jan 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much better but I still think that we should create a function that is only responsible for setting and removing css class names. Something like:

function removeCssClassName(element: HTMLImageElement | HTMLDivElement, cssClassName: string) {
  element.className = element.className.replace(cssClassName, '');
}

function addCssClassName(element: HTMLImageElement | HTMLDivElement, cssClassName: string) {
  if (!element.className.includes(cssClassName)) {
    element.className += `  ${cssClassName}`
  }
}

And then use these two function instead of:

if (element.className && element.className.includes(cssClassNames.loaded)) {
  element.className = element.className.replace(cssClassNames.loaded, '');
}
element.className += ' ' + cssClassNames.failed;
setLoadedStyle

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rimlin, I saw your comment now, regarding that you addressed 2, 4, 5 and not 3 which this comment is about. I will therefore accept this PR without this change.

}

return (scrollObservable: Observable<Event>) => {
Expand All @@ -131,7 +132,7 @@ export function lazyLoadImage(element: HTMLImageElement | HTMLDivElement, imageP
.map(() => true)
.catch(() => {
setImageAndSourcesToError(element, errorImgPath, useSrcset);
element.className += ' ng-failed-lazyloaded';
element.className += ' ' + cssClassNames.failed;
return Observable.of(false);
})
.do(() => setLoadedStyle(element));
Expand Down
3 changes: 2 additions & 1 deletion src/scroll-listener.ts
Expand Up @@ -3,6 +3,7 @@ import 'rxjs/add/operator/sampleTime';
import 'rxjs/add/operator/share';
import 'rxjs/add/observable/empty';
import { Observable } from 'rxjs/Observable';
import { isWindowDefined } from './utils';

const scrollListeners = new WeakMap<any, Observable<any>>();

Expand All @@ -17,7 +18,7 @@ export function sampleObservable(obs: Observable<any>, scheduler?: any) {
// Typical, there will only be one observable per application
export const getScrollListener = (scrollTarget): Observable<any> => {
if (!scrollTarget || typeof scrollTarget.addEventListener !== 'function') {
if (typeof window !== 'undefined') {
if (isWindowDefined()) {
console.warn('`addEventListener` on ' + scrollTarget + ' (scrollTarget) is not a function. Skipping this target');
}
return Observable.empty();
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
@@ -0,0 +1,3 @@
export function isWindowDefined() {
return typeof window !== 'undefined';
}