modal.js 7.76 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
38
39
  Modal.DEFAULTS = {
    backdrop: true,
    keyboard: true,
    show: true
  }
40

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

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

49
    this.$element.trigger(e)
50

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

53
    this.isShown = true
54

55
56
    this.checkScrollbar()
    this.$body.addClass('modal-open')
fat's avatar
fat committed
57

58
59
    this.setScrollbar()
    this.escape()
60

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

63
64
    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
65

66
67
68
      if (!that.$element.parent().length) {
        that.$element.appendTo(that.$body) // don't move modals dom position
      }
69

70
71
72
      that.$element
        .show()
        .scrollTop(0)
73

74
75
76
      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }
77

78
79
80
      that.$element
        .addClass('in')
        .attr('aria-hidden', false)
81

82
      that.enforceFocus()
83

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

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

96
97
  Modal.prototype.hide = function (e) {
    if (e) e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
98

99
    e = $.Event('hide.bs.modal')
100

101
    this.$element.trigger(e)
102

103
    if (!this.isShown || e.isDefaultPrevented()) return
104

105
    this.isShown = false
106

107
    this.$body.removeClass('modal-open')
fat's avatar
fat committed
108

109
110
    this.resetScrollbar()
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
111

112
    $(document).off('focusin.bs.modal')
113

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

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    $.support.transition && this.$element.hasClass('fade') ?
      this.$element
        .one('bsTransitionEnd', $.proxy(this.hideModal, this))
        .emulateTransitionEnd(300) :
      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) {
138
      this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) {
139
140
141
        e.which == 27 && this.hide()
      }, this))
    } else if (!this.isShown) {
142
      this.$element.off('keydown.dismiss.bs.modal')
fat's avatar
fat committed
143
    }
144
  }
145

146
147
148
149
150
151
152
  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
153

154
155
156
157
  Modal.prototype.removeBackdrop = function () {
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
158

159
160
161
  Modal.prototype.backdrop = function (callback) {
    var that = this
    var animate = this.$element.hasClass('fade') ? 'fade' : ''
162

163
164
    if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate
165

166
167
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(this.$body)
168

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

176
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
177

178
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
179

180
      if (!callback) return
181

182
183
184
185
186
      doAnimate ?
        this.$backdrop
          .one('bsTransitionEnd', callback)
          .emulateTransitionEnd(150) :
        callback()
187

188
189
    } else if (!this.isShown && this.$backdrop) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
190

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

201
202
    } else if (callback) {
      callback()
203
    }
204
  }
205

206
207
208
209
  Modal.prototype.checkScrollbar = function () {
    if (document.body.clientWidth >= window.innerWidth) return
    this.scrollbarWidth = this.scrollbarWidth || this.measureScrollbar()
  }
fat's avatar
fat committed
210

211
212
213
214
  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
215

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

220
221
222
223
224
225
226
227
  Modal.prototype.measureScrollbar = function () { // thx walsh
    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
228
229


230
231
  // MODAL PLUGIN DEFINITION
  // =======================
fat's avatar
fat committed
232

233
234
235
236
237
  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
238

239
240
241
242
243
      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)
    })
  }
244

245
  var old = $.fn.modal
Jacob Thornton's avatar
Jacob Thornton committed
246

247
248
  $.fn.modal             = Plugin
  $.fn.modal.Constructor = Modal
249

250

251
252
  // MODAL NO CONFLICT
  // =================
253

254
255
256
257
  $.fn.modal.noConflict = function () {
    $.fn.modal = old
    return this
  }
258

259

260
261
  // MODAL DATA-API
  // ==============
262

263
264
265
266
267
  $(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())
268

269
270
271
272
273
274
    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
275
      })
276
    })
277
    Plugin.call($target, option, this)
Jacob Thornton's avatar
Jacob Thornton committed
278
  })
279

280
}(jQuery);