/** * @author Script47 (https://github.com/Script47/Toast) * @description Toast - A Bootstrap 4.2+ jQuery plugin for the toast component * @version 0.7.0 **/ (function ($) { var TOAST_CONTAINER_HTML = '
'; var TOAST_WRAPPER_HTML = '
'; $.toast = function (opts) { if (!$('#toast-container').length) { $('body').prepend(TOAST_CONTAINER_HTML); $('#toast-container').append(TOAST_WRAPPER_HTML); $('body').on('hidden.bs.toast', '.toast', function () { $(this).remove(); }); } var id = 'toast-' + ($('.toast').length + 1), html = '', bg_header_class = '', fg_header_class = '', fg_subtitle_class = 'text-muted', fg_dismiss_class = '', title = opts.title || 'Notice!', subtitle = opts.subtitle || '', content = opts.content || '', type = opts.type || 'info', delay = opts.delay || -1, img = opts.img, pause_on_hover = opts.pause_on_hover || false, pause = false, delay_or_autohide = ''; switch (type) { case 'info': bg_header_class = 'bg-info'; fg_header_class = 'text-white'; fg_subtitle_class = 'text-white'; fg_dismiss_class = 'text-white'; break; case 'success': bg_header_class = 'bg-success'; fg_header_class = 'text-white'; fg_subtitle_class = 'text-white'; fg_dismiss_class = 'text-white'; break; case 'warning': case 'warn': bg_header_class = 'bg-warning'; fg_header_class = 'text-white'; fg_subtitle_class = 'text-white'; fg_dismiss_class = 'text-white'; break; case 'error': case 'danger': bg_header_class = 'bg-danger'; fg_header_class = 'text-white'; fg_subtitle_class = 'text-white'; fg_dismiss_class = 'text-white'; break; } if (pause_on_hover !== false) { var hide_timestamp = Math.floor(Date.now() / 1000) + (delay / 1000); delay_or_autohide = 'data-autohide="false"'; pause_on_hover = 'data-hide-timestamp="' + hide_timestamp + '"'; } else { if (delay === -1) { delay_or_autohide = 'data-autohide="false"'; } else { delay_or_autohide = 'data-delay="' + delay + '"'; } } html = ''; $('#toast-wrapper').append(html); $('#toast-wrapper .toast:last').toast('show'); if (pause_on_hover !== false) { setTimeout(function () { if (!pause) { $('#' + id).toast('hide'); } }, delay); $(document).on('mouseover', '#' + id, function () { pause = true; }); $(document).on('mouseleave', '#' + id, function () { var current = Math.floor(Date.now() / 1000), future = parseInt($(this).data('hide-timestamp')); pause = false; if (current >= future) { $(this).toast('hide'); } }); } } }(jQuery));