update bootstrap to 3.0.0-rc2
[bootswatch] / bower_components / bootstrap / js / modal.js
1 /* ========================================================================
2  * Bootstrap: modal.js v3.0.0
3  * http://twbs.github.com/bootstrap/javascript.html#modals
4  * ========================================================================
5  * Copyright 2012 Twitter, Inc.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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  * ======================================================================== */
19
20
21 +function ($) { "use strict";
22
23   // MODAL CLASS DEFINITION
24   // ======================
25
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))
29     this.$backdrop =
30     this.isShown   = null
31
32     if (this.options.remote) this.$element.load(this.options.remote)
33   }
34
35   Modal.DEFAULTS = {
36       backdrop: true
37     , keyboard: true
38     , show: true
39   }
40
41   Modal.prototype.toggle = function (_relatedTarget) {
42     return this[!this.isShown ? 'show' : 'hide'](_relatedTarget)
43   }
44
45   Modal.prototype.show = function (_relatedTarget) {
46     var that = this
47     var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
48
49     this.$element.trigger(e)
50
51     if (this.isShown || e.isDefaultPrevented()) return
52
53     this.isShown = true
54
55     this.escape()
56
57     this.backdrop(function () {
58       var transition = $.support.transition && that.$element.hasClass('fade')
59
60       if (!that.$element.parent().length) {
61         that.$element.appendTo(document.body) // don't move modals dom position
62       }
63
64       that.$element.show()
65
66       if (transition) {
67         that.$element[0].offsetWidth // force reflow
68       }
69
70       that.$element
71         .addClass('in')
72         .attr('aria-hidden', false)
73
74       that.enforceFocus()
75
76       var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
77
78       transition ?
79         that.$element
80           .one($.support.transition.end, function () {
81             that.$element.focus().trigger(e)
82           })
83           .emulateTransitionEnd(300) :
84         that.$element.focus().trigger(e)
85     })
86   }
87
88   Modal.prototype.hide = function (e) {
89     if (e) e.preventDefault()
90
91     e = $.Event('hide.bs.modal')
92
93     this.$element.trigger(e)
94
95     if (!this.isShown || e.isDefaultPrevented()) return
96
97     this.isShown = false
98
99     this.escape()
100
101     $(document).off('focusin.bs.modal')
102
103     this.$element
104       .removeClass('in')
105       .attr('aria-hidden', true)
106       .off('click.dismiss.modal')
107
108     $.support.transition && this.$element.hasClass('fade') ?
109       this.$element
110         .one($.support.transition.end, $.proxy(this.hideModal, this))
111         .emulateTransitionEnd(300) :
112       this.hideModal()
113   }
114
115   Modal.prototype.enforceFocus = function () {
116     $(document)
117       .off('focusin.bs.modal') // guard against infinite focus loop
118       .on('focusin.bs.modal', $.proxy(function (e) {
119         if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
120           this.$element.focus()
121         }
122       }, this))
123   }
124
125   Modal.prototype.escape = function () {
126     if (this.isShown && this.options.keyboard) {
127       this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
128         e.which == 27 && this.hide()
129       }, this))
130     } else if (!this.isShown) {
131       this.$element.off('keyup.dismiss.bs.modal')
132     }
133   }
134
135   Modal.prototype.hideModal = function () {
136     var that = this
137     this.$element.hide()
138     this.backdrop(function () {
139       that.removeBackdrop()
140       that.$element.trigger('hidden.bs.modal')
141     })
142   }
143
144   Modal.prototype.removeBackdrop = function () {
145     this.$backdrop && this.$backdrop.remove()
146     this.$backdrop = null
147   }
148
149   Modal.prototype.backdrop = function (callback) {
150     var that    = this
151     var animate = this.$element.hasClass('fade') ? 'fade' : ''
152
153     if (this.isShown && this.options.backdrop) {
154       var doAnimate = $.support.transition && animate
155
156       this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
157         .appendTo(document.body)
158
159       this.$element.on('click.dismiss.modal', $.proxy(function (e) {
160         if (e.target !== e.currentTarget) return
161         this.options.backdrop == 'static'
162           ? this.$element[0].focus.call(this.$element[0])
163           : this.hide.call(this)
164       }, this))
165
166       if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
167
168       this.$backdrop.addClass('in')
169
170       if (!callback) return
171
172       doAnimate ?
173         this.$backdrop
174           .one($.support.transition.end, callback)
175           .emulateTransitionEnd(150) :
176         callback()
177
178     } else if (!this.isShown && this.$backdrop) {
179       this.$backdrop.removeClass('in')
180
181       $.support.transition && this.$element.hasClass('fade')?
182         this.$backdrop
183           .one($.support.transition.end, callback)
184           .emulateTransitionEnd(150) :
185         callback()
186
187     } else if (callback) {
188       callback()
189     }
190   }
191
192
193   // MODAL PLUGIN DEFINITION
194   // =======================
195
196   var old = $.fn.modal
197
198   $.fn.modal = function (option, _relatedTarget) {
199     return this.each(function () {
200       var $this   = $(this)
201       var data    = $this.data('bs.modal')
202       var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
203
204       if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
205       if (typeof option == 'string') data[option](_relatedTarget)
206       else if (options.show) data.show(_relatedTarget)
207     })
208   }
209
210   $.fn.modal.Constructor = Modal
211
212
213   // MODAL NO CONFLICT
214   // =================
215
216   $.fn.modal.noConflict = function () {
217     $.fn.modal = old
218     return this
219   }
220
221
222   // MODAL DATA-API
223   // ==============
224
225   $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
226     var $this   = $(this)
227     var href    = $this.attr('href')
228     var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
229     var option  = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
230
231     e.preventDefault()
232
233     $target
234       .modal(option, this)
235       .one('hide', function () {
236         $this.is(':visible') && $this.focus()
237       })
238   })
239
240   $(document)
241     .on('shown.bs.modal',  '.modal', function () { $(document.body).addClass('modal-open') })
242     .on('hidden.bs.modal', '.modal', function () { $(document.body).removeClass('modal-open') })
243
244 }(window.jQuery);