twbs--bootstrap/js/modal.js

287 lines
8.3 KiB
JavaScript
Raw Normal View History

2013-05-22 02:30:33 +00:00
/* ========================================================================
2014-02-13 09:12:26 +00:00
* Bootstrap: modal.js v3.1.1
* http://getbootstrap.com/javascript/#modals
2013-05-22 02:30:33 +00:00
* ========================================================================
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2013-05-22 02:30:33 +00:00
* ======================================================================== */
2011-09-11 05:24:31 +00:00
+function () { 'use strict';
(function (o_o) {
typeof define == 'function' && define.amd ? define(['jquery'], o_o) :
typeof exports == 'object' ? o_o(require('jquery')) : o_o(jQuery)
})(function ($) {
// MODAL CLASS DEFINITION
// ======================
var Modal = function (element, options) {
this.options = options
this.$body = $(document.body)
this.$element = $(element)
this.$backdrop =
this.isShown = null
this.scrollbarWidth = 0
if (this.options.remote) {
this.$element
.find('.modal-content')
.load(this.options.remote, $.proxy(function () {
this.$element.trigger('loaded.bs.modal')
}, this))
}
}
Modal.VERSION = '3.1.1'
2011-08-27 06:57:35 +00:00
Modal.DEFAULTS = {
backdrop: true,
keyboard: true,
show: true
}
2011-08-27 06:57:35 +00:00
Modal.prototype.toggle = function (_relatedTarget) {
return this.isShown ? this.hide() : this.show(_relatedTarget)
}
2011-08-27 06:57:35 +00:00
Modal.prototype.show = function (_relatedTarget) {
var that = this
var e = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
2014-05-13 04:15:16 +00:00
this.$element.trigger(e)
2011-12-01 06:42:22 +00:00
if (this.isShown || e.isDefaultPrevented()) return
2011-09-11 05:14:57 +00:00
this.isShown = true
2011-11-25 04:04:07 +00:00
this.checkScrollbar()
this.$body.addClass('modal-open')
this.setScrollbar()
this.escape()
2011-11-25 04:04:07 +00:00
this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
this.backdrop(function () {
var transition = $.support.transition && that.$element.hasClass('fade')
2014-03-17 01:38:47 +00:00
if (!that.$element.parent().length) {
that.$element.appendTo(that.$body) // don't move modals dom position
}
2012-05-17 02:09:57 +00:00
that.$element
.show()
.scrollTop(0)
2013-08-17 21:24:38 +00:00
if (transition) {
that.$element[0].offsetWidth // force reflow
}
2011-11-21 02:19:50 +00:00
that.$element
.addClass('in')
.attr('aria-hidden', false)
that.enforceFocus()
2011-11-21 02:19:50 +00:00
var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
2011-11-21 02:19:50 +00:00
transition ?
that.$element.find('.modal-dialog') // wait for modal to slide in
.one('bsTransitionEnd', function () {
that.$element.trigger('focus').trigger(e)
})
.emulateTransitionEnd(300) :
that.$element.trigger('focus').trigger(e)
})
}
2011-11-21 02:19:50 +00:00
Modal.prototype.hide = function (e) {
if (e) e.preventDefault()
2012-05-17 02:09:57 +00:00
e = $.Event('hide.bs.modal')
2013-08-09 07:16:47 +00:00
this.$element.trigger(e)
if (!this.isShown || e.isDefaultPrevented()) return
2011-08-27 06:57:35 +00:00
this.isShown = false
this.$body.removeClass('modal-open')
this.resetScrollbar()
this.escape()
$(document).off('focusin.bs.modal')
this.$element
.removeClass('in')
.attr('aria-hidden', true)
.off('click.dismiss.bs.modal')
2014-03-17 01:38:47 +00:00
$.support.transition && this.$element.hasClass('fade') ?
this.$element
.one('bsTransitionEnd', $.proxy(this.hideModal, this))
.emulateTransitionEnd(300) :
this.hideModal()
}
2011-08-27 06:57:35 +00:00
Modal.prototype.enforceFocus = function () {
$(document)
.off('focusin.bs.modal') // guard against infinite focus loop
.on('focusin.bs.modal', $.proxy(function (e) {
if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
this.$element.trigger('focus')
}
}, this))
}
Modal.prototype.escape = function () {
if (this.isShown && this.options.keyboard) {
this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
e.which == 27 && this.hide()
}, this))
} else if (!this.isShown) {
this.$element.off('keyup.dismiss.bs.modal')
}
}
2011-08-27 06:57:35 +00:00
Modal.prototype.hideModal = function () {
var that = this
this.$element.hide()
this.backdrop(function () {
that.$element.trigger('hidden.bs.modal')
})
2013-05-17 00:18:15 +00:00
}
2011-11-21 02:19:50 +00:00
Modal.prototype.removeBackdrop = function () {
this.$backdrop && this.$backdrop.remove()
this.$backdrop = null
}
2011-11-25 04:04:07 +00:00
Modal.prototype.backdrop = function (callback) {
var that = this
var animate = this.$element.hasClass('fade') ? 'fade' : ''
2011-10-05 04:48:53 +00:00
if (this.isShown && this.options.backdrop) {
var doAnimate = $.support.transition && animate
this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
.appendTo(this.$body)
2011-10-05 04:48:53 +00:00
this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
if (e.target !== e.currentTarget) return
this.options.backdrop == 'static'
? this.$element[0].focus.call(this.$element[0])
: this.hide.call(this)
}, this))
2011-10-05 04:48:53 +00:00
if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
2011-10-05 04:48:53 +00:00
this.$backdrop.addClass('in')
2011-10-05 04:48:53 +00:00
if (!callback) return
2011-08-27 06:57:35 +00:00
doAnimate ?
this.$backdrop
.one('bsTransitionEnd', callback)
.emulateTransitionEnd(150) :
callback()
} else if (!this.isShown && this.$backdrop) {
this.$backdrop.removeClass('in')
2011-11-21 02:19:50 +00:00
var callbackRemove = function () {
that.removeBackdrop()
callback && callback()
}
$.support.transition && this.$element.hasClass('fade') ?
this.$backdrop
.one('bsTransitionEnd', callbackRemove)
.emulateTransitionEnd(150) :
callbackRemove()
2011-08-27 06:57:35 +00:00
} else if (callback) {
callback()
}
2013-05-17 00:18:15 +00:00
}
2011-08-27 06:57:35 +00:00
Modal.prototype.checkScrollbar = function () {
if (document.body.clientWidth >= window.innerWidth) return
this.scrollbarWidth = this.scrollbarWidth || this.measureScrollbar()
}
Modal.prototype.setScrollbar = function () {
var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
if (this.scrollbarWidth) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
}
2014-03-17 01:38:47 +00:00
Modal.prototype.resetScrollbar = function () {
this.$body.css('padding-right', '')
}
2014-03-17 01:38:47 +00:00
Modal.prototype.measureScrollbar = function () { // thx walsh
var scrollDiv = document.createElement('div')
scrollDiv.className = 'modal-scrollbar-measure'
this.$body.append(scrollDiv)
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
this.$body[0].removeChild(scrollDiv)
return scrollbarWidth
}
2014-03-17 01:38:47 +00:00
2011-08-27 06:57:35 +00:00
// MODAL PLUGIN DEFINITION
// =======================
2011-08-27 06:57:35 +00:00
function Plugin(option, _relatedTarget) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.modal')
var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
2013-05-17 00:18:15 +00:00
if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
if (typeof option == 'string') data[option](_relatedTarget)
else if (options.show) data.show(_relatedTarget)
})
}
2011-08-27 06:57:35 +00:00
var old = $.fn.modal
$.fn.modal = Plugin
$.fn.modal.Constructor = Modal
2011-11-25 04:04:07 +00:00
// MODAL NO CONFLICT
// =================
$.fn.modal.noConflict = function () {
$.fn.modal = old
return this
}
// MODAL DATA-API
// ==============
$(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
var $this = $(this)
var href = $this.attr('href')
var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) // strip for ie7
var option = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
if ($this.is('a')) e.preventDefault()
$target.one('show.bs.modal', function (showEvent) {
if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
$target.one('hidden.bs.modal', function () {
$this.is(':visible') && $this.trigger('focus')
})
})
Plugin.call($target, option, this)
})
2013-07-25 03:21:23 +00:00
})
}();