modal.js 6.79 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: modal.js v3.0.3
3
 * http://getbootstrap.com/javascript/#modals
4
 * ========================================================================
Zlatan Vasović's avatar
Zlatan Vasović committed
5
 * Copyright 2013 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
18
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
19
20


XhmikosR's avatar
XhmikosR committed
21
+function ($) { 'use strict';
22

fat's avatar
fat committed
23
24
  // MODAL CLASS DEFINITION
  // ======================
Jacob Thornton's avatar
Jacob Thornton committed
25

Jacob Thornton's avatar
Jacob Thornton committed
26
  var Modal = function (element, options) {
fat's avatar
fat committed
27
    this.options   = options
fat's avatar
fat committed
28
    this.$element  = $(element)
fat's avatar
fat committed
29
30
    this.$backdrop =
    this.isShown   = null
Jacob Thornton's avatar
Jacob Thornton committed
31

Jacob Thornton's avatar
Jacob Thornton committed
32
    if (this.options.remote) this.$element.load(this.options.remote)
fat's avatar
fat committed
33
  }
Jacob Thornton's avatar
Jacob Thornton committed
34

fat's avatar
fat committed
35
  Modal.DEFAULTS = {
Zlatan Vasović's avatar
Zlatan Vasović committed
36
37
38
    backdrop: true,
    keyboard: true,
    show: true
fat's avatar
fat committed
39
  }
40

Jacob Thornton's avatar
Jacob Thornton committed
41
42
  Modal.prototype.toggle = function (_relatedTarget) {
    return this[!this.isShown ? 'show' : 'hide'](_relatedTarget)
fat's avatar
fat committed
43
  }
Jacob Thornton's avatar
Jacob Thornton committed
44

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

fat's avatar
fat committed
49
    this.$element.trigger(e)
50

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

fat's avatar
fat committed
53
    this.isShown = true
54

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

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

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) {
Jacob Thornton's avatar
Jacob Thornton committed
63
        that.$element.appendTo(document.body) // don't move modals dom position
fat's avatar
fat committed
64
      }
65

fat's avatar
fat committed
66
      that.$element.show()
67

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

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

fat's avatar
fat committed
76
      that.enforceFocus()
77

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

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

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

93
    e = $.Event('hide.bs.modal')
94

fat's avatar
fat committed
95
    this.$element.trigger(e)
96

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

fat's avatar
fat committed
99
    this.isShown = false
100

fat's avatar
fat committed
101
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
102

103
    $(document).off('focusin.bs.modal')
104

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

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

fat's avatar
fat committed
117
  Modal.prototype.enforceFocus = function () {
Jacob Thornton's avatar
Jacob Thornton committed
118
119
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
fat's avatar
fat committed
120
121
122
123
124
      .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
125
  }
Jacob Thornton's avatar
Jacob Thornton committed
126

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

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

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

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

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

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

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

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

fat's avatar
fat committed
169
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
170

fat's avatar
fat committed
171
      if (!callback) return
172

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

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

Chris Rebert's avatar
Chris Rebert committed
182
      $.support.transition && this.$element.hasClass('fade') ?
183
184
185
        this.$backdrop
          .one($.support.transition.end, callback)
          .emulateTransitionEnd(150) :
fat's avatar
fat committed
186
        callback()
187

fat's avatar
fat committed
188
189
190
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
191
192
193
  }


fat's avatar
fat committed
194
195
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
196

197
198
  var old = $.fn.modal

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

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

211
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
212

213

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

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


fat's avatar
fat committed
223
224
  // MODAL DATA-API
  // ==============
225

226
  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
fat's avatar
fat committed
227
228
229
    var $this   = $(this)
    var href    = $this.attr('href')
    var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
Jacob Thornton's avatar
Jacob Thornton committed
230
    var option  = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
231
232
233
234

    e.preventDefault()

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

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

245
}(jQuery);