bootstrap-modal.js 4.94 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* =========================================================
 * bootstrap-modal.js
 * 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 /* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
  * ======================================================= */

  var transitionEnd

  $(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
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)
Jacob Thornton's avatar
Jacob Thornton committed
57

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

62
    this.$element = $(content)
Jacob Thornton's avatar
Jacob Thornton committed
63
64
      .bind('modal:show', $.proxy(this.show, this))
      .bind('modal:hide', $.proxy(this.hide, this))
65
      .bind('modal:toggle', $.proxy(this.toggle, this))
Jacob Thornton's avatar
Jacob Thornton committed
66
      .delegate('.close', 'click', $.proxy(this.hide, this))
67

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

  Modal.prototype = {

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

Jacob Thornton's avatar
Jacob Thornton committed
77
  , show: function () {
Jacob Thornton's avatar
Jacob Thornton committed
78
      var that = this
Jacob Thornton's avatar
Jacob Thornton committed
79
      this.isShown = true
Jacob Thornton's avatar
Jacob Thornton committed
80

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

Jacob Thornton's avatar
Jacob Thornton committed
83
84
85
86
      _.backdrop.call(this, function () {
        that.$element
          .appendTo(document.body)
          .show()
87

Jacob Thornton's avatar
Jacob Thornton committed
88
89
90
91
92
93
        setTimeout(function () {
          that.$element
            .addClass('in')
            .trigger('modal:shown')
        }, 1)
      })
Jacob Thornton's avatar
Jacob Thornton committed
94
95
96
97

      return this
    }

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

Jacob Thornton's avatar
Jacob Thornton committed
101
102
      var that = this

Jacob Thornton's avatar
Jacob Thornton committed
103
      this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
104

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

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

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

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

117
      $.support.transition && this.$element.hasClass('fade') ?
118
        this.$element.one(transitionEnd, removeElement) :
Jacob Thornton's avatar
Jacob Thornton committed
119
120
121
122
123
124
125
126
127
128
129
        removeElement()

      return this
    }

  }


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

130
  var _ = {
Jacob Thornton's avatar
Jacob Thornton committed
131

Jacob Thornton's avatar
Jacob Thornton committed
132
    backdrop: function ( callback ) {
Jacob Thornton's avatar
Jacob Thornton committed
133
      var that = this
134
        , animate = this.$element.hasClass('fade') ? 'fade' : ''
Jacob Thornton's avatar
Jacob Thornton committed
135
      if ( this.isShown && this.settings.backdrop ) {
136
        this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
Jacob Thornton's avatar
Jacob Thornton committed
137
          .click($.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
138
          .appendTo(document.body)
Jacob Thornton's avatar
Jacob Thornton committed
139
140
141
142
143
144
145

        setTimeout(function () {
          that.$backdrop && that.$backdrop.addClass('in')
          $.support.transition && that.$backdrop.hasClass('fade') ?
            that.$backdrop.one(transitionEnd, callback) :
            callback()
        })
Jacob Thornton's avatar
Jacob Thornton committed
146
      } else if ( !this.isShown && this.$backdrop ) {
147
        this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
148
149
150
151
152
153

        function removeElement() {
          that.$backdrop.remove()
          that.$backdrop = null
        }

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

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

  }


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

Jacob Thornton's avatar
Jacob Thornton committed
181
  $.fn.modal = function ( options ) {
Jacob Thornton's avatar
Jacob Thornton committed
182
    options = options || {}
183
184
185
    return this.each(function () {
      return new Modal(this, options)
    })
Jacob Thornton's avatar
Jacob Thornton committed
186
187
  }

188
189
  $.fn.modal.Modal = Modal

190
191
  $.fn.modal.defaults = {
    backdrop: false
Jacob Thornton's avatar
Jacob Thornton committed
192
  , hideOnEscape: false
193
194
  }

Jacob Thornton's avatar
Jacob Thornton committed
195
})( jQuery || ender )