bootstrap-modal.js 5.14 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
54
55
56
57
58
59
60
61
62
63
64
        backdrop.call(this, function () {
          var transition = $.support.transition && that.$element.hasClass('fade')

          that.$element
            .appendTo(document.body)
            .show()

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

          that.$element.addClass('in')

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

        })
69
      }
70

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

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

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

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

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

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

  }


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

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

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

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

    backdrop.call(this)
  }
116

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

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

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

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

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

      this.$backdrop.addClass('in')

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

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

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

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

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

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


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

Jacob Thornton's avatar
Jacob Thornton committed
171
172
173
174
175
176
177
  $.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]()
178
      else if (data.options.show) data.show()
Jacob Thornton's avatar
Jacob Thornton committed
179
    })
Jacob Thornton's avatar
Jacob Thornton committed
180
181
  }

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

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

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
        , target = $this.attr('data-target') || $this.attr('href')
Jacob Thornton's avatar
Jacob Thornton committed
198
        , option = $(target).data('modal') ? 'toggle' : $this.data()
Jacob Thornton's avatar
Jacob Thornton committed
199
      e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
200
      $(target).modal(option)
201
202
203
    })
  })

204
}( window.jQuery )