unignore bower_components
[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.find('.modal-body').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 () {
42     return this[!this.isShown ? 'show' : 'hide']()
43   }
44
45   Modal.prototype.show = function () {
46     var that = this
47     var e    = $.Event('show.bs.modal')
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       transition ?
77         that.$element
78           .one($.support.transition.end, function () {
79             that.$element.focus().trigger('shown.bs.modal')
80           })
81           .emulateTransitionEnd(300) :
82         that.$element.focus().trigger('shown.bs.modal')
83     })
84   }
85
86   Modal.prototype.hide = function (e) {
87     if (e) e.preventDefault()
88
89     e = $.Event('hide.bs.modal')
90
91     this.$element.trigger(e)
92
93     if (!this.isShown || e.isDefaultPrevented()) return
94
95     this.isShown = false
96
97     this.escape()
98
99     $(document).off('focusin.bs.modal')
100
101     this.$element
102       .removeClass('in')
103       .attr('aria-hidden', true)
104
105     $.support.transition && this.$element.hasClass('fade') ?
106       this.$element
107         .one($.support.transition.end, $.proxy(this.hideModal, this))
108         .emulateTransitionEnd(300) :
109       this.hideModal()
110   }
111
112   Modal.prototype.enforceFocus = function () {
113     $(document)
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()
118         }
119       }, this))
120   }
121
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()
126       }, this))
127     } else if (!this.isShown) {
128       this.$element.off('keyup.dismiss.bs.modal')
129     }
130   }
131
132   Modal.prototype.hideModal = function () {
133     var that = this
134     this.$element.hide()
135     this.backdrop(function () {
136       that.removeBackdrop()
137       that.$element.trigger('hidden.bs.modal')
138     })
139   }
140
141   Modal.prototype.removeBackdrop = function () {
142     this.$backdrop && this.$backdrop.remove()
143     this.$backdrop = null
144   }
145
146   Modal.prototype.backdrop = function (callback) {
147     var that    = this
148     var animate = this.$element.hasClass('fade') ? 'fade' : ''
149
150     if (this.isShown && this.options.backdrop) {
151       var doAnimate = $.support.transition && animate
152
153       this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
154         .appendTo(document.body)
155
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)
161       }, this))
162
163       if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
164
165       this.$backdrop.addClass('in')
166
167       if (!callback) return
168
169       doAnimate ?
170         this.$backdrop
171           .one($.support.transition.end, callback)
172           .emulateTransitionEnd(150) :
173         callback()
174
175     } else if (!this.isShown && this.$backdrop) {
176       this.$backdrop.removeClass('in')
177
178       $.support.transition && this.$element.hasClass('fade')?
179         this.$backdrop
180           .one($.support.transition.end, callback)
181           .emulateTransitionEnd(150) :
182         callback()
183
184     } else if (callback) {
185       callback()
186     }
187   }
188
189
190   // MODAL PLUGIN DEFINITION
191   // =======================
192
193   var old = $.fn.modal
194
195   $.fn.modal = function (option) {
196     return this.each(function () {
197       var $this   = $(this)
198       var data    = $this.data('bs.modal')
199       var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
200
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()
204     })
205   }
206
207   $.fn.modal.Constructor = Modal
208
209
210   // MODAL NO CONFLICT
211   // =================
212
213   $.fn.modal.noConflict = function () {
214     $.fn.modal = old
215     return this
216   }
217
218
219   // MODAL DATA-API
220   // ==============
221
222   $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
223     var $this   = $(this)
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())
227
228     e.preventDefault()
229
230     $target
231       .modal(option)
232       .one('hide', function () {
233         $this.is(':visible') && $this.focus()
234       })
235   })
236
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') })
240
241 }(window.jQuery);