Class ComponentObserver
java.lang.Object
com.vaadin.flow.component.Component
org.vaadin.miki.superfields.lazyload.ComponentObserver
- 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<ComponentObserver>
- Direct Known Subclasses:
ObservedField.ObservedFieldElement
@JsModule("./component-observer.js")
@Tag("component-observer")
public class ComponentObserver
extends com.vaadin.flow.component.Component
implements WithIdMixin<ComponentObserver>
A component that wraps Intersection Observer on the client-side to notify server-side about visibility changes.
Note: Intersection Observer API is experimental and not all browsers implement it.
- Since:
- 2020-04-28
- Author:
- miki
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionComponentObserver(double... visibilityRanges) Creates the observer using entire document as viewport.ComponentObserver(com.vaadin.flow.component.Component viewportRoot, String rootMargin, double... visibilityRanges) Creates the observer using provided document as viewport root and provided root margins. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationAdds a listener that will be informed about changes in observed components.protected voidBroadcasts the event usingComponent.getEventBus().double[]Returns an array with visibility ranges registered for this component.protected voidbooleanisObserving(com.vaadin.flow.component.Component component) Checks whether or not givenComponentis being observed by this object.voidobserve(com.vaadin.flow.component.Component... components) Starts observation of given components.protected voidObserves the given component with given index.protected voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent) protected voidonDetach(com.vaadin.flow.component.DetachEvent detachEvent) voidunobserve(com.vaadin.flow.component.Component... components) Stops observation of given components.Methods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, 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.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface org.vaadin.miki.markers.WithIdMixin
withId
-
Constructor Details
-
ComponentObserver
public ComponentObserver(double... visibilityRanges) Creates the observer using entire document as viewport.- Parameters:
visibilityRanges- Ranges at which to trigger visibility change events. If not specified,[0.0, 1.0]will be used.
-
ComponentObserver
public ComponentObserver(com.vaadin.flow.component.Component viewportRoot, String rootMargin, double... visibilityRanges) Creates the observer using provided document as viewport root and provided root margins.- Parameters:
viewportRoot- Component to use as viewport root.rootMargin- Root margin (CSS-like expression).visibilityRanges- Ranges at which to trigger visibility change events. If not specified,[0.0, 1.0]will be used.
-
-
Method Details
-
initClient
protected void initClient() -
onDetach
protected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent) - Overrides:
onDetachin classcom.vaadin.flow.component.Component
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttachin classcom.vaadin.flow.component.Component
-
fireComponentObservationEvent
Broadcasts the event usingComponent.getEventBus().- Parameters:
event- Event with relevant information about what happened.
-
observe
Observes the given component with given index. Will override the component currently associated with given index, if the was any.- Parameters:
indexString- Index string to register the component with.component- Component to observe.
-
observe
public void observe(com.vaadin.flow.component.Component... components) Starts observation of given components. If a given component is already being observed, it will not be observed an additional time. Given component will stop being observed when it gets detached or whenunobserve(Component...)method is called.- Parameters:
components- Components to observe.
-
unobserve
public void unobserve(com.vaadin.flow.component.Component... components) Stops observation of given components. If a given component has not been registered withobserve(Component...), nothing will happen.- Parameters:
components- Components to stop observing.
-
addComponentObservationListener
public com.vaadin.flow.shared.Registration addComponentObservationListener(ComponentObservationListener listener) Adds a listener that will be informed about changes in observed components.- Parameters:
listener- Listener to add.- Returns:
- Registration that can be used to stop listening.
-
isObserving
public boolean isObserving(com.vaadin.flow.component.Component component) Checks whether or not givenComponentis being observed by this object.- Parameters:
component- AComponent.- Returns:
truewhen thecomponenthas been added throughobserve(Component...)and not removed withunobserve(Component...).
-
getVisibilityRanges
public double[] getVisibilityRanges()Returns an array with visibility ranges registered for this component. Modifying the resulting array has no effect on this object.- Returns:
- A non-empty array with visibility ranges.
-