0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/src/toast.js

239 lines
5.4 KiB
JavaScript
Raw Normal View History

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