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

195 lines
4.9 KiB
JavaScript
Raw Normal View History

2011-09-11 07:24:31 +02:00
/* =========================================================
* bootstrap-modal.js
* http://twitter.github.com/bootstrap/javascript.html#modal
* =========================================================
* Copyright 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================= */
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)
2011-09-11 07:14:57 +02:00
2011-09-11 04:01:16 +02:00
_.backdrop.call(this, function () {
that.$element
.appendTo(document.body)
.show()
2011-09-11 04:01:16 +02:00
setTimeout(function () {
that.$element
.addClass('in')
.trigger('modal:shown')
}, 1)
})
2011-08-27 08:57:35 +02:00
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)
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
.detach()
.trigger('modal:hidden')
2011-09-11 04:01:16 +02:00
_.backdrop.call(that)
2011-08-27 08:57:35 +02:00
}
2011-08-28 03:03:01 +02:00
$.support.transition && this.$element.hasClass('fade') ?
this.$element.one(transitionEnd, removeElement) :
2011-08-27 08:57:35 +02:00
removeElement()
return this
}
}
/* MODAL PRIVATE METHODS
* ===================== */
var _ = {
2011-08-27 08:57:35 +02:00
2011-09-11 04:01:16 +02:00
backdrop: function ( callback ) {
2011-08-27 08:57:35 +02:00
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-11 04:01:16 +02:00
setTimeout(function () {
that.$backdrop && that.$backdrop.addClass('in')
$.support.transition && that.$backdrop.hasClass('fade') ?
that.$backdrop.one(transitionEnd, callback) :
callback()
})
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')?
this.$backdrop.one(transitionEnd, removeElement) :
2011-08-27 08:57:35 +02:00
removeElement()
2011-09-11 07:14:57 +02:00
} else {
callback()
2011-08-27 08:57:35 +02:00
}
}
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 )