modal.js 7.27 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) {
fat's avatar
fat committed
17
    this.options   = options
fat's avatar
fat committed
18
    this.$body     = $(document.body)
fat's avatar
fat committed
19
    this.$element  = $(element)
fat's avatar
fat committed
20
21
    this.$backdrop =
    this.isShown   = null
Jacob Thornton's avatar
Jacob Thornton committed
22

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))
    }
fat's avatar
fat committed
30
  }
Jacob Thornton's avatar
Jacob Thornton committed
31

fat's avatar
fat committed
32
  Modal.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
33
34
35
    backdrop: true,
    keyboard: true,
    show: true
fat's avatar
fat committed
36
  }
37

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

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

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

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

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

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

    this.setScrollbar()
fat's avatar
fat committed
55
    this.escape()
56

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

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

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

66
67
68
      that.$element
        .show()
        .scrollTop(0)
69

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

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

fat's avatar
fat committed
78
      that.enforceFocus()
79

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

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

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

95
    e = $.Event('hide.bs.modal')
96

fat's avatar
fat committed
97
    this.$element.trigger(e)
98

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

fat's avatar
fat committed
101
    this.isShown = false
102

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

    this.resetScrollbar()
fat's avatar
fat committed
106
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
107

108
    $(document).off('focusin.bs.modal')
109

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

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

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

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

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

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

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

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

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

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

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

fat's avatar
fat committed
174
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
175

fat's avatar
fat committed
176
      if (!callback) return
177

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

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

Chris Rebert's avatar
Chris Rebert committed
187
      $.support.transition && this.$element.hasClass('fade') ?
188
189
190
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
191
        callback()
192

fat's avatar
fat committed
193
194
195
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
196
197
  }

fat's avatar
fat committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  Modal.prototype.setScrollbar =  function () {
    if (document.body.clientHeight <= window.innerHeight) return
    var scrollbarWidth = this.measureScrollbar()
    var bodyPad        = parseInt(this.$body.css('padding-right') || 0)
    if (scrollbarWidth) this.$body.css('padding-right', bodyPad + scrollbarWidth)
  }

  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
218

fat's avatar
fat committed
219
220
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
221

222
223
  var old = $.fn.modal

Jacob Thornton's avatar
Jacob Thornton committed
224
  $.fn.modal = function (option, _relatedTarget) {
Jacob Thornton's avatar
Jacob Thornton committed
225
    return this.each(function () {
fat's avatar
fat committed
226
      var $this   = $(this)
227
      var data    = $this.data('bs.modal')
fat's avatar
fat committed
228
229
      var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)

230
      if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
231
232
      if (typeof option == 'string') data[option](_relatedTarget)
      else if (options.show) data.show(_relatedTarget)
Jacob Thornton's avatar
Jacob Thornton committed
233
    })
Jacob Thornton's avatar
Jacob Thornton committed
234
235
  }

236
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
237

238

fat's avatar
fat committed
239
240
  // MODAL NO CONFLICT
  // =================
241
242
243
244
245
246
247

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


fat's avatar
fat committed
248
249
  // MODAL DATA-API
  // ==============
250

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

257
    if ($this.is('a')) e.preventDefault()
258
259

    $target
Jacob Thornton's avatar
Jacob Thornton committed
260
      .modal(option, this)
261
      .one('hide', function () {
262
        $this.is(':visible') && $this.trigger('focus')
263
      })
Jacob Thornton's avatar
Jacob Thornton committed
264
  })
265

266
}(jQuery);