@JsModule(value="./unload-observer.js") @Tag(value="unload-observer") public class UnloadObserver extends com.vaadin.flow.component.Component implements WithIdMixin<UnloadObserver>
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.| Modifier and Type | Method and Description |
|---|---|
com.vaadin.flow.shared.Registration |
addUnloadListener(UnloadListener listener)
Adds an
UnloadListener. |
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> |
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. |
addListener, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onEnabledStateChanged, set, setElement, setId, setVisibleclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitwithIdpublic static UnloadObserver get()
UnloadObserver.IllegalStateException - if there is no current UI.public static UnloadObserver get(com.vaadin.flow.component.UI ui)
ui - A UI to register the instance of UnloadObserver in. Must not be null.UnloadObserver.public static UnloadObserver getAttached()
getAttached(Component) with UI.getCurrent().UnloadObserver.public static <C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasComponents> UnloadObserver getAttached(C parent)
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.C - Generic type to ensure the parent can have other components added to it.parent - A Component to add the UnloadObserver to. Must not be null.UnloadObserver.IllegalStateException - when an instance of UnloadObserver is attached to a component it cannot be removed fromIllegalArgumentException - when the Component.getUI() for parent is not presentpublic void setQueryingOnUnload(boolean queryingOnUnload)
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.public boolean isQueryingOnUnload()
true when unloading the document from browser window results in showing a browser-native confirmation dialog and notifying UnloadListeners; false otherwise.public UnloadObserver withQueryingOnUnload(boolean value)
setQueryingOnUnload(boolean) and returns itself.value - Whether or not to query on document unload.setQueryingOnUnload(boolean)public UnloadObserver withQueryingOnUnload()
withQueryingOnUnload(true).withQueryingOnUnload(boolean)public UnloadObserver withoutQueryingOnUnload()
withQueryingOnUnload(false).withQueryingOnUnload(boolean)protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
onAttach in class com.vaadin.flow.component.Componentprotected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
onDetach in class com.vaadin.flow.component.Componentprotected void fireUnloadEvent(UnloadEvent event)
UnloadEvent.event - Event to fire.public com.vaadin.flow.shared.Registration addUnloadListener(UnloadListener listener)
UnloadListener.listener - Listener to add.Registration that can be used to stop listening to the event.Copyright © 2023 Miki. All rights reserved.