bootstrap-modal.js 3.79 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
62
      _.escape.call(this)
      _.backdrop.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
63

64
      this.$element
Jacob Thornton's avatar
Jacob Thornton committed
65
        .appendTo(document.body)
Jacob Thornton's avatar
Jacob Thornton committed
66
        .show()
Jacob Thornton's avatar
Jacob Thornton committed
67
68

      setTimeout(function () {
69
70
        that.$element.addClass('in')
        that.$backdrop && that.$backdrop.addClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
71
72
73
74
75
      }, 1)

      return this
    }

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

Jacob Thornton's avatar
Jacob Thornton committed
79
80
      var that = this

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

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

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

      function removeElement () {
89
90
        that.$element.unbind(transitionEnd)
        that.$element.detach()
Jacob Thornton's avatar
Jacob Thornton committed
91
92
      }

93
      $.support.transition && this.$element.hasClass('fade') ?
Jacob Thornton's avatar
Jacob Thornton committed
94
95
96
97
98
99
100
101
102
103
104
105
        this.$element.bind(transitionEnd, removeElement) :
        removeElement()

      return this
    }

  }


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

106
  var _ = {
Jacob Thornton's avatar
Jacob Thornton committed
107
108
109

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

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

123
        $.support.transition && this.$element.hasClass('fade')?
Jacob Thornton's avatar
Jacob Thornton committed
124
125
126
127
128
          this.$backdrop.bind(transitionEnd, removeElement) :
          removeElement()
      }
    }

Jacob Thornton's avatar
Jacob Thornton committed
129
  , escape: function () {
Jacob Thornton's avatar
Jacob Thornton committed
130
      var that = this
Jacob Thornton's avatar
Jacob Thornton committed
131
      if ( this.isShown && this.settings.closeOnEscape ) {
Jacob Thornton's avatar
Jacob Thornton committed
132
        $('body').bind('keyup.modal.escape', function ( e ) {
Jacob Thornton's avatar
Jacob Thornton committed
133
          if ( e.which == 27 ) {
Jacob Thornton's avatar
Jacob Thornton committed
134
            that.hide()
Jacob Thornton's avatar
Jacob Thornton committed
135
136
          }
        })
Jacob Thornton's avatar
Jacob Thornton committed
137
      } else if ( !this.isShown ) {
Jacob Thornton's avatar
Jacob Thornton committed
138
        $('body').unbind('keyup.modal.escape')
Jacob Thornton's avatar
Jacob Thornton committed
139
140
141
142
143
144
145
146
147
      }
    }

  }


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

Jacob Thornton's avatar
Jacob Thornton committed
148
  $.fn.modal = function ( options ) {
Jacob Thornton's avatar
Jacob Thornton committed
149
    options = options || {}
150
151
152
    return this.each(function () {
      return new Modal(this, options)
    })
Jacob Thornton's avatar
Jacob Thornton committed
153
154
  }

155
156
  $.fn.modal.Modal = Modal

157
158
  $.fn.modal.defaults = {
    backdrop: false
Jacob Thornton's avatar
Jacob Thornton committed
159
  , hideOnEscape: false
160
161
  }

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