0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/alert.js

261 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-01-03 22:58:44 +01:00
/** =======================================================================
* Bootstrap: alert.js v4.0.0
* http://getbootstrap.com/javascript/#alerts
2013-05-22 04:30:33 +02:00
* ========================================================================
2015-01-01 01:23:48 +01:00
* Copyright 2011-2015 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2015-01-03 22:58:44 +01:00
* ========================================================================
* @fileoverview - Bootstrap's generic alert component. Add dismiss
* functionality to all alert messages with this plugin.
*
* Public Methods & Properties:
*
* + $.alert
* + $.alert.noConflict
* + $.alert.Constructor
* + $.alert.Constructor.VERSION
* + $.alert.Constructor.prototype.close
*
* ========================================================================
*/
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
'use strict';
2014-06-10 07:18:05 +02:00
2015-01-03 22:58:44 +01:00
/**
* Our Alert class.
* @param {Element=} opt_element
* @constructor
*/
var Alert = function (opt_element) {
if (opt_element) {
$(opt_element).on('click', Alert._DISMISS_SELECTOR, Alert._handleDismiss(this))
}
2015-01-03 22:58:44 +01:00
}
2014-05-13 06:15:16 +02:00
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {string}
*/
Alert['VERSION'] = '4.0.0'
2012-01-28 10:35:13 +01:00
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {string}
* @private
*/
Alert._NAME = 'alert'
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {string}
* @private
*/
Alert._DATA_KEY = 'bs.alert'
/**
* @const
* @type {string}
* @private
*/
Alert._DISMISS_SELECTOR = '[data-dismiss="alert"]'
/**
* @const
* @type {number}
* @private
*/
Alert._TRANSITION_DURATION = 150
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {Function}
* @private
*/
Alert._JQUERY_NO_CONFLICT = $.fn[Alert._NAME]
2011-10-05 06:48:53 +02:00
2015-01-03 22:58:44 +01:00
/**
* @const
* @enum {string}
* @private
*/
Alert._Event = {
CLOSE : 'close.bs.alert',
CLOSED : 'closed.bs.alert'
}
2015-01-03 22:58:44 +01:00
/**
* @const
* @enum {string}
* @private
*/
Alert._ClassName = {
ALERT : 'alert',
FADE : 'fade',
IN : 'in'
}
/**
* Provides the jQuery Interface for the alert component.
* @param {string=} opt_config
* @this {jQuery}
* @return {jQuery}
* @private
*/
Alert._jQueryInterface = function (opt_config) {
return this.each(function () {
var $this = $(this)
var data = $this.data(Alert._DATA_KEY)
if (!data) {
data = new Alert(this)
$this.data(Alert._DATA_KEY, data)
}
2015-01-03 22:58:44 +01:00
if (opt_config === 'close') {
data[opt_config](this)
}
})
}
/**
* Close the alert component
* @param {Alert} alertInstance
* @return {Function}
* @private
*/
Alert._handleDismiss = function (alertInstance) {
return function (event) {
if (event) {
event.preventDefault()
}
alertInstance['close'](this)
}
2015-01-03 22:58:44 +01:00
}
/**
* Close the alert component
* @param {Element} element
*/
Alert.prototype['close'] = function (element) {
var rootElement = this._getRootElement(element)
var customEvent = this._triggerCloseEvent(rootElement)
if (customEvent.isDefaultPrevented()) return
2015-01-03 22:58:44 +01:00
this._removeElement(rootElement)
}
2015-01-03 22:58:44 +01:00
/**
* Tries to get the alert's root element
* @return {Element}
* @private
*/
Alert.prototype._getRootElement = function (element) {
var parent = false
var selector = Bootstrap.getSelectorFromElement(element)
2013-05-16 20:06:30 +02:00
2015-01-03 22:58:44 +01:00
if (selector) {
parent = $(selector)[0]
}
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
if (!parent) {
parent = $(element).closest('.' + Alert._ClassName.ALERT)[0]
}
return parent
}
2015-01-03 22:58:44 +01:00
/**
* Trigger close event on element
* @return {$.Event}
* @private
*/
Alert.prototype._triggerCloseEvent = function (element) {
var closeEvent = $.Event(Alert._Event.CLOSE)
$(element).trigger(closeEvent)
return closeEvent
}
2015-01-03 22:58:44 +01:00
/**
* Trigger closed event and remove element from dom
* @private
*/
Alert.prototype._removeElement = function (element) {
$(element).removeClass(Alert._ClassName.IN)
if (!Bootstrap.transition || !$(element).hasClass(Alert._ClassName.FADE)) {
this._destroyElement(element)
return
}
2015-01-03 22:58:44 +01:00
$(element)
.one(Bootstrap.TRANSITION_END, this._destroyElement.bind(this, element))
.emulateTransitionEnd(Alert._TRANSITION_DURATION)
}
/**
* clean up any lingering jquery data and kill element
* @private
*/
Alert.prototype._destroyElement = function (element) {
$(element)
.detach()
.trigger(Alert._Event.CLOSED)
.remove()
}
/**
* ------------------------------------------------------------------------
* jQuery Interface + noConflict implementaiton
* ------------------------------------------------------------------------
*/
/**
* @const
* @type {Function}
*/
$.fn[Alert._NAME] = Alert._jQueryInterface
/**
* @const
* @type {Function}
*/
$.fn[Alert._NAME]['Constructor'] = Alert
/**
* @return {Function}
*/
$.fn[Alert._NAME]['noConflict'] = function () {
$.fn[Alert._NAME] = Alert._JQUERY_NO_CONFLICT
return Alert._jQueryInterface
}
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2011-09-12 06:03:17 +02:00
2015-01-03 22:58:44 +01:00
$(document).on('click.bs.alert.data-api', Alert._DISMISS_SELECTOR, Alert._handleDismiss(new Alert))