modal.js 6.78 KB
Newer Older
1
/* ========================================================================
fat's avatar
fat committed
2
 * Bootstrap: modal.js v3.0.0
Chris Rebert's avatar
Chris Rebert committed
3
 * http://twbs.github.com/bootstrap/javascript.html#modals
4
 * ========================================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 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


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
36
37
38
39
  Modal.DEFAULTS = {
      backdrop: true
    , keyboard: true
    , show: true
  }
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 ?
81
82
        that.$element
          .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
153
    var that    = this
    var animate = this.$element.hasClass('fade') ? 'fade' : ''
154

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

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

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

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

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

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

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

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

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

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


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

198
199
  var old = $.fn.modal

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

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

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

214

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

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


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

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

    e.preventDefault()

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

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

246
}(window.jQuery);