2015-05-07 21:48:22 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2020-11-11 18:07:37 +01:00
|
|
|
* Bootstrap (v5.0.0-alpha3): alert.js
|
2020-06-16 20:41:47 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-07 21:48:22 +02:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
import {
|
2019-08-02 15:51:05 +02:00
|
|
|
getjQuery,
|
2020-11-01 14:32:36 +01:00
|
|
|
onDOMContentLoaded,
|
2019-02-22 23:37:55 +01:00
|
|
|
TRANSITION_END,
|
|
|
|
emulateTransitionEnd,
|
2019-07-23 21:15:00 +02:00
|
|
|
getElementFromSelector,
|
2019-02-22 23:37:55 +01:00
|
|
|
getTransitionDurationFromElement
|
2019-10-02 11:43:54 +02:00
|
|
|
} from './util/index'
|
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/event-handler'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-26 12:20:34 +01:00
|
|
|
const NAME = 'alert'
|
2020-11-11 18:07:37 +01:00
|
|
|
const VERSION = '5.0.0-alpha3'
|
2019-02-26 12:20:34 +01:00
|
|
|
const DATA_KEY = 'bs.alert'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-07-22 21:33:11 +02:00
|
|
|
const SELECTOR_DISMISS = '[data-bs-dismiss="alert"]'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
const EVENT_CLOSE = `close${EVENT_KEY}`
|
|
|
|
const EVENT_CLOSED = `closed${EVENT_KEY}`
|
|
|
|
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
const CLASSNAME_ALERT = 'alert'
|
|
|
|
const CLASSNAME_FADE = 'fade'
|
|
|
|
const CLASSNAME_SHOW = 'show'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
class Alert {
|
|
|
|
constructor(element) {
|
|
|
|
this._element = element
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2018-06-24 22:02:03 +02:00
|
|
|
if (this._element) {
|
|
|
|
Data.setData(element, DATA_KEY, this)
|
|
|
|
}
|
2015-05-08 01:34:28 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
|
|
|
|
|
|
|
static get VERSION() {
|
|
|
|
return VERSION
|
2015-05-08 01:34:28 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
close(element) {
|
2020-06-10 17:41:32 +02:00
|
|
|
const rootElement = element ? this._getRootElement(element) : this._element
|
2018-09-26 10:39:01 +02:00
|
|
|
const customEvent = this._triggerCloseEvent(rootElement)
|
2015-05-11 04:45:38 +02:00
|
|
|
|
2017-08-23 16:28:01 +02:00
|
|
|
if (customEvent === null || customEvent.defaultPrevented) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
2015-05-11 04:45:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._removeElement(rootElement)
|
|
|
|
}
|
2015-05-08 01:34:28 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
dispose() {
|
2017-08-21 09:11:37 +02:00
|
|
|
Data.removeData(this._element, DATA_KEY)
|
2018-09-26 10:39:01 +02:00
|
|
|
this._element = null
|
|
|
|
}
|
2015-05-08 02:07:38 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Private
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_getRootElement(element) {
|
2020-04-28 21:17:23 +02:00
|
|
|
return getElementFromSelector(element) || element.closest(`.${CLASSNAME_ALERT}`)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_triggerCloseEvent(element) {
|
2020-03-07 10:31:42 +01:00
|
|
|
return EventHandler.trigger(element, EVENT_CLOSE)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_removeElement(element) {
|
2020-03-07 10:31:42 +01:00
|
|
|
element.classList.remove(CLASSNAME_SHOW)
|
2015-08-19 04:22:46 +02:00
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
if (!element.classList.contains(CLASSNAME_FADE)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
this._destroyElement(element)
|
|
|
|
return
|
2015-05-07 21:48:22 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
const transitionDuration = getTransitionDurationFromElement(element)
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2020-06-10 17:40:52 +02:00
|
|
|
EventHandler.one(element, TRANSITION_END, () => this._destroyElement(element))
|
2019-02-22 23:37:55 +01:00
|
|
|
emulateTransitionEnd(element, transitionDuration)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_destroyElement(element) {
|
2018-03-11 16:18:56 +01:00
|
|
|
if (element.parentNode) {
|
|
|
|
element.parentNode.removeChild(element)
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:31:42 +01:00
|
|
|
EventHandler.trigger(element, EVENT_CLOSED)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2018-03-13 09:59:20 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Static
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static jQueryInterface(config) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return this.each(function () {
|
2017-08-21 09:11:37 +02:00
|
|
|
let data = Data.getData(this, DATA_KEY)
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (!data) {
|
|
|
|
data = new Alert(this)
|
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (config === 'close') {
|
|
|
|
data[config](this)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-06-07 21:43:04 +02:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static handleDismiss(alertInstance) {
|
2018-07-25 11:29:16 +02:00
|
|
|
return function (event) {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
alertInstance.close(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static getInstance(element) {
|
2018-06-07 21:43:04 +02:00
|
|
|
return Data.getData(element, DATA_KEY)
|
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2020-06-20 18:00:53 +02:00
|
|
|
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()))
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2020-11-01 14:49:51 +01:00
|
|
|
* add .Alert to jQuery only if jQuery is present
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2020-11-01 14:32:36 +01:00
|
|
|
onDOMContentLoaded(() => {
|
|
|
|
const $ = getjQuery()
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if ($) {
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
$.fn[NAME] = Alert.jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = Alert
|
|
|
|
$.fn[NAME].noConflict = () => {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return Alert.jQueryInterface
|
|
|
|
}
|
2017-08-23 12:03:50 +02:00
|
|
|
}
|
2020-11-01 14:32:36 +01:00
|
|
|
})
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2015-05-08 01:34:28 +02:00
|
|
|
export default Alert
|