modal.js 7.92 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: modal.js v3.2.0
3
 * http://getbootstrap.com/javascript/#modals
4
 * ========================================================================
5
 * Copyright 2011-2014 Twitter, Inc.
6
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
8
9


10
11
+function ($) {
  'use strict';
12

13
14
  // MODAL CLASS DEFINITION
  // ======================
Jacob Thornton's avatar
Jacob Thornton committed
15

16
17
18
19
20
21
22
  var Modal = function (element, options) {
    this.options        = options
    this.$body          = $(document.body)
    this.$element       = $(element)
    this.$backdrop      =
    this.isShown        = null
    this.scrollbarWidth = 0
Jacob Thornton's avatar
Jacob Thornton committed
23

24
25
26
27
28
29
    if (this.options.remote) {
      this.$element
        .find('.modal-content')
        .load(this.options.remote, $.proxy(function () {
          this.$element.trigger('loaded.bs.modal')
        }, this))
30
    }
31
  }
Jacob Thornton's avatar
Jacob Thornton committed
32

Mark Otto's avatar
Mark Otto committed
33
  Modal.VERSION  = '3.2.0'
fat's avatar
fat committed
34

35
36
37
  Modal.TRANSITION_DURATION = 300
  Modal.BACKDROP_TRANSITION_DURATION = 150

38
39
40
41
42
  Modal.DEFAULTS = {
    backdrop: true,
    keyboard: true,
    show: true
  }
43

44
45
46
  Modal.prototype.toggle = function (_relatedTarget) {
    return this.isShown ? this.hide() : this.show(_relatedTarget)
  }
Jacob Thornton's avatar
Jacob Thornton committed
47

48
49
50
  Modal.prototype.show = function (_relatedTarget) {
    var that = this
    var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
Jacob Thornton's avatar
Jacob Thornton committed
51

52
    this.$element.trigger(e)
53

54
    if (this.isShown || e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
55

56
    this.isShown = true
57

58
59
    this.checkScrollbar()
    this.$body.addClass('modal-open')
fat's avatar
fat committed
60

61
62
    this.setScrollbar()
    this.escape()
63

64
    this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
fat's avatar
fat committed
65

66
67
    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
68

69
70
71
      if (!that.$element.parent().length) {
        that.$element.appendTo(that.$body) // don't move modals dom position
      }
72

73
74
75
      that.$element
        .show()
        .scrollTop(0)
76

77
78
79
      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }
80

81
82
83
      that.$element
        .addClass('in')
        .attr('aria-hidden', false)
84

85
      that.enforceFocus()
86

87
      var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
Jacob Thornton's avatar
Jacob Thornton committed
88

89
90
91
92
93
      transition ?
        that.$element.find('.modal-dialog') // wait for modal to slide in
          .one('bsTransitionEnd', function () {
            that.$element.trigger('focus').trigger(e)
          })
94
          .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
95
96
97
        that.$element.trigger('focus').trigger(e)
    })
  }
98

99
100
  Modal.prototype.hide = function (e) {
    if (e) e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
101

102
    e = $.Event('hide.bs.modal')
103

104
    this.$element.trigger(e)
105

106
    if (!this.isShown || e.isDefaultPrevented()) return
107

108
    this.isShown = false
109

110
    this.$body.removeClass('modal-open')
fat's avatar
fat committed
111

112
113
    this.resetScrollbar()
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
114

115
    $(document).off('focusin.bs.modal')
116

117
118
119
120
    this.$element
      .removeClass('in')
      .attr('aria-hidden', true)
      .off('click.dismiss.bs.modal')
Jacob Thornton's avatar
Jacob Thornton committed
121

122
123
124
    $.support.transition && this.$element.hasClass('fade') ?
      this.$element
        .one('bsTransitionEnd', $.proxy(this.hideModal, this))
125
        .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
      this.hideModal()
  }

  Modal.prototype.enforceFocus = function () {
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
      .on('focusin.bs.modal', $.proxy(function (e) {
        if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
          this.$element.trigger('focus')
        }
      }, this))
  }

  Modal.prototype.escape = function () {
    if (this.isShown && this.options.keyboard) {
141
      this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) {
142
143
144
        e.which == 27 && this.hide()
      }, this))
    } else if (!this.isShown) {
145
      this.$element.off('keydown.dismiss.bs.modal')
fat's avatar
fat committed
146
    }
