0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/dist/alert.js

205 lines
6.4 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap alert.js v5.0.1 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 07:41:12 +01:00
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/selector-engine.js'), require('./dom/data.js'), require('./dom/event-handler.js'), require('./base-component.js')) :
typeof define === 'function' && define.amd ? define(['./dom/selector-engine', './dom/data', './dom/event-handler', './base-component'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Alert = factory(global.SelectorEngine, global.Data, global.EventHandler, global.Base));
}(this, (function (SelectorEngine, Data, EventHandler, BaseComponent) { 'use strict';
2018-07-24 02:51:14 +02:00
2020-09-14 17:12:06 +02:00
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
2021-03-23 17:26:54 +01:00
const getSelector = element => {
let selector = element.getAttribute('data-bs-target');
2019-03-01 17:31:34 +01:00
if (!selector || selector === '#') {
2021-03-23 17:26:54 +01:00
let hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttr || !hrefAttr.includes('#') && !hrefAttr.startsWith('.')) {
return null;
} // Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
hrefAttr = `#${hrefAttr.split('#')[1]}`;
}
2019-08-27 15:03:21 +02:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 17:31:34 +01:00
}
2019-08-27 15:03:21 +02:00
return selector;
};
2021-03-23 17:26:54 +01:00
const getElementFromSelector = element => {
const selector = getSelector(element);
2019-08-27 15:03:21 +02:00
return selector ? document.querySelector(selector) : null;
2019-03-01 17:31:34 +01:00
};
2021-03-23 17:26:54 +01:00
const getjQuery = () => {
const {
jQuery
} = window;
2019-08-27 15:03:21 +02:00
2020-11-23 14:17:16 +01:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 15:03:21 +02:00
return jQuery;
}
return null;
};
2021-03-23 17:26:54 +01:00
const onDOMContentLoaded = callback => {
2020-11-11 18:07:37 +01:00
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
};
const defineJQueryPlugin = plugin => {
2021-03-23 17:26:54 +01:00
onDOMContentLoaded(() => {
const $ = getjQuery();
/* istanbul ignore if */
2020-12-03 15:18:59 +01:00
if ($) {
const name = plugin.NAME;
2021-03-23 17:26:54 +01:00
const JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
$.fn[name].noConflict = () => {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 15:18:59 +01:00
}
});
};
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.1): alert.js
2021-03-23 17:26:54 +01:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2020-12-03 15:18:59 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2021-03-23 17:26:54 +01:00
const NAME = 'alert';
const DATA_KEY = 'bs.alert';
const EVENT_KEY = `.${DATA_KEY}`;
const DATA_API_KEY = '.data-api';
const SELECTOR_DISMISS = '[data-bs-dismiss="alert"]';
const EVENT_CLOSE = `close${EVENT_KEY}`;
const EVENT_CLOSED = `closed${EVENT_KEY}`;
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
const CLASS_NAME_ALERT = 'alert';
const CLASS_NAME_FADE = 'fade';
const CLASS_NAME_SHOW = 'show';
2019-10-08 08:39:10 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2021-03-23 17:26:54 +01:00
class Alert extends BaseComponent__default['default'] {
// Getters
static get NAME() {
return NAME;
2021-03-23 17:26:54 +01:00
} // Public
2015-05-07 21:48:22 +02:00
2015-08-13 06:12:03 +02:00
2021-03-23 17:26:54 +01:00
close(element) {
const rootElement = element ? this._getRootElement(element) : this._element;
2015-05-08 02:07:38 +02:00
2021-03-23 17:26:54 +01:00
const customEvent = this._triggerCloseEvent(rootElement);
2015-05-07 21:48:22 +02:00
2019-03-01 17:31:34 +01:00
if (customEvent === null || customEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
this._removeElement(rootElement);
2019-01-04 17:29:45 +01:00
} // Private
2017-09-30 23:28:03 +02:00
2015-05-07 21:48:22 +02:00
2021-03-23 17:26:54 +01:00
_getRootElement(element) {
return getElementFromSelector(element) || element.closest(`.${CLASS_NAME_ALERT}`);
}
2015-05-07 21:48:22 +02:00
2021-03-23 17:26:54 +01:00
_triggerCloseEvent(element) {
return EventHandler__default['default'].trigger(element, EVENT_CLOSE);
}
2016-11-01 05:36:10 +01:00
2021-03-23 17:26:54 +01:00
_removeElement(element) {
element.classList.remove(CLASS_NAME_SHOW);
const isAnimated = element.classList.contains(CLASS_NAME_FADE);
2015-05-07 21:48:22 +02:00
this._queueCallback(() => this._destroyElement(element), element, isAnimated);
2021-03-23 17:26:54 +01:00
}
2015-05-07 21:48:22 +02:00
2021-03-23 17:26:54 +01:00
_destroyElement(element) {
2019-03-01 17:31:34 +01:00
if (element.parentNode) {
element.parentNode.removeChild(element);
}
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].trigger(element, EVENT_CLOSED);
2019-01-04 17:29:45 +01:00
} // Static
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
static jQueryInterface(config) {
2018-11-13 07:41:12 +01:00
return this.each(function () {
2021-03-23 17:26:54 +01:00
let data = Data__default['default'].get(this, DATA_KEY);
2017-09-06 06:05:12 +02:00
2018-11-13 07:41:12 +01:00
if (!data) {
data = new Alert(this);
}
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
if (config === 'close') {
data[config](this);
}
});
2021-03-23 17:26:54 +01:00
}
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
static handleDismiss(alertInstance) {
2018-11-13 07:41:12 +01:00
return function (event) {
if (event) {
event.preventDefault();
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
alertInstance.close(this);
2017-09-30 23:28:03 +02:00
};
2021-03-23 17:26:54 +01:00
}
2015-05-07 21:48:22 +02:00
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 21:48:22 +02:00
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()));
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 18:07:37 +01:00
* add .Alert to jQuery only if jQuery is present
2018-11-13 07:41:12 +01:00
*/
2017-09-30 23:28:03 +02:00
defineJQueryPlugin(Alert);
2015-05-07 21:48:22 +02:00
return Alert;
2018-07-24 02:51:14 +02:00
2019-11-08 09:11:23 +01:00
})));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=alert.js.map