1 /* ========================================================================
2 * Bootstrap: modal.js v3.0.0
3 * http://twbs.github.com/bootstrap/javascript.html#modals
4 * ========================================================================
5 * Copyright 2012 Twitter, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ======================================================================== */
21 +function ($) { "use strict";
23 // MODAL CLASS DEFINITION
24 // ======================
26 var Modal = function (element, options) {
27 this.options = options
28 this.$element = $(element).on('click.dismiss.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
32 if (this.options.remote) this.$element.find('.modal-body').load(this.options.remote)
41 Modal.prototype.toggle = function () {
42 return this[!this.isShown ? 'show' : 'hide']()
45 Modal.prototype.show = function () {
47 var e = $.Event('show.bs.modal')
49 this.$element.trigger(e)
51 if (this.isShown || e.isDefaultPrevented()) return
57 this.backdrop(function () {
58 var transition = $.support.transition && that.$element.hasClass('fade')
60 if (!that.$element.parent().length) {
61 that.$element.appendTo(document.body) // don't move modals dom position
67 that.$element[0].offsetWidth // force reflow
72 .attr('aria-hidden', false)
78 .one($.support.transition.end, function () {
79 that.$element.focus().trigger('shown.bs.modal')
81 .emulateTransitionEnd(300) :
82 that.$element.focus().trigger('shown.bs.modal')
86 Modal.prototype.hide = function (e) {
87 if (e) e.preventDefault()
89 e = $.Event('hide.bs.modal')
91 this.$element.trigger(e)
93 if (!this.isShown || e.isDefaultPrevented()) return
99 $(document).off('focusin.bs.modal')
103 .attr('aria-hidden', true)
105 $.support.transition && this.$element.hasClass('fade') ?
107 .one($.support.transition.end, $.proxy(this.hideModal, this))
108 .emulateTransitionEnd(300) :
112 Modal.prototype.enforceFocus = function () {
114 .off('focusin.bs.modal') // guard against infinite focus loop
115 .on('focusin.bs.modal', $.proxy(function (e) {
116 if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
117 this.$element.focus()
122 Modal.prototype.escape = function () {
123 if (this.isShown && this.options.keyboard) {
124 this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
125 e.which == 27 && this.hide()
127 } else if (!this.isShown) {
128 this.$element.off('keyup.dismiss.bs.modal')
132 Modal.prototype.hideModal = function () {
135 this.backdrop(function () {
136 that.removeBackdrop()
137 that.$element.trigger('hidden.bs.modal')
141 Modal.prototype.removeBackdrop = function () {
142 this.$backdrop && this.$backdrop.remove()
143 this.$backdrop = null
146 Modal.prototype.backdrop = function (callback) {
148 var animate = this.$element.hasClass('fade') ? 'fade' : ''
150 if (this.isShown && this.options.backdrop) {
151 var doAnimate = $.support.transition && animate
153 this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
154 .appendTo(document.body)
156 this.$element.on('click', $.proxy(function (e) {
157 if (e.target !== e.currentTarget) return
158 this.options.backdrop == 'static'
159 ? this.$element[0].focus.call(this.$element[0])
160 : this.hide.call(this)
163 if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
165 this.$backdrop.addClass('in')
167 if (!callback) return
171 .one($.support.transition.end, callback)
172 .emulateTransitionEnd(150) :
175 } else if (!this.isShown && this.$backdrop) {
176 this.$backdrop.removeClass('in')
178 $.support.transition && this.$element.hasClass('fade')?
180 .one($.support.transition.end, callback)
181 .emulateTransitionEnd(150) :
184 } else if (callback) {
190 // MODAL PLUGIN DEFINITION
191 // =======================
195 $.fn.modal = function (option) {
196 return this.each(function () {
198 var data = $this.data('bs.modal')
199 var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
201 if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
202 if (typeof option == 'string') data[option]()
203 else if (options.show) data.show()
207 $.fn.modal.Constructor = Modal
213 $.fn.modal.noConflict = function () {
222 $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
224 var href = $this.attr('href')
225 var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
226 var option = $target.data('modal') ? 'toggle' : $.extend({ remote:!/#/.test(href) && href }, $target.data(), $this.data())
232 .one('hide', function () {
233 $this.is(':visible') && $this.focus()
237 var $body = $(document.body)
238 .on('shown.bs.modal', '.modal', function () { $body.addClass('modal-open') })
239 .on('hidden.bs.modal', '.modal', function () { $body.removeClass('modal-open') })