diff options
| author | fat <[email protected]> | 2015-05-06 13:34:14 -0700 |
|---|---|---|
| committer | fat <[email protected]> | 2015-05-06 13:34:14 -0700 |
| commit | d1fbe200f46002431cdeebf965c4b789ef7ed267 (patch) | |
| tree | 43a7cc7667492e519b906f8a428935da2972ac14 /js/alert.js | |
| parent | 09fb80568a52af6c440db971cdc6fd88eab8f8b5 (diff) | |
| download | bootstrap-d1fbe200f46002431cdeebf965c4b789ef7ed267.tar.xz bootstrap-d1fbe200f46002431cdeebf965c4b789ef7ed267.zip | |
remove closureness from plugins
Diffstat (limited to 'js/alert.js')
| -rw-r--r-- | js/alert.js | 280 |
1 files changed, 57 insertions, 223 deletions
diff --git a/js/alert.js b/js/alert.js index 38bce06a4..1925ef011 100644 --- a/js/alert.js +++ b/js/alert.js @@ -1,260 +1,94 @@ -/** ======================================================================= - * Bootstrap: alert.js v4.0.0 +/* ======================================================================== + * Bootstrap: alert.js v3.3.4 * http://getbootstrap.com/javascript/#alerts * ======================================================================== * Copyright 2011-2015 Twitter, Inc. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) - * ======================================================================== - * @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 - * - * ======================================================================== - */ - -'use strict'; - - -/** - * 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)) - } -} - - -/** - * @const - * @type {string} - */ -Alert['VERSION'] = '4.0.0' - - -/** - * @const - * @type {string} - * @private - */ -Alert._NAME = 'alert' - - -/** - * @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 - + * ======================================================================== */ -/** - * @const - * @type {Function} - * @private - */ -Alert._JQUERY_NO_CONFLICT = $.fn[Alert._NAME] ++function ($) { + 'use strict'; -/** - * @const - * @enum {string} - * @private - */ -Alert._Event = { - CLOSE : 'close.bs.alert', - CLOSED : 'closed.bs.alert' -} + // ALERT CLASS DEFINITION + // ====================== + var dismiss = '[data-dismiss="alert"]' + var Alert = function (el) { + $(el).on('click', dismiss, this.close) + } -/** - * @const - * @enum {string} - * @private - */ -Alert._ClassName = { - ALERT : 'alert', - FADE : 'fade', - IN : 'in' -} + Alert.VERSION = '3.3.4' + Alert.TRANSITION_DURATION = 150 -/** - * 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) + Alert.prototype.close = function (e) { + var $this = $(this) + var selector = $this.attr('data-target') - if (!data) { - data = new Alert(this) - $this.data(Alert._DATA_KEY, data) + if (!selector) { + selector = $this.attr('href') + selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7 } - if (opt_config === 'close') { - data[opt_config](this) - } - }) -} + var $parent = $(selector) + if (e) e.preventDefault() -/** - * Close the alert component - * @param {Alert} alertInstance - * @return {Function} - * @private - */ -Alert._handleDismiss = function (alertInstance) { - return function (event) { - if (event) { - event.preventDefault() + if (!$parent.length) { + $parent = $this.closest('.alert') } - alertInstance['close'](this) - } -} - + $parent.trigger(e = $.Event('close.bs.alert')) -/** - * Close the alert component - * @param {Element} element - */ -Alert.prototype['close'] = function (element) { - var rootElement = this._getRootElement(element) - var customEvent = this._triggerCloseEvent(rootElement) + if (e.isDefaultPrevented()) return - if (customEvent.isDefaultPrevented()) return + $parent.removeClass('in') - this._removeElement(rootElement) -} - - -/** - * Tries to get the alert's root element - * @return {Element} - * @private - */ -Alert.prototype._getRootElement = function (element) { - var parent = false - var selector = Bootstrap.getSelectorFromElement(element) - - if (selector) { - parent = $(selector)[0] - } + function removeElement() { + // detach from parent, fire event then clean up data + $parent.detach().trigger('closed.bs.alert').remove() + } - if (!parent) { - parent = $(element).closest('.' + Alert._ClassName.ALERT)[0] + $.support.transition && $parent.hasClass('fade') ? + $parent + .one('bsTransitionEnd', removeElement) + .emulateTransitionEnd(Alert.TRANSITION_DURATION) : + removeElement() } - return parent -} - -/** - * Trigger close event on element - * @return {$.Event} - * @private - */ -Alert.prototype._triggerCloseEvent = function (element) { - var closeEvent = $.Event(Alert._Event.CLOSE) - $(element).trigger(closeEvent) - return closeEvent -} + // ALERT PLUGIN DEFINITION + // ======================= + function Plugin(option) { + return this.each(function () { + var $this = $(this) + var data = $this.data('bs.alert') -/** - * 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 + if (!data) $this.data('bs.alert', (data = new Alert(this))) + if (typeof option == 'string') data[option].call($this) + }) } - $(element) - .one(Bootstrap.TRANSITION_END, this._destroyElement.bind(this, element)) - .emulateTransitionEnd(Alert._TRANSITION_DURATION) -} - + var old = $.fn.alert -/** - * clean up any lingering jquery data and kill element - * @private - */ -Alert.prototype._destroyElement = function (element) { - $(element) - .detach() - .trigger(Alert._Event.CLOSED) - .remove() -} + $.fn.alert = Plugin + $.fn.alert.Constructor = Alert -/** - * ------------------------------------------------------------------------ - * jQuery Interface + noConflict implementaiton - * ------------------------------------------------------------------------ - */ - -/** - * @const - * @type {Function} - */ -$.fn[Alert._NAME] = Alert._jQueryInterface - - -/** - * @const - * @type {Function} - */ -$.fn[Alert._NAME]['Constructor'] = Alert + // ALERT NO CONFLICT + // ================= + $.fn.alert.noConflict = function () { + $.fn.alert = old + return this + } -/** - * @return {Function} - */ -$.fn[Alert._NAME]['noConflict'] = function () { - $.fn[Alert._NAME] = Alert._JQUERY_NO_CONFLICT - return Alert._jQueryInterface -} + // ALERT DATA-API + // ============== -/** - * ------------------------------------------------------------------------ - * Data Api implementation - * ------------------------------------------------------------------------ - */ + $(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close) -$(document).on('click.bs.alert.data-api', Alert._DISMISS_SELECTOR, Alert._handleDismiss(new Alert)) +}(jQuery); |
