0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-08 05:54:23 +01:00
Bootstrap/js/src/alert.js

121 lines
3.0 KiB
JavaScript
Raw Normal View History

2015-05-07 12:48:22 -07:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.2): 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
* --------------------------------------------------------------------------
*/
import {
defineJQueryPlugin,
getElementFromSelector,
isDisabled
} from './util/index'
import EventHandler from './dom/event-handler'
2019-09-04 17:58:29 +03:00
import BaseComponent from './base-component'
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
const SELECTOR_DISMISS = '[data-bs-dismiss="alert"]'
2018-09-26 10:39:01 +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
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
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
close() {
const closeEvent = EventHandler.trigger(this._element, EVENT_CLOSE)
if (closeEvent.defaultPrevented) {
2018-09-26 10:39:01 +02:00
return
}
this._element.classList.remove(CLASS_NAME_SHOW)
2015-05-07 12:48:22 -07:00
const isAnimated = this._element.classList.contains(CLASS_NAME_FADE)
this._queueCallback(() => this._destroyElement(), this._element, isAnimated)
2018-09-26 10:39:01 +02:00
}
2015-05-07 12:48:22 -07:00
// Private
_destroyElement() {
this._element.remove()
EventHandler.trigger(this._element, EVENT_CLOSED)
this.dispose()
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 () {
const data = Alert.getOrCreateInstance(this)
2015-05-07 12:48:22 -07:00
if (typeof config !== 'string') {
return
2018-09-26 10:39:01 +02:00
}
if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
throw new TypeError(`No method named "${config}"`)
2018-07-25 11:29:16 +02:00
}
data[config](this)
})
2018-07-25 11:29:16 +02:00
}
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
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, function (event) {
if (['A', 'AREA'].includes(this.tagName)) {
event.preventDefault()
}
if (isDisabled(this)) {
return
}
const target = getElementFromSelector(this) || this.closest(`.${CLASS_NAME_ALERT}`)
const alert = Alert.getOrCreateInstance(target)
alert.close()
})
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
defineJQueryPlugin(Alert)
2015-05-07 12:48:22 -07:00
export default Alert