modal.js 7.53 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
  Modal.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
34
35
36
    backdrop: true,
    keyboard: true,
    show: true
fat's avatar
fat committed
37
  }
38

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

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

fat's avatar
fat committed
47
    this.$element.trigger(e)
48

fat's avatar
fat committed
49
    if (this.isShown || e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
50

fat's avatar
fat committed
51
    this.isShown = true
52

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

    this.setScrollbar()
fat's avatar
fat committed
57
    this.escape()
58

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

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

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

68
69
70
      that.$element
        .show()
        .scrollTop(0)
71

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

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

fat's avatar
fat committed
80
      that.enforceFocus()
81

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

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

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

97
    e = $.Event('hide.bs.modal')
98

fat's avatar
fat committed
99
    this.$element.trigger(e)
100

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

fat's avatar
fat committed
103
    this.isShown = false
104

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

    this.resetScrollbar()
fat's avatar
fat committed
108
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
109

110
    $(document).off('focusin.bs.modal')
111

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

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

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

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

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

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

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

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

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

167
      this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
fat's avatar
fat committed
168
169
170
171
172
        if (e.target !== e.currentTarget) return
        this.options.backdrop == 'static'
          ? this.$element[0].focus.call(this.$element[0])
          : this.hide.call(this)
      }, this))
173

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

fat's avatar
fat committed
176
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
177

fat's avatar
fat committed
178
      if (!callback) return
179

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

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

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

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

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

fat's avatar
fat committed
209
  Modal.prototype.setScrollbar =  function () {
210
211
    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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  }

  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
227

fat's avatar
fat committed
228
229
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
230

231
232
  var old = $.fn.modal

Jacob Thornton's avatar
Jacob Thornton committed
233
  $.fn.modal = function (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
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
246

247

fat's avatar
fat committed
248
249
  // MODAL NO CONFLICT
  // =================
250
251
252
253
254
255
256

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


fat's avatar
fat committed
257
258
  // MODAL DATA-API
  // ==============
259

260
  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
fat's avatar
fat committed
261
262
263
    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
264
    var option  = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
265

266
    if ($this.is('a')) e.preventDefault()
267
268

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

275
}(jQuery);