bootstrap-modal.js 5.34 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
27
 /* MODAL PUBLIC CLASS DEFINITION
  * ============================= */

28
  var Modal = function ( content, options ) {
29
    this.settings = $.extend({}, $.fn.modal.defaults, options)
30
    this.$element = $(content)
31
      .delegate('.close', 'click.modal', $.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
32

33
34
    if ( this.settings.show ) {
      this.show()
35
    }
36

Jacob Thornton's avatar
Jacob Thornton committed
37
38
39
40
41
    return this
  }

  Modal.prototype = {

42
43
44
      toggle: function () {
        return this[!this.isShown ? 'show' : 'hide']()
      }
Jacob Thornton's avatar
Jacob Thornton committed
45

46
47
48
49
    , show: function () {
        var that = this
        this.isShown = true
        this.$element.trigger('show')
50

51
        escape.call(this)
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        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 ?
66
            that.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) :
67
68
69
            that.$element.trigger('shown')

        })
Jacob Thornton's avatar
Jacob Thornton committed
70

71
72
        return this
      }
73

74
75
    , hide: function (e) {
        e && e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
76

77
78
79
80
        if ( !this.isShown ) {
          return this
        }

81
82
        var that = this
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
83

84
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
85

86
87
88
        this.$element
          .trigger('hide')
          .removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
89

90
        $.support.transition && this.$element.hasClass('fade') ?
91
92
          hideWithTransition.call(this) :
          hideModal.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
93

94
95
        return this
      }
Jacob Thornton's avatar
Jacob Thornton committed
96
97
98
99
100
101
102

  }


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

103
  function hideWithTransition() {
104
    var that = this
105
      , timeout = setTimeout(function () {
106
          that.$element.unbind($.support.transition.end)
107
108
109
          hideModal.call(that)
        }, 500)

110
    this.$element.one($.support.transition.end, function () {
111
112
113
114
115
116
117
118
119
120
121
122
      clearTimeout(timeout)
      hideModal.call(that)
    })
  }

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

    backdrop.call(this)
  }
123

124
125
126
  function backdrop ( callback ) {
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
127
    if ( this.isShown && this.settings.backdrop ) {
128
129
      var doAnimate = $.support.transition && animate

130
131
132
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

133
134
135
136
137
138
139
140
141
142
143
      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 ?
144
        this.$backdrop.one($.support.transition.end, callback) :
145
146
        callback()

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

150
      $.support.transition && this.$element.hasClass('fade')?
151
        this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
152
153
        removeBackdrop.call(this)

154
155
    } else if ( callback ) {
       callback()
Jacob Thornton's avatar
Jacob Thornton committed
156
    }
157
  }
Jacob Thornton's avatar
Jacob Thornton committed
158

159
160
161
  function removeBackdrop() {
    this.$backdrop.remove()
    this.$backdrop = null
162
163
  }

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


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

Jacob Thornton's avatar
Jacob Thornton committed
181
  $.fn.modal = function ( options ) {
182
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
    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
208
209
  }

210
211
  $.fn.modal.Modal = Modal

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

218

219
220
 /* MODAL DATA- IMPLEMENTATION
  * ========================== */
221

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

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