1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/dist/alert.js

293 lines
8.6 KiB
JavaScript
Raw Normal View History

2018-11-13 01:41:12 -05:00
/*!
2019-02-13 11:01:40 -05:00
* Bootstrap alert.js v4.3.1 (https://getbootstrap.com/)
2019-01-04 11:29:45 -05:00
* Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2018-11-13 01:41:12 -05:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
2018-07-23 20:51:14 -04:00
(function (global, factory) {
2019-10-08 02:39:10 -04:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/selector-engine.js')) :
typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/event-handler.js', './dom/selector-engine.js'], factory) :
2019-03-01 11:31:34 -05:00
(global = global || self, global.Alert = factory(global.Data, global.EventHandler, global.SelectorEngine));
2019-11-08 03:11:23 -05:00
}(this, (function (Data, EventHandler, SelectorEngine) { 'use strict';
2018-07-23 20:51:14 -04:00
2019-03-01 11:31:34 -05:00
Data = Data && Data.hasOwnProperty('default') ? Data['default'] : Data;
EventHandler = EventHandler && EventHandler.hasOwnProperty('default') ? EventHandler['default'] : EventHandler;
SelectorEngine = SelectorEngine && SelectorEngine.hasOwnProperty('default') ? SelectorEngine['default'] : SelectorEngine;
2018-07-23 20:51:14 -04: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;
}
2017-09-06 00:05:12 -04:00
2019-03-01 11:31:34 -05:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): util/index.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
var MILLISECONDS_MULTIPLIER = 1000;
2019-08-27 09:03:21 -04:00
var TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-03-01 11:31:34 -05:00
2019-08-27 09:03:21 -04:00
var getSelector = function getSelector(element) {
2019-03-01 11:31:34 -05:00
var selector = element.getAttribute('data-target');
if (!selector || selector === '#') {
var hrefAttr = element.getAttribute('href');
2019-08-27 09:03:21 -04:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 11:31:34 -05:00
}
2019-08-27 09:03:21 -04:00
return selector;
};
var getElementFromSelector = function getElementFromSelector(element) {
var selector = getSelector(element);
return selector ? document.querySelector(selector) : null;
2019-03-01 11:31:34 -05: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;
var floatTransitionDuration = parseFloat(transitionDuration);
var floatTransitionDelay = parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
if (!floatTransitionDuration && !floatTransitionDelay) {
return 0;
} // If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0];
transitionDelay = transitionDelay.split(',')[0];
return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
};
var triggerTransitionEnd = function triggerTransitionEnd(element) {
2019-04-18 07:47:52 -04:00
var evt = document.createEvent('HTMLEvents');
evt.initEvent(TRANSITION_END, true, true);
element.dispatchEvent(evt);
2019-03-01 11:31:34 -05: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 09:03:21 -04:00
var getjQuery = function getjQuery() {
var _window = window,
jQuery = _window.jQuery;
if (jQuery && !document.body.hasAttribute('data-no-jquery')) {
return jQuery;
}
return null;
};
2018-11-13 01:41:12 -05:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'alert';
2019-02-13 11:01:40 -05:00
var VERSION = '4.3.1';
2018-11-13 01:41:12 -05:00
var DATA_KEY = 'bs.alert';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
var Selector = {
DISMISS: '[data-dismiss="alert"]'
};
2019-04-18 07:47:52 -04:00
var Event = {
2018-11-13 01:41:12 -05:00
CLOSE: "close" + EVENT_KEY,
CLOSED: "closed" + EVENT_KEY,
CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
};
var ClassName = {
ALERT: 'alert',
FADE: 'fade',
SHOW: 'show'
};
2019-10-08 02:39:10 -04:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2018-11-13 01:41:12 -05:00
var Alert =
/*#__PURE__*/
function () {
function Alert(element) {
this._element = element;
2019-03-01 11:31:34 -05:00
if (this._element) {
Data.setData(element, DATA_KEY, this);
}
2018-11-13 01:41:12 -05:00
} // Getters
2017-09-30 17:28:03 -04:00
2015-05-07 15:48:22 -04:00
2018-11-13 01:41:12 -05:00
var _proto = Alert.prototype;
2015-08-13 00:12:03 -04:00
2018-11-13 01:41:12 -05:00
// Public
_proto.close = function close(element) {
var rootElement = this._element;
2015-05-07 15:48:22 -04:00
2018-11-13 01:41:12 -05:00
if (element) {
rootElement = this._getRootElement(element);
}
2015-05-07 20:07:38 -04:00
2018-11-13 01:41:12 -05:00
var customEvent = this._triggerCloseEvent(rootElement);
2015-05-07 15:48:22 -04:00
2019-03-01 11:31:34 -05:00
if (customEvent === null || customEvent.defaultPrevented) {
2018-11-13 01:41:12 -05:00
return;
}
2016-10-09 20:26:51 -04:00
2018-11-13 01:41:12 -05:00
this._removeElement(rootElement);
};
2016-10-09 20:26:51 -04:00
2018-11-13 01:41:12 -05:00
_proto.dispose = function dispose() {
2019-03-01 11:31:34 -05:00
Data.removeData(this._element, DATA_KEY);
2018-11-13 01:41:12 -05:00
this._element = null;
2019-01-04 11:29:45 -05:00
} // Private
;
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
_proto._getRootElement = function _getRootElement(element) {
2019-08-27 09:03:21 -04:00
var parent = getElementFromSelector(element);
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
if (!parent) {
2019-03-01 11:31:34 -05:00
parent = SelectorEngine.closest(element, "." + ClassName.ALERT);
2018-11-13 01:41:12 -05:00
}
2015-05-07 15:48:22 -04:00
2018-11-13 01:41:12 -05:00
return parent;
};
2015-05-07 15:48:22 -04:00
2018-11-13 01:41:12 -05:00
_proto._triggerCloseEvent = function _triggerCloseEvent(element) {
2019-04-18 07:47:52 -04:00
return EventHandler.trigger(element, Event.CLOSE);
2018-11-13 01:41:12 -05:00
};
2015-05-07 15:48:22 -04:00
2018-11-13 01:41:12 -05:00
_proto._removeElement = function _removeElement(element) {
var _this = this;
2016-11-01 00:36:10 -04:00
2019-03-01 11:31:34 -05:00
element.classList.remove(ClassName.SHOW);
2015-05-07 15:48:22 -04:00
2019-03-01 11:31:34 -05:00
if (!element.classList.contains(ClassName.FADE)) {
2018-11-13 01:41:12 -05:00
this._destroyElement(element);
2015-08-18 23:28:28 -04:00
2018-11-13 01:41:12 -05:00
return;
}
2015-05-07 15:48:22 -04:00
2019-03-01 11:31:34 -05:00
var transitionDuration = getTransitionDurationFromElement(element);
EventHandler.one(element, TRANSITION_END, function () {
return _this._destroyElement(element);
2019-03-01 11:31:34 -05:00
});
emulateTransitionEnd(element, transitionDuration);
2018-11-13 01:41:12 -05:00
};
2015-05-07 15:48:22 -04:00
2018-11-13 01:41:12 -05:00
_proto._destroyElement = function _destroyElement(element) {
2019-03-01 11:31:34 -05:00
if (element.parentNode) {
element.parentNode.removeChild(element);
}
2019-04-18 07:47:52 -04:00
EventHandler.trigger(element, Event.CLOSED);
2019-01-04 11:29:45 -05:00
} // Static
;
2016-10-09 20:26:51 -04:00
2019-08-27 09:03:21 -04:00
Alert.jQueryInterface = function jQueryInterface(config) {
2018-11-13 01:41:12 -05:00
return this.each(function () {
2019-03-01 11:31:34 -05:00
var data = Data.getData(this, DATA_KEY);
2017-09-06 00:05:12 -04:00
2018-11-13 01:41:12 -05:00
if (!data) {
data = new Alert(this);
}
2016-10-09 20:26:51 -04:00
2018-11-13 01:41:12 -05:00
if (config === 'close') {
data[config](this);
}
});
};
2016-10-09 20:26:51 -04:00
2019-08-27 09:03:21 -04:00
Alert.handleDismiss = function handleDismiss(alertInstance) {
2018-11-13 01:41:12 -05:00
return function (event) {
if (event) {
event.preventDefault();
}
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
alertInstance.close(this);
2017-09-30 17:28:03 -04:00
};
2018-11-13 01:41:12 -05:00
};
2017-09-30 17:28:03 -04:00
2019-08-27 09:03:21 -04:00
Alert.getInstance = function getInstance(element) {
2019-03-01 11:31:34 -05:00
return Data.getData(element, DATA_KEY);
};
2018-11-13 01:41:12 -05:00
_createClass(Alert, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}]);
2015-05-07 15:48:22 -04:00
2018-11-13 01:41:12 -05:00
return Alert;
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 15:48:22 -04:00
2019-08-27 09:03:21 -04:00
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert.handleDismiss(new Alert()));
var $ = getjQuery();
2018-11-13 01:41:12 -05:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2019-03-01 11:31:34 -05:00
* add .alert to jQuery only if jQuery is present
2018-11-13 01:41:12 -05:00
*/
2017-09-30 17:28:03 -04:00
/* istanbul ignore if */
2019-08-27 09:03:21 -04:00
if ($) {
var JQUERY_NO_CONFLICT = $.fn[NAME];
$.fn[NAME] = Alert.jQueryInterface;
$.fn[NAME].Constructor = Alert;
2018-07-23 20:51:14 -04:00
2019-08-27 09:03:21 -04:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Alert.jQueryInterface;
2019-03-01 11:31:34 -05:00
};
}
2015-05-07 15:48:22 -04:00
return Alert;
2018-07-23 20:51:14 -04:00
2019-11-08 03:11:23 -05:00
})));
2018-07-23 20:51:14 -04:00
//# sourceMappingURL=alert.js.map