modal.js 6.39 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* =========================================================
fat's avatar
fat committed
2
 * Bootstrap: modal.js v3.0.0
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#modals
Jacob Thornton's avatar
Jacob Thornton committed
4
 * =========================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================= */


fat's avatar
fat committed
21
!function ($) { "use strict";
22

fat's avatar
fat committed
23
24
  // MODAL CLASS DEFINITION
  // ======================
Jacob Thornton's avatar
Jacob Thornton committed
25

Jacob Thornton's avatar
Jacob Thornton committed
26
  var Modal = function (element, options) {
fat's avatar
fat committed
27
28
29
30
    this.options   = options
    this.$element  = $(element).delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
    this.$backdrop =
    this.isShown   = null
Jacob Thornton's avatar
Jacob Thornton committed
31

fat's avatar
fat committed
32
33
    if (this.options.remote) this.$element.find('.modal-body').load(this.options.remote)
  }
Jacob Thornton's avatar
Jacob Thornton committed
34

fat's avatar
fat committed
35
36
37
38
39
  Modal.DEFAULTS = {
      backdrop: true
    , keyboard: true
    , show: true
  }
40

fat's avatar
fat committed
41
42
43
  Modal.prototype.toggle = function () {
    return this[!this.isShown ? 'show' : 'hide']()
  }
Jacob Thornton's avatar
Jacob Thornton committed
44

fat's avatar
fat committed
45
46
47
  Modal.prototype.show = function () {
    var that = this
    var e    = $.Event('bs:modal:show')
Jacob Thornton's avatar
Jacob Thornton committed
48

fat's avatar
fat committed
49
    this.$element.trigger(e)
50

fat's avatar
fat committed
51
    if (this.isShown || e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
52

fat's avatar
fat committed
53
    this.isShown = true
54

fat's avatar
fat committed
55
    this.escape()
56

fat's avatar
fat committed
57
58
    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
59

fat's avatar
fat committed
60
61
62
      if (!that.$element.parent().length) {
        that.$element.appendTo(document.body) //don't move modals dom position
      }
63

fat's avatar
fat committed
64
      that.$element.show()
65

fat's avatar
fat committed
66
67
68
      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }
69

fat's avatar
fat committed
70
71
72
      that.$element
        .addClass('in')
        .attr('aria-hidden', false)
73

fat's avatar
fat committed
74
      that.enforceFocus()
75

fat's avatar
fat committed
76
77
78
      transition ?
        that.$element.one($.support.transition.end, function () { that.$element.focus().trigger('bs:modal:shown') }) :
        that.$element.focus().trigger('bs:modal:shown')
79

fat's avatar
fat committed
80
81
    })
  }
82

fat's avatar
fat committed
83
  Modal.prototype.hide = function (e) {
fat's avatar
fat committed
84
    if (e) e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
85

fat's avatar
fat committed
86
    e = $.Event('bs:modal:hide')
87

fat's avatar
fat committed
88
    this.$element.trigger(e)
89

fat's avatar
fat committed
90
    if (!this.isShown || e.isDefaultPrevented()) return
91

fat's avatar
fat committed
92
    this.isShown = false
93

fat's avatar
fat committed
94
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
95

fat's avatar
fat committed
96
    $(document).off('focusin.modal')
97

fat's avatar
fat committed
98
99
100
    this.$element
      .removeClass('in')
      .attr('aria-hidden', true)
Jacob Thornton's avatar
Jacob Thornton committed
101

fat's avatar
fat committed
102
103
104
105
    $.support.transition && this.$element.hasClass('fade') ?
      this.hideWithTransition() :
      this.hideModal()
  }
Jacob Thornton's avatar
Jacob Thornton committed
106

fat's avatar
fat committed
107
108
109
110
  Modal.prototype.enforceFocus = function () {
    $(document).on('focusin.modal', function (e) {
      if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
        this.$element.focus()
111
      }
fat's avatar
fat committed
112
113
    }, this)
  }
Jacob Thornton's avatar
Jacob Thornton committed
114

