2018-08-23 18:31:25 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2021-05-05 21:32:12 +02:00
|
|
|
* Bootstrap (v5.0.0): toast.js
|
2020-06-16 20:41:47 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2018-08-23 18:31:25 +02:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
import {
|
2020-12-08 07:16:50 +01:00
|
|
|
defineJQueryPlugin,
|
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'
|
2019-09-04 16:58:29 +02:00
|
|
|
import BaseComponent from './base-component'
|
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 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}`
|
2021-05-11 07:37:57 +02:00
|
|
|
const EVENT_MOUSEOVER = `mouseover${EVENT_KEY}`
|
|
|
|
const EVENT_MOUSEOUT = `mouseout${EVENT_KEY}`
|
|
|
|
const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
|
|
|
|
const EVENT_FOCUSOUT = `focusout${EVENT_KEY}`
|
2020-03-07 10:31:42 +01:00
|
|
|
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,
|
2020-07-12 14:43:26 +02:00
|
|
|
delay: 5000
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 21:33:11 +02:00
|
|
|
const SELECTOR_DATA_DISMISS = '[data-bs-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
|
|
|
|
2019-09-04 16:58:29 +02:00
|
|
|
class Toast extends BaseComponent {
|
2018-11-14 10:16:56 +01:00
|
|
|
constructor(element, config) {
|
2019-09-04 16:58:29 +02:00
|
|
|
super(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
|
2021-05-11 07:37:57 +02:00
|
|
|
this._hasMouseInteraction = false
|
|
|
|
this._hasKeyboardInteraction = false
|
2018-11-14 10:16:56 +01:00
|
|
|
this._setListeners()
|
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 DefaultType() {
|
|
|
|
return DefaultType
|
|
|
|
}
|
2018-08-23 18:31:25 +02:00
|
|
|
|
2019-02-06 14:19:04 +01:00
|
|
|
static get Default() {
|
|
|
|
return Default
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:58:29 +02:00
|
|
|
static get DATA_KEY() {
|
|
|
|
return DATA_KEY
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-07-11 20:51:04 +02:00
|
|
|
this._clearTimeout()
|
|
|
|
|
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
|
|
|
|
2021-05-11 07:37:57 +02:00
|
|
|
this._maybeScheduleHide()
|
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-08-23 18:31:25 +02:00
|
|
|
|
2021-04-11 01:27:18 +02:00
|
|
|
this._queueCallback(complete, this._element, this._config.animation)
|
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)
|
2021-04-11 01:27:18 +02:00
|
|
|
this._queueCallback(complete, this._element, this._config.animation)
|
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() {
|
2020-07-11 20:51:04 +02:00
|
|
|
this._clearTimeout()
|
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-11-20 11:13:11 +01:00
|
|
|
super.dispose()
|
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),
|
2020-06-26 17:50:04 +02:00
|
|
|
...(typeof config === 'object' && config ? config : {})
|
2018-08-31 09:18:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 17:40:52 +02:00
|
|
|
typeCheckConfig(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
|
|
|
|
2021-05-11 07:37:57 +02:00
|
|
|
_maybeScheduleHide() {
|
|
|
|
if (!this._config.autohide) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._hasMouseInteraction || this._hasKeyboardInteraction) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this._timeout = setTimeout(() => {
|
|
|
|
this.hide()
|
|
|
|
}, this._config.delay)
|
|
|
|
}
|
|
|
|
|
|
|
|
_onInteraction(event, isInteracting) {
|
|
|
|
switch (event.type) {
|
|
|
|
case 'mouseover':
|
|
|
|
case 'mouseout':
|
|
|
|
this._hasMouseInteraction = isInteracting
|
|
|
|
break
|
|
|
|
case 'focusin':
|
|
|
|
case 'focusout':
|
|
|
|
this._hasKeyboardInteraction = isInteracting
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInteracting) {
|
|
|
|
this._clearTimeout()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextElement = event.relatedTarget
|
|
|
|
if (this._element === nextElement || this._element.contains(nextElement)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this._maybeScheduleHide()
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:16:56 +01:00
|
|
|
_setListeners() {
|
2020-06-20 18:00:53 +02:00
|
|
|
EventHandler.on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, () => this.hide())
|
2021-05-11 07:37:57 +02:00
|
|
|
EventHandler.on(this._element, EVENT_MOUSEOVER, event => this._onInteraction(event, true))
|
|
|
|
EventHandler.on(this._element, EVENT_MOUSEOUT, event => this._onInteraction(event, false))
|
|
|
|
EventHandler.on(this._element, EVENT_FOCUSIN, event => this._onInteraction(event, true))
|
|
|
|
EventHandler.on(this._element, EVENT_FOCUSOUT, event => this._onInteraction(event, false))
|
2018-11-14 10:16:56 +01:00
|
|
|
}
|
2018-08-31 09:18:28 +02:00
|
|
|
|
2020-07-11 20:51:04 +02:00
|
|
|
_clearTimeout() {
|
|
|
|
clearTimeout(this._timeout)
|
|
|
|
this._timeout = null
|
|
|
|
}
|
|
|
|
|
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 () {
|
2021-03-02 15:55:44 +01:00
|
|
|
let data = Data.get(this, DATA_KEY)
|
2019-02-26 12:20:34 +01:00
|
|
|
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 10:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2020-11-01 14:49:51 +01:00
|
|
|
* add .Toast to jQuery only if jQuery is present
|
2018-11-14 10:16:56 +01:00
|
|
|
*/
|
2020-11-01 14:32:36 +01:00
|
|
|
|
2020-12-08 07:16:50 +01:00
|
|
|
defineJQueryPlugin(NAME, Toast)
|
2018-08-23 18:31:25 +02:00
|
|
|
|
|
|
|
export default Toast
|