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

  /* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
   * ======================================================= */

Jacob Thornton's avatar
Jacob Thornton committed
6
  $.support.transition = (function () {
Jacob Thornton's avatar
Jacob Thornton committed
7
8
    var thisBody = document.body || document.documentElement
      , thisStyle = thisBody.style
9
10
11
      , support = thisStyle.transition !== undefined || thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.MsTransition !== undefined || thisStyle.OTransition !== undefined
    return support
  })()
Jacob Thornton's avatar
Jacob Thornton committed
12
13
14
15
16
17
18
19
20


 /* SHARED VARS
  * =========== */

  var $window = $(window)
    , transitionEnd

  // set CSS transition event type
Jacob Thornton's avatar
Jacob Thornton committed
21
  if ( $.support.transition ) {
Jacob Thornton's avatar
Jacob Thornton committed
22
    transitionEnd = "TransitionEnd"
Jacob Thornton's avatar
Jacob Thornton committed
23
    if ( $.browser.webkit ) {
Jacob Thornton's avatar
Jacob Thornton committed
24
    	transitionEnd = "webkitTransitionEnd"
Jacob Thornton's avatar
Jacob Thornton committed
25
    } else if ( $.browser.mozilla ) {
Jacob Thornton's avatar
Jacob Thornton committed
26
    	transitionEnd = "transitionend"
Jacob Thornton's avatar
Jacob Thornton committed
27
    } else if ( $.browser.opera ) {
Jacob Thornton's avatar
Jacob Thornton committed
28
29
30
31
32
33
34
35
    	transitionEnd = "oTransitionEnd"
    }
  }


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

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

    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

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

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

63
      this.$element
Jacob Thornton's avatar
Jacob Thornton committed
64
65
        .delegate('.close', 'click', function (e) { e.preventDefault(); that.close() })
        .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
76
77
78
79
80
      }, 1)

      return this
    }

  , close: function () {
      var that = this

      this.isOpen = false

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

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

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

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

      return this
    }

  }


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

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

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

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

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

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

  }


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

Jacob Thornton's avatar
Jacob Thornton committed
146
  $.fn.modal = function ( options ) {
Jacob Thornton's avatar
Jacob Thornton committed
147
148
149
150
151
    options = options || {}
    options.content = this
    return new Modal(options)
  }

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

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