2018-08-23 18:31:25 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2020-05-13 21:36:00 +02:00
|
|
|
* Bootstrap (v5.0.0-alpha1): toast.js
|
2018-08-23 18:31:25 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
import {
|
2019-08-02 15:51:05 +02:00
|
|
|
getjQuery,
|
2019-02-22 23:37:55 +01:00
|
|
|
TRANSITION_END,
|
|
|
|
emulateTransitionEnd,
|
|
|
|
getTransitionDurationFromElement,
|
2019-07-23 16:21:23 +02:00
|
|
|
reflow,
|
2019-02-22 23:37:55 +01:00
|
|
|
typeCheckConfig
|
2019-10-02 11:43:54 +02:00
|
|
|
} from './util/index'
|
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/event-handler'
|
|
|
|
import Manipulator from './dom/manipulator'
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2019-02-26 12:20:34 +01:00
|
|
|
const NAME = 'toast'
|
2020-05-13 21:36:00 +02:00
|
|
|
const VERSION = '5.0.0-alpha1'
|
2019-02-26 12:20:34 +01:00
|
|
|
const DATA_KEY = 'bs.toast'
|
2018-11-14 12:02:18 +01:00
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
|
|
|
|
const EVENT_HIDE = `hide${EVENT_KEY}`
|
|
|
|
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
|
|
|
const EVENT_SHOW = `show${EVENT_KEY}`
|
|
|
|
const EVENT_SHOWN = `shown${EVENT_KEY}`
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
const CLASS_NAME_FADE = 'fade'
|
|
|
|
const CLASS_NAME_HIDE = 'hide'
|
|
|
|
const CLASS_NAME_SHOW = 'show'
|
|
|
|
const CLASS_NAME_SHOWING = 'showing'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
|
|
|
const DefaultType = {
|
2019-02-26 12:20:34 +01:00
|
|
|
animation: 'boolean',
|
|
|
|
autohide: 'boolean',
|
|
|
|
delay: 'number'
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const Default = {
|
2019-02-26 12:20:34 +01:00
|
|
|
animation: true,
|
|
|
|
autohide: true,
|
|
|
|
delay: 500
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
const SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]'
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
class Toast {
|
|
|
|
constructor(element, config) {
|
|
|
|
this._element = element
|
2019-02-26 12:20:34 +01:00
|
|
|
this._config = this._getConfig(config)
|
2018-11-14 10:16:56 +01:00
|
|
|
this._timeout = null
|
|
|
|
this._setListeners()
|
2018-11-14 12:02:18 +01:00
|
|
|
Data.setData(element, DATA_KEY, this)
|
2018-08-31 09:18:28 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
// Getters
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
static get VERSION() {
|
|
|
|
return VERSION
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
static get DefaultType() {
|
|
|
|
return DefaultType
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2019-02-06 14:19:04 +01:00
|
|
|
static get Default() {
|
|
|
|
return Default
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
// Public
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
show() {
|
2020-03-07 10:31:42 +01:00
|
|
|
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW)
|
2019-05-16 11:57:05 +02:00
|
|
|
|
|
|
|
if (showEvent.defaultPrevented) {
|
|
|
|
return
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
if (this._config.animation) {
|
2020-03-07 10:31:42 +01:00
|
|
|
this._element.classList.add(CLASS_NAME_FADE)
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
const complete = () => {
|
2020-03-07 10:31:42 +01:00
|
|
|
this._element.classList.remove(CLASS_NAME_SHOWING)
|
|
|
|
this._element.classList.add(CLASS_NAME_SHOW)
|
2018-12-06 13:53:01 +01:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
EventHandler.trigger(this._element, EVENT_SHOWN)
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
if (this._config.autohide) {
|
2019-04-09 12:18:17 +02:00
|
|
|
this._timeout = setTimeout(() => {
|
|
|
|
this.hide()
|
|
|
|
}, this._config.delay)
|
2018-08-23 18:31:25 +02:00
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
this._element.classList.remove(CLASS_NAME_HIDE)
|
2019-07-23 16:21:23 +02:00
|
|
|
reflow(this._element)
|
2020-03-07 10:31:42 +01:00
|
|
|
this._element.classList.add(CLASS_NAME_SHOWING)
|
2018-11-14 10:16:56 +01:00
|
|
|
if (this._config.animation) {
|
2019-02-22 23:37:55 +01:00
|
|
|
const transitionDuration = getTransitionDurationFromElement(this._element)
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
EventHandler.one(this._element, TRANSITION_END, complete)
|
|
|
|
emulateTransitionEnd(this._element, transitionDuration)
|
2018-11-14 10:16:56 +01:00
|
|
|
} else {
|
|
|
|
complete()
|
2018-08-23 18:31:25 +02:00
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2019-04-09 12:18:17 +02:00
|
|
|
hide() {
|
2020-03-07 10:31:42 +01:00
|
|
|
if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
|
2018-11-14 10:16:56 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE)
|
2019-05-16 11:57:05 +02:00
|
|
|
|
|
|
|
if (hideEvent.defaultPrevented) {
|
|
|
|
return
|
|
|
|
}
|
2019-04-09 13:12:05 +02:00
|
|
|
|
|
|
|
const complete = () => {
|
2020-03-07 10:31:42 +01:00
|
|
|
this._element.classList.add(CLASS_NAME_HIDE)
|
|
|
|
EventHandler.trigger(this._element, EVENT_HIDDEN)
|
2019-04-09 13:12:05 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
this._element.classList.remove(CLASS_NAME_SHOW)
|
2019-04-09 13:12:05 +02:00
|
|
|
if (this._config.animation) {
|
|
|
|
const transitionDuration = getTransitionDurationFromElement(this._element)
|
|
|
|
|
|
|
|
EventHandler.one(this._element, TRANSITION_END, complete)
|
|
|
|
emulateTransitionEnd(this._element, transitionDuration)
|
|
|
|
} else {
|
|
|
|
complete()
|
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
dispose() {
|
|
|
|
clearTimeout(this._timeout)
|
|
|
|
this._timeout = null
|
2018-08-31 09:18:28 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
if (this._element.classList.contains(CLASS_NAME_SHOW)) {
|
|
|
|
this._element.classList.remove(CLASS_NAME_SHOW)
|
2018-08-23 18:31:25 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
EventHandler.off(this._element, EVENT_CLICK_DISMISS)
|
2018-11-14 12:02:18 +01:00
|
|
|
Data.removeData(this._element, DATA_KEY)
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
this._element = null
|
2019-02-26 12:20:34 +01:00
|
|
|
this._config = null
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
// Private
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
_getConfig(config) {
|
|
|
|
config = {
|
|
|
|
...Default,
|
2018-11-14 12:02:18 +01:00
|
|
|
...Manipulator.getDataAttributes(this._element),
|
2018-11-14 10:16:56 +01:00
|
|
|
...typeof config === 'object' && config ? config : {}
|
2018-08-31 09:18:28 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
typeCheckConfig(
|
2018-11-14 10:16:56 +01:00
|
|
|
NAME,
|
|
|
|
config,
|
|
|
|
this.constructor.DefaultType
|
|
|
|
)
|
2018-08-31 09:18:28 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
return config
|
|
|
|
}
|
2018-08-31 09:18:28 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
_setListeners() {
|
2018-11-14 12:02:18 +01:00
|
|
|
EventHandler.on(
|
|
|
|
this._element,
|
2020-03-07 10:31:42 +01:00
|
|
|
EVENT_CLICK_DISMISS,
|
|
|
|
SELECTOR_DATA_DISMISS,
|
2019-04-09 12:18:17 +02:00
|
|
|
() => this.hide()
|
2018-11-14 10:16:56 +01:00
|
|
|
)
|
|
|
|
}
|
2018-08-31 09:18:28 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
// Static
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static jQueryInterface(config) {
|
2018-11-14 10:16:56 +01:00
|
|
|
return this.each(function () {
|
2019-02-26 12:20:34 +01:00
|
|
|
let data = Data.getData(this, DATA_KEY)
|
|
|
|
const _config = typeof config === 'object' && config
|
2018-11-14 10:16:56 +01:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = new Toast(this, _config)
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
if (typeof config === 'string') {
|
|
|
|
if (typeof data[config] === 'undefined') {
|
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2018-08-23 18:31:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
data[config](this)
|
|
|
|
}
|
|
|
|
})
|
2018-08-23 18:31:25 +02:00
|
|
|
}
|
2018-11-14 12:02:18 +01:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static getInstance(element) {
|
2018-11-14 12:02:18 +01:00
|
|
|
return Data.getData(element, DATA_KEY)
|
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
|
|
|
|
2019-08-02 15:51:05 +02:00
|
|
|
const $ = getjQuery()
|
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2018-11-14 12:02:18 +01:00
|
|
|
* add .toast to jQuery only if jQuery is present
|
2018-11-14 10:16:56 +01:00
|
|
|
*/
|
2019-03-29 19:43:56 +01:00
|
|
|
/* istanbul ignore if */
|
2019-08-02 15:51:05 +02:00
|
|
|
if ($) {
|
2018-11-14 12:02:18 +01:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
2019-07-28 15:24:46 +02:00
|
|
|
$.fn[NAME] = Toast.jQueryInterface
|
2019-02-26 12:20:34 +01:00
|
|
|
$.fn[NAME].Constructor = Toast
|
|
|
|
$.fn[NAME].noConflict = () => {
|
2018-11-14 12:02:18 +01:00
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
2019-07-28 15:24:46 +02:00
|
|
|
return Toast.jQueryInterface
|
2018-11-14 12:02:18 +01:00
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
|
|
|
export default Toast
|