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