0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-10 22:24:19 +01:00
Bootstrap/js/src/alert.js

186 lines
3.8 KiB
JavaScript
Raw Normal View History

import Util from './util'
2015-05-07 21:48:22 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.0.0): alert.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2015-05-08 02:07:38 +02:00
const Alert = (($) => {
2015-05-07 21:48:22 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2015-05-07 21:48:22 +02:00
const NAME = 'alert'
const VERSION = '4.0.0'
const DATA_KEY = 'bs.alert'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 150
2015-05-07 21:48:22 +02:00
const Selector = {
DISMISS : '[data-dismiss="alert"]'
}
2015-05-07 21:48:22 +02:00
const Event = {
CLOSE : 'close.bs.alert',
CLOSED : 'closed.bs.alert',
CLICK : 'click.bs.alert.data-api'
}
2015-05-07 21:48:22 +02:00
const ClassName = {
ALERT : 'alert',
FADE : 'fade',
IN : 'in'
}
2015-05-07 21:48:22 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-07 21:48:22 +02:00
class Alert {
2015-05-07 21:48:22 +02:00
constructor(element) {
2015-05-08 07:26:40 +02:00
this._element = element
2015-05-07 21:48:22 +02:00
}
// getters
static get VERSION() {
return VERSION
}
// public
close(element) {
2015-05-08 07:26:40 +02:00
element = element || this._element
2015-05-08 02:07:38 +02:00
let rootElement = this._getRootElement(element)
let customEvent = this._triggerCloseEvent(rootElement)
2015-05-07 21:48:22 +02:00
if (customEvent.isDefaultPrevented()) {
return
}
2015-05-07 21:48:22 +02:00
this._removeElement(rootElement)
2015-05-07 21:48:22 +02:00
}
// private
2015-05-07 21:48:22 +02:00
_getRootElement(element) {
let parent = false
let selector = Util.getSelectorFromElement(element)
2015-05-07 21:48:22 +02:00
if (selector) {
parent = $(selector)[0]
}
2015-05-07 21:48:22 +02:00
if (!parent) {
2015-05-10 08:00:59 +02:00
parent = $(element).closest(`.${ClassName.ALERT}`)[0]
}
return parent
2015-05-07 21:48:22 +02:00
}
_triggerCloseEvent(element) {
var closeEvent = $.Event(Event.CLOSE)
$(element).trigger(closeEvent)
return closeEvent
2015-05-07 21:48:22 +02:00
}
_removeElement(element) {
$(element).removeClass(ClassName.IN)
2015-05-07 21:48:22 +02:00
if (!Util.supportsTransitionEnd() ||
!$(element).hasClass(ClassName.FADE)) {
this._destroyElement(element)
return
}
2015-05-07 21:48:22 +02:00
$(element)
.one(Util.TRANSITION_END, this._destroyElement.bind(this, element))
.emulateTransitionEnd(TRANSITION_DURATION)
}
2015-05-07 21:48:22 +02:00
_destroyElement(element) {
$(element)
.detach()
.trigger(Event.CLOSED)
.remove()
2015-05-07 21:48:22 +02:00
}
// static
2015-05-07 21:48:22 +02:00
static _jQueryInterface(config) {
return this.each(function () {
let $element = $(this)
let data = $element.data(DATA_KEY)
2015-05-07 21:48:22 +02:00
if (!data) {
data = new Alert(this)
$element.data(DATA_KEY, data)
}
2015-05-07 21:48:22 +02:00
if (config === 'close') {
data[config](this)
}
})
}
2015-05-07 21:48:22 +02:00
static _handleDismiss(alertInstance) {
return function (event) {
if (event) {
event.preventDefault()
}
2015-05-07 21:48:22 +02:00
alertInstance.close(this)
2015-05-07 21:48:22 +02:00
}
}
2015-05-07 21:48:22 +02:00
}
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 21:48:22 +02:00
$(document).on(
Event.CLICK,
Selector.DISMISS,
Alert._handleDismiss(new Alert())
)
2015-05-07 21:48:22 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2015-05-07 21:48:22 +02:00
$.fn[NAME] = Alert._jQueryInterface
$.fn[NAME].Constructor = Alert
$.fn[NAME].noConflict = function () {
2015-05-08 02:07:38 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT
return Alert._jQueryInterface
}
2015-05-07 21:48:22 +02:00
return Alert
2015-05-07 21:48:22 +02:00
2015-05-08 02:07:38 +02:00
})(jQuery)
2015-05-07 21:48:22 +02:00
export default Alert