2018-08-23 16:31:25 +00:00
|
|
|
/**
|
|
|
|
|
* --------------------------------------------------------------------------
|
2022-10-03 07:44:02 +00:00
|
|
|
* Bootstrap (v5.2.2): toast.js
|
2020-06-16 18:41:47 +00:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2018-08-23 16:31:25 +00:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-10 16:18:18 +00:00
|
|
|
import { defineJQueryPlugin, reflow } from './util/index'
|
2019-10-02 09:43:54 +00:00
|
|
|
import EventHandler from './dom/event-handler'
|
2019-09-04 14:58:29 +00:00
|
|
|
import BaseComponent from './base-component'
|
2021-07-28 14:39:32 +00:00
|
|
|
import { enableDismissTrigger } from './util/component-functions'
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
/**
|
|
|
|
|
* Constants
|
|
|
|
|
*/
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2019-02-26 11:20:34 +00:00
|
|
|
const NAME = 'toast'
|
|
|
|
|
const DATA_KEY = 'bs.toast'
|
2018-11-14 11:02:18 +00:00
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
2018-11-14 09:16:56 +00:00
|
|
|
|
2021-05-11 05:37:57 +00: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 09:31:42 +00: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 09:16:56 +00:00
|
|
|
|
2020-03-07 09:31:42 +00:00
|
|
|
const CLASS_NAME_FADE = 'fade'
|
2021-07-22 15:13:13 +00:00
|
|
|
const CLASS_NAME_HIDE = 'hide' // @deprecated - kept here only for backwards compatibility
|
2020-03-07 09:31:42 +00:00
|
|
|
const CLASS_NAME_SHOW = 'show'
|
|
|
|
|
const CLASS_NAME_SHOWING = 'showing'
|
2018-11-14 09:16:56 +00:00
|
|
|
|
|
|
|
|
const DefaultType = {
|
2019-02-26 11:20:34 +00:00
|
|
|
animation: 'boolean',
|
|
|
|
|
autohide: 'boolean',
|
|
|
|
|
delay: 'number'
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Default = {
|
2019-02-26 11:20:34 +00:00
|
|
|
animation: true,
|
|
|
|
|
autohide: true,
|
2020-07-12 12:43:26 +00:00
|
|
|
delay: 5000
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-13 12:19:28 +00:00
|
|
|
* Class definition
|
2018-11-14 09:16:56 +00:00
|
|
|
*/
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2019-09-04 14:58:29 +00:00
|
|
|
class Toast extends BaseComponent {
|
2018-11-14 09:16:56 +00:00
|
|
|
constructor(element, config) {
|
2021-12-10 16:18:18 +00:00
|
|
|
super(element, config)
|
2019-09-04 14:58:29 +00:00
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
this._timeout = null
|
2021-05-11 05:37:57 +00:00
|
|
|
this._hasMouseInteraction = false
|
|
|
|
|
this._hasKeyboardInteraction = false
|
2018-11-14 09:16:56 +00:00
|
|
|
this._setListeners()
|
2018-08-31 07:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
// Getters
|
2019-02-06 13:19:04 +00:00
|
|
|
static get Default() {
|
|
|
|
|
return Default
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 16:18:18 +00:00
|
|
|
static get DefaultType() {
|
|
|
|
|
return DefaultType
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 07:49:30 +00:00
|
|
|
static get NAME() {
|
|
|
|
|
return NAME
|
2019-09-04 14:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
// Public
|
|
|
|
|
show() {
|
2020-03-07 09:31:42 +00:00
|
|
|
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW)
|
2019-05-16 09:57:05 +00:00
|
|
|
|
|
|
|
|
if (showEvent.defaultPrevented) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2020-07-11 18:51:04 +00:00
|
|
|
this._clearTimeout()
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
if (this._config.animation) {
|
2020-03-07 09:31:42 +00:00
|
|
|
this._element.classList.add(CLASS_NAME_FADE)
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
const complete = () => {
|
2020-03-07 09:31:42 +00:00
|
|
|
this._element.classList.remove(CLASS_NAME_SHOWING)
|
|
|
|
|
EventHandler.trigger(this._element, EVENT_SHOWN)
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2021-05-11 05:37:57 +00:00
|
|
|
this._maybeScheduleHide()
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2021-07-22 15:13:13 +00:00
|
|
|
this._element.classList.remove(CLASS_NAME_HIDE) // @deprecated
|
2019-07-23 14:21:23 +00:00
|
|
|
reflow(this._element)
|
2021-12-10 05:42:08 +00:00
|
|
|
this._element.classList.add(CLASS_NAME_SHOW, CLASS_NAME_SHOWING)
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2021-04-10 23:27:18 +00:00
|
|
|
this._queueCallback(complete, this._element, this._config.animation)
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2019-04-09 10:18:17 +00:00
|
|
|
hide() {
|
2022-05-07 04:29:21 +00:00
|
|
|
if (!this.isShown()) {
|
2018-11-14 09:16:56 +00:00
|
|
|
return
|
|
|
|
|
}
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2020-03-07 09:31:42 +00:00
|
|
|
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE)
|
2019-05-16 09:57:05 +00:00
|
|
|
|
|
|
|
|
if (hideEvent.defaultPrevented) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-09 11:12:05 +00:00
|
|
|
|
|
|
|
|
const complete = () => {
|
2021-07-22 15:13:13 +00:00
|
|
|
this._element.classList.add(CLASS_NAME_HIDE) // @deprecated
|
2021-12-10 05:42:08 +00:00
|
|
|
this._element.classList.remove(CLASS_NAME_SHOWING, CLASS_NAME_SHOW)
|
2020-03-07 09:31:42 +00:00
|
|
|
EventHandler.trigger(this._element, EVENT_HIDDEN)
|
2019-04-09 11:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 15:13:13 +00:00
|
|
|
this._element.classList.add(CLASS_NAME_SHOWING)
|
2021-04-10 23:27:18 +00:00
|
|
|
this._queueCallback(complete, this._element, this._config.animation)
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
dispose() {
|
2020-07-11 18:51:04 +00:00
|
|
|
this._clearTimeout()
|
2018-08-31 07:18:28 +00:00
|
|
|
|
2022-05-07 04:29:21 +00:00
|
|
|
if (this.isShown()) {
|
2020-03-07 09:31:42 +00:00
|
|
|
this._element.classList.remove(CLASS_NAME_SHOW)
|
2018-08-23 16:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-20 10:13:11 +00:00
|
|
|
super.dispose()
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2022-05-07 04:29:21 +00:00
|
|
|
isShown() {
|
|
|
|
|
return this._element.classList.contains(CLASS_NAME_SHOW)
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
// Private
|
2018-08-31 07:18:28 +00:00
|
|
|
|
2021-05-11 05:37:57 +00: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':
|
2022-10-02 05:24:41 +00:00
|
|
|
case 'mouseout': {
|
2021-05-11 05:37:57 +00:00
|
|
|
this._hasMouseInteraction = isInteracting
|
|
|
|
|
break
|
2022-10-02 05:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-11 05:37:57 +00:00
|
|
|
case 'focusin':
|
2022-10-02 05:24:41 +00:00
|
|
|
case 'focusout': {
|
2021-05-11 05:37:57 +00:00
|
|
|
this._hasKeyboardInteraction = isInteracting
|
|
|
|
|
break
|
2022-10-02 05:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: {
|
2021-05-11 05:37:57 +00:00
|
|
|
break
|
2022-10-02 05:24:41 +00:00
|
|
|
}
|
2021-05-11 05:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isInteracting) {
|
|
|
|
|
this._clearTimeout()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextElement = event.relatedTarget
|
|
|
|
|
if (this._element === nextElement || this._element.contains(nextElement)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._maybeScheduleHide()
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
_setListeners() {
|
2021-05-11 05:37:57 +00: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 09:16:56 +00:00
|
|
|
}
|
2018-08-31 07:18:28 +00:00
|
|
|
|
2020-07-11 18:51:04 +00:00
|
|
|
_clearTimeout() {
|
|
|
|
|
clearTimeout(this._timeout)
|
|
|
|
|
this._timeout = null
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
// Static
|
2019-07-28 13:24:46 +00:00
|
|
|
static jQueryInterface(config) {
|
2018-11-14 09:16:56 +00:00
|
|
|
return this.each(function () {
|
2021-06-03 15:53:27 +00:00
|
|
|
const data = Toast.getOrCreateInstance(this, config)
|
2018-08-23 16:31:25 +00:00
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
if (typeof config === 'string') {
|
|
|
|
|
if (typeof data[config] === 'undefined') {
|
|
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2018-08-23 16:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
data[config](this)
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-08-23 16:31:25 +00:00
|
|
|
}
|
2018-11-14 09:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-13 12:19:28 +00:00
|
|
|
/**
|
|
|
|
|
* Data API implementation
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-28 14:39:32 +00:00
|
|
|
enableDismissTrigger(Toast)
|
|
|
|
|
|
2018-11-14 09:16:56 +00:00
|
|
|
/**
|
|
|
|
|
* jQuery
|
|
|
|
|
*/
|
2020-11-01 13:32:36 +00:00
|
|
|
|
2021-05-11 07:49:30 +00:00
|
|
|
defineJQueryPlugin(Toast)
|
2018-08-23 16:31:25 +00:00
|
|
|
|
|
|
|
|
export default Toast
|