@@ -69,12 +69,14 @@ $.fn.toast = function(parameters) {
69
69
element = this ,
70
70
instance = isToastComponent ? $module . data ( moduleNamespace ) : undefined ,
71
71
72
+ id ,
72
73
module
73
74
;
74
75
module = {
75
76
76
77
initialize : function ( ) {
77
78
module . verbose ( 'Initializing element' ) ;
79
+ module . create . id ( ) ;
78
80
if ( ! module . has . container ( ) ) {
79
81
module . create . container ( ) ;
80
82
}
@@ -124,17 +126,22 @@ $.fn.toast = function(parameters) {
124
126
} ,
125
127
126
128
show : function ( callback ) {
127
- callback = callback || function ( ) { } ;
128
- module . debug ( 'Showing toast' ) ;
129
129
if ( settings . onShow . call ( $toastBox , element ) === false ) {
130
130
module . debug ( 'onShow callback returned false, cancelling toast animation' ) ;
131
131
return ;
132
132
}
133
+ callback = callback || function ( ) { } ;
134
+ module . debug ( 'Showing toast' ) ;
133
135
module . animate . show ( callback ) ;
134
136
} ,
135
137
136
138
close : function ( callback ) {
139
+ if ( settings . onHide . call ( $toastBox , element ) === false ) {
140
+ module . debug ( 'onHide callback returned false, cancelling toast animation' ) ;
141
+ return ;
142
+ }
137
143
callback = callback || function ( ) { } ;
144
+ module . debug ( 'Closing toast' ) ;
138
145
module . remove . visible ( ) ;
139
146
module . unbind . events ( ) ;
140
147
module . animate . close ( callback ) ;
@@ -146,12 +153,16 @@ $.fn.toast = function(parameters) {
146
153
module . verbose ( 'Creating container' ) ;
147
154
$context . append ( $ ( '<div/>' , { class : settings . position + ' ' + className . container + ' ' + ( settings . horizontal ? className . horizontal : '' ) } ) ) ;
148
155
} ,
156
+ id : function ( ) {
157
+ id = ( Math . random ( ) . toString ( 16 ) + '000000000' ) . substr ( 2 , 8 ) ;
158
+ module . verbose ( 'Creating unique id for element' , id ) ;
159
+ } ,
149
160
toast : function ( ) {
150
161
$toastBox = $ ( '<div/>' , { class : className . box } ) ;
151
162
var iconClass = module . get . iconClass ( ) ;
152
163
if ( ! isToastComponent ) {
153
164
module . verbose ( 'Creating toast' ) ;
154
- $toast = $ ( '<div/>' ) ;
165
+ $toast = $ ( '<div/>' , { role : 'alert' } ) ;
155
166
var $content = $ ( '<div/>' , { class : className . content } ) ;
156
167
if ( iconClass !== '' ) {
157
168
$toast . append ( $ ( '<i/>' , { class : iconClass + ' ' + className . icon } ) ) ;
@@ -164,21 +175,29 @@ $.fn.toast = function(parameters) {
164
175
} ) ) ;
165
176
}
166
177
if ( settings . title !== '' ) {
178
+ var titleId = '_' + module . get . id ( ) + 'title' ;
179
+ $toast . attr ( 'aria-labelledby' , titleId ) ;
167
180
$content . append ( $ ( '<div/>' , {
168
181
class : className . title ,
169
- text : settings . title
182
+ id : titleId ,
183
+ html : module . helpers . escape ( settings . title , settings . preserveHTML )
170
184
} ) ) ;
171
185
}
172
-
173
- $content . append ( $ ( '<div/>' , { class : className . message , html : module . helpers . escape ( settings . message , settings . preserveHTML ) } ) ) ;
186
+ var descId = '_' + module . get . id ( ) + 'desc' ;
187
+ $toast . attr ( 'aria-describedby' , descId ) ;
188
+ $content . append ( $ ( '<div/>' , {
189
+ class : className . message ,
190
+ id : descId ,
191
+ html : module . helpers . escape ( settings . message , settings . preserveHTML )
192
+ } ) ) ;
174
193
175
194
$toast
176
195
. addClass ( settings . class + ' ' + className . toast )
177
196
. append ( $content )
178
197
;
179
198
$toast . css ( 'opacity' , settings . opacity ) ;
180
199
if ( settings . closeIcon ) {
181
- $close = $ ( '<i/>' , { class : className . close + ' ' + ( typeof settings . closeIcon === 'string' ? settings . closeIcon : '' ) } ) ;
200
+ $close = $ ( '<i/>' , { class : className . close + ' ' + ( typeof settings . closeIcon === 'string' ? settings . closeIcon : '' ) , role : 'button' , tabindex : 0 , 'aria-label' : settings . text . close } ) ;
182
201
if ( $close . hasClass ( className . left ) ) {
183
202
$toast . prepend ( $close ) ;
184
203
} else {
@@ -221,12 +240,13 @@ $.fn.toast = function(parameters) {
221
240
}
222
241
}
223
242
settings . actions . forEach ( function ( el ) {
224
- var icon = el [ fields . icon ] ? '<i class="' + module . helpers . deQuote ( el [ fields . icon ] ) + ' icon"></i>' : '' ,
243
+ var icon = el [ fields . icon ] ? '<i ' + ( el [ fields . text ] ? 'aria-hidden="true"' : '' ) + ' class="' + module . helpers . deQuote ( el [ fields . icon ] ) + ' icon"></i>' : '' ,
225
244
text = module . helpers . escape ( el [ fields . text ] || '' , settings . preserveHTML ) ,
226
245
cls = module . helpers . deQuote ( el [ fields . class ] || '' ) ,
227
246
click = el [ fields . click ] && $ . isFunction ( el [ fields . click ] ) ? el [ fields . click ] : function ( ) { } ;
228
247
$actions . append ( $ ( '<button/>' , {
229
248
html : icon + text ,
249
+ 'aria-label' : $ ( '<div>' + ( el [ fields . text ] || el [ fields . icon ] || '' ) + '</div>' ) . text ( ) ,
230
250
class : className . button + ' ' + cls ,
231
251
click : function ( ) {
232
252
var button = $ ( this ) ;
@@ -330,13 +350,12 @@ $.fn.toast = function(parameters) {
330
350
bind : {
331
351
events : function ( ) {
332
352
module . debug ( 'Binding events to toast' ) ;
333
- if ( settings . closeOnClick || settings . closeIcon ) {
334
- ( settings . closeIcon ? $close : $toast )
335
- . on ( 'click' + eventNamespace , module . event . click )
336
- ;
353
+ if ( settings . closeIcon ) {
354
+ $close . on ( 'click' + eventNamespace , module . event . close ) ;
337
355
}
356
+ $toast . on ( 'click' + eventNamespace , module . event . click ) ;
338
357
if ( $animationObject ) {
339
- $animationObject . on ( 'animationend' + eventNamespace , module . close ) ;
358
+ $animationObject . on ( 'animationend' + eventNamespace , module . event . close ) ;
340
359
}
341
360
$toastBox
342
361
. on ( 'click' + eventNamespace , selector . approve , module . event . approve )
@@ -348,11 +367,10 @@ $.fn.toast = function(parameters) {
348
367
unbind : {
349
368
events : function ( ) {
350
369
module . debug ( 'Unbinding events to toast' ) ;
351
- if ( settings . closeOnClick || settings . closeIcon ) {
352
- ( settings . closeIcon ? $close : $toast )
353
- . off ( 'click' + eventNamespace )
354
- ;
370
+ if ( settings . closeIcon ) {
371
+ $close . off ( 'click' + eventNamespace ) ;
355
372
}
373
+ $toast . off ( 'click' + eventNamespace ) ;
356
374
if ( $animationObject ) {
357
375
$animationObject . off ( 'animationend' + eventNamespace ) ;
358
376
}
@@ -384,11 +402,6 @@ $.fn.toast = function(parameters) {
384
402
} ,
385
403
close : function ( callback ) {
386
404
callback = $ . isFunction ( callback ) ? callback : function ( ) { } ;
387
- module . debug ( 'Closing toast' ) ;
388
- if ( settings . onHide . call ( $toastBox , element ) === false ) {
389
- module . debug ( 'onHide callback returned false, cancelling toast animation' ) ;
390
- return ;
391
- }
392
405
if ( settings . transition && $ . fn . transition !== undefined && $module . transition ( 'is supported' ) ) {
393
406
$toastBox
394
407
. transition ( {
@@ -458,6 +471,9 @@ $.fn.toast = function(parameters) {
458
471
} ,
459
472
460
473
get : {
474
+ id : function ( ) {
475
+ return id ;
476
+ } ,
461
477
container : function ( ) {
462
478
return ( $context . find ( module . helpers . toClass ( settings . position ) + selector . container + ( settings . horizontal ? module . helpers . toClass ( className . horizontal ) : ':not(' + module . helpers . toClass ( className . horizontal ) + ')' ) ) [ 0 ] ) ;
463
479
} ,
@@ -491,9 +507,15 @@ $.fn.toast = function(parameters) {
491
507
} ,
492
508
493
509
event : {
510
+ close : function ( ) {
511
+ module . close ( ) ;
512
+ } ,
494
513
click : function ( event ) {
495
514
if ( $ ( event . target ) . closest ( 'a' ) . length === 0 ) {
496
- settings . onClick . call ( $toastBox , element ) ;
515
+ if ( settings . onClick . call ( $toastBox , element ) === false || ! settings . closeOnClick ) {
516
+ module . verbose ( 'Click callback returned false or close denied by setting cancelling close' ) ;
517
+ return ;
518
+ }
497
519
module . close ( ) ;
498
520
}
499
521
} ,
@@ -834,6 +856,10 @@ $.fn.toast.settings = {
834
856
unclickable : 'unclickable'
835
857
} ,
836
858
859
+ text : {
860
+ close : 'Close'
861
+ } ,
862
+
837
863
icons : {
838
864
info : 'info' ,
839
865
success : 'checkmark' ,
0 commit comments