0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-23 15:52:21 +01:00
Bootstrap/js/dist/alert.js

308 lines
9.9 KiB
JavaScript
Raw Normal View History

2018-11-13 08:41:12 +02:00
/*!
* Bootstrap alert.js v5.0.0-beta2 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 21:50:01 +03:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 08:41:12 +02:00
*/
2018-07-23 17:51:14 -07:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js'), require('./base-component.js')) :
typeof define === 'function' && define.amd ? define(['./dom/data', './dom/event-handler', './base-component'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Alert = factory(global.Data, global.EventHandler, global.Base));
}(this, (function (Data, EventHandler, BaseComponent) { 'use strict';
2018-07-23 17:51:14 -07:00
2020-09-14 18:12:06 +03: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);
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
2018-07-23 17:51:14 -07:00
2019-03-01 18:31:34 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.0-beta2): util/index.js
2020-06-16 21:50:01 +03:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 18:31:34 +02:00
* --------------------------------------------------------------------------
*/
var MILLISECONDS_MULTIPLIER = 1000;
2019-08-27 16:03:21 +03:00
var TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-03-01 18:31:34 +02:00
2019-08-27 16:03:21 +03:00
var getSelector = function getSelector(element) {
2020-11-23 15:17:16 +02:00
var selector = element.getAttribute('data-bs-target');
2019-03-01 18:31:34 +02:00
if (!selector || selector === '#') {
var 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 16:03:21 +03:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 18:31:34 +02:00
}
2019-08-27 16:03:21 +03:00
return selector;
};
var getElementFromSelector = function getElementFromSelector(element) {
var selector = getSelector(element);
return selector ? document.querySelector(selector) : null;
2019-03-01 18:31:34 +02:00
};
var getTransitionDurationFromElement = function getTransitionDurationFromElement(element) {
if (!element) {
return 0;
} // Get transition-duration of the element
var _window$getComputedSt = window.getComputedStyle(element),
transitionDuration = _window$getComputedSt.transitionDuration,
transitionDelay = _window$getComputedSt.transitionDelay;
2020-11-23 15:17:16 +02:00
var floatTransitionDuration = Number.parseFloat(transitionDuration);
var floatTransitionDelay = Number.parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
2019-03-01 18:31:34 +02:00
if (!floatTransitionDuration && !floatTransitionDelay) {
return 0;
} // If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0];
transitionDelay = transitionDelay.split(',')[0];
2020-11-23 15:17:16 +02:00
return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
2019-03-01 18:31:34 +02:00
};
var triggerTransitionEnd = function triggerTransitionEnd(element) {
2020-03-28 12:29:08 +02:00
element.dispatchEvent(new Event(TRANSITION_END));
2019-03-01 18:31:34 +02:00
};
var emulateTransitionEnd = function emulateTransitionEnd(element, duration) {
var called = false;
var durationPadding = 5;
var emulatedDuration = duration + durationPadding;
function listener() {
called = true;
element.removeEventListener(TRANSITION_END, listener);
}
element.addEventListener(TRANSITION_END, listener);
setTimeout(function () {
if (!called) {
triggerTransitionEnd(element);
}
}, emulatedDuration);
};
2019-08-27 16:03:21 +03:00
var getjQuery = function getjQuery() {
var _window = window,
jQuery = _window.jQuery;
2020-11-23 15:17:16 +02:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 16:03:21 +03:00
return jQuery;
}
return null;
};
2020-11-11 19:07:37 +02:00
var onDOMContentLoaded = function onDOMContentLoaded(callback) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
};
document.documentElement.dir === 'rtl';
2020-12-03 16:18:59 +02:00
var defineJQueryPlugin = function defineJQueryPlugin(name, plugin) {
onDOMContentLoaded(function () {
var $ = getjQuery();
/* istanbul ignore if */
2020-12-03 16:18:59 +02:00
if ($) {
var JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2020-12-03 16:18:59 +02:00
$.fn[name].noConflict = function () {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 16:18:59 +02:00
}
});
};
2020-12-03 16:18:59 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'alert';
2018-11-13 08:41:12 +02:00
var DATA_KEY = 'bs.alert';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
2020-11-23 15:17:16 +02:00
var SELECTOR_DISMISS = '[data-bs-dismiss="alert"]';
2020-03-28 12:29:08 +02:00
var EVENT_CLOSE = "close" + EVENT_KEY;
var EVENT_CLOSED = "closed" + EVENT_KEY;
var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
var CLASS_NAME_ALERT = 'alert';
var CLASS_NAME_FADE = 'fade';
var CLASS_NAME_SHOW = 'show';
2019-10-08 09:39:10 +03:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2020-12-03 16:18:59 +02:00
var Alert = /*#__PURE__*/function (_BaseComponent) {
_inheritsLoose(Alert, _BaseComponent);
2017-09-30 14:28:03 -07:00
2020-12-03 16:18:59 +02:00
function Alert() {
return _BaseComponent.apply(this, arguments) || this;
}
2015-05-07 12:48:22 -07:00
2018-11-13 08:41:12 +02:00
var _proto = Alert.prototype;
2015-08-12 21:12:03 -07:00
2018-11-13 08:41:12 +02:00
// Public
_proto.close = function close(element) {
2020-09-14 18:12:06 +03:00
var rootElement = element ? this._getRootElement(element) : this._element;
2015-05-07 17:07:38 -07:00
2018-11-13 08:41:12 +02:00
var customEvent = this._triggerCloseEvent(rootElement);
2015-05-07 12:48:22 -07:00
2019-03-01 18:31:34 +02:00
if (customEvent === null || customEvent.defaultPrevented) {
2018-11-13 08:41:12 +02:00
return;
}
2016-10-09 17:26:51 -07:00
2018-11-13 08:41:12 +02:00
this._removeElement(rootElement);
2019-01-04 08:29:45 -08:00
} // Private
;
2017-09-30 14:28:03 -07:00
2018-11-13 08:41:12 +02:00
_proto._getRootElement = function _getRootElement(element) {
return getElementFromSelector(element) || element.closest("." + CLASS_NAME_ALERT);
2018-11-13 08:41:12 +02:00
};
2015-05-07 12:48:22 -07:00
2018-11-13 08:41:12 +02:00
_proto._triggerCloseEvent = function _triggerCloseEvent(element) {
2020-09-14 18:12:06 +03:00
return EventHandler__default['default'].trigger(element, EVENT_CLOSE);
2018-11-13 08:41:12 +02:00
};
2015-05-07 12:48:22 -07:00
2018-11-13 08:41:12 +02:00
_proto._removeElement = function _removeElement(element) {
var _this = this;
2016-10-31 21:36:10 -07:00
element.classList.remove(CLASS_NAME_SHOW);
2015-05-07 12:48:22 -07:00
if (!element.classList.contains(CLASS_NAME_FADE)) {
2018-11-13 08:41:12 +02:00
this._destroyElement(element);
2015-08-18 20:28:28 -07:00
2018-11-13 08:41:12 +02:00
return;
}
2015-05-07 12:48:22 -07:00
2019-03-01 18:31:34 +02:00
var transitionDuration = getTransitionDurationFromElement(element);
EventHandler__default['default'].one(element, 'transitionend', function () {
return _this._destroyElement(element);
2019-03-01 18:31:34 +02:00
});
emulateTransitionEnd(element, transitionDuration);
2018-11-13 08:41:12 +02:00
};
2015-05-07 12:48:22 -07:00
2018-11-13 08:41:12 +02:00
_proto._destroyElement = function _destroyElement(element) {
2019-03-01 18:31:34 +02:00
if (element.parentNode) {
element.parentNode.removeChild(element);
}
2020-09-14 18:12:06 +03:00
EventHandler__default['default'].trigger(element, EVENT_CLOSED);
2019-01-04 08:29:45 -08:00
} // Static
;
2016-10-09 17:26:51 -07:00
2019-08-27 16:03:21 +03:00
Alert.jQueryInterface = function jQueryInterface(config) {
2018-11-13 08:41:12 +02:00
return this.each(function () {
2020-09-14 18:12:06 +03:00
var data = Data__default['default'].getData(this, DATA_KEY);
2017-09-05 21:05:12 -07:00
2018-11-13 08:41:12 +02:00
if (!data) {
data = new Alert(this);
}
2016-10-09 17:26:51 -07:00
2018-11-13 08:41:12 +02:00
if (config === 'close') {
data[config](this);
}
});
};
2016-10-09 17:26:51 -07:00
2019-08-27 16:03:21 +03:00
Alert.handleDismiss = function handleDismiss(alertInstance) {
2018-11-13 08:41:12 +02:00
return function (event) {
if (event) {
event.preventDefault();
}
2017-09-30 14:28:03 -07:00
2018-11-13 08:41:12 +02:00
alertInstance.close(this);
2017-09-30 14:28:03 -07:00
};
2018-11-13 08:41:12 +02:00
};
2017-09-30 14:28:03 -07:00
_createClass(Alert, null, [{
2020-12-03 16:18:59 +02:00
key: "DATA_KEY",
get: // Getters
function get() {
2020-12-03 16:18:59 +02:00
return DATA_KEY;
2018-11-13 08:41:12 +02:00
}
}]);
2015-05-07 12:48:22 -07:00
2018-11-13 08:41:12 +02:00
return Alert;
}(BaseComponent__default['default']);
2018-11-13 08:41:12 +02:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 12:48:22 -07:00
2020-09-14 18:12:06 +03:00
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()));
2018-11-13 08:41:12 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 19:07:37 +02:00
* add .Alert to jQuery only if jQuery is present
2018-11-13 08:41:12 +02:00
*/
2017-09-30 14:28:03 -07:00
defineJQueryPlugin(NAME, Alert);
2015-05-07 12:48:22 -07:00
return Alert;
2018-07-23 17:51:14 -07:00
2019-11-08 10:11:23 +02:00
})));
2018-07-23 17:51:14 -07:00
//# sourceMappingURL=alert.js.map