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

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

26
  var Modal = function ( content, options ) {
27
    this.settings = $.extend({}, $.fn.modal.defaults, options)
28
    this.$element = $(content)
29
      .delegate('[data-modal-dismiss]', 'click', $.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
30

31
32
    if ( this.settings.show ) {
      this.show()
33
    }
34

Jacob Thornton's avatar
Jacob Thornton committed
35
36
37
38
39
    return this
  }

  Modal.prototype = {

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

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

49
        escape.call(this)
50
        backdrop.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
51

52
53
        return this
      }
54

55
56
    , hide: function (e) {
        e && e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
57

58
59
60
61
        if ( !this.isShown ) {
          return this
        }

62
63
        var that = this
        this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
64

65
        escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
66

67
68
69
        this.$element
          .trigger('hide')
          .removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
70

71
72
73
74
        function removeElement () {
          that.$element
            .hide()
            .trigger('hidden')
Jacob Thornton's avatar
Jacob Thornton committed
75

76
77
          backdrop.call(that)
        }
Jacob Thornton's avatar
Jacob Thornton committed
78

79
        $.support.transition && this.$element.hasClass('fade') ?
80
          this.$element.one($.support.transition.end, removeElement) :
81
          removeElement()
Jacob Thornton's avatar
Jacob Thornton committed
82

83
84
        return this
      }
Jacob Thornton's avatar
Jacob Thornton committed
85
86
87
88
89
90
91

  }


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

92
  function backdrop () {
93
94
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
95
96
      , callback = $.proxy(show, this)

97
    if ( this.isShown && this.settings.backdrop ) {
98
99
      var doAnimate = $.support.transition && animate

100
101
102
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .appendTo(document.body)

103
104
105
106
107
108
109
110
111
112
113
      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 ?
114
        this.$backdrop.one($.support.transition.end, callback) :
115
116
        callback()

117
118
    } else if ( !this.isShown && this.$backdrop ) {
      this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
119

120
121
122
      function removeElement() {
        that.$backdrop.remove()
        that.$backdrop = null
Jacob Thornton's avatar
Jacob Thornton committed
123
124
      }

125
      $.support.transition && this.$element.hasClass('fade')?
126
        this.$backdrop.one($.support.transition.end, removeElement) :
127
128
129
        removeElement()
    } else if ( callback ) {
       callback()
Jacob Thornton's avatar
Jacob Thornton committed
130
    }
131
  }
Jacob Thornton's avatar
Jacob Thornton committed
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  function show() {
    var transition = $.support.transition && that.$element.hasClass('fade')
      , that = this

    this.$element
      .appendTo(document.body)
      .show()

    if (transition) {
      this.$element[0].offsetWidth // force reflow
    }

    this.$element
      .addClass('in')

    transition ?
      this.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) :
      this.$element.trigger('shown')
  }

153
154
155
  function escape() {
    var that = this
    if ( this.isShown && this.settings.keyboard ) {
156
      $(document).bind('keyup.modal', function ( e ) {
157
158
159
160
161
        if ( e.which == 27 ) {
          that.hide()
        }
      })
    } else if ( !this.isShown ) {
162
      $(document).unbind('keyup.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
  $.fn.modal = function ( options ) {
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    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
197
198
  }

199
200
  $.fn.modal.Modal = Modal

201
202
  $.fn.modal.defaults = {
    backdrop: false
203
  , keyboard: false
204
  , show: false
205
206
  }

207

208
209
 /* MODAL DATA-IMPLEMENTATION
  * ========================= */
210

211
  $(function () {
212
213
214
215
216
217
218
    $('body').delegate('[data-controls-modal]', 'click', function (e) {
      e.preventDefault()
      var $this = $(this).data('show', true)
      $('#' + $this.attr('data-controls-modal')).modal( $this.data() )
    })
  })

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