1 /* =========================================================
2 * bootstrap-modal.js v2.0.4
3 * http://twitter.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 * ========================================================= */
23 "use strict"; // jshint ;_;
26 /* MODAL CLASS DEFINITION
27 * ====================== */
29 var Modal = function (content, options) {
30 this.options = options
31 this.$element = $(content)
32 .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
39 , toggle: function () {
40 return this[!this.isShown ? 'show' : 'hide']()
47 this.$element.trigger(e)
49 if (this.isShown || e.isDefaultPrevented()) return
51 $('body').addClass('modal-open')
56 backdrop.call(this, function () {
57 var transition = $.support.transition && that.$element.hasClass('fade')
59 if (!that.$element.parent().length) {
60 that.$element.appendTo(document.body) //don't move modals dom position
67 that.$element[0].offsetWidth // force reflow
70 that.$element.addClass('in')
73 that.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) :
74 that.$element.trigger('shown')
79 , hide: function (e) {
80 e && e.preventDefault()
86 this.$element.trigger(e)
88 if (!this.isShown || e.isDefaultPrevented()) return
92 $('body').removeClass('modal-open')
96 this.$element.removeClass('in')
98 $.support.transition && this.$element.hasClass('fade') ?
99 hideWithTransition.call(this) :
106 /* MODAL PRIVATE METHODS
107 * ===================== */
109 function hideWithTransition() {
111 , timeout = setTimeout(function () {
112 that.$element.off($.support.transition.end)
116 this.$element.one($.support.transition.end, function () {
117 clearTimeout(timeout)
122 function hideModal(that) {
130 function backdrop(callback) {
132 , animate = this.$element.hasClass('fade') ? 'fade' : ''
134 if (this.isShown && this.options.backdrop) {
135 var doAnimate = $.support.transition && animate
137 this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
138 .appendTo(document.body)
140 if (this.options.backdrop != 'static') {
141 this.$backdrop.click($.proxy(this.hide, this))
144 if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
146 this.$backdrop.addClass('in')
149 this.$backdrop.one($.support.transition.end, callback) :
152 } else if (!this.isShown && this.$backdrop) {
153 this.$backdrop.removeClass('in')
155 $.support.transition && this.$element.hasClass('fade')?
156 this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
157 removeBackdrop.call(this)
159 } else if (callback) {
164 function removeBackdrop() {
165 this.$backdrop.remove()
166 this.$backdrop = null
171 if (this.isShown && this.options.keyboard) {
172 $(document).on('keyup.dismiss.modal', function ( e ) {
173 e.which == 27 && that.hide()
175 } else if (!this.isShown) {
176 $(document).off('keyup.dismiss.modal')
181 /* MODAL PLUGIN DEFINITION
182 * ======================= */
184 $.fn.modal = function (option) {
185 return this.each(function () {
187 , data = $this.data('modal')
188 , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option)
189 if (!data) $this.data('modal', (data = new Modal(this, options)))
190 if (typeof option == 'string') data[option]()
191 else if (options.show) data.show()
195 $.fn.modal.defaults = {
201 $.fn.modal.Constructor = Modal
208 $('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) {
209 var $this = $(this), href
210 , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
211 , option = $target.data('modal') ? 'toggle' : $.extend({}, $target.data(), $this.data())
214 $target.modal(option)