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


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
+function () { 'use strict';

  (function (o_o) {
    typeof define  == 'function' && define.amd ? define(['jquery'], o_o) :
    typeof exports == 'object' ? o_o(require('jquery')) : o_o(jQuery)
  })(function ($) {

    // MODAL CLASS DEFINITION
    // ======================

    var Modal = function (element, options) {
      this.options        = options
      this.$body          = $(document.body)
      this.$element       = $(element)
      this.$backdrop      =
      this.isShown        = null
      this.scrollbarWidth = 0

      if (this.options.remote) {
        this.$element
          .find('.modal-content')
          .load(this.options.remote, $.proxy(function () {
            this.$element.trigger('loaded.bs.modal')
          }, this))
      }
    }
36

37
    Modal.VERSION  = '3.1.1'
Jacob Thornton's avatar
Jacob Thornton committed
38

39
40
41
42
43
    Modal.DEFAULTS = {
      backdrop: true,
      keyboard: true,
      show: true
    }
Jacob Thornton's avatar
Jacob Thornton committed
44

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

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

53
      this.$element.trigger(e)
54

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

57
      this.isShown = true
Jacob Thornton's avatar
Jacob Thornton committed
58

59
60
      this.checkScrollbar()
      this.$body.addClass('modal-open')
61

62
63
      this.setScrollbar()
      this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
64

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

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

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

74
75
76
        that.$element
          .show()
          .scrollTop(0)
fat's avatar
fat committed
77

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

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

86
        that.enforceFocus()
87

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

90
91
92
93
94
95
96
97
98
        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)
      })
    }
99

100
101
    Modal.prototype.hide = function (e) {
      if (e) e.preventDefault()
102

103
      e = $.Event('hide.bs.modal')
Jacob Thornton's avatar
Jacob Thornton committed
104

105
      this.$element.trigger(e)
106

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

109
      this.isShown = false
110

111
      this.$body.removeClass('modal-open')
112

113
114
      this.resetScrollbar()
      this.escape()
115

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

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

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

130
131
132
133
134
135
136
137
138
    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))
    }
139

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

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

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

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

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

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

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

180
        if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
181

182
        this.$backdrop.addClass('in')
183

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

186
187
188
189
190
        doAnimate ?
          this.$backdrop
            .one('bsTransitionEnd', callback)
            .emulateTransitionEnd(150) :
          callback()
191

192
193
      } else if (!this.isShown && this.$backdrop) {
        this.$backdrop.removeClass('in')
194

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

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

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

215
216
217
218
    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
219

220
221
222
    Modal.prototype.resetScrollbar = function () {
      this.$body.css('padding-right', '')
    }
fat's avatar
fat committed
223

224
225
226
227
228
229
230
231
    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
    }
fat's avatar
fat committed
232

Jacob Thornton's avatar
Jacob Thornton committed
233

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

237
238
239
240
241
    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)
fat's avatar
fat committed
242

243
244
245
246
247
        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)
      })
    }
Jacob Thornton's avatar
Jacob Thornton committed
248

249
    var old = $.fn.modal
250

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

254

255
256
    // MODAL NO CONFLICT
    // =================
257

258
259
260
261
    $.fn.modal.noConflict = function () {
      $.fn.modal = old
      return this
    }
262
263


264
265
    // MODAL DATA-API
    // ==============
266

267
268
269
270
271
    $(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())
272

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

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

Jacob Thornton's avatar
Jacob Thornton committed
284
  })
285

286
}();