@@ -102,8 +102,6 @@ function padNumber(num) {
102102 return ( "00" + num ) . substr ( - 2 , 2 ) ;
103103}
104104
105- let showAlertBox = null ;
106-
107105// Gets ToastContainer if it exists, otherwise creates a new one and return it
108106function getOrCreateToastContainer ( ) {
109107 const existing =
@@ -127,6 +125,8 @@ function createToast(alertState) {
127125 const toast = document . createElement ( "div" ) ;
128126 toast . className = "toast align-items-center border-0 shadow rounded overflow-hidden" ;
129127 toast . dataset . toastType = alertState . type ;
128+ toast . dataset . bsAutohide = String ( alertState . autohide ) ;
129+ toast . dataset . bsDelay = String ( alertState . delay ) ;
130130 toast . setAttribute ( "role" , alertState . role ) ;
131131 toast . setAttribute ( "aria-live" , alertState . live ) ;
132132 toast . setAttribute ( "aria-atomic" , alertState . ariaAtomic ) ;
@@ -182,7 +182,7 @@ function createToast(alertState) {
182182 return toast ;
183183}
184184
185- function showAlert ( type , icon , title , message , toast ) {
185+ function showAlert ( type , icon , title , message ) {
186186 // sets all properties of the alertState object based on the type of alert and the provided parameters
187187 const alertState = {
188188 title : " <strong>" + escapeHtml ( title ) + "</strong><br>" ,
@@ -249,97 +249,17 @@ function showAlert(type, icon, title, message, toast) {
249249 }
250250
251251 const container = getOrCreateToastContainer ( ) ;
252+ const toastElement = createToast ( alertState ) ;
253+ const toastInstance = new bootstrap . Toast ( toastElement ) ;
252254
253- const toastController = {
254- element : createToast ( alertState ) ,
255- instance : null ,
256- alertState,
257- update ( partialState ) {
258- this . alertState = { ...this . alertState , ...partialState } ;
259- this . render ( ) ;
260- this . show ( ) ;
261- return this ;
262- } ,
263- render ( ) {
264- const renderedToast = createToast ( this . alertState ) ;
265-
266- this . element . className = renderedToast . className ;
267- this . element . dataset . toastType = renderedToast . dataset . toastType ;
268- this . element . setAttribute ( "role" , renderedToast . getAttribute ( "role" ) ) ;
269- this . element . setAttribute ( "aria-live" , renderedToast . getAttribute ( "aria-live" ) ) ;
270- this . element . setAttribute ( "aria-atomic" , renderedToast . getAttribute ( "aria-atomic" ) ) ;
271- this . element . replaceChildren ( ...renderedToast . childNodes ) ;
272- } ,
273- ensureInstance ( ) {
274- if ( this . instance === null ) {
275- this . instance = new bootstrap . Toast ( this . element , {
276- autohide : this . alertState . autohide ,
277- delay : this . alertState . delay ,
278- } ) ;
279- }
280-
281- return this . instance ;
282- } ,
283- show ( ) {
284- if ( ! this . element . isConnected ) {
285- container . prepend ( this . element ) ;
286- }
287-
288- this . ensureInstance ( ) . show ( ) ;
289- return this ;
290- } ,
291- hide ( ) {
292- if ( this . instance !== null ) {
293- this . instance . hide ( ) ;
294- }
295-
296- return this ;
297- } ,
298- dispose ( ) {
299- if ( this . instance !== null ) {
300- this . instance . dispose ( ) ;
301- this . instance = null ;
302- }
303-
304- this . element . remove ( ) ;
305- if ( showAlertBox === this ) {
306- showAlertBox = null ;
307- }
308-
309- return this ;
310- } ,
311- } ;
312-
313- toastController . element . addEventListener ( "hidden.bs.toast" , ( ) => {
314- toastController . element . remove ( ) ;
315- if ( showAlertBox === toastController ) {
316- showAlertBox = null ;
317- }
255+ toastElement . addEventListener ( "hidden.bs.toast" , ( ) => {
256+ toastElement . remove ( ) ;
257+ toastInstance . dispose ( ) ;
318258 } ) ;
319259
320- if ( toast === null ) {
321- // Always create a new toast
322- return toastController . show ( ) ;
323- }
324-
325- if ( toast !== undefined ) {
326- // Update existing toast
327- return toast . update ( alertState ) ;
328- }
329-
330- if ( type === "info" ) {
331- // Create a new notification for info boxes
332- showAlertBox = toastController ;
333- return toastController . show ( ) ;
334- }
335-
336- if ( showAlertBox !== null ) {
337- // Update existing notification for other boxes (if available)
338- return showAlertBox . update ( alertState ) ;
339- }
340-
341- // Create a new notification for other boxes if no previous info box exists
342- return toastController . show ( ) ;
260+ container . prepend ( toastElement ) ;
261+ toastInstance . show ( ) ;
262+ return toastInstance ;
343263}
344264
345265function datetime ( date , html , humanReadable ) {
0 commit comments