function showToast(options) { var theToast = new Toaster(options) theToast.makeToast() theToast.show() return theToast.$toast } class Toaster { constructor(options) { this.options = Object.assign({}, Toaster.defaultOptions, options) this.bsToastOptions = { autohide: this.options.autohide, delay: this.options.delay, } } static defaultOptions = { title: false, muted: false, body: false, variant: 'default', autohide: true, delay: 5000, titleHtml: false, mutedHtml: false, bodyHtml: false, closeButton: true, } makeToast() { if (this.isValid()) { this.$toast = Toaster.buildToast(this.options) $('#mainToastContainer').append(this.$toast) } } show() { if (this.isValid()) { var that = this this.$toast.toast(this.bsToastOptions) .toast('show') .on('hidden.bs.toast', function () { that.removeToast() }) } } removeToast() { this.$toast.remove(); } isValid() { return this.options.title !== false || this.options.muted !== false || this.options.body !== false } static buildToast(options) { var $toast = $('
') $toast.addClass('toast-' + options.variant) if (options.title !== false || options.muted !== false) { var $toastHeader = $('') $toastHeader.addClass('toast-' + options.variant) if (options.title !== false) { var $toastHeaderText if (options.titleHtml) { $toastHeaderText = $('').html(options.title); } else { $toastHeaderText = $('').text(options.title) } $toastHeader.append($toastHeaderText) } if (options.muted !== false) { var $toastHeaderMuted if (options.mutedHtml) { $toastHeaderMuted = $('').html(options.muted) } else { $toastHeaderMuted = $('').text(options.muted) } $toastHeader.append($toastHeaderMuted) } if (options.closeButton) { var $closeButton = $('') $toastHeader.append($closeButton) } $toast.append($toastHeader) } if (options.body !== false) { var $toastBody if (options.bodyHtml) { $toastBody = $('').html(options.body) } else { $toastBody = $('').text(options.body) } $toast.append($toastBody) } return $toast } }