Class ComponentObserver

  • All Implemented Interfaces:
    com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.template.internal.DeprecatedPolymerTemplate, com.vaadin.flow.internal.Template, Serializable, HasId, WithIdMixin<ComponentObserver>
    Direct Known Subclasses:
    ObservedField.ObservedFieldElement

    @JsModule("./component-observer.js")
    @Tag("component-observer")
    public class ComponentObserver
    extends com.vaadin.flow.component.polymertemplate.PolymerTemplate<com.vaadin.flow.templatemodel.TemplateModel>
    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:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      ComponentObserver​(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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.vaadin.flow.shared.Registration addComponentObservationListener​(ComponentObservationListener listener)
      Adds a listener that will be informed about changes in observed components.
      protected void fireComponentObservationEvent​(ComponentObservationEvent event)
      Broadcasts the event using Component.getEventBus().
      double[] getVisibilityRanges()
      Returns an array with visibility ranges registered for this component.
      protected void initClient()  
      boolean isObserving​(com.vaadin.flow.component.Component component)
      Checks whether or not given Component is being observed by this object.
      void observe​(com.vaadin.flow.component.Component... components)
      Starts observation of given components.
      protected void observe​(String indexString, com.vaadin.flow.component.Component component)
      Observes the given component with given index.
      protected void onAttach​(com.vaadin.flow.component.AttachEvent attachEvent)  
      protected void onDetach​(com.vaadin.flow.component.DetachEvent detachEvent)  
      void unobserve​(com.vaadin.flow.component.Component... components)
      Stops observation of given components.
      • Methods inherited from class com.vaadin.flow.component.polymertemplate.PolymerTemplate

        getChildren
      • Methods inherited from class com.vaadin.flow.component.polymertemplate.AbstractTemplate

        getModel, getModelType, getModelType, getStateNode, initModel, isSupportedClass
      • Methods inherited from class com.vaadin.flow.component.Component

        addListener, findAncestor, fireEvent, from, get, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onEnabledStateChanged, scrollIntoView, set, setElement, setId, setVisible
      • Methods inherited from interface com.vaadin.flow.component.AttachNotifier

        addAttachListener
      • Methods inherited from interface com.vaadin.flow.component.DetachNotifier

        addDetachListener
    • Constructor Detail

      • 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 Detail

      • initClient

        protected void initClient()
      • onDetach

        protected void onDetach​(com.vaadin.flow.component.DetachEvent detachEvent)
        Overrides:
        onDetach in class com.vaadin.flow.component.Component
      • onAttach

        protected void onAttach​(com.vaadin.flow.component.AttachEvent attachEvent)
        Overrides:
        onAttach in class com.vaadin.flow.component.Component
      • fireComponentObservationEvent

        protected void fireComponentObservationEvent​(ComponentObservationEvent event)
        Broadcasts the event using Component.getEventBus().
        Parameters:
        event - Event with relevant information about what happened.
      • observe

        protected void observe​(String indexString,
                               com.vaadin.flow.component.Component component)
        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 when unobserve(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 with observe(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 given Component is being observed by this object.
        Parameters:
        component - A Component.
        Returns:
        true when the component has been added through observe(Component...) and not removed with unobserve(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.