modal.js 6.53 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: modal.js v3.1.0
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.$element  = $(element)
fat's avatar
fat committed
19
20
    this.$backdrop =
    this.isShown   = null
Jacob Thornton's avatar
Jacob Thornton committed
21

22
23
24
25
26
27
28
    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
29
  }
Jacob Thornton's avatar
Jacob Thornton committed
30

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

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

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

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

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

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

fat's avatar
fat committed
51
    this.escape()
52

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

fat's avatar
fat committed
55
56
    this.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
57

fat's avatar
fat committed
58
      if (!that.$element.parent().length) {
Jacob Thornton's avatar
Jacob Thornton committed
59
        that.$element.appendTo(document.body) // don't move modals dom position
fat's avatar
fat committed
60
      }
61

62
63
64
      that.$element
        .show()
        .scrollTop(0)
65

fat's avatar
fat committed
66
67
68
      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }
69

fat's avatar
fat committed
70
71
72
      that.$element
        .addClass('in')
        .attr('aria-hidden', false)
73

fat's avatar
fat committed
74
      that.enforceFocus()
75

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

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

fat's avatar
fat committed
88
  Modal.prototype.hide = function (e) {
fat's avatar
fat committed
89
    if (e) e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
90

91
    e = $.Event('hide.bs.modal')
92

fat's avatar
fat committed
93
    this.$element.trigger(e)
94

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

fat's avatar
fat committed
97
    this.isShown = false
98

fat's avatar
fat committed
99
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
100

101
    $(document).off('focusin.bs.modal')
102

fat's avatar
fat committed
103
104
105
    this.$element
      .removeClass('in')
      .attr('aria-hidden', true)
106
      .off('click.dismiss.bs.modal')
Jacob Thornton's avatar
Jacob Thornton committed
107

fat's avatar
fat committed
108
    $.support.transition && this.$element.hasClass('fade') ?
109
110
111
      this.$element
        .one($.support.transition.end, $.proxy(this.hideModal, this))
        .emulateTransitionEnd(300) :
fat's avatar
fat committed
112
113
      this.hideModal()
  }
Jacob Thornton's avatar
Jacob Thornton committed
114

fat's avatar
fat committed
115
  Modal.prototype.enforceFocus = function () {
Jacob Thornton's avatar
Jacob Thornton committed
116
117
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
fat's avatar
fat committed
118
119
120
121
122
      .on('focusin.bs.modal', $.proxy(function (e) {
        if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
          this.$element.focus()
        }
      }, this))
fat's avatar
fat committed
123
  }
Jacob Thornton's avatar
Jacob Thornton committed
124

fat's avatar
fat committed
125
126
  Modal.prototype.escape = function () {
    if (this.isShown && this.options.keyboard) {
fat's avatar
fat committed
127
      this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
fat's avatar
fat committed
128
        e.which == 27 && this.hide()
fat's avatar
fat committed
129
      }, this))
fat's avatar
fat committed
130
    } else if (!this.isShown) {
131
      this.$element.off('keyup.dismiss.bs.modal')
fat's avatar
fat committed
132
133
    }
  }
134

fat's avatar
fat committed
135
136
137
138
139
  Modal.prototype.hideModal = function () {
    var that = this
    this.$element.hide()
    this.backdrop(function () {
      that.removeBackdrop()
140
      that.$element.trigger('hidden.bs.modal')
fat's avatar
fat committed
141
142
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
143

fat's avatar
fat committed
144
  Modal.prototype.removeBackdrop = function () {
fat's avatar
fat committed
145
146
147
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
148

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

fat's avatar
fat committed
152
153
    if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate
154

fat's avatar
fat committed
155
156
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)
157

158
      this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
fat's avatar
fat committed
159
160
161
162
163
        if (e.target !== e.currentTarget) return
        this.options.backdrop == 'static'
          ? this.$element[0].focus.call(this.$element[0])
          : this.hide.call(this)
      }, this))
164

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

fat's avatar
fat committed
167
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
168

fat's avatar
fat committed
169
      if (!callback) return
170

fat's avatar
fat committed
171
      doAnimate ?
172
173
174
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
175
        callback()
176

fat's avatar
fat committed
177
178
    } else if (!this.isShown && this.$backdrop) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
179

Chris Rebert's avatar
Chris Rebert committed
180
      $.support.transition && this.$element.hasClass('fade') ?
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
188
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
189
190
191
  }


fat's avatar
fat committed
192
193
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
194

195
196
  var old = $.fn.modal

Jacob Thornton's avatar
Jacob Thornton committed
197
  $.fn.modal = function (option, _relatedTarget) {
Jacob Thornton's avatar
Jacob Thornton committed
198
    return this.each(function () {
fat's avatar
fat committed
199
      var $this   = $(this)
200
      var data    = $this.data('bs.modal')
fat's avatar
fat committed
201
202
      var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)

203
      if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
204
205
      if (typeof option == 'string') data[option](_relatedTarget)
      else if (options.show) data.show(_relatedTarget)
Jacob Thornton's avatar
Jacob Thornton committed
206
    })
Jacob Thornton's avatar
Jacob Thornton committed
207
208
  }

209
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
210

211

fat's avatar
fat committed
212
213
  // MODAL NO CONFLICT
  // =================
214
215
216
217
218
219
220

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


fat's avatar
fat committed
221
222
  // MODAL DATA-API
  // ==============
223

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

230
    if ($this.is('a')) e.preventDefault()
231
232

    $target
Jacob Thornton's avatar
Jacob Thornton committed
233
      .modal(option, this)
234
      .one('hide', function () {
235
        $this.is(':visible') && $this.focus()
236
      })
Jacob Thornton's avatar
Jacob Thornton committed
237
  })
238

Jacob Thornton's avatar
Jacob Thornton committed
239
  $(document)
240
    .on('show.bs.modal', '.modal', function () { $(document.body).addClass('modal-open') })
Jacob Thornton's avatar
Jacob Thornton committed
241
    .on('hidden.bs.modal', '.modal', function () { $(document.body).removeClass('modal-open') })
242

243
}(jQuery);