2018-08-23 18:31:25 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2019-02-13 17:01:40 +01:00
|
|
|
* Bootstrap (v4.3.1): 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 {
|
|
|
|
jQuery as $,
|
|
|
|
TRANSITION_END,
|
|
|
|
emulateTransitionEnd,
|
|
|
|
getTransitionDurationFromElement,
|
|
|
|
typeCheckConfig
|
|
|
|
} from './util/index'
|
2018-11-14 12:02:18 +01:00
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/eventHandler'
|
|
|
|
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'
|
|
|
|
const VERSION = '4.3.1'
|
|
|
|
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
|
|
|
|
|
|
|
const Event = {
|
2019-02-26 12:20:34 +01:00
|
|
|
CLICK_DISMISS: `click.dismiss${EVENT_KEY}`,
|
|
|
|
HIDE: `hide${EVENT_KEY}`,
|
|
|
|
HIDDEN: `hidden${EVENT_KEY}`,
|
|
|
|
SHOW: `show${EVENT_KEY}`,
|
|
|
|
SHOWN: `shown${EVENT_KEY}`
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const ClassName = {
|
2019-02-26 12:20:34 +01:00
|
|
|
FADE: 'fade',
|
|
|
|
HIDE: 'hide',
|
|
|
|
SHOW: 'show',
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
2019-02-26 12:20:34 +01:00
|
|
|
DATA_DISMISS: '[data-dismiss="toast"]'
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
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() {
|
2018-11-14 12:02:18 +01:00
|
|
|
EventHandler.trigger(this._element, Event.SHOW)
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
if (this._config.animation) {
|
|
|
|
this._element.classList.add(ClassName.FADE)
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
const complete = () => {
|
2018-12-06 13:53:01 +01:00
|
|
|
this._element.classList.remove(ClassName.SHOWING)
|
|
|
|
this._element.classList.add(ClassName.SHOW)
|
|
|
|
|
2018-11-14 12:02:18 +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
|
|
|
|
2018-12-06 13:53:01 +01:00
|
|
|
this._element.classList.remove(ClassName.HIDE)
|
|
|
|
this._element.classList.add(ClassName.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() {
|
2018-11-14 10:16:56 +01:00
|
|
|
if (!this._element.classList.contains(ClassName.SHOW)) {
|
|
|
|
return
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 12:02:18 +01:00
|
|
|
EventHandler.trigger(this._element, Event.HIDE)
|
2019-04-09 12:18:17 +02:00
|
|
|
this._close()
|
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
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
if (this._element.classList.contains(ClassName.SHOW)) {
|
|
|
|
this._element.classList.remove(ClassName.SHOW)
|
2018-08-23 18:31:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:02:18 +01:00
|
|
|
EventHandler.off(this._element, Event.CLICK_DISMISS)
|
|
|
|
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,
|
2018-11-14 10:16:56 +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
|
|
|
_close() {
|
|
|
|
const complete = () => {
|
2018-12-06 13:53:01 +01:00
|
|
|
this._element.classList.add(ClassName.HIDE)
|
2018-11-14 12:02:18 +01:00
|
|
|
EventHandler.trigger(this._element, Event.HIDDEN)
|
2018-08-31 09:18:28 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
this._element.classList.remove(ClassName.SHOW)
|
|
|
|
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
|
|
|
// Static
|
|
|
|
|
|
|
|
static _jQueryInterface(config) {
|
|
|
|
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
|
|
|
|
|
|
|
static _getInstance(element) {
|
|
|
|
return Data.getData(element, DATA_KEY)
|
|
|
|
}
|
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
|
|
|
*/
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2018-11-14 12:02:18 +01:00
|
|
|
if (typeof $ !== 'undefined') {
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
2019-02-26 12:20:34 +01:00
|
|
|
$.fn[NAME] = Toast._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = Toast
|
|
|
|
$.fn[NAME].noConflict = () => {
|
2018-11-14 12:02:18 +01:00
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return Toast._jQueryInterface
|
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
|
|
|
export default Toast
|