0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-04 16:24:22 +01:00
Bootstrap/js/bootstrap-modal.js

245 lines
6.0 KiB
JavaScript
Raw Normal View History

2011-09-11 07:24:31 +02:00
/* =========================================================
2011-09-12 20:15:32 +02:00
* bootstrap-modal.js v1.3.0
2011-09-11 07:24:31 +02:00
* 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-09-19 04:05:21 +02:00
!function( $ ){
2011-08-27 08:57:35 +02:00
2011-08-29 01:47:38 +02:00
/* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
* ======================================================= */
var transitionEnd
$(document).ready(function () {
2011-08-29 01:47:38 +02:00
$.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, options)
this.$element = $(content)
.delegate('.close', 'click.modal', $.proxy(this.hide, this))
2011-08-27 08:57:35 +02:00
2011-10-03 07:26:26 +02:00
if ( this.settings.show ) {
this.show()
}
2011-08-27 08:57:35 +02:00
return this
}
Modal.prototype = {
toggle: function () {
return this[!this.isShown ? 'show' : 'hide']()
}
2011-09-11 07:14:57 +02:00
, show: function () {
var that = this
this.isShown = true
this.$element.trigger('show')
escape.call(this)
backdrop.call(this, function () {
var transition = $.support.transition && that.$element.hasClass('fade')
2011-09-11 04:01:16 +02:00
that.$element
.appendTo(document.body)
.show()
if (transition) {
that.$element[0].offsetWidth // force reflow
}
that.$element
.addClass('in')
transition ?
that.$element.one(transitionEnd, function () { that.$element.trigger('shown') }) :
that.$element.trigger('shown')
})
2011-08-27 08:57:35 +02:00
return this
}
, hide: function (e) {
e && e.preventDefault()
2011-08-27 08:57:35 +02:00
2011-09-30 08:32:10 +02:00
if ( !this.isShown ) {
return this
2011-09-23 20:38:08 +02:00
}
var that = this
this.isShown = false
2011-08-27 08:57:35 +02:00
escape.call(this)
2011-08-27 08:57:35 +02:00
this.$element
.trigger('hide')
.removeClass('in')
2011-08-27 08:57:35 +02:00
function removeElement () {
that.$element
.hide()
.trigger('hidden')
2011-09-11 04:01:16 +02:00
backdrop.call(that)
}
2011-08-27 08:57:35 +02:00
$.support.transition && this.$element.hasClass('fade') ?
this.$element.one(transitionEnd, removeElement) :
removeElement()
2011-08-27 08:57:35 +02:00
return this
}
2011-08-27 08:57:35 +02:00
}
/* MODAL PRIVATE METHODS
* ===================== */
function backdrop ( callback ) {
var that = this
, animate = this.$element.hasClass('fade') ? 'fade' : ''
if ( this.isShown && this.settings.backdrop ) {
var doAnimate = $.support.transition && animate
this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
.appendTo(document.body)
if ( this.settings.backdrop != 'static' ) {
this.$backdrop.click($.proxy(this.hide, this))
}
if ( doAnimate ) {
this.$backdrop[0].offsetWidth // force reflow
}
this.$backdrop.addClass('in')
doAnimate ?
this.$backdrop.one(transitionEnd, callback) :
callback()
} else if ( !this.isShown && this.$backdrop ) {
this.$backdrop.removeClass('in')
2011-08-27 08:57:35 +02:00
function removeElement() {
that.$backdrop.remove()
that.$backdrop = null
2011-08-27 08:57:35 +02:00
}
$.support.transition && this.$element.hasClass('fade')?
this.$backdrop.one(transitionEnd, removeElement) :
removeElement()
} else if ( callback ) {
callback()
2011-08-27 08:57:35 +02:00
}
}
2011-08-27 08:57:35 +02:00
function escape() {
var that = this
if ( this.isShown && this.settings.keyboard ) {
2011-09-19 06:13:56 +02:00
$(document).bind('keyup.modal', function ( e ) {
if ( e.which == 27 ) {
that.hide()
}
})
} else if ( !this.isShown ) {
2011-09-19 06:13:56 +02:00
$(document).unbind('keyup.modal')
}
2011-08-27 08:57:35 +02:00
}
/* MODAL PLUGIN DEFINITION
* ======================= */
2011-08-27 09:19:05 +02:00
$.fn.modal = function ( options ) {
var modal = this.data('modal')
if (!modal) {
if (typeof options == 'string') {
options = {
show: /show|toggle/.test(options)
}
}
return this.each(function () {
$(this).data('modal', new Modal(this, options))
})
}
if ( options === true ) {
return modal
}
if ( typeof options == 'string' ) {
modal[options]()
} else if ( modal ) {
modal.toggle()
}
return this
2011-08-27 08:57:35 +02:00
}
$.fn.modal.Modal = Modal
$.fn.modal.defaults = {
backdrop: false
, keyboard: false
2011-09-30 08:12:27 +02:00
, show: false
}
/* MODAL DATA- IMPLEMENTATION
* ========================== */
$(document).ready(function () {
$('body').delegate('[data-controls-modal]', 'click', function (e) {
e.preventDefault()
var $this = $(this).data('show', true)
$('#' + $this.attr('data-controls-modal')).modal( $this.data() )
})
})
}( window.jQuery || window.ender );