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
  * ============================= */

Jacob Thornton's avatar
Jacob Thornton committed
35
  var Modal = function ( options ) {
36
    this.settings = $.extend({}, $.fn.modal.defaults)
Jacob Thornton's avatar
Jacob Thornton committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

    if ( typeof options == 'string' ) {
      this.settings.content = options
    } else if ( options ) {
      $.extend( this.settings, options )
    }

    return this
  }

  Modal.prototype = {

    toggle: function () {
      return this[!this.isOpen ? 'open' : 'close']()
    }

  , open: function () {
      var that = this
      this.isOpen = true

57
58
      this.$element = $(this.settings.content)

59
60
      _.escape.call(this)
      _.backdrop.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
61

62
      this.$element
Jacob Thornton's avatar
Jacob Thornton committed
63
64
        .delegate('.close', 'click', function (e) { e.preventDefault(); that.close() })
        .appendTo(document.body)
Jacob Thornton's avatar
Jacob Thornton committed
65
        .show()
Jacob Thornton's avatar
Jacob Thornton committed
66
67

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

      return this
    }

  , close: function () {
      var that = this

      this.isOpen = false

80
81
      _.escape.call(this)
      _.backdrop.call(this)
Jacob Thornton's avatar
Jacob Thornton committed
82

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

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

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

      return this
    }

  }


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

103
  var _ = {
Jacob Thornton's avatar
Jacob Thornton committed
104
105
106

    backdrop: function () {
      var that = this
107
        , animate = this.$element.hasClass('fade') ? 'fade' : ''
Jacob Thornton's avatar
Jacob Thornton committed
108
      if ( this.isOpen && this.settings.backdrop ) {
109
        this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
Jacob Thornton's avatar
Jacob Thornton committed
110
          .appendTo(document.body)
Jacob Thornton's avatar
Jacob Thornton committed
111
112
113
        $('body').delegate('.modal-backdrop', 'click.modal.backdrop', function () {
          that.close()
        })
Jacob Thornton's avatar
Jacob Thornton committed
114
      } else if ( !this.isOpen && this.$backdrop ) {
115
        this.$backdrop.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
116
        $('body').undelegate('click.modal.backdrop')
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.isOpen && 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
134
135
136
          if ( e.which == 27 ) {
            that.close()
          }
        })
Jacob Thornton's avatar
Jacob Thornton committed
137
      } else if ( !this.isOpen ) {
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
150
151
152
153
    options = options || {}
    options.content = this
    return new Modal(options)
  }

154
155
156
157
158
159
  $.fn.modal.defaults = {
    backdrop: false
  , closeOnEscape: false
  , content: false
  }

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