Class UnloadObserver
java.lang.Object
com.vaadin.flow.component.Component
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.HasStyle,Serializable,HasId,WithIdMixin<UnloadObserver>
@JsModule("./unload-observer.js")
@Tag("unload-observer")
public class UnloadObserver
extends com.vaadin.flow.component.Component
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:
-
Method Summary
Modifier and TypeMethodDescriptioncom.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 UnloadObserverReturns 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.booleanReturns 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.Shortcut forwithQueryingOnUnload(false).Shortcut forwithQueryingOnUnload(true).withQueryingOnUnload(boolean value) ChainssetQueryingOnUnload(boolean)and returns itself.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
-
Method Details
-
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
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
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
ChainssetQueryingOnUnload(boolean)and returns itself.- Parameters:
value- Whether or not to query on document unload.- Returns:
- This.
- See Also:
-
withQueryingOnUnload
Shortcut forwithQueryingOnUnload(true).- Returns:
- This.
- See Also:
-
withoutQueryingOnUnload
Shortcut forwithQueryingOnUnload(false).- Returns:
- This.
- See Also:
-
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
Fires theUnloadEvent.- Parameters:
event- Event to fire.
-
addUnloadListener
Adds anUnloadListener.- Parameters:
listener- Listener to add.- Returns:
- A
Registrationthat can be used to stop listening to the event.
-