bootstrap-modal.js 5.15 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
 /* MODAL CLASS DEFINITION
  * ====================== */
Jacob Thornton's avatar
Jacob Thornton committed
27

28
  var Modal = function ( content, options ) {
29
    this.settings = $.extend({}, $.fn.modal.defaults, options)
30
    this.$element = $(content)
Jacob Thornton's avatar
Jacob Thornton committed
31
32
      .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
    this.settings.show && this.show()
Jacob Thornton's avatar
Jacob Thornton committed
33
34
35
36
  }

  Modal.prototype = {

37
38
39
      toggle: function () {
        return this[!this.isShown ? 'show' : 'hide']()
      }
Jacob Thornton's avatar
Jacob Thornton committed
40

41
42
    , show: function () {
        var that = this
Jacob Thornton's avatar
Jacob Thornton committed
43
44
45

        if (this.isShown) return

46
47
        this.isShown = true
        this.$element.trigger('show')
48

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

        })
68
      }
69

Jacob Thornton's avatar
Jacob Thornton committed
70
    , hide: function ( e ) {
71
        e && e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
72

Jacob Thornton's avatar
Jacob Thornton committed
73
        if (!this.isShown) return
74

75
76
        var that = this
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
77

78
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
79

80
81
82
        this.$element
          .trigger('hide')
          .removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
83

84
        $.support.transition && this.$element.hasClass('fade') ?
85
86
          hideWithTransition.call(this) :
          hideModal.call(this)
87
      }
Jacob Thornton's avatar
Jacob Thornton committed
88
89
90
91
92
93
94

  }


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

95
  function hideWithTransition() {
96
    var that = this
97
      , timeout = setTimeout(function () {
98
          that.$element.unbind($.support.transition.end)
99
100
101
          hideModal.call(that)
        }, 500)

102
    this.$element.one($.support.transition.end, function () {
103
104
105
106
107
108
109
110
111
112
113
114
      clearTimeout(timeout)
      hideModal.call(that)
    })
  }

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

    backdrop.call(this)
  }
115

116
117
118
  function backdrop ( callback ) {
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
Jacob Thornton's avatar
Jacob Thornton committed
119
120

    if (this.isShown && this.settings.backdrop) {
121
122
      var doAnimate = $.support.transition && animate

123
124
125
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

Jacob Thornton's avatar
Jacob Thornton committed
126
      if (this.settings.backdrop != 'static') {
127
128
129
        this.$backdrop.click($.proxy(this.hide, this))
      }

Jacob Thornton's avatar
Jacob Thornton committed
130
      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
131
132
133
134

      this.$backdrop.addClass('in')

      doAnimate ?
135
        this.$backdrop.one($.support.transition.end, callback) :
136
137
        callback()

Jacob Thornton's avatar
Jacob Thornton committed
138
    } else if (!this.isShown && this.$backdrop) {
139
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
140

141
      $.support.transition && this.$element.hasClass('fade')?
142
        this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
143
144
        removeBackdrop.call(this)

Jacob Thornton's avatar
Jacob Thornton committed
145
146
    } else if (callback) {
      callback()
Jacob Thornton's avatar
Jacob Thornton committed
147
    }
148
  }
Jacob Thornton's avatar
Jacob Thornton committed
149

150
151
152
  function removeBackdrop() {
    this.$backdrop.remove()
    this.$backdrop = null
153
154
  }

155
156
  function escape() {
    var that = this
Jacob Thornton's avatar
Jacob Thornton committed
157
158
159
    if (this.isShown && this.settings.keyboard) {
      $(document).bind('keyup.dismiss.modal', function ( e ) {
        e.which == 27 && that.hide()
160
      })
Jacob Thornton's avatar
Jacob Thornton committed
161
162
    } else if (!this.isShown) {
      $(document).unbind('keyup.dismiss.modal')
163
    }
Jacob Thornton's avatar
Jacob Thornton committed
164
165
166
167
168
169
  }


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

Jacob Thornton's avatar
Jacob Thornton committed
170
171
172
173
174
175
176
177
  $.fn.modal = function ( option ) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('modal')
        , options = typeof option == 'object' && option
      if (!data) $this.data('modal', (data = new Modal(this, options)))
      if (typeof option == 'string') data[option]()
    })
Jacob Thornton's avatar
Jacob Thornton committed
178
179
  }

180
  $.fn.modal.defaults = {
Jacob Thornton's avatar
Jacob Thornton committed
181
182
183
      backdrop: true
    , keyboard: true
    , show: true
184
185
  }

Jacob Thornton's avatar
Jacob Thornton committed
186
187
  $.fn.modal.Modal = Modal

188

Jacob Thornton's avatar
Jacob Thornton committed
189
190
 /* MODAL DATA-API
  * ============== */
191

192
  $(document).ready(function () {
Jacob Thornton's avatar
Jacob Thornton committed
193
    $('body').delegate('[data-toggle="modal"]', 'click.modal.data-api', function ( e ) {
194
      var $this = $(this)
195
        , target = $this.attr('data-target') || $this.attr('href')
Jacob Thornton's avatar
Jacob Thornton committed
196
        , option = $(target).data('modal') ? 'toggle' : $this.data()
Jacob Thornton's avatar
Jacob Thornton committed
197
      e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
198
      $(target).modal(option)
199
200
201
    })
  })

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