Class UnloadObserver
- java.lang.Object
-
- com.vaadin.flow.component.Component
-
- com.vaadin.flow.component.polymertemplate.AbstractTemplate<M>
-
- com.vaadin.flow.component.polymertemplate.PolymerTemplate<com.vaadin.flow.templatemodel.TemplateModel>
-
- org.vaadin.miki.superfields.unload.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 tobeforeunloadevents. Based on the code by Kaspar Scherrer and Stuart Robinson. This component will broadcast events onbeforeunloadevent in the browser. IfisQueryingOnUnload()istrue, before the event the user will be prompted about leaving the page. However, there is no way to find out what the user selected. IfisQueryingOnUnload()isfalse, 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 aUI-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.RegistrationaddUnloadListener(UnloadListener listener)Adds anUnloadListener.protected voidfireUnloadEvent(UnloadEvent event)Fires theUnloadEvent.static UnloadObserverget()Returns or creates an instance for current UI.static UnloadObserverget(com.vaadin.flow.component.UI ui)Returns or creates an instance for a given UI.static UnloadObservergetAttached()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>
UnloadObservergetAttached(C parent)Returns or creates an instance for the UI associated with givenparentand attaches that instance toparent.booleanisQueryingOnUnload()Returns whether or not querying on document unload will happen.protected voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent)protected voidonDetach(com.vaadin.flow.component.DetachEvent detachEvent)voidsetQueryingOnUnload(boolean queryingOnUnload)Controls whether or not there should be querying when the document is going to be unloaded.UnloadObserverwithoutQueryingOnUnload()Shortcut forwithQueryingOnUnload(false).UnloadObserverwithQueryingOnUnload()Shortcut forwithQueryingOnUnload(true).UnloadObserverwithQueryingOnUnload(boolean value)ChainssetQueryingOnUnload(boolean)and returns itself.-
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 class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.vaadin.miki.markers.WithIdMixin
withId
-
-
-
-
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 currentUI.
-
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- AUIto register the instance ofUnloadObserverin. Must not benull.- 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 callinggetAttached(Component)withUI.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 givenparentand attaches that instance toparent. 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, anIllegalStateExceptionwill be thrown. If the UI associated withparentisnull, anIllegalArgumentExceptionwill be thrown.- Type Parameters:
C- Generic type to ensure the parent can have other components added to it.- Parameters:
parent- AComponentto add theUnloadObserverto. Must not benull.- Returns:
- An instance of
UnloadObserver. - Throws:
IllegalStateException- when an instance ofUnloadObserveris attached to a component it cannot be removed fromIllegalArgumentException- when theComponent.getUI()forparentis 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- Whentrue,UnloadListeners registered throughaddUnloadListener(UnloadListener)will be notified and document unloading can be prevented. Whenfalse, nothing will happen when the document gets unloaded.
-
isQueryingOnUnload
public boolean isQueryingOnUnload()
Returns whether or not querying on document unload will happen.- Returns:
truewhen unloading the document from browser window results in showing a browser-native confirmation dialog and notifyingUnloadListeners;falseotherwise.
-
withQueryingOnUnload
public UnloadObserver withQueryingOnUnload(boolean value)
ChainssetQueryingOnUnload(boolean)and returns itself.- Parameters:
value- Whether or not to query on document unload.- Returns:
- This.
- See Also:
setQueryingOnUnload(boolean)
-
withQueryingOnUnload
public UnloadObserver withQueryingOnUnload()
Shortcut forwithQueryingOnUnload(true).- Returns:
- This.
- See Also:
withQueryingOnUnload(boolean)
-
withoutQueryingOnUnload
public UnloadObserver withoutQueryingOnUnload()
Shortcut forwithQueryingOnUnload(false).- Returns:
- This.
- See Also:
withQueryingOnUnload(boolean)
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
- Overrides:
onAttachin classcom.vaadin.flow.component.Component
-
onDetach
protected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
- Overrides:
onDetachin classcom.vaadin.flow.component.Component
-
fireUnloadEvent
protected void fireUnloadEvent(UnloadEvent event)
Fires theUnloadEvent.- Parameters:
event- Event to fire.
-
addUnloadListener
public com.vaadin.flow.shared.Registration addUnloadListener(UnloadListener listener)
Adds anUnloadListener.- Parameters:
listener- Listener to add.- Returns:
- A
Registrationthat can be used to stop listening to the event.
-
-