Class UnloadObserver

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

    @JsModule("./unload-observer.js")
    @Tag("unload-observer")
    public class UnloadObserver
    extends com.vaadin.flow.component.polymertemplate.PolymerTemplate<com.vaadin.flow.templatemodel.TemplateModel>
    implements WithIdMixin<UnloadObserver>
    Server-side component that listens to beforeunload events. Based on the code by Kaspar Scherrer and Stuart Robinson. This component will broadcast events on beforeunload event in the browser. If isQueryingOnUnload() is true, before the event the user will be prompted about leaving the page. However, there is no way to find out what the user selected. If isQueryingOnUnload() is false, the event on the server will be called just before the page is unloaded. Note that the component must be present in the DOM structure in the browser for the event to be received on the server. Warning: this class is pretty much a UI-scoped singleton; the class is final, the constructors are private and there is at most one global instance per UI.
    Since:
    2020-04-29
    Author:
    Kaspar Scherrer, Stuart Robinson; adapted to web-component by miki; bugfixing and enhancements by Jean-François Lamy
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.vaadin.flow.shared.Registration addUnloadListener​(UnloadListener listener)
      protected void fireUnloadEvent​(UnloadEvent event)
      Fires the UnloadEvent.
      static UnloadObserver get()
      Returns or creates an instance for current UI.
      static UnloadObserver get​(com.vaadin.flow.component.UI ui)
      Returns or creates an instance for a given UI.
      static UnloadObserver getAttached()
      Returns or creates an instance for current UI and attaches that instance to the UI, if not yet attached.
      static <C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasComponents>
      UnloadObserver
      getAttached​(C parent)
      Returns or creates an instance for the UI associated with given parent and attaches that instance to parent.
      boolean isQueryingOnUnload()
      Returns whether or not querying on document unload will happen.
      protected void onAttach​(com.vaadin.flow.component.AttachEvent attachEvent)  
      protected void onDetach​(com.vaadin.flow.component.DetachEvent detachEvent)  
      void setQueryingOnUnload​(boolean queryingOnUnload)
      Controls whether or not there should be querying when the document is going to be unloaded.
      UnloadObserver withoutQueryingOnUnload()
      Shortcut for withQueryingOnUnload(false).
      UnloadObserver withQueryingOnUnload()
      Shortcut for withQueryingOnUnload(true).
      UnloadObserver withQueryingOnUnload​(boolean value)
      Chains setQueryingOnUnload(boolean) and returns itself.
      • 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
    • Method Detail

      • get

        public static UnloadObserver get()
        Returns or creates an instance for current UI. The result is associated with the UI, but not added to any of its components.
        Returns:
        An instance of UnloadObserver.
        Throws:
        IllegalStateException - if there is no current UI.
      • get

        public static UnloadObserver get​(com.vaadin.flow.component.UI ui)
        Returns or creates an instance for a given UI. The result is associated with the UI, but not added to any of its components.
        Parameters:
        ui - A UI to register the instance of UnloadObserver in. Must not be null.
        Returns:
        An instance of UnloadObserver.
      • getAttached

        public static UnloadObserver getAttached()
        Returns or creates an instance for current UI and attaches that instance to the UI, if not yet attached. The result is associated with the UI and added to it directly. Identical to calling getAttached(Component) with UI.getCurrent().
        Returns:
        An instance of UnloadObserver.
      • getAttached

        public static <C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasComponents> UnloadObserver getAttached​(C parent)
        Returns or creates an instance for the UI associated with given parent and attaches that instance to parent. If the instance was already created and belonged to some other parent and that parent allows removing components, an attempt will be made to remove the instance before adding it to the new parent. If that fails, an IllegalStateException will be thrown. If the UI associated with parent is null, an IllegalArgumentException will be thrown.
        Type Parameters:
        C - Generic type to ensure the parent can have other components added to it.
        Parameters:
        parent - A Component to add the UnloadObserver to. Must not be null.
        Returns:
        An instance of UnloadObserver.
        Throws:
        IllegalStateException - when an instance of UnloadObserver is attached to a component it cannot be removed from
        IllegalArgumentException - when the Component.getUI() for parent is not present
      • setQueryingOnUnload

        public void setQueryingOnUnload​(boolean queryingOnUnload)
        Controls whether or not there should be querying when the document is going to be unloaded.
        Parameters:
        queryingOnUnload - When true, UnloadListeners registered through addUnloadListener(UnloadListener) will be notified and document unloading can be prevented. When false, nothing will happen when the document gets unloaded.
      • isQueryingOnUnload

        public boolean isQueryingOnUnload()
        Returns whether or not querying on document unload will happen.
        Returns:
        true when unloading the document from browser window results in showing a browser-native confirmation dialog and notifying UnloadListeners; false otherwise.
      • onAttach

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

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

        protected void fireUnloadEvent​(UnloadEvent event)
        Fires the UnloadEvent.
        Parameters:
        event - Event to fire.
      • addUnloadListener

        public com.vaadin.flow.shared.Registration addUnloadListener​(UnloadListener listener)
        Parameters:
        listener - Listener to add.
        Returns:
        A Registration that can be used to stop listening to the event.