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

307 lines
10 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
2020-12-07 16:50:24 +01:00
* Bootstrap alert.js v5.0.0-beta1 (https://getbootstrap.com/)
2020-03-28 11:29:08 +01:00
* Copyright 2011-2020 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) {
2020-05-13 20:53:43 +02:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js')) :
2020-12-03 15:18:59 +01:00
typeof define === 'function' && define.amd ? define(['./dom/data', './dom/event-handler'], factory) :
2020-09-14 17:12:06 +02:00
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Alert = factory(global.Data, global.EventHandler));
2020-05-13 20:53:43 +02:00
}(this, (function (Data, EventHandler) { '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);
2018-07-24 02:51:14 +02:00
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
2020-12-07 16:50:24 +01:00
* Bootstrap (v5.0.0-beta1): util/index.js
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 17:31:34 +01:00
* --------------------------------------------------------------------------
*/
var MILLISECONDS_MULTIPLIER = 1000;
2019-08-27 15:03:21 +02:00
var TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-03-01 17:31:34 +01:00
2019-08-27 15:03:21 +02:00
var getSelector = function getSelector(element) {
2020-11-23 14:17:16 +01:00
var selector = element.getAttribute('data-bs-target');
2019-03-01 17:31:34 +01:00
if (!selector || selector === '#') {
var hrefAttr = element.getAttribute('href');
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;
};
var getElementFromSelector = function getElementFromSelector(element) {
var selector = getSelector(element);
return selector ? document.querySelector(selector) : null;
2019-03-01 17:31:34 +01: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 14:17:16 +01:00
var floatTransitionDuration = Number.parseFloat(transitionDuration);
var floatTransitionDelay = Number.parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
2019-03-01 17:31:34 +01: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 14:17:16 +01:00
return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
2019-03-01 17:31:34 +01:00
};
var triggerTransitionEnd = function triggerTransitionEnd(element) {
2020-03-28 11:29:08 +01:00
element.dispatchEvent(new Event(TRANSITION_END));
2019-03-01 17:31:34 +01: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 15:03:21 +02:00
var getjQuery = function getjQuery() {
var _window = window,
jQuery = _window.jQuery;
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;
};
2020-11-11 18:07:37 +01:00
var onDOMContentLoaded = function onDOMContentLoaded(callback) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
};
2020-12-03 14:08:31 +01:00
var isRTL = document.documentElement.dir === 'rtl';
2020-06-14 00:40:28 +02:00
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; }
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2020-12-07 16:50:24 +01:00
var VERSION = '5.0.0-beta1';
2020-12-03 15:18:59 +01:00
var BaseComponent = /*#__PURE__*/function () {
function BaseComponent(element) {
if (!element) {
return;
}
this._element = element;
Data__default['default'].setData(element, this.constructor.DATA_KEY, this);
}
var _proto = BaseComponent.prototype;
_proto.dispose = function dispose() {
Data__default['default'].removeData(this._element, this.constructor.DATA_KEY);
this._element = null;
}
/** Static */
;
BaseComponent.getInstance = function getInstance(element) {
return Data__default['default'].getData(element, this.DATA_KEY);
};
_createClass(BaseComponent, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}]);
return BaseComponent;
}();
function _defineProperties$1(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$1(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties$1(Constructor.prototype, protoProps); if (staticProps) _defineProperties$1(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'alert';
2018-11-13 07:41:12 +01:00
var DATA_KEY = 'bs.alert';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
2020-11-23 14:17:16 +01:00
var SELECTOR_DISMISS = '[data-bs-dismiss="alert"]';
2020-03-28 11:29:08 +01: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 CLASSNAME_ALERT = 'alert';
var CLASSNAME_FADE = 'fade';
var CLASSNAME_SHOW = 'show';
2019-10-08 08:39:10 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2020-12-03 15:18:59 +01:00
var Alert = /*#__PURE__*/function (_BaseComponent) {
_inheritsLoose(Alert, _BaseComponent);
2017-09-30 23:28:03 +02:00
2020-12-03 15:18:59 +01:00
function Alert() {
return _BaseComponent.apply(this, arguments) || this;
}
2015-05-07 21:48:22 +02:00
2018-11-13 07:41:12 +01:00
var _proto = Alert.prototype;
2015-08-13 06:12:03 +02:00
2018-11-13 07:41:12 +01:00
// Public
_proto.close = function close(element) {
2020-09-14 17:12:06 +02:00
var rootElement = element ? this._getRootElement(element) : this._element;
2015-05-08 02:07:38 +02:00
2018-11-13 07:41:12 +01:00
var 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
2018-11-13 07:41:12 +01:00
_proto._getRootElement = function _getRootElement(element) {
2020-05-13 20:53:43 +02:00
return getElementFromSelector(element) || element.closest("." + CLASSNAME_ALERT);
2018-11-13 07:41:12 +01:00
};
2015-05-07 21:48:22 +02:00
2018-11-13 07:41:12 +01:00
_proto._triggerCloseEvent = function _triggerCloseEvent(element) {
2020-09-14 17:12:06 +02:00
return EventHandler__default['default'].trigger(element, EVENT_CLOSE);
2018-11-13 07:41:12 +01:00
};
2015-05-07 21:48:22 +02:00
2018-11-13 07:41:12 +01:00
_proto._removeElement = function _removeElement(element) {
var _this = this;
2016-11-01 05:36:10 +01:00
2020-03-28 11:29:08 +01:00
element.classList.remove(CLASSNAME_SHOW);
2015-05-07 21:48:22 +02:00
2020-03-28 11:29:08 +01:00
if (!element.classList.contains(CLASSNAME_FADE)) {
2018-11-13 07:41:12 +01:00
this._destroyElement(element);
2015-08-19 05:28:28 +02:00
2018-11-13 07:41:12 +01:00
return;
}
2015-05-07 21:48:22 +02:00
2019-03-01 17:31:34 +01:00
var transitionDuration = getTransitionDurationFromElement(element);
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].one(element, TRANSITION_END, function () {
return _this._destroyElement(element);
2019-03-01 17:31:34 +01:00
});
emulateTransitionEnd(element, transitionDuration);
2018-11-13 07:41:12 +01:00
};
2015-05-07 21:48:22 +02:00
2018-11-13 07:41:12 +01:00
_proto._destroyElement = function _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
2019-08-27 15:03:21 +02:00
Alert.jQueryInterface = function jQueryInterface(config) {
2018-11-13 07:41:12 +01:00
return this.each(function () {
2020-09-14 17:12:06 +02:00
var data = Data__default['default'].getData(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);
}
});
};
2016-10-10 02:26:51 +02:00
2019-08-27 15:03:21 +02:00
Alert.handleDismiss = function 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
};
2018-11-13 07:41:12 +01:00
};
2017-09-30 23:28:03 +02:00
2020-12-03 15:18:59 +01:00
_createClass$1(Alert, null, [{
key: "DATA_KEY",
// Getters
2018-11-13 07:41:12 +01:00
get: function get() {
2020-12-03 15:18:59 +01:00
return DATA_KEY;
2018-11-13 07:41:12 +01:00
}
}]);
2015-05-07 21:48:22 +02:00
2018-11-13 07:41:12 +01:00
return Alert;
2020-12-03 15:18:59 +01:00
}(BaseComponent);
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
2020-11-11 18:07:37 +01:00
onDOMContentLoaded(function () {
var $ = getjQuery();
/* istanbul ignore if */
2020-11-11 18:07:37 +01:00
if ($) {
var JQUERY_NO_CONFLICT = $.fn[NAME];
$.fn[NAME] = Alert.jQueryInterface;
$.fn[NAME].Constructor = Alert;
2018-07-24 02:51:14 +02:00
2020-11-11 18:07:37 +01:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Alert.jQueryInterface;
};
}
});
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