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

599 lines
13 KiB
JavaScript
Raw Normal View History

2015-01-03 22:58:44 +01:00
/** =======================================================================
* Bootstrap: modal.js v4.0.0
* http://getbootstrap.com/javascript/#modal
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 modal plugin. Modals are streamlined, but
* flexible, dialog prompts with the minimum required functionality and
* smart defaults.
*
* Public Methods & Properties:
*
* + $.modal
* + $.modal.noConflict
* + $.modal.Constructor
* + $.modal.Constructor.VERSION
* + $.modal.Constructor.Defaults
* + $.modal.Constructor.Defaults.backdrop
* + $.modal.Constructor.Defaults.keyboard
* + $.modal.Constructor.Defaults.show
* + $.modal.Constructor.prototype.toggle
* + $.modal.Constructor.prototype.show
* + $.modal.Constructor.prototype.hide
*
* ========================================================================
*/
2011-09-11 07:24:31 +02:00
2015-01-03 22:58:44 +01:00
'use strict';
2011-09-11 07:24:31 +02:00
2015-01-03 22:58:44 +01:00
/**
* Our modal class.
* @param {Element} element
* @param {Object} config
* @constructor
*/
var Modal = function (element, config) {
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
/** @private {Object} */
this._config = config
/** @private {Element} */
this._element = element
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
/** @private {Element} */
this._backdrop = null
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
/** @private {boolean} */
this._isShown = false
/** @private {boolean} */
this._isBodyOverflowing = false
/** @private {number} */
this._scrollbarWidth = 0
2014-05-13 06:15:16 +02:00
2015-01-03 22:58:44 +01:00
}
2011-12-01 07:42:22 +01:00
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {string}
*/
Modal['VERSION'] = '4.0.0'
/**
* @const
* @type {Object}
*/
Modal['Defaults'] = {
'backdrop' : true,
'keyboard' : true,
'show' : true
}
/**
* @const
* @type {string}
* @private
*/
Modal._NAME = 'modal'
/**
* @const
* @type {string}
* @private
*/
Modal._DATA_KEY = 'bs.modal'
/**
* @const
* @type {number}
* @private
*/
Modal._TRANSITION_DURATION = 300
/**
* @const
* @type {number}
* @private
*/
Modal._BACKDROP_TRANSITION_DURATION = 150
/**
* @const
* @type {Function}
* @private
*/
Modal._JQUERY_NO_CONFLICT = $.fn[Modal._NAME]
/**
* @const
* @enum {string}
* @private
*/
Modal._Event = {
HIDE : 'hide.bs.modal',
HIDDEN : 'hidden.bs.modal',
SHOW : 'show.bs.modal',
SHOWN : 'shown.bs.modal'
}
/**
* @const
* @enum {string}
* @private
*/
Modal._ClassName = {
BACKDROP : 'modal-backdrop',
OPEN : 'modal-open',
FADE : 'fade',
IN : 'in'
}
/**
* @const
* @enum {string}
* @private
*/
Modal._Selector = {
DIALOG : '.modal-dialog',
DATA_TOGGLE : '[data-toggle="modal"]',
DATA_DISMISS : '[data-dismiss="modal"]',
SCROLLBAR_MEASURER : 'modal-scrollbar-measure'
}
/**
* Provides the jQuery Interface for the alert component.
* @param {Object|string=} opt_config
* @param {Element=} opt_relatedTarget
* @this {jQuery}
* @return {jQuery}
* @private
*/
Modal._jQueryInterface = function Plugin(opt_config, opt_relatedTarget) {
return this.each(function () {
var data = $(this).data(Modal._DATA_KEY)
var config = $.extend({}, Modal['Defaults'], $(this).data(), typeof opt_config == 'object' && opt_config)
if (!data) {
data = new Modal(this, config)
$(this).data(Modal._DATA_KEY, data)
}
2011-09-11 07:14:57 +02:00
2015-01-03 22:58:44 +01:00
if (typeof opt_config == 'string') {
data[opt_config](opt_relatedTarget)
2011-11-25 05:04:07 +01:00
2015-01-03 22:58:44 +01:00
} else if (config['show']) {
data['show'](opt_relatedTarget)
}
})
}
2011-11-25 05:04:07 +01:00
2015-01-03 22:58:44 +01:00
/**
* @param {Element} relatedTarget
*/
Modal.prototype['toggle'] = function (relatedTarget) {
return this._isShown ? this['hide']() : this['show'](relatedTarget)
}
2014-03-17 02:38:47 +01:00
2015-01-03 22:58:44 +01:00
/**
* @param {Element} relatedTarget
*/
Modal.prototype['show'] = function (relatedTarget) {
var showEvent = $.Event(Modal._Event.SHOW, { relatedTarget: relatedTarget })
2012-05-17 04:09:57 +02:00
2015-01-03 22:58:44 +01:00
$(this._element).trigger(showEvent)
2013-08-17 23:24:38 +02:00
2015-01-03 22:58:44 +01:00
if (this._isShown || showEvent.isDefaultPrevented()) {
return
}
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
this._isShown = true
2015-01-03 22:58:44 +01:00
this._checkScrollbar()
this._setScrollbar()
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
$(document.body).addClass(Modal._ClassName.OPEN)
2015-01-03 22:58:44 +01:00
this._escape()
this._resize()
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
$(this._element).on('click.dismiss.bs.modal', Modal._Selector.DATA_DISMISS, this['hide'].bind(this))
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
this._showBackdrop(this._showElement.bind(this, relatedTarget))
}
2012-05-17 04:09:57 +02:00
2013-08-09 09:16:47 +02:00
2015-01-03 22:58:44 +01:00
/**
* @param {Event} event
*/
Modal.prototype['hide'] = function (event) {
if (event) {
event.preventDefault()
}
2015-01-03 22:58:44 +01:00
var hideEvent = $.Event(Modal._Event.HIDE)
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
$(this._element).trigger(hideEvent)
2015-01-03 22:58:44 +01:00
if (!this._isShown || hideEvent.isDefaultPrevented()) {
return
}
2015-01-03 22:58:44 +01:00
this._isShown = false
2015-01-03 22:58:44 +01:00
this._escape()
this._resize()
2015-01-03 22:58:44 +01:00
$(document).off('focusin.bs.modal')
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
$(this._element).removeClass(Modal._ClassName.IN)
this._element.setAttribute('aria-hidden', true)
2015-01-03 22:58:44 +01:00
$(this._element).off('click.dismiss.bs.modal')
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
if (Bootstrap.transition && $(this._element).hasClass(Modal._ClassName.FADE)) {
$(this._element)
.one(Bootstrap.TRANSITION_END, this._hideModal.bind(this))
.emulateTransitionEnd(Modal._TRANSITION_DURATION)
} else {
this._hideModal()
}
2015-01-03 22:58:44 +01:00
}
2015-01-03 22:58:44 +01:00
/**
* @param {Element} relatedTarget
* @private
*/
Modal.prototype._showElement = function (relatedTarget) {
var transition = Bootstrap.transition && $(this._element).hasClass(Modal._ClassName.FADE)
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
if (!this._element.parentNode || this._element.parentNode.nodeType != Node.ELEMENT_NODE) {
document.body.appendChild(this._element) // don't move modals dom position
}
2015-01-03 22:58:44 +01:00
this._element.style.display = 'block'
this._element.scrollTop = 0
2011-11-25 05:04:07 +01:00
2015-01-03 22:58:44 +01:00
if (this._config['backdrop']) {
this._adjustBackdrop()
}
2011-10-05 06:48:53 +02:00
2015-01-03 22:58:44 +01:00
if (transition) {
Bootstrap.reflow(this._element)
}
2015-01-03 22:58:44 +01:00
$(this._element).addClass(Modal._ClassName.IN)
this._element.setAttribute('aria-hidden', false)
2011-10-05 06:48:53 +02:00
2015-01-03 22:58:44 +01:00
this._enforceFocus()
2011-10-05 06:48:53 +02:00
2015-01-03 22:58:44 +01:00
var shownEvent = $.Event(Modal._Event.SHOWN, { relatedTarget: relatedTarget })
2011-10-05 06:48:53 +02:00
2015-01-03 22:58:44 +01:00
var transitionComplete = function () {
this._element.focus()
$(this._element).trigger(shownEvent)
}.bind(this)
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
if (transition) {
var dialog = $(this._element).find(Modal._Selector.DIALOG)[0]
$(dialog)
.one(Bootstrap.TRANSITION_END, transitionComplete)
.emulateTransitionEnd(Modal._TRANSITION_DURATION)
} else {
transitionComplete()
}
}
2011-11-21 03:19:50 +01:00
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
/**
* @private
*/
Modal.prototype._enforceFocus = function () {
$(document)
.off('focusin.bs.modal') // guard against infinite focus loop
.on('focusin.bs.modal', function (e) {
if (this._element !== e.target && !$(this._element).has(e.target).length) {
this._element.focus()
}
2015-01-03 22:58:44 +01:00
}.bind(this))
}
/**
* @private
*/
Modal.prototype._escape = function () {
if (this._isShown && this._config['keyboard']) {
$(this._element).on('keydown.dismiss.bs.modal', function (event) {
if (event.which === 27) {
this['hide']()
}
}.bind(this))
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
} else if (!this._isShown) {
$(this._element).off('keydown.dismiss.bs.modal')
}
2015-01-03 22:58:44 +01:00
}
2014-10-30 11:25:28 +01:00
2015-01-03 22:58:44 +01:00
/**
* @private
*/
Modal.prototype._resize = function () {
if (this._isShown) {
$(window).on('resize.bs.modal', this._handleUpdate.bind(this))
} else {
$(window).off('resize.bs.modal')
2014-10-30 11:25:28 +01:00
}
2015-01-03 22:58:44 +01:00
}
/**
* @private
*/
Modal.prototype._hideModal = function () {
this._element.style.display = 'none'
this._showBackdrop(function () {
$(document.body).removeClass(Modal._ClassName.OPEN)
this._resetAdjustments()
this._resetScrollbar()
$(this._element).trigger(Modal._Event.HIDDEN)
}.bind(this))
}
/**
* @private
*/
Modal.prototype._removeBackdrop = function () {
if (this._backdrop) {
this._backdrop.parentNode.removeChild(this._backdrop)
this._backdrop = null
}
2015-01-03 22:58:44 +01:00
}
2014-10-30 11:25:28 +01:00
2015-01-03 22:58:44 +01:00
/**
* @param {Function} callback
* @private
*/
Modal.prototype._showBackdrop = function (callback) {
var animate = $(this._element).hasClass(Modal._ClassName.FADE) ? Modal._ClassName.FADE : ''
2014-10-30 11:25:28 +01:00
2015-01-03 22:58:44 +01:00
if (this._isShown && this._config['backdrop']) {
var doAnimate = Bootstrap.transition && animate
2014-10-30 11:25:28 +01:00
2015-01-03 22:58:44 +01:00
this._backdrop = document.createElement('div')
this._backdrop.className = Modal._ClassName.BACKDROP
2014-03-17 02:38:47 +01:00
2015-01-03 22:58:44 +01:00
if (animate) {
$(this._backdrop).addClass(animate)
}
2014-03-17 02:38:47 +01:00
2015-01-03 22:58:44 +01:00
$(this._element).prepend(this._backdrop)
2014-03-17 02:38:47 +01:00
2015-01-03 22:58:44 +01:00
$(this._backdrop).on('click.dismiss.bs.modal', function (event) {
if (event.target !== event.currentTarget) return
this._config['backdrop'] === 'static'
? this._element.focus()
: this['hide']()
}.bind(this))
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
if (doAnimate) {
Bootstrap.reflow(this._backdrop)
}
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
$(this._backdrop).addClass(Modal._ClassName.IN)
2013-05-17 02:18:15 +02:00
2015-01-03 22:58:44 +01:00
if (!callback) {
return
}
2011-08-27 08:57:35 +02:00
2015-01-03 22:58:44 +01:00
if (!doAnimate) {
callback()
return
}
2015-01-03 22:58:44 +01:00
$(this._backdrop)
.one(Bootstrap.TRANSITION_END, callback)
.emulateTransitionEnd(Modal._BACKDROP_TRANSITION_DURATION)
2011-11-25 05:04:07 +01:00
2015-01-03 22:58:44 +01:00
} else if (!this._isShown && this._backdrop) {
$(this._backdrop).removeClass(Modal._ClassName.IN)
2015-01-03 22:58:44 +01:00
var callbackRemove = function () {
this._removeBackdrop()
if (callback) {
callback()
}
}.bind(this)
if (Bootstrap.transition && $(this._element).hasClass(Modal._ClassName.FADE)) {
$(this._backdrop)
.one(Bootstrap.TRANSITION_END, callbackRemove)
.emulateTransitionEnd(Modal._BACKDROP_TRANSITION_DURATION)
} else {
callbackRemove()
}
2015-01-03 22:58:44 +01:00
} else if (callback) {
callback()
}
}
/**
* ------------------------------------------------------------------------
* the following methods are used to handle overflowing modals
* todo (fat): these should probably be refactored into a
* ------------------------------------------------------------------------
*/
/**
* @private
*/
Modal.prototype._handleUpdate = function () {
if (this._config['backdrop']) this._adjustBackdrop()
this._adjustDialog()
}
/**
* @private
*/
Modal.prototype._adjustBackdrop = function () {
this._backdrop.style.height = 0 // todo (fat): no clue why we do this
this._backdrop.style.height = this._element.scrollHeight + 'px'
}
/**
* @private
*/
Modal.prototype._adjustDialog = function () {
var isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight
if (!this._isBodyOverflowing && isModalOverflowing) {
this._element.style.paddingLeft = this._scrollbarWidth + 'px'
}
2015-01-03 22:58:44 +01:00
if (this._isBodyOverflowing && !isModalOverflowing) {
this._element.style.paddingRight = this._scrollbarWidth + 'px'
}
2015-01-03 22:58:44 +01:00
}
2015-01-03 22:58:44 +01:00
/**
* @private
*/
Modal.prototype._resetAdjustments = function () {
this._element.style.paddingLeft = ''
this._element.style.paddingRight = ''
}
2015-01-03 22:58:44 +01:00
/**
* @private
*/
Modal.prototype._checkScrollbar = function () {
this._isBodyOverflowing = document.body.scrollHeight > document.documentElement.clientHeight
this._scrollbarWidth = this._getScrollbarWidth()
}
2015-01-03 22:58:44 +01:00
/**
* @private
*/
Modal.prototype._setScrollbar = function () {
var bodyPadding = parseInt(($(document.body).css('padding-right') || 0), 10)
if (this._isBodyOverflowing) {
document.body.style.paddingRight = bodyPadding + this._scrollbarWidth + 'px'
}
}
/**
* @private
*/
Modal.prototype._resetScrollbar = function () {
document.body.style.paddingRight = ''
}
/**
* @private
*/
Modal.prototype._getScrollbarWidth = function () { // thx walsh
var scrollDiv = document.createElement('div')
scrollDiv.className = Modal._Selector.SCROLLBAR_MEASURER
document.body.appendChild(scrollDiv)
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
document.body.removeChild(scrollDiv)
return scrollbarWidth
}
/**
* ------------------------------------------------------------------------
* jQuery Interface + noConflict implementaiton
* ------------------------------------------------------------------------
*/
/**
* @const
* @type {Function}
*/
$.fn[Modal._NAME] = Modal._jQueryInterface
/**
* @const
* @type {Function}
*/
$.fn[Modal._NAME]['Constructor'] = Modal
/**
* @const
* @type {Function}
*/
$.fn[Modal._NAME]['noConflict'] = function () {
$.fn[Modal._NAME] = Modal._JQUERY_NO_CONFLICT
return this
}
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
$(document).on('click.bs.modal.data-api', Modal._Selector.DATA_TOGGLE, function (event) {
var selector = Bootstrap.getSelectorFromElement(this)
if (selector) {
var target = $(selector)[0]
}
var config = $(target).data(Modal._DATA_KEY) ? 'toggle' : $.extend({}, $(target).data(), $(this).data())
if (this.tagName == 'A') {
event.preventDefault()
}
var $target = $(target).one(Modal._Event.SHOW, function (showEvent) {
if (showEvent.isDefaultPrevented()) {
return // only register focus restorer if modal will actually get shown
}
$target.one(Modal._Event.HIDDEN, function () {
if ($(this).is(':visible')) {
this.focus()
}
}.bind(this))
}.bind(this))
Modal._jQueryInterface.call($(target), config, this)
})