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

Created functions to control css class names #298

Merged
merged 3 commits into from Feb 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 6 additions & 14 deletions src/lazyload-image.ts
Expand Up @@ -9,6 +9,7 @@ import { Observable } from 'rxjs/Observable';
import { getScrollListener } from './scroll-listener';
import { Rect } from './rect';
import { cssClassNames } from './constants';
import { hasCssClassName, removeCssClassName, addCssClassName } from './utils';

export function isVisible(element: HTMLElement, threshold = 0, _window: Window, scrollContainer?: HTMLElement) {
const elementBounds = Rect.fromElement(element);
Expand Down Expand Up @@ -107,20 +108,11 @@ const setImageAndSourcesToDefault = setImageAndSources(setSourcesToDefault);
const setImageAndSourcesToLazy = setImageAndSources(setSourcesToLazy);
const setImageAndSourcesToError = setImageAndSources(setSourcesToError);

function setLoadedStyle(element: HTMLImageElement | HTMLDivElement) {
const styles = element.className
.split(' ')
.filter(s => !!s)
.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(cssClassNames.loaded)) {
element.className = element.className.replace(cssClassNames.loaded, '');

if (hasCssClassName(element, cssClassNames.loaded)) {
removeCssClassName(element, cssClassNames.loaded)
}

return (scrollObservable: Observable<Event>) => {
Expand All @@ -132,9 +124,9 @@ export function lazyLoadImage(element: HTMLImageElement | HTMLDivElement, imageP
.map(() => true)
.catch(() => {
setImageAndSourcesToError(element, errorImgPath, useSrcset);
element.className += ' ' + cssClassNames.failed;
addCssClassName(element, cssClassNames.failed);
return Observable.of(false);
})
.do(() => setLoadedStyle(element));
.do(() => addCssClassName(element, cssClassNames.loaded));
};
}
14 changes: 14 additions & 0 deletions src/utils.ts
@@ -1,3 +1,17 @@
export function isWindowDefined() {
return typeof window !== 'undefined';
}

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

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

export function hasCssClassName(element: HTMLImageElement | HTMLDivElement, cssClassName: string) {
return element.className && element.className.includes(cssClassName);
}
Copy link
Owner

Choose a reason for hiding this comment

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

Even though these functions are quite simple, it should be nice to add some unit tests.

Copy link
Owner

Choose a reason for hiding this comment

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

We do not need to test replace and includes but the code has logic that checks if className is defined and that line 11 only should run if cssClassName don't exist.