@@ -48,6 +48,7 @@ function UINotification(options){
48
48
}
49
49
} )
50
50
51
+ // Notification Clicked
51
52
$ ( el_notification ) . on ( 'click' , function ( e ) {
52
53
// close button clicked
53
54
if ( $ ( e . target ) . hasClass ( 'notification-close' ) ) {
@@ -58,8 +59,55 @@ function UINotification(options){
58
59
if ( options . click && typeof options . click === 'function' ) {
59
60
options . click ( options . value ) ;
60
61
}
62
+
63
+ // close notification
64
+ close_notification ( el_notification ) ;
61
65
} )
62
66
67
+ // Close Button Clicked
68
+ $ ( el_notification ) . find ( '.notification-close' ) . on ( 'click' , function ( e , data ) {
69
+ let closingMultiple = false ;
70
+ if ( data ?. closingAll )
71
+ closingMultiple = true ;
72
+
73
+ close_notification ( el_notification , closingMultiple ) ;
74
+ e . stopPropagation ( ) ;
75
+ e . preventDefault ( ) ;
76
+ return false ;
77
+ } ) ;
78
+
79
+ const close_notification = function ( el_notification , closingMultiple = false ) {
80
+ // hide notification wrapper by animating height and opacity
81
+ // only if closing one notification and there are multiple notifications
82
+ // otherwise the animation is not needed
83
+ if ( ! closingMultiple && $ ( '.notification' ) . length > 1 ) {
84
+ $ ( el_notification ) . closest ( '.notification-wrapper' ) . animate ( {
85
+ height : 0 ,
86
+ opacity : 0
87
+ } , 300 ) ;
88
+ }
89
+
90
+ // hide notification by fading out to the right
91
+ $ ( el_notification ) . addClass ( 'animate__fadeOutRight' ) ;
92
+
93
+ // close callback
94
+ if ( options . close && typeof options . close === 'function' ) {
95
+ options . close ( options . value ) ;
96
+ }
97
+
98
+ // remove notification and wrapper after animation
99
+ setTimeout ( function ( ) {
100
+ $ ( el_notification ) . closest ( '.notification-wrapper' ) . remove ( ) ;
101
+ $ ( el_notification ) . remove ( ) ;
102
+ // count notifications
103
+ let count = $ ( '.notification-container' ) . find ( '.notification-wrapper' ) . length ;
104
+ if ( count <= 1 ) {
105
+ $ ( '.notification-container' ) . removeClass ( 'has-multiple' ) ;
106
+ } else {
107
+ $ ( '.notification-container' ) . addClass ( 'has-multiple' ) ;
108
+ }
109
+ } , 500 ) ;
110
+ }
63
111
// Show Notification
64
112
$ ( el_notification ) . delay ( 100 ) . show ( 0 ) ;
65
113
@@ -74,52 +122,19 @@ function UINotification(options){
74
122
return el_notification ;
75
123
}
76
124
77
- $ ( document ) . on ( 'click' , '.notification' , function ( e ) {
78
- if ( $ ( e . target ) . hasClass ( 'notification' ) ) {
79
- window . close_notification ( e . target ) ;
80
- } else {
81
- window . close_notification ( $ ( e . target ) . closest ( '.notification' ) ) ;
82
- }
83
- } ) ;
84
-
85
- $ ( document ) . on ( 'click' , '.notification-close' , function ( e ) {
86
- window . close_notification ( $ ( e . target ) . closest ( '.notification' ) ) ;
87
- e . stopPropagation ( ) ;
88
- e . preventDefault ( ) ;
89
- return false ;
90
- } ) ;
91
-
92
125
$ ( document ) . on ( 'click' , '.notifications-close-all' , function ( e ) {
93
- $ ( '.notification-container' ) . find ( '.notification-wrapper' ) . each ( function ( ) {
94
- window . close_notification ( this ) ;
95
- } ) ;
126
+ // close all notifications
127
+ $ ( '.notification-container' ) . find ( '.notification-close' ) . trigger ( 'click' , { closingAll : true } ) ;
128
+ // hide 'Close all' button
96
129
$ ( '.notifications-close-all' ) . animate ( {
97
130
opacity : 0
98
131
} , 300 ) ;
132
+ // remove the 'has-multiple' class
99
133
$ ( '.notification-container' ) . removeClass ( 'has-multiple' ) ;
134
+ // prevent default
100
135
e . stopPropagation ( ) ;
101
136
e . preventDefault ( ) ;
102
137
return false ;
103
138
} )
104
- window . close_notification = function ( el_notification ) {
105
- $ ( el_notification ) . closest ( '.notification-wrapper' ) . animate ( {
106
- height : 0 ,
107
- opacity : 0
108
- } , 300 ) ;
109
- $ ( el_notification ) . addClass ( 'animate__fadeOutRight' ) ;
110
-
111
- // remove notification
112
- setTimeout ( function ( ) {
113
- $ ( el_notification ) . closest ( '.notification-wrapper' ) . remove ( ) ;
114
- $ ( el_notification ) . remove ( ) ;
115
- // count notifications
116
- let count = $ ( '.notification-container' ) . find ( '.notification-wrapper' ) . length ;
117
- if ( count <= 1 ) {
118
- $ ( '.notification-container' ) . removeClass ( 'has-multiple' ) ;
119
- } else {
120
- $ ( '.notification-container' ) . addClass ( 'has-multiple' ) ;
121
- }
122
- } , 500 ) ;
123
- }
124
139
125
140
export default UINotification ;
0 commit comments