modal.js 6.64 KB
Newer Older
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
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
 *
 * 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.
18
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
19
20


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
    this.options   = options
fat's avatar
fat committed
28
    this.$element  = $(element).on('click.dismiss.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
fat's avatar
fat committed
29
30
    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
  Modal.prototype.show = function () {
    var that = this
47
    var e    = $.Event('show.bs.modal')
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
      if (!that.$element.parent().length) {
Jacob Thornton's avatar
Jacob Thornton committed
61
        that.$element.appendTo(document.body) // don't move modals dom position
fat's avatar
fat committed
62
      }
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
      transition ?
77
78
        that.$element.one($.support.transition.end, function () { that.$element.focus().trigger('shown.bs.modal') }) :
        that.$element.focus().trigger('shown.bs.modal')
fat's avatar
fat committed
79
80
    })
  }
81

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

85
    e = $.Event('hide.bs.modal')
86

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

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

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

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

95
    $(document).off('focusin.bs.modal')
96

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

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

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

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

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

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

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

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

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

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

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

fat's avatar
fat committed
163
164
165
166
167
168
      this.$element.on('click', $.proxy(function (e) {
        if (e.target !== e.currentTarget) return
        this.options.backdrop == 'static'
          ? this.$element[0].focus.call(this.$element[0])
          : this.hide.call(this)
      }, this))
169

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

fat's avatar
fat committed
172
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
173

fat's avatar
fat committed
174
      if (!callback) return
175

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

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

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

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


fat's avatar
fat committed
193
194
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
195

196
197
  var old = $.fn.modal

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

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

210
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
211

212

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

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


fat's avatar
fat committed
222
223
  // MODAL DATA-API
  // ==============
224

225
  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
fat's avatar
fat committed
226
227
228
229
    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())
230
231
232
233
234
235

    e.preventDefault()

    $target
      .modal(option)
      .one('hide', function () {
236
        $this.is(':visible') && $this.focus()
237
      })
238
239
    })

240
    var $body = $(document.body)
Jacob Thornton's avatar
Jacob Thornton committed
241
242
      .on('shown.bs.modal',  '.modal', function () { $body.addClass('modal-open') })
      .on('hidden.bs.modal', '.modal', function () { $body.removeClass('modal-open') })
243

244
}(window.jQuery);