Class LazyLoad<C extends com.vaadin.flow.component.Component>
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.Composite<LazyLoad.LazyLoadElement>
org.vaadin.miki.superfields.lazyload.LazyLoad<C>
- Type Parameters:
C- Type of component that is lazy-loaded.
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasStyle,Serializable,HasId,WithIdMixin<LazyLoad<C>>
public class LazyLoad<C extends com.vaadin.flow.component.Component>
extends com.vaadin.flow.component.Composite<LazyLoad.LazyLoadElement>
implements WithIdMixin<LazyLoad<C>>, com.vaadin.flow.component.HasStyle
A component that remains empty until it gets into view. After that it displays the lazy-loaded content.
Note that by default the size of this component is 0px, as there is no styling to it. Be sure to apply styling.
As a result and by default, if there are many of lazy load components one next to another, all of them will trigger.
- Since:
- 2020-04-22
- Author:
- miki
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThis class exists so thatLazyLoadcan have a custom tag name. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates lazy load wrapper for given contents.Creates lazy load wrapper for given component supplier.Creates lazy load wrapper for given component supplier.LazyLoad(com.vaadin.flow.function.SerializableSupplier<C> contentSupplier, com.vaadin.flow.function.SerializableSupplier<ComponentObserver> observerSupplier) Creates lazy load wrapper for given component supplier.LazyLoad(com.vaadin.flow.function.SerializableSupplier<C> contentSupplier, com.vaadin.flow.function.SerializableSupplier<ComponentObserver> observerSupplier, boolean removeOnHide) Creates lazy load wrapper for given component supplier.Creates lazy load wrapper for given contents.LazyLoad(C contents, com.vaadin.flow.function.SerializableSupplier<ComponentObserver> observerSupplier) Creates lazy load wrapper for given contents. -
Method Summary
Modifier and TypeMethodDescriptiondoubleReturns the current range for considering the lazy-loaded component hidden.doubleReturns the current range for considering the lazy-loaded component visible.Gets the content if it was already loaded (isOnlyLoadedOnce()()} istrue) or if it is currently showing.booleanisLoaded()Checks if the content has been already loaded (isOnlyLoadedOnce()()} istrue) or is currently loaded.booleanReturns if the lazy loading happens only on the first showing.protected voidCalled when the content becomes hidden.protected voidCalled when the content becomes visible.voidsetContentVisibilityRanges(double hiddenOnOrBelow, double visibleOnOrAbove) Defines visibility ranges.withContentVisibilityRanges(double hiddenOnOrBelow, double visibleOnOrAbove) ChainssetContentVisibilityRanges(double, double)and returns itself.Methods inherited from class com.vaadin.flow.component.Composite
getChildren, getContent, getElement, initContentMethods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface org.vaadin.miki.markers.WithIdMixin
withId
-
Field Details
-
EMPTY_CLASS_NAME
Style name when this component is empty (not showing anything).- See Also:
-
LOADED_CLASS_NAME
Style name when this component has content.- See Also:
-
-
Constructor Details
-
LazyLoad
Creates lazy load wrapper for given contents. It will be displayed the first time this component becomes shown on screen.- Parameters:
contents- Contents to wrap for lazy loading.
-
LazyLoad
Creates lazy load wrapper for given component supplier. It will be called exactly once, the first time this component becomes shown on screen.- Parameters:
supplier-Supplierthat will be called when the component gets into view.
-
LazyLoad
Creates lazy load wrapper for given contents.- Parameters:
contents- Contents to wrap for lazy loading.removeOnHide- Whether to remove the component when this object gets hidden.
-
LazyLoad
Creates lazy load wrapper for given component supplier.- Parameters:
supplier-Supplierthat will be called when the component gets into view.removeOnHide- Whether to remove the component when this object gets hidden.
-
LazyLoad
public LazyLoad(C contents, com.vaadin.flow.function.SerializableSupplier<ComponentObserver> observerSupplier) Creates lazy load wrapper for given contents. It will be displayed the first time this component becomes shown on screen.- Parameters:
contents- Contents to wrap for lazy loading.observerSupplier- a way to create a customisedComponentObserverthat will be used by this component.
-
LazyLoad
public LazyLoad(com.vaadin.flow.function.SerializableSupplier<C> contentSupplier, com.vaadin.flow.function.SerializableSupplier<ComponentObserver> observerSupplier) Creates lazy load wrapper for given component supplier. It will be called exactly once, the first time this component becomes shown on screen.- Parameters:
contentSupplier-Supplierthat will be called when the component gets into view.observerSupplier- a way to create a customisedComponentObserverthat will be used by this component.
-
LazyLoad
public LazyLoad(com.vaadin.flow.function.SerializableSupplier<C> contentSupplier, com.vaadin.flow.function.SerializableSupplier<ComponentObserver> observerSupplier, boolean removeOnHide) Creates lazy load wrapper for given component supplier.- Parameters:
contentSupplier-Supplierthat will be called when the component gets into view.observerSupplier- a way to create a customisedComponentObserverthat will be used by this component.removeOnHide- Whether to remove the component when this object gets hidden.
-
-
Method Details
-
getContentHiddenVisibilityRange
public double getContentHiddenVisibilityRange()Returns the current range for considering the lazy-loaded component hidden.- Returns:
- A number between 0 (inclusive) and
getContentShownVisibilityRange()(inclusive). - See Also:
-
getContentShownVisibilityRange
public double getContentShownVisibilityRange()Returns the current range for considering the lazy-loaded component visible.- Returns:
- A number between
getContentHiddenVisibilityRange()(inclusive) and 1 (inclusive). - See Also:
-
setContentVisibilityRanges
public void setContentVisibilityRanges(double hiddenOnOrBelow, double visibleOnOrAbove) Defines visibility ranges.LazyLoaduses aComponentObserverto decide if the content should be shown or not. By default, the component is lazy-loaded whenLazyLoadis fully visible and hidden when it is fully invisible. Defining ranges allows for lazy-loading the contents when it becomes partially visible. Be sure to know what you are doing and why.- Parameters:
hiddenOnOrBelow- Visibility range at which or below which the component is considered hidden. Must be between 0 (inclusive) and the other parameter (inclusive).visibleOnOrAbove- Visibility range at which or above which the component is considered visible. Must be between the other parameter (inclusive) and 1 (inclusive).- Throws:
IllegalArgumentException- when either range is below 0, above 1, or the first parameter is greater than the second- See Also:
-
withContentVisibilityRanges
ChainssetContentVisibilityRanges(double, double)and returns itself.- Parameters:
hiddenOnOrBelow- Visibility range at which or below which the component is considered hidden. Must be between 0 (inclusive) and the other parameter (inclusive).visibleOnOrAbove- Visibility range at which or above which the component is considered visible. Must be between the other parameter (inclusive) and 1 (inclusive).- Returns:
- This.
- See Also:
-
onNowHidden
protected void onNowHidden()Called when the content becomes hidden. -
onNowVisible
protected void onNowVisible()Called when the content becomes visible. -
isOnlyLoadedOnce
public boolean isOnlyLoadedOnce()Returns if the lazy loading happens only on the first showing.- Returns:
truewhen the target component will be loaded only once, the first time this component is shown; otherwisefalse.
-
getLoadedContent
Gets the content if it was already loaded (isOnlyLoadedOnce()()} istrue) or if it is currently showing.- Returns:
- A component that was lazy-loaded. If the component was not yet shown on-screen, returns
Optional.empty(). - See Also:
-
isLoaded
public boolean isLoaded()Checks if the content has been already loaded (isOnlyLoadedOnce()()} istrue) or is currently loaded.- Returns:
- Whether the content has been loaded.
- See Also:
-