bootstrap-modal.js 4.1 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
2
(function( $ ){

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
21
22
23
24
25
26
 /* 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
27
    }
Jacob Thornton's avatar
Jacob Thornton committed
28
29

  })
Jacob Thornton's avatar
Jacob Thornton committed
30
31
32
33
34


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

35
  var Modal = function ( content, options ) {
36
    this.settings = $.extend({}, $.fn.modal.defaults)
Jacob Thornton's avatar
Jacob Thornton committed
37

38
    if ( options ) {
Jacob Thornton's avatar
Jacob Thornton committed
39
40
41
      $.extend( this.settings, options )
    }

42
    this.$element = $(content)
Jacob Thornton's avatar
Jacob Thornton committed
43
44
      .bind('modal:show', $.proxy(this.show, this))
      .bind('modal:hide', $.proxy(this.hide, this))
45
      .bind('modal:toggle', $.proxy(this.toggle, this))
Jacob Thornton's avatar
Jacob Thornton committed
46
      .delegate('.close', 'click', $.proxy(this.hide, this))
47

Jacob Thornton's avatar
Jacob Thornton committed
48
49
50
51
52
53
    return this
  }

  Modal.prototype = {

    toggle: function () {
Jacob Thornton's avatar
Jacob Thornton committed
54
      return this[!this.isShown ? 'show' : 'hide']()
Jacob Thornton's avatar
Jacob Thornton committed
55
56
    }

Jacob Thornton's avatar
Jacob Thornton committed
57
  , show: function () {
Jacob Thornton's avatar
Jacob Thornton committed
58
      var that = this
Jacob Thornton's avatar
Jacob Thornton committed
59
      this.isShown = true
Jacob Thornton's avatar
Jacob Thornton committed
60

61
      _.escape.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
62

Jacob Thornton's avatar
Jacob Thornton committed
63
64
65
66
      _.backdrop.call(this, function () {
        that.$element
          .appendTo(document.body)
          .show()
67

Jacob Thornton's avatar
Jacob Thornton committed
68
69
70
71
72
73
        setTimeout(function () {
          that.$element
            .addClass('in')
            .trigger('modal:shown')
        }, 1)
      })
Jacob Thornton's avatar
Jacob Thornton committed
74
75
76
77

      return this
    }

Jacob Thornton's avatar
Jacob Thornton committed
78
  , hide: function (e) {
79
80
      e && e.preventDefault()

Jacob Thornton's avatar
Jacob Thornton committed
81
82
      var that = this

Jacob Thornton's avatar
Jacob Thornton committed
83
      this.isShown = false
Jacob Thornton's avatar
Jacob Thornton committed
84

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

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

      function removeElement () {
90
91
92
        that.$element
          .detach()
          .trigger('modal:hidden')
Jacob Thornton's avatar
Jacob Thornton committed
93
94

        _.backdrop.call(that)
Jacob Thornton's avatar
Jacob Thornton committed
95
96
      }

97
      $.support.transition && this.$element.hasClass('fade') ?
98
        this.$element.one(transitionEnd, removeElement) :
Jacob Thornton's avatar
Jacob Thornton committed
99
100
101
102
103
104
105
106
107
108
109
        removeElement()

      return this
    }

  }


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

110
  var _ = {
Jacob Thornton's avatar
Jacob Thornton committed
111

Jacob Thornton's avatar
Jacob Thornton committed
112
    backdrop: function ( callback ) {
Jacob Thornton's avatar
Jacob Thornton committed
113
      var that = this
114
        , animate = this.$element.hasClass('fade') ? 'fade' : ''
Jacob Thornton's avatar
Jacob Thornton committed
115
      if ( this.isShown && this.settings.backdrop ) {
116
        this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
Jacob Thornton's avatar
Jacob Thornton committed
117
          .click($.proxy(this.hide, this))
Jacob Thornton's avatar
Jacob Thornton committed
118
          .appendTo(document.body)
Jacob Thornton's avatar
Jacob Thornton committed
119
120
121
122
123
124
125

        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
126
      } else if ( !this.isShown && this.$backdrop ) {
127
        this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
128
129
130
131
132
133

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

134
        $.support.transition && this.$element.hasClass('fade')?
135
          this.$backdrop.one(transitionEnd, removeElement) :
Jacob Thornton's avatar
Jacob Thornton committed
136
          removeElement()
Jacob Thornton's avatar
Jacob Thornton committed
137
138
      } else {
        callback()
Jacob Thornton's avatar
Jacob Thornton committed
139
140
141
      }
    }

Jacob Thornton's avatar
Jacob Thornton committed
142
  , escape: function () {
Jacob Thornton's avatar
Jacob Thornton committed
143
      var that = this
Jacob Thornton's avatar
Jacob Thornton committed
144
      if ( this.isShown && this.settings.closeOnEscape ) {
Jacob Thornton's avatar
Jacob Thornton committed
145
        $('body').bind('keyup.modal.escape', function ( e ) {
Jacob Thornton's avatar
Jacob Thornton committed
146
          if ( e.which == 27 ) {
Jacob Thornton's avatar
Jacob Thornton committed
147
            that.hide()
Jacob Thornton's avatar
Jacob Thornton committed
148
149
          }
        })
Jacob Thornton's avatar
Jacob Thornton committed
150
      } else if ( !this.isShown ) {
Jacob Thornton's avatar
Jacob Thornton committed
151
        $('body').unbind('keyup.modal.escape')
Jacob Thornton's avatar
Jacob Thornton committed
152
153
154
155
156
157
158
159
160
      }
    }

  }


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

Jacob Thornton's avatar
Jacob Thornton committed
161
  $.fn.modal = function ( options ) {
Jacob Thornton's avatar
Jacob Thornton committed
162
    options = options || {}
163
164
165
    return this.each(function () {
      return new Modal(this, options)
    })
Jacob Thornton's avatar
Jacob Thornton committed
166
167
  }

168
169
  $.fn.modal.Modal = Modal

170
171
  $.fn.modal.defaults = {
    backdrop: false
Jacob Thornton's avatar
Jacob Thornton committed
172
  , hideOnEscape: false
173
174
  }

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