2015-05-07 12:48:22 -07:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2021-05-05 22:32:12 +03:00
|
|
|
* Bootstrap (v5.0.0): alert.js
|
2020-06-16 21:41:47 +03:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-07 12:48:22 -07:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
import {
|
2020-12-08 07:16:50 +01:00
|
|
|
defineJQueryPlugin,
|
2021-04-11 02:27:18 +03:00
|
|
|
getElementFromSelector
|
2019-10-02 11:43:54 +02:00
|
|
|
} from './util/index'
|
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/event-handler'
|
2019-09-04 17:58:29 +03:00
|
|
|
import BaseComponent from './base-component'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-26 13:20:34 +02:00
|
|
|
const NAME = 'alert'
|
|
|
|
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 22:33:11 +03:00
|
|
|
const SELECTOR_DISMISS = '[data-bs-dismiss="alert"]'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-03-07 11:31:42 +02: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-12-09 15:09:41 +02:00
|
|
|
const CLASS_NAME_ALERT = 'alert'
|
|
|
|
const CLASS_NAME_FADE = 'fade'
|
|
|
|
const CLASS_NAME_SHOW = 'show'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2019-09-04 17:58:29 +03:00
|
|
|
class Alert extends BaseComponent {
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
|
|
|
|
2021-05-11 10:49:30 +03:00
|
|
|
static get NAME() {
|
|
|
|
return NAME
|
2019-09-04 17:58:29 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
close(element) {
|
2020-06-10 18:41:32 +03:00
|
|
|
const rootElement = element ? this._getRootElement(element) : this._element
|
2018-09-26 10:39:01 +02:00
|
|
|
const customEvent = this._triggerCloseEvent(rootElement)
|
2015-05-10 19:45:38 -07: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-10 19:45:38 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._removeElement(rootElement)
|
|
|
|
}
|
2015-05-07 16:34:28 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Private
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_getRootElement(element) {
|
2020-12-09 15:09:41 +02:00
|
|
|
return getElementFromSelector(element) || element.closest(`.${CLASS_NAME_ALERT}`)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_triggerCloseEvent(element) {
|
2020-03-07 11:31:42 +02:00
|
|
|
return EventHandler.trigger(element, EVENT_CLOSE)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_removeElement(element) {
|
2020-12-09 15:09:41 +02:00
|
|
|
element.classList.remove(CLASS_NAME_SHOW)
|
2015-08-18 19:22:46 -07:00
|
|
|
|
2021-04-11 02:27:18 +03:00
|
|
|
const isAnimated = element.classList.contains(CLASS_NAME_FADE)
|
|
|
|
this._queueCallback(() => this._destroyElement(element), element, isAnimated)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 12:48:22 -07: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 11:31:42 +02: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 12:48:22 -07:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static jQueryInterface(config) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return this.each(function () {
|
2021-03-02 15:55:44 +01:00
|
|
|
let data = Data.get(this, DATA_KEY)
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (!data) {
|
|
|
|
data = new Alert(this)
|
|
|
|
}
|
2015-05-07 12:48:22 -07: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)
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2021-02-18 11:37:20 +01:00
|
|
|
|
2020-06-20 19:00:53 +03:00
|
|
|
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()))
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2020-11-01 15:49:51 +02:00
|
|
|
* add .Alert to jQuery only if jQuery is present
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2021-05-11 10:49:30 +03:00
|
|
|
defineJQueryPlugin(Alert)
|
2015-05-07 12:48:22 -07:00
|
|
|
|
2015-05-07 16:34:28 -07:00
|
|
|
export default Alert
|