0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-12 00:08:59 +01:00
Bootstrap/dist/js/umd/alert.js

212 lines
6.2 KiB
JavaScript
Raw Normal View History

2015-05-13 19:13:34 +02:00
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports', 'module', './util'], factory);
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
factory(exports, module, require('./util'));
} else {
var mod = {
exports: {}
};
factory(mod.exports, mod, global.Util);
global.alert = mod.exports;
}
})(this, function (exports, module, _util) {
'use strict';
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; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
2015-05-13 19:13:34 +02:00
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _Util = _interopRequireDefault(_util);
2015-05-13 19:13:34 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.0.0-alpha.2): alert.js
2015-05-13 19:13:34 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
var Alert = (function ($) {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'alert';
2015-12-24 11:50:15 +01:00
var VERSION = '4.0.0-alpha.2';
2015-05-13 19:13:34 +02:00
var DATA_KEY = 'bs.alert';
2015-05-13 21:48:34 +02:00
var EVENT_KEY = '.' + DATA_KEY;
var DATA_API_KEY = '.data-api';
2015-05-13 19:13:34 +02:00
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
2015-05-13 19:13:34 +02:00
};
var ClassName = {
ALERT: 'alert',
FADE: 'fade',
IN: 'in'
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
var Alert = (function () {
function Alert(element) {
_classCallCheck(this, Alert);
this._element = element;
}
2015-08-13 06:12:03 +02:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
// getters
2015-05-13 19:13:34 +02:00
_createClass(Alert, [{
key: 'close',
// public
value: function close(element) {
element = element || this._element;
var rootElement = this._getRootElement(element);
var customEvent = this._triggerCloseEvent(rootElement);
if (customEvent.isDefaultPrevented()) {
return;
}
this._removeElement(rootElement);
}
2015-05-13 21:48:34 +02:00
}, {
key: 'dispose',
value: function dispose() {
$.removeData(this._element, DATA_KEY);
this._element = null;
}
2015-05-13 19:13:34 +02:00
// private
2015-08-13 06:12:03 +02:00
}, {
key: '_getRootElement',
2015-05-13 19:13:34 +02:00
value: function _getRootElement(element) {
var selector = _Util['default'].getSelectorFromElement(element);
2015-08-19 04:22:46 +02:00
var parent = false;
2015-05-13 19:13:34 +02:00
if (selector) {
parent = $(selector)[0];
}
if (!parent) {
parent = $(element).closest('.' + ClassName.ALERT)[0];
}
return parent;
}
}, {
key: '_triggerCloseEvent',
value: function _triggerCloseEvent(element) {
var closeEvent = $.Event(Event.CLOSE);
2015-08-19 04:22:46 +02:00
2015-05-13 19:13:34 +02:00
$(element).trigger(closeEvent);
return closeEvent;
}
}, {
key: '_removeElement',
value: function _removeElement(element) {
$(element).removeClass(ClassName.IN);
if (!_Util['default'].supportsTransitionEnd() || !$(element).hasClass(ClassName.FADE)) {
2015-05-13 19:13:34 +02:00
this._destroyElement(element);
return;
}
2015-08-19 02:01:56 +02:00
$(element).one(_Util['default'].TRANSITION_END, $.proxy(this._destroyElement, this, element)).emulateTransitionEnd(TRANSITION_DURATION);
2015-05-13 19:13:34 +02:00
}
}, {
key: '_destroyElement',
value: function _destroyElement(element) {
$(element).detach().trigger(Event.CLOSED).remove();
}
// static
2015-08-13 06:12:03 +02:00
}], [{
key: '_jQueryInterface',
2015-05-13 19:13:34 +02:00
value: function _jQueryInterface(config) {
return this.each(function () {
var $element = $(this);
var data = $element.data(DATA_KEY);
if (!data) {
data = new Alert(this);
$element.data(DATA_KEY, data);
}
if (config === 'close') {
data[config](this);
}
});
}
}, {
key: '_handleDismiss',
value: function _handleDismiss(alertInstance) {
return function (event) {
if (event) {
event.preventDefault();
}
alertInstance.close(this);
};
}
2015-06-19 00:48:54 +02:00
}, {
key: 'VERSION',
2015-08-13 06:12:03 +02:00
get: function get() {
2015-06-19 00:48:54 +02:00
return VERSION;
}
2015-05-13 19:13:34 +02:00
}]);
return Alert;
})();
2015-05-13 21:48:34 +02:00
$(document).on(Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()));
2015-05-13 19:13:34 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Alert._jQueryInterface;
$.fn[NAME].Constructor = Alert;
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Alert._jQueryInterface;
};
return Alert;
})(jQuery);
module.exports = Alert;
2015-06-19 00:48:54 +02:00
});