bootstrap-modal.js 5.4 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* =========================================================
Jacob Thornton's avatar
Jacob Thornton committed
2
 * bootstrap-modal.js v2.0.0
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
Jacob Thornton's avatar
Jacob Thornton committed
44
45
46

        if (this.isShown) return

47
48
        $('body').addClass('modal-open')

49
50
        this.isShown = true
        this.$element.trigger('show')
51

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

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

58
59
60
61
62
63
64
65
66
67
          that.$element
            .show()

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

          that.$element.addClass('in')

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

        })
72
      }
73

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

Jacob Thornton's avatar
Jacob Thornton committed
77
        if (!this.isShown) return
78

79
80
        var that = this
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
81

82
83
        $('body').removeClass('modal-open')

84
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
85

86
87
88
        this.$element
          .trigger('hide')
          .removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
89

90
        $.support.transition && this.$element.hasClass('fade') ?
91
92
          hideWithTransition.call(this) :
          hideModal.call(this)
93
      }
Jacob Thornton's avatar
Jacob Thornton committed
94
95
96
97
98
99
100

  }


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

101
  function hideWithTransition() {
102
    var that = this
103
      , timeout = setTimeout(function () {
104
          that.$element.off($.support.transition.end)
105
106
107
          hideModal.call(that)
        }, 500)

108
    this.$element.one($.support.transition.end, function () {
109
110
111
112
113
      clearTimeout(timeout)
      hideModal.call(that)
    })
  }

114
  function hideModal( that ) {
115
116
117
118
119
120
    this.$element
      .hide()
      .trigger('hidden')

    backdrop.call(this)
  }
121

122
  function backdrop( callback ) {
123
124
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
Jacob Thornton's avatar
Jacob Thornton committed
125

126
    if (this.isShown && this.options.backdrop) {
127
128
      var doAnimate = $.support.transition && animate

129
130
131
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

132
      if (this.options.backdrop != 'static') {
133
134
135
        this.$backdrop.click($.proxy(this.hide, this))
      }

Jacob Thornton's avatar
Jacob Thornton committed
136
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
137
138
139
140

      this.$backdrop.addClass('in')

      doAnimate ?
141
        this.$backdrop.one($.support.transition.end, callback) :
142
143
        callback()

Jacob Thornton's avatar
Jacob Thornton committed
144
    } else if (!this.isShown && this.$backdrop) {
145
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
146

147
      $.support.transition && this.$element.hasClass('fade')?
148
        this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
149
150
        removeBackdrop.call(this)

Jacob Thornton's avatar
Jacob Thornton committed
151
152
    } else if (callback) {
      callback()
Jacob Thornton's avatar
Jacob Thornton committed
153
    }
154
  }
Jacob Thornton's avatar
Jacob Thornton committed
155

156
157
158
  function removeBackdrop() {
    this.$backdrop.remove()
    this.$backdrop = null
159
160
  }

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


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

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

187
  $.fn.modal.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
188
189
      backdrop: true
    , keyboard: true
190
    , show: true
191
192
  }

193
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
194

195

Jacob Thornton's avatar
Jacob Thornton committed
196
197
 /* MODAL DATA-API
  * ============== */
198

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

Jacob Thornton's avatar
Jacob Thornton committed
205
      e.preventDefault()
206
      $target.modal(option)
207
208
209
    })
  })

210
}( window.jQuery )