AdminLTE/build/js/Toasts.js

210 lines
5.4 KiB
JavaScript
Raw Normal View History

/**
* --------------------------------------------
* AdminLTE Toasts.js
* License MIT
* --------------------------------------------
*/
2020-06-02 15:23:22 +02:00
import $ from 'jquery'
2020-06-02 15:23:22 +02:00
/**
* Constants
* ====================================================
*/
2020-06-02 15:23:22 +02:00
const NAME = 'Toasts'
const DATA_KEY = 'lte.toasts'
const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const EVENT_INIT = `init${EVENT_KEY}`
const EVENT_CREATED = `created${EVENT_KEY}`
const EVENT_REMOVED = `removed${EVENT_KEY}`
2020-06-02 15:23:22 +02:00
const SELECTOR_CONTAINER_TOP_RIGHT = '#toastsContainerTopRight'
const SELECTOR_CONTAINER_TOP_LEFT = '#toastsContainerTopLeft'
const SELECTOR_CONTAINER_BOTTOM_RIGHT = '#toastsContainerBottomRight'
const SELECTOR_CONTAINER_BOTTOM_LEFT = '#toastsContainerBottomLeft'
2020-06-02 15:23:22 +02:00
const CLASS_NAME_TOP_RIGHT = 'toasts-top-right'
const CLASS_NAME_TOP_LEFT = 'toasts-top-left'
const CLASS_NAME_BOTTOM_RIGHT = 'toasts-bottom-right'
const CLASS_NAME_BOTTOM_LEFT = 'toasts-bottom-left'
2020-06-02 15:23:22 +02:00
const POSITION_TOP_RIGHT = 'topRight'
const POSITION_TOP_LEFT = 'topLeft'
const POSITION_BOTTOM_RIGHT = 'bottomRight'
const POSITION_BOTTOM_LEFT = 'bottomLeft'
2020-06-02 15:23:22 +02:00
const Default = {
position: POSITION_TOP_RIGHT,
2020-06-02 15:23:22 +02:00
fixed: true,
autohide: false,
autoremove: true,
delay: 1000,
fade: true,
icon: null,
image: null,
imageAlt: null,
imageHeight: '25px',
title: null,
subtitle: null,
close: true,
body: null,
class: null
}
2020-06-02 15:23:22 +02:00
/**
* Class Definition
* ====================================================
*/
class Toasts {
constructor(element, config) {
this._config = config
this._prepareContainer()
$('body').trigger($.Event(EVENT_INIT))
}
2020-06-02 15:23:22 +02:00
// Public
create() {
const toast = $('<div class="toast" role="alert" aria-live="assertive" aria-atomic="true"/>')
2020-06-02 15:23:22 +02:00
toast.data('autohide', this._config.autohide)
toast.data('animation', this._config.fade)
if (this._config.class) {
toast.addClass(this._config.class)
}
2020-06-02 15:23:22 +02:00
if (this._config.delay && this._config.delay != 500) {
toast.data('delay', this._config.delay)
}
2020-06-02 15:23:22 +02:00
const toastHeader = $('<div class="toast-header">')
2020-06-02 15:23:22 +02:00
if (this._config.image != null) {
const toastImage = $('<img />').addClass('rounded mr-2').attr('src', this._config.image).attr('alt', this._config.imageAlt)
2020-05-30 15:44:20 +02:00
2020-06-02 15:23:22 +02:00
if (this._config.imageHeight != null) {
toastImage.height(this._config.imageHeight).width('auto')
}
2020-06-02 15:23:22 +02:00
toastHeader.append(toastImage)
}
2020-06-02 15:23:22 +02:00
if (this._config.icon != null) {
toastHeader.append($('<i />').addClass('mr-2').addClass(this._config.icon))
}
2020-06-02 15:23:22 +02:00
if (this._config.title != null) {
toastHeader.append($('<strong />').addClass('mr-auto').html(this._config.title))
}
2020-05-30 15:44:20 +02:00
2020-06-02 15:23:22 +02:00
if (this._config.subtitle != null) {
toastHeader.append($('<small />').html(this._config.subtitle))
}
2020-06-02 15:23:22 +02:00
if (this._config.close == true) {
const toastClose = $('<button data-dismiss="toast" />').attr('type', 'button').addClass('ml-2 mb-1 close').attr('aria-label', 'Close').append('<span aria-hidden="true">&times;</span>')
2020-06-02 15:23:22 +02:00
if (this._config.title == null) {
toastClose.toggleClass('ml-2 ml-auto')
}
2020-06-02 15:23:22 +02:00
toastHeader.append(toastClose)
}
2020-06-02 15:23:22 +02:00
toast.append(toastHeader)
2020-06-02 15:23:22 +02:00
if (this._config.body != null) {
toast.append($('<div class="toast-body" />').html(this._config.body))
}
2020-05-30 15:44:20 +02:00
2020-06-02 15:23:22 +02:00
$(this._getContainerId()).prepend(toast)
2020-05-30 15:44:20 +02:00
2020-06-04 20:06:38 +02:00
const $body = $('body')
$body.trigger($.Event(EVENT_CREATED))
2020-06-02 15:23:22 +02:00
toast.toast('show')
2020-06-02 15:23:22 +02:00
if (this._config.autoremove) {
toast.on('hidden.bs.toast', function () {
$(this).delay(200).remove()
$body.trigger($.Event(EVENT_REMOVED))
2020-06-02 15:23:22 +02:00
})
}
}
2020-06-02 15:23:22 +02:00
// Static
2020-06-02 15:23:22 +02:00
_getContainerId() {
if (this._config.position == POSITION_TOP_RIGHT) {
return SELECTOR_CONTAINER_TOP_RIGHT
2020-06-02 15:23:22 +02:00
}
if (this._config.position == POSITION_TOP_LEFT) {
return SELECTOR_CONTAINER_TOP_LEFT
2020-06-02 15:23:22 +02:00
}
if (this._config.position == POSITION_BOTTOM_RIGHT) {
return SELECTOR_CONTAINER_BOTTOM_RIGHT
}
if (this._config.position == POSITION_BOTTOM_LEFT) {
return SELECTOR_CONTAINER_BOTTOM_LEFT
2020-06-02 15:23:22 +02:00
}
}
2020-06-02 15:23:22 +02:00
_prepareContainer() {
if ($(this._getContainerId()).length === 0) {
const container = $('<div />').attr('id', this._getContainerId().replace('#', ''))
if (this._config.position == POSITION_TOP_RIGHT) {
container.addClass(CLASS_NAME_TOP_RIGHT)
} else if (this._config.position == POSITION_TOP_LEFT) {
container.addClass(CLASS_NAME_TOP_LEFT)
} else if (this._config.position == POSITION_BOTTOM_RIGHT) {
container.addClass(CLASS_NAME_BOTTOM_RIGHT)
} else if (this._config.position == POSITION_BOTTOM_LEFT) {
container.addClass(CLASS_NAME_BOTTOM_LEFT)
2020-05-30 15:44:20 +02:00
}
2020-06-02 15:23:22 +02:00
$('body').append(container)
}
2020-06-02 15:23:22 +02:00
if (this._config.fixed) {
$(this._getContainerId()).addClass('fixed')
} else {
$(this._getContainerId()).removeClass('fixed')
}
2020-06-02 15:23:22 +02:00
}
2020-06-02 15:23:22 +02:00
// Static
2020-06-02 15:23:22 +02:00
static _jQueryInterface(option, config) {
return this.each(function () {
const _options = $.extend({}, Default, config)
const toast = new Toasts($(this), _options)
2020-06-02 15:23:22 +02:00
if (option === 'create') {
toast[option]()
}
})
}
2020-06-02 15:23:22 +02:00
}
2020-06-02 15:23:22 +02:00
/**
* jQuery API
* ====================================================
*/
2020-06-02 15:23:22 +02:00
$.fn[NAME] = Toasts._jQueryInterface
$.fn[NAME].Constructor = Toasts
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Toasts._jQueryInterface
}
export default Toasts