1 /* ========================================================================
2 * Bootstrap: modal.js v3.2.0
3 * http://getbootstrap.com/javascript/#modals
4 * ========================================================================
5 * Copyright 2011-2014 Twitter, Inc.
6 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7 * ======================================================================== */
13 // MODAL CLASS DEFINITION
14 // ======================
16 var Modal = function (element, options) {
17 this.options = options
18 this.$body = $(document.body)
19 this.$element = $(element)
22 this.scrollbarWidth = 0
24 if (this.options.remote) {
26 .find('.modal-content')
27 .load(this.options.remote, $.proxy(function () {
28 this.$element.trigger('loaded.bs.modal')
33 Modal.VERSION = '3.2.0'
41 Modal.prototype.toggle = function (_relatedTarget) {
42 return this.isShown ? this.hide() : this.show(_relatedTarget)
45 Modal.prototype.show = function (_relatedTarget) {
47 var e = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
49 this.$element.trigger(e)
51 if (this.isShown || e.isDefaultPrevented()) return
56 this.$body.addClass('modal-open')
61 this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
63 this.backdrop(function () {
64 var transition = $.support.transition && that.$element.hasClass('fade')
66 if (!that.$element.parent().length) {
67 that.$element.appendTo(that.$body) // don't move modals dom position
75 that.$element[0].offsetWidth // force reflow
80 .attr('aria-hidden', false)
84 var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
87 that.$element.find('.modal-dialog') // wait for modal to slide in
88 .one('bsTransitionEnd', function () {
89 that.$element.trigger('focus').trigger(e)
91 .emulateTransitionEnd(300) :
92 that.$element.trigger('focus').trigger(e)
96 Modal.prototype.hide = function (e) {
97 if (e) e.preventDefault()
99 e = $.Event('hide.bs.modal')
101 this.$element.trigger(e)
103 if (!this.isShown || e.isDefaultPrevented()) return
107 this.$body.removeClass('modal-open')
109 this.resetScrollbar()
112 $(document).off('focusin.bs.modal')
116 .attr('aria-hidden', true)
117 .off('click.dismiss.bs.modal')
119 $.support.transition && this.$element.hasClass('fade') ?
121 .one('bsTransitionEnd', $.proxy(this.hideModal, this))
122 .emulateTransitionEnd(300) :
126 Modal.prototype.enforceFocus = function () {
128 .off('focusin.bs.modal') // guard against infinite focus loop
129 .on('focusin.bs.modal', $.proxy(function (e) {
130 if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
131 this.$element.trigger('focus')
136 Modal.prototype.escape = function () {
137 if (this.isShown && this.options.keyboard) {
138 this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
139 e.which == 27 && this.hide()
141 } else if (!this.isShown) {
142 this.$element.off('keyup.dismiss.bs.modal')
146 Modal.prototype.hideModal = function () {
149 this.backdrop(function () {
150 that.$element.trigger('hidden.bs.modal')
154 Modal.prototype.removeBackdrop = function () {
155 this.$backdrop && this.$backdrop.remove()
156 this.$backdrop = null
159 Modal.prototype.backdrop = function (callback) {
161 var animate = this.$element.hasClass('fade') ? 'fade' : ''
163 if (this.isShown && this.options.backdrop) {
164 var doAnimate = $.support.transition && animate
166 this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
167 .appendTo(this.$body)
169 this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
170 if (e.target !== e.currentTarget) return
171 this.options.backdrop == 'static'
172 ? this.$element[0].focus.call(this.$element[0])
173 : this.hide.call(this)
176 if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
178 this.$backdrop.addClass('in')
180 if (!callback) return
184 .one('bsTransitionEnd', callback)
185 .emulateTransitionEnd(150) :
188 } else if (!this.isShown && this.$backdrop) {
189 this.$backdrop.removeClass('in')
191 var callbackRemove = function () {
192 that.removeBackdrop()
193 callback && callback()
195 $.support.transition && this.$element.hasClass('fade') ?
197 .one('bsTransitionEnd', callbackRemove)
198 .emulateTransitionEnd(150) :
201 } else if (callback) {
206 Modal.prototype.checkScrollbar = function () {
207 if (document.body.clientWidth >= window.innerWidth) return
208 this.scrollbarWidth = this.scrollbarWidth || this.measureScrollbar()
211 Modal.prototype.setScrollbar = function () {
212 var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
213 if (this.scrollbarWidth) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
216 Modal.prototype.resetScrollbar = function () {
217 this.$body.css('padding-right', '')
220 Modal.prototype.measureScrollbar = function () { // thx walsh
221 var scrollDiv = document.createElement('div')
222 scrollDiv.className = 'modal-scrollbar-measure'
223 this.$body.append(scrollDiv)
224 var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
225 this.$body[0].removeChild(scrollDiv)
226 return scrollbarWidth
230 // MODAL PLUGIN DEFINITION
231 // =======================
233 function Plugin(option, _relatedTarget) {
234 return this.each(function () {
236 var data = $this.data('bs.modal')
237 var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
239 if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
240 if (typeof option == 'string') data[option](_relatedTarget)
241 else if (options.show) data.show(_relatedTarget)
248 $.fn.modal.Constructor = Modal
254 $.fn.modal.noConflict = function () {
263 $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
265 var href = $this.attr('href')
266 var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) // strip for ie7
267 var option = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
269 if ($this.is('a')) e.preventDefault()
271 $target.one('show.bs.modal', function (showEvent) {
272 if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
273 $target.one('hidden.bs.modal', function () {
274 $this.is(':visible') && $this.trigger('focus')
277 Plugin.call($target, option, this)