bootstrap-modal.js 5.67 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.
 * ========================================================= */


Jacob Thornton's avatar
Jacob Thornton committed
21
22
(function( $ ){

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)
57
58
    this.$element = $(content)
      .delegate('.close', 'click.modal', $.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
59

60
    if ( options ) {
Jacob Thornton's avatar
Jacob Thornton committed
61
62
      $.extend( this.settings, options )

63
64
65
66
      if ( options.show ) {
        this.show()
      }
    }
67

Jacob Thornton's avatar
Jacob Thornton committed
68
69
70
71
72
    return this
  }

  Modal.prototype = {

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

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

82
83
        escape.call(this)
        backdrop.call(this, function () {
Jacob Thornton's avatar
Jacob Thornton committed
84
          that.$element
85
86
87
88
89
90
91
            .appendTo(document.body)
            .show()

          setTimeout(function () {
            that.$element
              .addClass('in')
              .trigger('shown')
92
          }, 0)
93
        })
Jacob Thornton's avatar
Jacob Thornton committed
94

95
96
        return this
      }
97

98
99
    , hide: function (e) {
        e && e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
100

101
102
        var that = this
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
103

104
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
105

106
107
108
        this.$element
          .trigger('hide')
          .removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
109

110
111
112
113
        function removeElement () {
          that.$element
            .hide()
            .trigger('hidden')
Jacob Thornton's avatar
Jacob Thornton committed
114

115
116
          backdrop.call(that)
        }
Jacob Thornton's avatar
Jacob Thornton committed
117

118
119
120
        $.support.transition && this.$element.hasClass('fade') ?
          this.$element.one(transitionEnd, removeElement) :
          removeElement()
Jacob Thornton's avatar
Jacob Thornton committed
121

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

  }


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

131
132
133
134
135
  function backdrop ( callback ) {
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
    if ( this.isShown && this.settings.backdrop ) {
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
136
137
138
139
        .appendTo(document.body)

      if ( this.settings.backdrop != 'static' ) {
        this.$backdrop.click($.proxy(this.hide, this))
140
      }
141
142
143
144
145
146

      setTimeout(function () {
        that.$backdrop && that.$backdrop.addClass('in')
        $.support.transition && that.$backdrop.hasClass('fade') ?
          that.$backdrop.one(transitionEnd, callback) :
          callback()
147
148
      }, 0)

149
150
    } else if ( !this.isShown && this.$backdrop ) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
151

152
153
154
      function removeElement() {
        that.$backdrop.remove()
        that.$backdrop = null
Jacob Thornton's avatar
Jacob Thornton committed
155
156
      }

157
158
159
160
161
      $.support.transition && this.$element.hasClass('fade')?
        this.$backdrop.one(transitionEnd, removeElement) :
        removeElement()
    } else if ( callback ) {
       callback()
Jacob Thornton's avatar
Jacob Thornton committed
162
    }
163
  }
Jacob Thornton's avatar
Jacob Thornton committed
164

165
166
167
168
169
170
171
172
173
174
175
  function escape() {
    var that = this
    if ( this.isShown && this.settings.keyboard ) {
      $('body').bind('keyup.modal', function ( e ) {
        if ( e.which == 27 ) {
          that.hide()
        }
      })
    } else if ( !this.isShown ) {
      $('body').unbind('keyup.modal')
    }
Jacob Thornton's avatar
Jacob Thornton committed
176
177
178
179
180
181
  }


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

Jacob Thornton's avatar
Jacob Thornton committed
182
  $.fn.modal = function ( options ) {
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    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
209
210
  }

211
212
  $.fn.modal.Modal = Modal

213
214
  $.fn.modal.defaults = {
    backdrop: false
215
216
  , keyboard: false
  , show: true
217
218
  }

219
220
221
222

 /* MODAL DATA- IMPLEMENTATION
  * ========================== */

223
  $(document).ready(function () {
224
225
226
227
228
229
230
    $('body').delegate('[data-controls-modal]', 'click', function (e) {
      e.preventDefault()
      var $this = $(this).data('show', true)
      $('#' + $this.attr('data-controls-modal')).modal( $this.data() )
    })
  })

231
})( window.jQuery || window.ender )