1/** 2 * Appends BS alert configurable message to the DOM and removes it after a certain amount of time (5000 milliseconds by default) 3 * if it isn't configured as dismissible. 4 * @param message Can either be a string or an object that contains a set of the following options: 5 <ul> 6 <li>text - alert content text (can be HTML); default: 'Done.'</li> 7 <li>type - alert type, one of [success, info, warning, danger]; default: danger</li> 8 <li>dismissible - defines if alert is dismissable; defaults: false; overrides fade out behaviour by timeout</li> 9 <li>delay - an amount of milliseconds when alert starts to fade out; default: 5000</li> 10 <li>appendTo - selector where an alert will be appended; default: alert is prepended to .container or .container-fluid</li> 11 </ul> 12 */ 13function showToast( message ) 14{ 15 const messageOptions = constructMessageOptions( message ); 16 const $messageToast = constructMessageToast( messageOptions ); 17 18 if ( messageOptions.appendTo ) 19 { 20 $( messageOptions.appendTo ).first().append( $messageToast ); 21 } 22 else 23 { 24 $( '.container, .container-fluid' ).first().prepend( $messageToast ); 25 } 26 27 if ( !messageOptions.dismissible ) 28 { 29 setTimeout( function () { 30 $messageToast.fadeOut( "slow", function () { 31 $( this ).remove(); 32 } ); 33 }, messageOptions.delay ); 34 } 35 36 function constructMessageOptions( message ) 37 { 38 let messageOptions = { 39 text: 'Done.', 40 type: 'danger', 41 dismissible: false, 42 delay: 5000, 43 appendTo: undefined 44 }; 45 46 if ( typeof message === "string" ) 47 { 48 return Object.assign( messageOptions, {text: message} ); 49 } 50 else if ( typeof message === "object" ) 51 { 52 return Object.assign( messageOptions, message ); 53 } 54 else 55 { 56 throw new Error( "Unsupported type: " + message ); 57 } 58 } 59 60 function constructMessageToast( messageOptions ) 61 { 62 const $messageToast = $( '<div class="alert" role="alert"></div>' ); 63 64 switch ( messageOptions.type ) 65 { 66 case 'success': 67 $messageToast.addClass( 'alert-success' ); 68 break; 69 case 'info': 70 $messageToast.addClass( 'alert-info' ); 71 break; 72 case 'warning': 73 $messageToast.addClass( 'alert-warning' ); 74 break; 75 case 'danger': 76 default: 77 $messageToast.addClass( 'alert-danger' ); 78 break; 79 } 80 81 if ( messageOptions.dismissible ) 82 { 83 $messageToast.addClass( 'alert-dismissible' ); 84 $( '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>' ).prependTo( $messageToast ); 85 } 86 87 $messageToast.prepend( messageOptions.text ); 88 89 return $messageToast; 90 } 91 92}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.