2626import org .dominokit .domino .ui .button .RemoveButton ;
2727import org .dominokit .domino .ui .elements .DivElement ;
2828import org .dominokit .domino .ui .elements .SpanElement ;
29+ import org .dominokit .domino .ui .events .CustomEvents ;
2930import org .dominokit .domino .ui .events .EventType ;
3031import org .dominokit .domino .ui .style .*;
3132import org .dominokit .domino .ui .utils .BaseDominoElement ;
4950public class Notification extends BaseDominoElement <HTMLDivElement , Notification >
5051 implements NotificationStyles {
5152
53+ public static final String DUI_NOTIFICATION_CLOSE = "dui-notification-close" ;
5254 private final DivElement element ;
5355 private final DivElement root ;
5456 private final DivElement content ;
@@ -62,7 +64,8 @@ public class Notification extends BaseDominoElement<HTMLDivElement, Notification
6264 private boolean dismissible = true ;
6365 private boolean infinite = false ;
6466 private boolean closed = true ;
65- private final List <CloseHandler > closeHandlers = new ArrayList <>();
67+ private final List <NotificationHandler > closeHandlers = new ArrayList <>();
68+ private final List <NotificationHandler > showHandlers = new ArrayList <>();
6669
6770 /** Constructs a default notification instance. */
6871 public Notification () {
@@ -82,6 +85,9 @@ public Notification() {
8285 element .insertBefore (span ().addCss ("dui-notification-filler" ), closeButton .element ());
8386 });
8487 init (this );
88+ onAttached ((target , mutationRecord ) -> showHandlers .forEach (handler -> handler .apply (this )));
89+ onDetached ((target , mutationRecord ) -> closeHandlers .forEach (handler -> handler .apply (this )));
90+ addEventListener (DUI_NOTIFICATION_CLOSE , evt -> close ());
8591 }
8692
8793 /**
@@ -105,6 +111,19 @@ public static Notification create() {
105111 return new Notification ();
106112 }
107113
114+ /** Close all currently opened notifications */
115+ public static void dismissAll () {
116+ NodeList <Element > notifications =
117+ DomGlobal .document .querySelectorAll ("." + dui_notification_wrapper .getCssClass ());
118+ notifications
119+ .asList ()
120+ .forEach (
121+ notification -> {
122+ CustomEvent <Void > closeEvent = CustomEvents .create (DUI_NOTIFICATION_CLOSE );
123+ notification .dispatchEvent (closeEvent );
124+ });
125+ }
126+
108127 /**
109128 * {@inheritDoc}
110129 *
@@ -305,7 +324,6 @@ public final void close(int after) {
305324 animateClose (
306325 after ,
307326 () -> {
308- closeHandlers .forEach (CloseHandler ::onClose );
309327 this .closed = true ;
310328 });
311329 }
@@ -335,32 +353,67 @@ private void animateClose(int after, Runnable onComplete) {
335353 *
336354 * @return a list of close handlers
337355 */
338- public List <CloseHandler > getCloseHandlers () {
356+ public List <NotificationHandler > getCloseHandlers () {
339357 return closeHandlers ;
340358 }
341359
360+ /**
361+ * Retrieves the list of show handlers associated with the notification.
362+ *
363+ * @return a list of show handlers
364+ */
365+ public List <NotificationHandler > getShowHandlers () {
366+ return showHandlers ;
367+ }
368+
342369 /**
343370 * Adds a close handler to the notification.
344371 *
345- * @param closeHandler the close handler to be added
372+ * @param notificationHandler the close handler to be added
346373 * @return this notification for chaining
347374 */
348- public Notification addCloseHandler (CloseHandler closeHandler ) {
349- if (nonNull (closeHandler )) {
350- closeHandlers .add (closeHandler );
375+ public Notification addCloseHandler (NotificationHandler notificationHandler ) {
376+ if (nonNull (notificationHandler )) {
377+ closeHandlers .add (notificationHandler );
378+ }
379+ return this ;
380+ }
381+
382+ /**
383+ * Adds a show handler to the notification.
384+ *
385+ * @param showHandler the show handler to be added
386+ * @return this notification for chaining
387+ */
388+ public Notification addShowHandler (NotificationHandler showHandler ) {
389+ if (nonNull (showHandler )) {
390+ showHandlers .add (showHandler );
351391 }
352392 return this ;
353393 }
354394
355395 /**
356396 * Removes a specified close handler from the notification.
357397 *
358- * @param closeHandler the close handler to be removed
398+ * @param notificationHandler the close handler to be removed
399+ * @return this notification for chaining
400+ */
401+ public Notification removeCloseHandler (NotificationHandler notificationHandler ) {
402+ if (nonNull (notificationHandler )) {
403+ closeHandlers .remove (notificationHandler );
404+ }
405+ return this ;
406+ }
407+
408+ /**
409+ * Removes a specified show handler from the notification.
410+ *
411+ * @param showHandler the show handler to be removed
359412 * @return this notification for chaining
360413 */
361- public Notification removeCloseHandler ( CloseHandler closeHandler ) {
362- if (nonNull (closeHandler )) {
363- closeHandlers .remove (closeHandler );
414+ public Notification removeShowHandler ( NotificationHandler showHandler ) {
415+ if (nonNull (showHandler )) {
416+ showHandlers .remove (showHandler );
364417 }
365418 return this ;
366419 }
@@ -387,12 +440,13 @@ public HTMLDivElement element() {
387440 }
388441
389442 /**
390- * A functional interface representing handlers that are triggered when a notification is closed.
443+ * A functional interface representing handlers that are triggered when a notification is
444+ * shown/closed.
391445 */
392446 @ FunctionalInterface
393- public interface CloseHandler {
394- /** Triggered when the notification is closed. */
395- void onClose ( );
447+ public interface NotificationHandler {
448+ /** Triggered when the notification is shown/ closed. */
449+ void apply ( Notification notification );
396450 }
397451
398452 /**
0 commit comments