fat's avatar
fat committed
115
116
117
118
119
120
121
122
123
  Modal.prototype.escape = function () {
    if (this.isShown && this.options.keyboard) {
      this.$element.on('keyup.dismiss.modal', function ( e ) {
        e.which == 27 && this.hide()
      }, this)
    } else if (!this.isShown) {
      this.$element.off('keyup.dismiss.modal')
    }
  }
124

fat's avatar
fat committed
125
126
127
128
129
130
  Modal.prototype.hideWithTransition = function () {
    var that    = this
    var timeout = setTimeout(function () {
      that.$element.off($.support.transition.end)
      that.hideModal()
    }, 500)
131

fat's avatar
fat committed
132
133
134
135
136
    this.$element.one($.support.transition.end, function () {
      clearTimeout(timeout)
      that.hideModal()
    })
  }
137

fat's avatar
fat committed
138
139
140
141
142
143
144
145
  Modal.prototype.hideModal = function () {
    var that = this
    this.$element.hide()
    this.backdrop(function () {
      that.removeBackdrop()
      that.$element.trigger('bs:modal:hidden')
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
146

fat's avatar
fat committed
147
148
149
150
  Modal.prototype.removeBackdrop = function () {
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
151

fat's avatar
fat committed
152
153
154
  Modal.prototype.backdrop = function (callback) {
    var that    = this
    var animate = this.$element.hasClass('fade') ? 'fade' : ''
155

fat's avatar
fat committed
156
157
    if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate
158

fat's avatar
fat committed
159
160
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)
161

fat's avatar
fat committed
162
163
164
165
166
      this.$backdrop.click(
        this.options.backdrop == 'static' ?
          $.proxy(this.$element[0].focus, this.$element[0])
        : $.proxy(this.hide, this)
      )
167

fat's avatar
fat committed
168
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
169

fat's avatar
fat committed
170
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
171

fat's avatar
fat committed
172
      if (!callback) return
173

fat's avatar
fat committed
174
175
176
      doAnimate ?
        this.$backdrop.one($.support.transition.end, callback) :
        callback()
177

fat's avatar
fat committed
178
179
    } else if (!this.isShown && this.$backdrop) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
180

fat's avatar
fat committed
181
182
183
      $.support.transition && this.$element.hasClass('fade')?
        this.$backdrop.one($.support.transition.end, callback) :
        callback()
184

fat's avatar
fat committed
185
186
187
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
188
189
190
  }


fat's avatar
fat committed
191
192
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
193

194
195
  var old = $.fn.modal

196
  $.fn.modal = function (option) {
Jacob Thornton's avatar
Jacob Thornton committed
197
    return this.each(function () {
fat's avatar
fat committed
198
199
200
201
202
      var $this   = $(this)
      var data    = $this.data('bs-modal')
      var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)

      if (!data) $this.data('bs-modal', (data = new Modal(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
203
      if (typeof option == 'string') data[option]()
204
      else if (options.show) data.show()
Jacob Thornton's avatar
Jacob Thornton committed
205
    })
Jacob Thornton's avatar
Jacob Thornton committed
206
207
  }

208
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
209

210

fat's avatar
fat committed
211
212
  // MODAL NO CONFLICT
  // =================
213
214
215
216
217
218
219

  $.fn.modal.noConflict = function () {
    $.fn.modal = old
    return this
  }


fat's avatar
fat committed
220
221
  // MODAL DATA-API
  // ==============
222

223
  $(document).on('click.modal.data-api', '[data-toggle="modal"]', function (e) {
fat's avatar
fat committed
224
225
226
227
    var $this   = $(this)
    var href    = $this.attr('href')
    var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
    var option  = $target.data('modal') ? 'toggle' : $.extend({ remote:!/#/.test(href) && href }, $target.data(), $this.data())
228
229
230
231
232
233
234
235

    e.preventDefault()

    $target
      .modal(option)
      .one('hide', function () {
        $this.focus()
      })
236
237
    })

238
    var $body = $(document.body)
fat's avatar
fat committed
239
240
      .on('bs:modal:shown',  '.modal', function () { $body.addClass('modal-open') })
      .on('bs:modal:hidden', '.modal', function () { $body.removeClass('modal-open') })
241

242
}(window.jQuery);