147
  }
148

149
150
151
152
153
154
155
  Modal.prototype.hideModal = function () {
    var that = this
    this.$element.hide()
    this.backdrop(function () {
      that.$element.trigger('hidden.bs.modal')
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
156

157
158
159
160
  Modal.prototype.removeBackdrop = function () {
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
161

162
163
164
  Modal.prototype.backdrop = function (callback) {
    var that = this
    var animate = this.$element.hasClass('fade') ? 'fade' : ''
165

166
167
    if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate
168

169
170
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(this.$body)
171

172
      this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
173
174
175
176
177
        if (e.target !== e.currentTarget) return
        this.options.backdrop == 'static'
          ? this.$element[0].focus.call(this.$element[0])
          : this.hide.call(this)
      }, this))
178

179
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
180

181
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
182

183
      if (!callback) return
184

185
186
187
      doAnimate ?
        this.$backdrop
          .one('bsTransitionEnd', callback)
188
          .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) :
189
        callback()
190

191
192
    } else if (!this.isShown && this.$backdrop) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
193

194
195
196
      var callbackRemove = function () {
        that.removeBackdrop()
        callback && callback()
197
      }
198
199
200
      $.support.transition && this.$element.hasClass('fade') ?
        this.$backdrop
          .one('bsTransitionEnd', callbackRemove)
201
          .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) :
202
        callbackRemove()
Jacob Thornton's avatar
Jacob Thornton committed
203

204
205
    } else if (callback) {
      callback()
206
    }
207
  }
208

209
  Modal.prototype.checkScrollbar = function () {
210
    this.scrollbarWidth = this.measureScrollbar()
211
  }
fat's avatar
fat committed
212

213
214
215
216
  Modal.prototype.setScrollbar = function () {
    var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
    if (this.scrollbarWidth) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
  }
fat's avatar
fat committed
217

218
219
220
  Modal.prototype.resetScrollbar = function () {
    this.$body.css('padding-right', '')
  }
fat's avatar
fat committed
221

222
  Modal.prototype.measureScrollbar = function () { // thx walsh
223
    if (document.body.clientWidth >= window.innerWidth) return 0
224
225
226
227
228
229
230
    var scrollDiv = document.createElement('div')
    scrollDiv.className = 'modal-scrollbar-measure'
    this.$body.append(scrollDiv)
    var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
    this.$body[0].removeChild(scrollDiv)
    return scrollbarWidth
  }
Jacob Thornton's avatar
Jacob Thornton committed
231
232


233
234
  // MODAL PLUGIN DEFINITION
  // =======================
fat's avatar
fat committed
235

236
237
238
239
240
  function Plugin(option, _relatedTarget) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.modal')
      var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
Jacob Thornton's avatar
Jacob Thornton committed
241

242
243
244
245
246
      if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
      if (typeof option == 'string') data[option](_relatedTarget)
      else if (options.show) data.show(_relatedTarget)
    })
  }
247

248
  var old = $.fn.modal
Jacob Thornton's avatar
Jacob Thornton committed
249

250
251
  $.fn.modal             = Plugin
  $.fn.modal.Constructor = Modal
252

253

254
255
  // MODAL NO CONFLICT
  // =================
256

257
258
259
260
  $.fn.modal.noConflict = function () {
    $.fn.modal = old
    return this
  }
261

262

263
264
  // MODAL DATA-API
  // ==============
265

266
267
268
269
270
  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
    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('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
271

272
273
274
275
276
277
    if ($this.is('a')) e.preventDefault()

    $target.one('show.bs.modal', function (showEvent) {
      if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
      $target.one('hidden.bs.modal', function () {
        $this.is(':visible') && $this.trigger('focus')
Chris Rebert's avatar
Chris Rebert committed
278
      })
279
    })
280
    Plugin.call($target, option, this)
Jacob Thornton's avatar
Jacob Thornton committed
281
  })
282

283
}(jQuery);