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

Improved isVisible function for images within scoll containers. #241 #258

Merged

Conversation

sapierens
Copy link
Collaborator

Hey, here's the improved image visibility detection within a scroll container.

I'll comment some lines where I need your opinion.

@sapierens sapierens requested a review from tjoskar January 1, 2018 17:18
setImageAndSourcesToDefault(element, defaultImagePath, useSrcset);
if (element.className && element.className.includes('ng-lazyloaded')) {
element.className = element.className.replace('ng-lazyloaded', '');
}

return (scrollObservable: Observable<Event>) => {
return scrollObservable
.filter(() => isVisible(element, offset))
.filter(() => isVisible(element, offset, window, scrollContainer))
Copy link
Collaborator Author

@sapierens sapierens Jan 1, 2018

Choose a reason for hiding this comment

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

I chose to pass the window object here, which might not be the prettiest thing to do. But I didn't want to break the argument order.
Would you rather have me flip the arguments, so that _window = window and I change all of the tests to comply with the change?

Copy link
Owner

Choose a reason for hiding this comment

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

This is fine. It feels however a bit weird to have an optional argument (threshold) before a mandatory (_window) but I think we should keep threshold = 0 so it defaults to 0 if threshold is undefined, so keep it as it is.

}
} else {
return elementBounds.intersectsWith(windowBounds);
}
Copy link
Collaborator Author

@sapierens sapierens Jan 1, 2018

Choose a reason for hiding this comment

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

What's your preferred code convention for these kind of else statements? Do you prefer it like it is, or like this:

if (scrollContainer) {
    const scrollContainerBounds = Rect.fromElement(scrollContainer);
    if (scrollContainerBounds.intersectsWith(windowBounds)) {
        const intersection = scrollContainerBounds.getIntersectionWith(windowBounds);
        return elementBounds.intersectsWith(intersection);
    }
    return false;
}
return elementBounds.intersectsWith(windowBounds);

EDIT: Sorry, not the best line selection for a comment.

Copy link
Owner

@tjoskar tjoskar left a comment

Choose a reason for hiding this comment

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

Looking good, thanks 🎉


interface LazyLoadImageDirectiveProps {
lazyImage: string;
defaultImage: string;
errorImage: string;
scrollTarget: Object;
scrollTarget: HTMLElement;
Copy link
Owner

Choose a reason for hiding this comment

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

Since we add a type here we may also add it in scroll-listener.ts. You do not have to do anything here, I created a separated issue: #270

setImageAndSourcesToDefault(element, defaultImagePath, useSrcset);
if (element.className && element.className.includes('ng-lazyloaded')) {
element.className = element.className.replace('ng-lazyloaded', '');
}

return (scrollObservable: Observable<Event>) => {
return scrollObservable
.filter(() => isVisible(element, offset))
.filter(() => isVisible(element, offset, window, scrollContainer))
Copy link
Owner

Choose a reason for hiding this comment

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

This is fine. It feels however a bit weird to have an optional argument (threshold) before a mandatory (_window) but I think we should keep threshold = 0 so it defaults to 0 if threshold is undefined, so keep it as it is.

is(result, false);
});

it('Should not be visible when image is vertically in window\'s view, but not in scroll-container\'s', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

What do you think of using string template instead of '? Eg.

it(`Should not be visible when image is vertically in window's view, but not in scroll-container's`, () => 

@tjoskar tjoskar merged commit 09548f9 into tjoskar:master Jan 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants