modal.js 6.59 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).on('click.dismiss.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
fat's avatar
fat committed
29
30
    this.$backdrop =
    this.isShown   = null
Jacob Thornton's avatar
Jacob Thornton committed
31

fat's avatar
fat committed
32
33
    if (this.options.remote) this.$element.find('.modal-body').load(this.options.remote)
  }
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

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

fat's avatar
fat committed
45
46
  Modal.prototype.show = function () {
    var that = this
47
    var e    = $.Event('show.bs.modal')
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.backdrop(function () {
      var transition = $.support.transition && that.$element.hasClass('fade')
59

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

fat's avatar
fat committed
64
      that.$element.show()
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

fat's avatar
fat committed
76
      transition ?
77
78
79
80
81
        that.$element
          .one($.support.transition.end, function () {
            that.$element.focus().trigger('shown.bs.modal')
          })
          .emulateTransitionEnd(300) :
82
        that.$element.focus().trigger('shown.bs.modal')
fat's avatar
fat committed
83
84
    })
  }
85

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

89
    e = $.Event('hide.bs.modal')
90

fat's avatar
fat committed
91
    this.$element.trigger(e)
92

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

fat's avatar
fat committed
95
    this.isShown = false
96

fat's avatar
fat committed
97
    this.escape()
Jacob Thornton's avatar
Jacob Thornton committed
98

99
    $(document).off('focusin.bs.modal')
100

fat's avatar
fat committed
101
102
103
    this.$element
      .removeClass('in')
      .attr('aria-hidden', true)
Jacob Thornton's avatar
Jacob Thornton committed
104

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

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

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

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

fat's avatar
fat committed
141
  Modal.prototype.removeBackdrop = function () {
fat's avatar
fat committed
142
143
144
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }
145

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

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

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

fat's avatar
fat committed
156
157
158
159
160
161
      this.$element.on('click', $.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))
162

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

fat's avatar
fat committed
165
      this.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
166

fat's avatar
fat committed
167
      if (!callback) return
168

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

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

fat's avatar
fat committed
178
      $.support.transition && this.$element.hasClass('fade')?
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
186
    } else if (callback) {
      callback()
    }
Jacob Thornton's avatar
Jacob Thornton committed
187
188
189
  }


fat's avatar
fat committed
190
191
  // MODAL PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
192

193
194
  var old = $.fn.modal

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

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

207
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
208

209

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

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


fat's avatar
fat committed
219
220
  // MODAL DATA-API
  // ==============
221

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

    e.preventDefault()

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

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

243
}(window.jQuery);