0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00
Bootstrap/js/bootstrap-modal.js

162 lines
3.8 KiB
JavaScript
Raw Normal View History

2011-08-27 08:57:35 +02:00
(function( $ ){
2011-08-29 01:47:38 +02:00
/* 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"
}
2011-08-27 08:57:35 +02:00
}
2011-08-29 01:47:38 +02:00
})
2011-08-27 08:57:35 +02:00
/* MODAL PUBLIC CLASS DEFINITION
* ============================= */
var Modal = function ( content, options ) {
this.settings = $.extend({}, $.fn.modal.defaults)
2011-08-27 08:57:35 +02:00
if ( options ) {
2011-08-27 08:57:35 +02:00
$.extend( this.settings, options )
}
this.$element = $(content)
2011-09-10 21:49:21 +02:00
.bind('modal:show', $.proxy(this.show, this))
.bind('modal:hide', $.proxy(this.hide, this))
.bind('modal:toggle', $.proxy(this.toggle, this))
2011-09-10 21:49:21 +02:00
.delegate('.close', 'click', $.proxy(this.hide, this))
2011-08-27 08:57:35 +02:00
return this
}
Modal.prototype = {
toggle: function () {
2011-09-10 21:49:21 +02:00
return this[!this.isShown ? 'show' : 'hide']()
2011-08-27 08:57:35 +02:00
}
2011-09-10 21:49:21 +02:00
, show: function () {
2011-08-27 08:57:35 +02:00
var that = this
2011-09-10 21:49:21 +02:00
this.isShown = true
2011-08-27 08:57:35 +02:00
_.escape.call(this)
_.backdrop.call(this)
2011-08-27 08:57:35 +02:00
2011-08-28 03:03:01 +02:00
this.$element
2011-08-27 08:57:35 +02:00
.appendTo(document.body)
2011-08-27 09:28:58 +02:00
.show()
2011-08-27 08:57:35 +02:00
setTimeout(function () {
2011-08-28 03:03:01 +02:00
that.$element.addClass('in')
that.$backdrop && that.$backdrop.addClass('in')
2011-08-27 08:57:35 +02:00
}, 1)
return this
}
2011-09-10 21:49:21 +02:00
, hide: function (e) {
e && e.preventDefault()
2011-08-27 08:57:35 +02:00
var that = this
2011-09-10 21:49:21 +02:00
this.isShown = false
2011-08-27 08:57:35 +02:00
_.escape.call(this)
_.backdrop.call(this)
2011-08-27 08:57:35 +02:00
2011-08-28 03:03:01 +02:00
this.$element.removeClass('in')
2011-08-27 08:57:35 +02:00
function removeElement () {
that.$element.unbind(transitionEnd)
that.$element.detach()
2011-08-27 08:57:35 +02:00
}
2011-08-28 03:03:01 +02:00
$.support.transition && this.$element.hasClass('fade') ?
2011-08-27 08:57:35 +02:00
this.$element.bind(transitionEnd, removeElement) :
removeElement()
return this
}
}
/* MODAL PRIVATE METHODS
* ===================== */
var _ = {
2011-08-27 08:57:35 +02:00
backdrop: function () {
var that = this
2011-08-28 03:03:01 +02:00
, animate = this.$element.hasClass('fade') ? 'fade' : ''
2011-09-10 21:49:21 +02:00
if ( this.isShown && this.settings.backdrop ) {
2011-08-28 03:03:01 +02:00
this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
2011-09-10 21:49:21 +02:00
.click($.proxy(this.hide, this))
2011-08-27 08:57:35 +02:00
.appendTo(document.body)
2011-09-10 21:49:21 +02:00
} else if ( !this.isShown && this.$backdrop ) {
2011-08-28 03:03:01 +02:00
this.$backdrop.removeClass('in')
2011-08-27 08:57:35 +02:00
function removeElement() {
that.$backdrop.remove()
that.$backdrop = null
}
2011-08-28 03:03:01 +02:00
$.support.transition && this.$element.hasClass('fade')?
2011-08-27 08:57:35 +02:00
this.$backdrop.bind(transitionEnd, removeElement) :
removeElement()
}
}
2011-08-27 09:42:20 +02:00
, escape: function () {
2011-08-27 08:57:35 +02:00
var that = this
2011-09-10 21:49:21 +02:00
if ( this.isShown && this.settings.closeOnEscape ) {
2011-08-29 02:32:31 +02:00
$('body').bind('keyup.modal.escape', function ( e ) {
2011-08-27 08:57:35 +02:00
if ( e.which == 27 ) {
2011-09-10 21:49:21 +02:00
that.hide()
2011-08-27 08:57:35 +02:00
}
})
2011-09-10 21:49:21 +02:00
} else if ( !this.isShown ) {
2011-08-29 02:32:31 +02:00
$('body').unbind('keyup.modal.escape')
2011-08-27 08:57:35 +02:00
}
}
}
/* MODAL PLUGIN DEFINITION
* ======================= */
2011-08-27 09:19:05 +02:00
$.fn.modal = function ( options ) {
2011-08-27 08:57:35 +02:00
options = options || {}
return this.each(function () {
return new Modal(this, options)
})
2011-08-27 08:57:35 +02:00
}
$.fn.modal.Modal = Modal
$.fn.modal.defaults = {
backdrop: false
2011-09-10 21:49:21 +02:00
, hideOnEscape: false
}
2011-08-27 08:57:35 +02:00
})( jQuery || ender )