2015-05-07 21:48:22 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2019-02-13 17:01:40 +01:00
|
|
|
* Bootstrap (v4.3.1): alert.js
|
2015-05-07 21:48:22 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2017-08-21 09:11:37 +02:00
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/eventHandler'
|
|
|
|
import SelectorEngine from './dom/selectorEngine'
|
2018-11-14 10:16:56 +01:00
|
|
|
import Util from './util'
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
const NAME = 'alert'
|
2019-02-13 17:01:40 +01:00
|
|
|
const VERSION = '4.3.1'
|
2018-09-26 10:39:01 +02:00
|
|
|
const DATA_KEY = 'bs.alert'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
|
|
|
|
|
|
|
const Selector = {
|
|
|
|
DISMISS : '[data-dismiss="alert"]'
|
|
|
|
}
|
|
|
|
|
|
|
|
const Event = {
|
|
|
|
CLOSE : `close${EVENT_KEY}`,
|
|
|
|
CLOSED : `closed${EVENT_KEY}`,
|
|
|
|
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClassName = {
|
|
|
|
ALERT : 'alert',
|
|
|
|
FADE : 'fade',
|
|
|
|
SHOW : 'show'
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* 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
|
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) {
|
|
|
|
let rootElement = this._element
|
|
|
|
if (element) {
|
|
|
|
rootElement = this._getRootElement(element)
|
2015-05-07 21:48:22 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
const selector = Util.getSelectorFromElement(element)
|
|
|
|
let parent = false
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (selector) {
|
2017-08-27 00:57:27 +02:00
|
|
|
parent = SelectorEngine.findOne(selector)
|
2015-05-07 21:48:22 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (!parent) {
|
2017-08-21 09:11:37 +02:00
|
|
|
parent = SelectorEngine.closest(element, `.${ClassName.ALERT}`)
|
2015-05-13 21:48:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
return parent
|
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_triggerCloseEvent(element) {
|
2017-08-21 09:11:37 +02: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) {
|
2017-08-21 09:11:37 +02:00
|
|
|
element.classList.remove(ClassName.SHOW)
|
2015-08-19 04:22:46 +02:00
|
|
|
|
2017-08-21 09:11:37 +02: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
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
const transitionDuration = Util.getTransitionDurationFromElement(element)
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2017-08-21 09:11:37 +02:00
|
|
|
EventHandler
|
|
|
|
.one(element, Util.TRANSITION_END, (event) => this._destroyElement(element, event))
|
|
|
|
Util.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) {
|
2017-08-21 09:11:37 +02:00
|
|
|
EventHandler.trigger(element, Event.CLOSED)
|
|
|
|
element.parentNode.removeChild(element)
|
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
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
static _jQueryInterface(config) {
|
|
|
|
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)
|
2017-08-21 09:11:37 +02:00
|
|
|
Data.setData(this, DATA_KEY, data)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (config === 'close') {
|
|
|
|
data[config](this)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2017-08-23 12:01:38 +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
|
|
|
|
* ------------------------------------------------------------------------
|
2017-08-23 12:03:50 +02: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
|
|
|
|
2017-08-23 12:03:50 +02:00
|
|
|
if (typeof window.$ !== 'undefined' || typeof window.jQuery !== 'undefined') {
|
2017-08-24 20:50:27 +02:00
|
|
|
const $ = window.$ || window.jQuery
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
$.fn[NAME] = Alert._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = Alert
|
|
|
|
$.fn[NAME].noConflict = () => {
|
2017-08-23 12:03:50 +02:00
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return Alert._jQueryInterface
|
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 21:48:22 +02:00
|
|
|
|
2015-05-08 01:34:28 +02:00
|
|
|
export default Alert
|