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

197 lines
5.3 KiB
JavaScript
Raw Normal View History

2017-09-06 06:05:12 +02:00
'use strict';
2016-10-10 02:26:51 +02:00
var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
2015-05-07 21:48:22 +02:00
2016-10-10 02:26:51 +02:00
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2015-05-07 21:48:22 +02:00
/**
* --------------------------------------------------------------------------
2017-08-11 05:56:35 +02:00
* Bootstrap (v4.0.0-beta): alert.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2015-05-07 21:48:22 +02:00
* --------------------------------------------------------------------------
*/
2017-09-06 06:05:12 +02:00
var Alert = function () {
2015-05-07 21:48:22 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'alert';
2017-08-11 05:56:35 +02:00
var VERSION = '4.0.0-beta';
var DATA_KEY = 'bs.alert';
2015-05-13 21:48:34 +02:00
var EVENT_KEY = '.' + DATA_KEY;
var DATA_API_KEY = '.data-api';
var JQUERY_NO_CONFLICT = $.fn[NAME];
var TRANSITION_DURATION = 150;
var Selector = {
DISMISS: '[data-dismiss="alert"]'
};
var Event = {
2015-05-13 21:48:34 +02:00
CLOSE: 'close' + EVENT_KEY,
CLOSED: 'closed' + EVENT_KEY,
2015-08-13 06:12:03 +02:00
CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY
};
var ClassName = {
ALERT: 'alert',
FADE: 'fade',
2016-12-20 06:48:24 +01:00
SHOW: 'show'
2017-08-20 22:36:58 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2017-08-20 22:36:58 +02:00
};
2016-10-10 02:26:51 +02:00
var Alert = function () {
function Alert(element) {
_classCallCheck(this, Alert);
2015-05-08 07:26:40 +02:00
this._element = element;
2015-05-07 21:48:22 +02:00
}
2015-08-13 06:12:03 +02:00
// getters
2017-09-06 06:05:12 +02:00
_createClass(Alert, [{
key: 'close',
2015-05-07 21:48:22 +02:00
2015-05-08 02:07:38 +02:00
2017-09-06 06:05:12 +02:00
// public
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
value: function close(element) {
element = element || this._element;
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
var rootElement = this._getRootElement(element);
var customEvent = this._triggerCloseEvent(rootElement);
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
if (customEvent.isDefaultPrevented()) {
return;
}
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
this._removeElement(rootElement);
2015-05-07 21:48:22 +02:00
}
2017-09-06 06:05:12 +02:00
}, {
key: 'dispose',
value: function dispose() {
$.removeData(this._element, DATA_KEY);
this._element = null;
2015-05-13 21:48:34 +02:00
}
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
// private
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
}, {
key: '_getRootElement',
value: function _getRootElement(element) {
var selector = Util.getSelectorFromElement(element);
var parent = false;
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
if (selector) {
parent = $(selector)[0];
}
2016-11-01 05:36:10 +01:00
2017-09-06 06:05:12 +02:00
if (!parent) {
parent = $(element).closest('.' + ClassName.ALERT)[0];
}
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
return parent;
}
2017-09-06 06:05:12 +02:00
}, {
key: '_triggerCloseEvent',
value: function _triggerCloseEvent(element) {
var closeEvent = $.Event(Event.CLOSE);
2015-08-19 05:28:28 +02:00
2017-09-06 06:05:12 +02:00
$(element).trigger(closeEvent);
return closeEvent;
}
}, {
key: '_removeElement',
value: function _removeElement(element) {
var _this = this;
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
$(element).removeClass(ClassName.SHOW);
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
if (!Util.supportsTransitionEnd() || !$(element).hasClass(ClassName.FADE)) {
this._destroyElement(element);
return;
2016-10-10 02:26:51 +02:00
}
2015-05-07 21:48:22 +02:00
2017-09-06 06:05:12 +02:00
$(element).one(Util.TRANSITION_END, function (event) {
return _this._destroyElement(element, event);
}).emulateTransitionEnd(TRANSITION_DURATION);
}
}, {
key: '_destroyElement',
value: function _destroyElement(element) {
$(element).detach().trigger(Event.CLOSED).remove();
}
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
// static
}], [{
key: '_jQueryInterface',
value: function _jQueryInterface(config) {
return this.each(function () {
var $element = $(this);
var data = $element.data(DATA_KEY);
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
if (!data) {
data = new Alert(this);
$element.data(DATA_KEY, data);
}
2016-10-10 02:26:51 +02:00
2017-09-06 06:05:12 +02:00
if (config === 'close') {
data[config](this);
}
});
}
}, {
key: '_handleDismiss',
value: function _handleDismiss(alertInstance) {
return function (event) {
if (event) {
event.preventDefault();
}
alertInstance.close(this);
};
}
}, {
2015-08-13 06:12:03 +02:00
key: 'VERSION',
get: function get() {
return VERSION;
}
}]);
2015-05-07 21:48:22 +02:00
return Alert;
2016-10-10 02:26:51 +02:00
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-07 21:48:22 +02:00
2017-07-02 19:40:27 +02:00
$(document).on(Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()));
2015-05-07 21:48:22 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2015-05-07 21:48:22 +02:00
2017-07-02 19:40:27 +02:00
$.fn[NAME] = Alert._jQueryInterface;
$.fn[NAME].Constructor = Alert;
$.fn[NAME].noConflict = function () {
2015-05-08 02:07:38 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Alert._jQueryInterface;
};
2015-05-07 21:48:22 +02:00
return Alert;
2016-10-10 02:26:51 +02:00
}(jQuery);
2017-04-22 08:58:09 +02:00
//# sourceMappingURL=alert.js.map