bootstrap-modal.js 5.51 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* =========================================================
Jacob Thornton's avatar
Jacob Thornton committed
2
 * bootstrap-modal.js v2.0.2
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#modals
Jacob Thornton's avatar
Jacob Thornton committed
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
18
19
20
 *
 * 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.
 * ========================================================= */


21
!function ( $ ) {
Jacob Thornton's avatar
Jacob Thornton committed
22

23
24
  "use strict"

Jacob Thornton's avatar
Jacob Thornton committed
25
26
 /* MODAL CLASS DEFINITION
  * ====================== */
Jacob Thornton's avatar
Jacob Thornton committed
27

28
  var Modal = function ( content, options ) {
29
    this.options = options
30
    this.$element = $(content)
Jacob Thornton's avatar
Jacob Thornton committed
31
      .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
32
33
34
35
  }

  Modal.prototype = {

36
37
38
      constructor: Modal

    , toggle: function () {
39
40
        return this[!this.isShown ? 'show' : 'hide']()
      }
Jacob Thornton's avatar
Jacob Thornton committed
41

42
43
    , show: function () {
        var that = this
44
          , e = $.Event('show')
Jacob Thornton's avatar
Jacob Thornton committed
45

46
47
48
        this.$element.trigger(e)

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

50
51
        $('body').addClass('modal-open')

52
        this.isShown = true
53

54
        escape.call(this)
55
56
57
        backdrop.call(this, function () {
          var transition = $.support.transition && that.$element.hasClass('fade')

Jacob Thornton's avatar
Jacob Thornton committed
58
          !that.$element.parent().length && that.$element.appendTo(document.body) //don't move modals dom position
59

60
61
62
63
64
65
66
67
68
69
          that.$element
            .show()

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

          that.$element.addClass('in')

          transition ?
70
            that.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) :
71
72
73
            that.$element.trigger('shown')

        })
74
      }
75

Jacob Thornton's avatar
Jacob Thornton committed
76
    , hide: function ( e ) {
77
        e && e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
78

79
        var that = this
80
81
82
83
84
85
86

        e = $.Event('hide')

        this.$element.trigger(e)

        if (!this.isShown || e.isDefaultPrevented()) return

87
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
88

89
90
        $('body').removeClass('modal-open')

91
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
92

93
        this.$element.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
94

95
        $.support.transition && this.$element.hasClass('fade') ?
96
97
          hideWithTransition.call(this) :
          hideModal.call(this)
98
      }
Jacob Thornton's avatar
Jacob Thornton committed
99
100
101
102
103
104
105

  }


 /* MODAL PRIVATE METHODS
  * ===================== */

106
  function hideWithTransition() {
107
    var that = this
108
      , timeout = setTimeout(function () {
109
          that.$element.off($.support.transition.end)
110
111
112
          hideModal.call(that)
        }, 500)

113
    this.$element.one($.support.transition.end, function () {
114
115
116
117
118
      clearTimeout(timeout)
      hideModal.call(that)
    })
  }

119
  function hideModal( that ) {
120
121
122
123
124
125
    this.$element
      .hide()
      .trigger('hidden')

    backdrop.call(this)
  }
126

127
  function backdrop( callback ) {
128
129
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
Jacob Thornton's avatar
Jacob Thornton committed
130

131
    if (this.isShown && this.options.backdrop) {
132
133
      var doAnimate = $.support.transition && animate

134
135
136
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

137
      if (this.options.backdrop != 'static') {
138
139
140
        this.$backdrop.click($.proxy(this.hide, this))
      }

Jacob Thornton's avatar
Jacob Thornton committed
141
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
142
143
144
145

      this.$backdrop.addClass('in')

      doAnimate ?
146
        this.$backdrop.one($.support.transition.end, callback) :
147
148
        callback()

Jacob Thornton's avatar
Jacob Thornton committed
149
    } else if (!this.isShown && this.$backdrop) {
150
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
151

152
      $.support.transition && this.$element.hasClass('fade')?
153
        this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
154
155
        removeBackdrop.call(this)

Jacob Thornton's avatar
Jacob Thornton committed
156
157
    } else if (callback) {
      callback()
Jacob Thornton's avatar
Jacob Thornton committed
158
    }
159
  }
Jacob Thornton's avatar
Jacob Thornton committed
160

161
162
163
  function removeBackdrop() {
    this.$backdrop.remove()
    this.$backdrop = null
164
165
  }

166
167
  function escape() {
    var that = this
168
    if (this.isShown && this.options.keyboard) {
169
      $(document).on('keyup.dismiss.modal', function ( e ) {
Jacob Thornton's avatar
Jacob Thornton committed
170
        e.which == 27 && that.hide()
171
      })
Jacob Thornton's avatar
Jacob Thornton committed
172
    } else if (!this.isShown) {
173
      $(document).off('keyup.dismiss.modal')
174
    }
Jacob Thornton's avatar
Jacob Thornton committed
175
176
177
178
179
180
  }


 /* MODAL PLUGIN DEFINITION
  * ======================= */

Jacob Thornton's avatar
Jacob Thornton committed
181
182
183
184
  $.fn.modal = function ( option ) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('modal')
185
        , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option)
Jacob Thornton's avatar
Jacob Thornton committed
186
187
      if (!data) $this.data('modal', (data = new Modal(this, options)))
      if (typeof option == 'string') data[option]()
188
      else if (options.show) data.show()
Jacob Thornton's avatar
Jacob Thornton committed
189
    })
Jacob Thornton's avatar
Jacob Thornton committed
190
191
  }

192
  $.fn.modal.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
193
194
      backdrop: true
    , keyboard: true
195
    , show: true
196
197
  }

198
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
199

200

Jacob Thornton's avatar
Jacob Thornton committed
201
202
 /* MODAL DATA-API
  * ============== */
203

204
205
  $(function () {
    $('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) {
Jacob Thornton's avatar
Jacob Thornton committed
206
207
      var $this = $(this), href
        , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
208
209
        , option = $target.data('modal') ? 'toggle' : $.extend({}, $target.data(), $this.data())

Jacob Thornton's avatar
Jacob Thornton committed
210
      e.preventDefault()
211
      $target.modal(option)
212
213
214
    })
  })

215
}( window.jQuery );