modal.js 7.59 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: modal.js v3.1.1
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


Zlatan Vasović's avatar
Zlatan Vasović committed
10
11
+function ($) {
  'use strict';
12

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

Jacob Thornton's avatar
Jacob Thornton committed
16
  var Modal = function (element, options) {
17
18
19
20
21
22
    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
30
    if (this.options.remote) {
      this.$element
        .find('.modal-content')
        .load(this.options.remote, $.proxy(function () {
          this.$element.trigger('loaded.bs.modal')
        }, this))
    }
fat's avatar
fat committed
31
  }
Jacob Thornton's avatar
Jacob Thornton committed
32

fat's avatar
fat committed
33
34
  Modal.VERSION  = '3.1.1'

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

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

Jacob Thornton's avatar
Jacob Thornton committed
45
  Modal.prototype.show = function (_relatedTarget) {
fat's avatar
fat committed
46
    var that = this
Jacob Thornton's avatar
Jacob Thornton committed
47
    var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
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

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

    this.setScrollbar()
fat's avatar
fat committed
59
    this.escape()
60

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

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

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

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

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

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

fat's avatar
fat committed
82
      that.enforceFocus()
83

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

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

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

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

fat's avatar
fat committed
101
    this.$element.trigger(e)
102

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

fat's avatar
fat committed
105
    this.isShown = false
106

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

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

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

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

fat's avatar
fat committed
119
    $.support.transition && this.$element.hasClass('fade') ?
120
121
122
      this.$element
        .one($.support.transition.end, $.proxy(this.hideModal, this))
        .emulateTransitionEnd(300) :
fat's avatar
fat committed
123
124
      this.hideModal()
  }
Jacob Thornton's avatar
Jacob Thornton committed
125

fat's avatar
fat committed
126
  Modal.prototype.enforceFocus = function () {
Jacob Thornton's avatar
Jacob Thornton committed
127
128
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
fat's avatar
fat committed
129
130
      .on('focusin.bs.modal', $.proxy(function (e) {
        if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
131
          this.$element.trigger('focus')
fat's avatar
fat committed
132
133
        }
      }, this))
fat's avatar
fat committed
134
  }
Jacob Thornton's avatar
Jacob Thornton committed
135

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

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

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

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

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

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

169
      this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
fat's avatar
fat committed
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

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

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

fat's avatar
fat committed
180
      if (!callback) return
181

fat's avatar
fat committed
182
      doAnimate ?
183
184
185
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
186
        callback()
187

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

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

fat's avatar
fat committed
201
202
203
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
204
205
  }

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

fat's avatar
fat committed
211
  Modal.prototype.setScrollbar =  function () {
212
213
    var bodyPad = parseInt(this.$body.css('padding-right') || 0)
    if (this.scrollbarWidth) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
fat's avatar
fat committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  }

  Modal.prototype.resetScrollbar = function () {
    this.$body.css('padding-right', '')
  }

  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
229

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

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

239
      if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
240
241
      if (typeof option == 'string') data[option](_relatedTarget)
      else if (options.show) data.show(_relatedTarget)
Jacob Thornton's avatar
Jacob Thornton committed
242
    })
Jacob Thornton's avatar
Jacob Thornton committed
243
244
  }

245
246
247
  var old = $.fn.modal

  $.fn.modal             = Plugin
248
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
249

250

fat's avatar
fat committed
251
252
  // MODAL NO CONFLICT
  // =================
253
254
255
256
257
258
259

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


fat's avatar
fat committed
260
261
  // MODAL DATA-API
  // ==============
262

263
  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
fat's avatar
fat committed
264
265
266
    var $this   = $(this)
    var href    = $this.attr('href')
    var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
fat's avatar
fat committed
267
    var option  = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
268

269
    if ($this.is('a')) e.preventDefault()
270

271
    Plugin.call($target, option, this)
272
    $target.one('hide.bs.modal', function () {
273
274
      $this.is(':visible') && $this.trigger('focus')
    })
Jacob Thornton's avatar
Jacob Thornton committed
275
  })
276

277
}(jQuery);