0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-11 03:52:27 +01:00
Bootstrap/js/src/alert.js

174 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-05-07 12:48:22 -07:00
/**
* --------------------------------------------------------------------------
2020-08-06 19:22:43 +03:00
* Bootstrap (v4.5.2): alert.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2015-05-07 12:48:22 -07:00
* --------------------------------------------------------------------------
*/
import $ from 'jquery'
import Util from './util'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2020-06-12 21:50:30 +03:00
const NAME = 'alert'
const VERSION = '4.5.2'
const DATA_KEY = 'bs.alert'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]
2018-09-26 10:39:01 +02:00
const SELECTOR_DISMISS = '[data-dismiss="alert"]'
2018-09-26 10:39:01 +02:00
2020-06-12 21:50:30 +03: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
const CLASS_NAME_ALERT = 'alert'
2020-06-12 21:50:30 +03:00
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
2018-09-26 10:39:01 +02:00
class Alert {
constructor(element) {
this._element = element
}
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
// Getters
static get VERSION() {
return VERSION
}
2015-05-07 12:48:22 -07: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) {
let rootElement = this._element
if (element) {
rootElement = this._getRootElement(element)
2015-05-07 12:48:22 -07:00
}
2018-09-26 10:39:01 +02:00
const customEvent = this._triggerCloseEvent(rootElement)
2018-09-26 10:39:01 +02:00
if (customEvent.isDefaultPrevented()) {
return
}
2018-09-26 10:39:01 +02:00
this._removeElement(rootElement)
}
2018-09-26 10:39:01 +02:00
dispose() {
$.removeData(this._element, DATA_KEY)
this._element = null
}
2015-05-07 17:07:38 -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) {
const selector = Util.getSelectorFromElement(element)
2020-06-12 21:50:30 +03:00
let parent = false
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
if (selector) {
parent = document.querySelector(selector)
2015-05-07 12:48:22 -07:00
}
2018-09-26 10:39:01 +02:00
if (!parent) {
parent = $(element).closest(`.${CLASS_NAME_ALERT}`)[0]
2015-05-13 12:48:34 -07:00
}
2018-09-26 10:39:01 +02:00
return parent
}
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
_triggerCloseEvent(element) {
const closeEvent = $.Event(EVENT_CLOSE)
2018-09-26 10:39:01 +02:00
$(element).trigger(closeEvent)
return closeEvent
}
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
_removeElement(element) {
$(element).removeClass(CLASS_NAME_SHOW)
2015-08-18 19:22:46 -07:00
if (!$(element).hasClass(CLASS_NAME_FADE)) {
2018-09-26 10:39:01 +02:00
this._destroyElement(element)
return
2015-05-07 12:48:22 -07:00
}
2018-09-26 10:39:01 +02:00
const transitionDuration = Util.getTransitionDurationFromElement(element)
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
$(element)
2020-06-12 21:50:30 +03:00
.one(Util.TRANSITION_END, event => this._destroyElement(element, event))
2018-09-26 10:39:01 +02:00
.emulateTransitionEnd(transitionDuration)
}
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
_destroyElement(element) {
$(element)
.detach()
.trigger(EVENT_CLOSED)
2018-09-26 10:39:01 +02:00
.remove()
}
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
2018-09-26 10:39:01 +02:00
static _jQueryInterface(config) {
return this.each(function () {
const $element = $(this)
2020-06-12 21:50:30 +03:00
let data = $element.data(DATA_KEY)
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
if (!data) {
data = new Alert(this)
$element.data(DATA_KEY, data)
}
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
if (config === 'close') {
data[config](this)
}
})
}
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
static _handleDismiss(alertInstance) {
return function (event) {
if (event) {
event.preventDefault()
}
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
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
* ------------------------------------------------------------------------
*/
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
$(document).on(
EVENT_CLICK_DATA_API,
SELECTOR_DISMISS,
2018-09-26 10:39:01 +02:00
Alert._handleDismiss(new Alert())
)
2015-05-07 12:48:22 -07:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2015-05-07 12:48:22 -07:00
2020-06-12 21:50:30 +03:00
$.fn[NAME] = Alert._jQueryInterface
2018-09-26 10:39:01 +02:00
$.fn[NAME].Constructor = Alert
2020-06-12 21:50:30 +03:00
$.fn[NAME].noConflict = () => {
2018-09-26 10:39:01 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT
return Alert._jQueryInterface
}
2015-05-07 12:48:22 -07:00
export default Alert