bootstrap-modal.js 5.96 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* =========================================================
2
 * bootstrap-modal.js v1.3.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

Jacob Thornton's avatar
Jacob Thornton committed
23
24
25
26
27
 /* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
  * ======================================================= */

  var transitionEnd

28
  $(document).ready(function () {
Jacob Thornton's avatar
Jacob Thornton committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

    $.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
47
    }
Jacob Thornton's avatar
Jacob Thornton committed
48
49

  })
Jacob Thornton's avatar
Jacob Thornton committed
50
51
52
53
54


 /* MODAL PUBLIC CLASS DEFINITION
  * ============================= */

55
  var Modal = function ( content, options ) {
56
    this.settings = $.extend({}, $.fn.modal.defaults, options)
57
    this.$element = $(content)
58
      .delegate('[data-modal-dismiss]', 'click', $.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
59

60
61
    if ( this.settings.show ) {
      this.show()
62
    }
63

Jacob Thornton's avatar
Jacob Thornton committed
64
65
66
67
68
    return this
  }

  Modal.prototype = {

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

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

78
79
        escape.call(this)
        backdrop.call(this, function () {
80
81
          var transition = $.support.transition && that.$element.hasClass('fade')

Jacob Thornton's avatar
Jacob Thornton committed
82
          that.$element
83
84
85
            .appendTo(document.body)
            .show()

86
87
88
89
90
91
92
93
94
95
96
          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')

97
        })
Jacob Thornton's avatar
Jacob Thornton committed
98

99
100
        return this
      }
101

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

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

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

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

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

118
119
120
121
        function removeElement () {
          that.$element
            .hide()
            .trigger('hidden')
Jacob Thornton's avatar
Jacob Thornton committed
122

123
124
          backdrop.call(that)
        }
Jacob Thornton's avatar
Jacob Thornton committed
125

126
127
128
        $.support.transition && this.$element.hasClass('fade') ?
          this.$element.one(transitionEnd, removeElement) :
          removeElement()
Jacob Thornton's avatar
Jacob Thornton committed
129

130
131
        return this
      }
Jacob Thornton's avatar
Jacob Thornton committed
132
133
134
135
136
137
138

  }


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

139
140
141
142
  function backdrop ( callback ) {
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
    if ( this.isShown && this.settings.backdrop ) {
143
144
      var doAnimate = $.support.transition && animate

145
146
147
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

148
149
150
151
152
153
154
155
156
157
158
159
160
161
      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 ?
        this.$backdrop.one(transitionEnd, callback) :
        callback()

162
163
    } else if ( !this.isShown && this.$backdrop ) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
164

165
166
167
      function removeElement() {
        that.$backdrop.remove()
        that.$backdrop = null
Jacob Thornton's avatar
Jacob Thornton committed
168
169
      }

170
171
172
173
174
      $.support.transition && this.$element.hasClass('fade')?
        this.$backdrop.one(transitionEnd, removeElement) :
        removeElement()
    } else if ( callback ) {
       callback()
Jacob Thornton's avatar
Jacob Thornton committed
175
    }
176
  }
Jacob Thornton's avatar
Jacob Thornton committed
177

178
179
180
  function escape() {
    var that = this
    if ( this.isShown && this.settings.keyboard ) {
181
      $(document).bind('keyup.modal', function ( e ) {
182
183
184
185
186
        if ( e.which == 27 ) {
          that.hide()
        }
      })
    } else if ( !this.isShown ) {
187
      $(document).unbind('keyup.modal')
188
    }
Jacob Thornton's avatar
Jacob Thornton committed
189
190
191
192
193
194
  }


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

Jacob Thornton's avatar
Jacob Thornton committed
195
  $.fn.modal = function ( options ) {
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    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
222
223
  }

224
225
  $.fn.modal.Modal = Modal

226
227
  $.fn.modal.defaults = {
    backdrop: false
228
  , keyboard: false
229
  , show: false
230
231
  }

232

233
234
 /* MODAL DATA-IMPLEMENTATION
  * ========================= */
235

236
  $(document).ready(function () {
237
238
239
240
241
242
243
    $('body').delegate('[data-controls-modal]', 'click', function (e) {
      e.preventDefault()
      var $this = $(this).data('show', true)
      $('#' + $this.attr('data-controls-modal')).modal( $this.data() )
    })
  })

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