modal.js 7.94 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


Katie Zhu's avatar
Katie Zhu committed
10
11
12
13
14
(function (o_o) {
  typeof define  === 'function' && define.amd ? define(['jquery'], o_o) :
  typeof exports === 'object' ? o_o(require('jquery')) : o_o(this.jQuery)
})(function ($) {

Zlatan Vasović's avatar
Zlatan Vasović committed
15
  'use strict';
16

fat's avatar
fat committed
17
18
  // MODAL CLASS DEFINITION
  // ======================
Jacob Thornton's avatar
Jacob Thornton committed
19

Jacob Thornton's avatar
Jacob Thornton committed
20
  var Modal = function (element, options) {
21
22
23
24
25
26
    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
27

28
29
30
31
32
33
34
    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
35
  }
Jacob Thornton's avatar
Jacob Thornton committed
36

fat's avatar
fat committed
37
38
  Modal.VERSION  = '3.1.1'

fat's avatar
fat committed
39
  Modal.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
40
41
42
    backdrop: true,
    keyboard: true,
    show: true
fat's avatar
fat committed
43
  }
44

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

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

fat's avatar
fat committed
53
    this.$element.trigger(e)
54

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

fat's avatar
fat committed
57
    this.isShown = true
58

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

    this.setScrollbar()
fat's avatar
fat committed
63
    this.escape()
64

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

fat's avatar
fat committed
67
68
    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
69

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

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

fat's avatar
fat committed
78
79
80
      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }
81

fat's avatar
fat committed
82
83
84
      that.$element
        .addClass('in')
        .attr('aria-hidden', false)
85

fat's avatar
fat committed
86
      that.enforceFocus()
87

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

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

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

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

fat's avatar
fat committed
105
    this.$element.trigger(e)
106

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

fat's avatar
fat committed
109
    this.isShown = false
110

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

    this.resetScrollbar()
fat's avatar
fat committed
114
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
115

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

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

fat's avatar
fat committed
123
    $.support.transition && this.$element.hasClass('fade') ?
124
125
126
      this.$element
        .one($.support.transition.end, $.proxy(this.hideModal, this))
        .emulateTransitionEnd(300) :
fat's avatar
fat committed
127
128
      this.hideModal()
  }
Jacob Thornton's avatar
Jacob Thornton committed
129

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

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

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

fat's avatar
fat committed
158
  Modal.prototype.removeBackdrop = function () {
fat's avatar
fat committed
159
160
161
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
162

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

fat's avatar
fat committed
167
168
    if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate
169

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

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

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

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

fat's avatar
fat committed
184
      if (!callback) return
185

fat's avatar
fat committed
186
      doAnimate ?
187
188
189
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
190
        callback()
191

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

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

fat's avatar
fat committed
205
206
207
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
208
209
  }

210
211
212
213
214
  Modal.prototype.checkScrollbar = function () {
    if (document.body.clientWidth >= window.innerWidth) return
    this.scrollbarWidth = this.scrollbarWidth || this.measureScrollbar()
  }

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

  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
233

fat's avatar
fat committed
234
235
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
236

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

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

249
250
251
  var old = $.fn.modal

  $.fn.modal             = Plugin
252
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
253

254

fat's avatar
fat committed
255
256
  // MODAL NO CONFLICT
  // =================
257
258
259
260
261
262
263

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


fat's avatar
fat committed
264
265
  // MODAL DATA-API
  // ==============
266

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

273
    if ($this.is('a')) e.preventDefault()
274

Chris Rebert's avatar
Chris Rebert committed
275
276
277
278
279
    $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')
      })
280
    })
Chris Rebert's avatar
Chris Rebert committed
281
    Plugin.call($target, option, this)
Jacob Thornton's avatar
Jacob Thornton committed
282
  })
283

Katie Zhu's avatar
Katie Zhu committed
284
});