bootstrap-modal.js 5.21 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
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * http://twitter.github.com/bootstrap/javascript.html#modal
 * =========================================================
 * Copyright 2011 Twitter, Inc.
 *
 * 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 = $.extend({}, $.fn.modal.defaults, 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
        this.isShown = true
        this.$element.trigger('show')
49

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

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

56
57
58
59
60
61
62
63
64
65
          that.$element
            .show()

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

          that.$element.addClass('in')

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

        })
70
      }
71

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

Jacob Thornton's avatar
Jacob Thornton committed
75
        if (!this.isShown) return
76

77
78
        var that = this
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
79

80
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
81

82
83
84
        this.$element
          .trigger('hide')
          .removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
85

86
        $.support.transition && this.$element.hasClass('fade') ?
87
88
          hideWithTransition.call(this) :
          hideModal.call(this)
89
      }
Jacob Thornton's avatar
Jacob Thornton committed
90
91
92
93
94
95
96

  }


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

97
  function hideWithTransition() {
98
    var that = this
99
      , timeout = setTimeout(function () {
100
          that.$element.off($.support.transition.end)
101
102
103
          hideModal.call(that)
        }, 500)

104
    this.$element.one($.support.transition.end, function () {
105
106
107
108
109
      clearTimeout(timeout)
      hideModal.call(that)
    })
  }

110
  function hideModal( that ) {
111
112
113
114
115
116
    this.$element
      .hide()
      .trigger('hidden')

    backdrop.call(this)
  }
117

118
  function backdrop( callback ) {
119
120
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
Jacob Thornton's avatar
Jacob Thornton committed
121

122
    if (this.isShown && this.options.backdrop) {
123
124
      var doAnimate = $.support.transition && animate

125
126
127
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

128
      if (this.options.backdrop != 'static') {
129
130
131
        this.$backdrop.click($.proxy(this.hide, this))
      }

Jacob Thornton's avatar
Jacob Thornton committed
132
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
133
134
135
136

      this.$backdrop.addClass('in')

      doAnimate ?
137
        this.$backdrop.one($.support.transition.end, callback) :
138
139
        callback()

Jacob Thornton's avatar
Jacob Thornton committed
140
    } else if (!this.isShown && this.$backdrop) {
141
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
142

143
      $.support.transition && this.$element.hasClass('fade')?
144
        this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
145
146
        removeBackdrop.call(this)

Jacob Thornton's avatar
Jacob Thornton committed
147
148
    } else if (callback) {
      callback()
Jacob Thornton's avatar
Jacob Thornton committed
149
    }
150
  }
Jacob Thornton's avatar
Jacob Thornton committed
151

152
153
154
  function removeBackdrop() {
    this.$backdrop.remove()
    this.$backdrop = null
155
156
  }

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


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

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

183
  $.fn.modal.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
184
185
      backdrop: true
    , keyboard: true
186
187
  }

188
  $.fn.modal.Constructor = Modal
Jacob Thornton's avatar
Jacob Thornton committed
189

190

Jacob Thornton's avatar
Jacob Thornton committed
191
192
 /* MODAL DATA-API
  * ============== */
193

194
195
  $(function () {
    $('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) {
196
      var $this = $(this)
197
198
199
        , $target = $($this.attr('data-target') || $this.attr('href'))
        , option = $target.data('modal') ? 'toggle' : $.extend({}, $target.data(), $this.data())

Jacob Thornton's avatar
Jacob Thornton committed
200
      e.preventDefault()
201
      $target.modal(option)
202
203
204
    })
  })

205
}( window.jQuery )