|
@@ -1,8 +1,11 @@
|
|
-from typing import Any, Literal, Optional, Union
|
|
|
|
|
|
+import asyncio
|
|
|
|
+from typing import Any, Callable, Literal, Optional, Union
|
|
|
|
+
|
|
|
|
+from typing_extensions import Self
|
|
|
|
|
|
from ..context import context
|
|
from ..context import context
|
|
from ..element import Element
|
|
from ..element import Element
|
|
-from .timer import Timer
|
|
|
|
|
|
+from ..events import UiEventArguments, handle_event
|
|
|
|
|
|
NotificationPosition = Literal[
|
|
NotificationPosition = Literal[
|
|
'top-left',
|
|
'top-left',
|
|
@@ -37,6 +40,7 @@ class Notification(Element, component='notification.js'):
|
|
icon: Optional[str] = None,
|
|
icon: Optional[str] = None,
|
|
spinner: bool = False,
|
|
spinner: bool = False,
|
|
timeout: Optional[float] = 5.0,
|
|
timeout: Optional[float] = 5.0,
|
|
|
|
+ on_dismiss: Optional[Callable] = None,
|
|
**kwargs: Any,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
) -> None:
|
|
"""Notification element
|
|
"""Notification element
|
|
@@ -54,6 +58,7 @@ class Notification(Element, component='notification.js'):
|
|
:param icon: optional name of an icon to be displayed in the notification (default: `None`)
|
|
:param icon: optional name of an icon to be displayed in the notification (default: `None`)
|
|
:param spinner: display a spinner in the notification (default: False)
|
|
:param spinner: display a spinner in the notification (default: False)
|
|
:param timeout: optional timeout in seconds after which the notification is dismissed (default: 5.0)
|
|
:param timeout: optional timeout in seconds after which the notification is dismissed (default: 5.0)
|
|
|
|
+ :param on_dismiss: optional callback to be invoked when the notification is dismissed
|
|
|
|
|
|
Note: You can pass additional keyword arguments according to `Quasar's Notify API <https://quasar.dev/quasar-plugins/notify#notify-api>`_.
|
|
Note: You can pass additional keyword arguments according to `Quasar's Notify API <https://quasar.dev/quasar-plugins/notify#notify-api>`_.
|
|
"""
|
|
"""
|
|
@@ -76,17 +81,18 @@ class Notification(Element, component='notification.js'):
|
|
if icon is not None:
|
|
if icon is not None:
|
|
self._props['options']['icon'] = icon
|
|
self._props['options']['icon'] = icon
|
|
self._props['options'].update(kwargs)
|
|
self._props['options'].update(kwargs)
|
|
- with self:
|
|
|
|
- def delete():
|
|
|
|
- self.clear()
|
|
|
|
- self.delete()
|
|
|
|
|
|
|
|
- async def try_delete():
|
|
|
|
- query = f'''!!document.querySelector("[data-id='nicegui-dialog-{self.id}']")'''
|
|
|
|
- if not await self.client.run_javascript(query):
|
|
|
|
- delete()
|
|
|
|
|
|
+ if on_dismiss:
|
|
|
|
+ self.on_dismiss(on_dismiss)
|
|
|
|
|
|
- Timer(1.0, try_delete)
|
|
|
|
|
|
+ async def handle_dismiss() -> None:
|
|
|
|
+ if self.client.is_auto_index_client:
|
|
|
|
+ self.dismiss()
|
|
|
|
+ await asyncio.sleep(1.0) # NOTE: sent dismiss message to all browsers before deleting the element
|
|
|
|
+ if not self._deleted:
|
|
|
|
+ self.clear()
|
|
|
|
+ self.delete()
|
|
|
|
+ self.on('dismiss', handle_dismiss)
|
|
|
|
|
|
@property
|
|
@property
|
|
def message(self) -> str:
|
|
def message(self) -> str:
|
|
@@ -177,6 +183,11 @@ class Notification(Element, component='notification.js'):
|
|
self._props['options']['closeBtn'] = value
|
|
self._props['options']['closeBtn'] = value
|
|
self.update()
|
|
self.update()
|
|
|
|
|
|
|
|
+ def on_dismiss(self, callback: Callable[..., Any]) -> Self:
|
|
|
|
+ """Add a callback to be invoked when the notification is dismissed."""
|
|
|
|
+ self.on('dismiss', lambda _: handle_event(callback, UiEventArguments(sender=self, client=self.client)), [])
|
|
|
|
+ return self
|
|
|
|
+
|
|
def dismiss(self) -> None:
|
|
def dismiss(self) -> None:
|
|
"""Dismiss the notification."""
|
|
"""Dismiss the notification."""
|
|
self.run_method('dismiss')
|
|
self.run_method('dismiss')
|