bootstrap-modal.js 6.28 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* =========================================================
2
 * bootstrap-modal.js v1.4.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  "use strict"

 /* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
  * ======================================================= */

  var transitionEnd

  $(document).ready(function () {

    $.support.transition = (function () {
      var thisBody = document.body || document.documentElement
        , thisStyle = thisBody.style
        , support = thisStyle.transition !== undefined || thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.MsTransition !== undefined || thisStyle.OTransition !== undefined
      return support
    })()

    // set CSS transition event type
    if ( $.support.transition ) {
      transitionEnd = "TransitionEnd"
      if ( $.browser.webkit ) {
        transitionEnd = "webkitTransitionEnd"
      } else if ( $.browser.mozilla ) {
        transitionEnd = "transitionend"
      } else if ( $.browser.opera ) {
        transitionEnd = "oTransitionEnd"
      }
    }

  })


Jacob Thornton's avatar
Jacob Thornton committed
54
55
56
 /* MODAL PUBLIC CLASS DEFINITION
  * ============================= */

57
  var Modal = function ( content, options ) {
58
    this.settings = $.extend({}, $.fn.modal.defaults, options)
59
    this.$element = $(content)
60
      .delegate('.close', 'click.modal', $.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
61

62
63
    if ( this.settings.show ) {
      this.show()
64
    }
65

Jacob Thornton's avatar
Jacob Thornton committed
66
67
68
69
70
    return this
  }

  Modal.prototype = {

71
72
73
      toggle: function () {
        return this[!this.isShown ? 'show' : 'hide']()
      }
Jacob Thornton's avatar
Jacob Thornton committed
74

75
76
77
78
    , show: function () {
        var that = this
        this.isShown = true
        this.$element.trigger('show')
79

80
        escape.call(this)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        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 ?
            that.$element.one(transitionEnd, function () { that.$element.trigger('shown') }) :
            that.$element.trigger('shown')

        })
Jacob Thornton's avatar
Jacob Thornton committed
99

100
101
        return this
      }
102

103
104
    , hide: function (e) {
        e && e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
105

106
107
108
109
        if ( !this.isShown ) {
          return this
        }

110
111
        var that = this
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
112

113
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
114

115
116
117
        this.$element
          .trigger('hide')
          .removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
118

119
        $.support.transition && this.$element.hasClass('fade') ?
120
121
          hideWithTransition.call(this) :
          hideModal.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
122

123
124
        return this
      }
Jacob Thornton's avatar
Jacob Thornton committed
125
126
127
128
129
130
131

  }


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

132
133
  function hideWithTransition() {
    // firefox drops transitionEnd events :{o
134
    var that = this
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
      , timeout = setTimeout(function () {
          that.$element.unbind(transitionEnd)
          hideModal.call(that)
        }, 500)

    this.$element.one(transitionEnd, function () {
      clearTimeout(timeout)
      hideModal.call(that)
    })
  }

  function hideModal (that) {
    this.$element
      .hide()
      .trigger('hidden')

    backdrop.call(this)
  }
153

154
155
156
  function backdrop ( callback ) {
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
157
    if ( this.isShown && this.settings.backdrop ) {
158
159
      var doAnimate = $.support.transition && animate

160
161
162
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

163
164
165
166
167
168
169
170
171
172
173
      if ( this.settings.backdrop != 'static' ) {
        this.$backdrop.click($.proxy(this.hide, this))
      }

      if ( doAnimate ) {
        this.$backdrop[0].offsetWidth // force reflow
      }

      this.$backdrop.addClass('in')

      doAnimate ?
174
        this.$backdrop.one(transitionEnd, callback) :
175
176
        callback()

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

180
      $.support.transition && this.$element.hasClass('fade')?
181
182
183
        this.$backdrop.one(transitionEnd, $.proxy(removeBackdrop, this)) :
        removeBackdrop.call(this)

184
185
    } else if ( callback ) {
       callback()
Jacob Thornton's avatar
Jacob Thornton committed
186
    }
187
  }
Jacob Thornton's avatar
Jacob Thornton committed
188

189
190
191
  function removeBackdrop() {
    this.$backdrop.remove()
    this.$backdrop = null
192
193
  }

194
195
196
  function escape() {
    var that = this
    if ( this.isShown && this.settings.keyboard ) {
197
      $(document).bind('keyup.modal', function ( e ) {
198
199
200
201
202
        if ( e.which == 27 ) {
          that.hide()
        }
      })
    } else if ( !this.isShown ) {
203
      $(document).unbind('keyup.modal')
204
    }
Jacob Thornton's avatar
Jacob Thornton committed
205
206
207
208
209
210
  }


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

Jacob Thornton's avatar
Jacob Thornton committed
211
  $.fn.modal = function ( options ) {
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    var modal = this.data('modal')

    if (!modal) {

      if (typeof options == 'string') {
        options = {
          show: /show|toggle/.test(options)
        }
      }

      return this.each(function () {
        $(this).data('modal', new Modal(this, options))
      })
    }

    if ( options === true ) {
      return modal
    }

    if ( typeof options == 'string' ) {
      modal[options]()
    } else if ( modal ) {
      modal.toggle()
    }

    return this
Jacob Thornton's avatar
Jacob Thornton committed
238
239
  }

240
241
  $.fn.modal.Modal = Modal

242
243
  $.fn.modal.defaults = {
    backdrop: false
244
  , keyboard: false
245
  , show: false
246
247
  }

248

249
250
 /* MODAL DATA- IMPLEMENTATION
  * ========================== */
251

252
  $(document).ready(function () {
253
254
255
256
257
258
259
    $('body').delegate('[data-controls-modal]', 'click', function (e) {
      e.preventDefault()
      var $this = $(this).data('show', true)
      $('#' + $this.attr('data-controls-modal')).modal( $this.data() )
    })
  })

260
}( window.jQuery || window.ender );