modal.js 6.52 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
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
  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
109
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
      .on('focusin.bs.modal', function (e) {
fat's avatar
fat committed
110
111
      if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
        this.$element.focus()
112
      }
fat's avatar
fat committed
113
114
    }, this)
  }
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) {
118
      this.$element.on('keyup.dismiss.bs.modal', function ( e ) {
fat's avatar
fat committed
119
120
121
        e.which == 27 && this.hide()
      }, this)
    } 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
      this.$backdrop.click(
        this.options.backdrop == 'static' ?
          $.proxy(this.$element[0].focus, this.$element[0])
        : $.proxy(this.hide, this)
      )
168

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

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

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

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

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

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

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


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

195
196
  var old = $.fn.modal

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

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

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

211

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

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


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

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

    e.preventDefault()

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

239
    var $body = $(document.body)
240
241
      .on('bs.modal.shown',  '.modal', function () { $body.addClass('modal-open') })
      .on('bs.modal.hidden', '.modal', function () { $body.removeClass('modal-open') })
242

243
}(window.jQuery);