bootstrap/js/bootstrap-modal.js

226 lines
5.3 KiB
JavaScript
Raw Normal View History

2011-09-11 05:24:31 +00:00
/* =========================================================
2011-11-21 05:13:28 +00:00
* bootstrap-modal.js v2.0.0
2011-09-11 05:24:31 +00: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-10-05 04:48:53 +00:00
!function( $ ){
2011-08-27 06:57:35 +00:00
2011-11-21 02:19:50 +00:00
"use strict"
2011-08-27 06:57:35 +00:00
/* MODAL PUBLIC CLASS DEFINITION
* ============================= */
var Modal = function ( content, options ) {
2011-10-05 04:48:53 +00:00
this.settings = $.extend({}, $.fn.modal.defaults, options)
this.$element = $(content)
2011-11-21 02:19:50 +00:00
.delegate('.close', 'click.modal', $.proxy(this.hide, this))
2011-08-27 06:57:35 +00:00
2011-10-05 04:48:53 +00:00
if ( this.settings.show ) {
this.show()
}
2011-08-27 06:57:35 +00:00
return this
}
Modal.prototype = {
toggle: function () {
return this[!this.isShown ? 'show' : 'hide']()
}
2011-09-11 05:14:57 +00:00
, show: function () {
var that = this
this.isShown = true
this.$element.trigger('show')
escape.call(this)
2011-11-21 02:19:50 +00:00
backdrop.call(this, function () {
var transition = $.support.transition && that.$element.hasClass('fade')
that.$element
.appendTo(document.body)
.show()
if (transition) {
that.$element[0].offsetWidth // force reflow
}
that.$element.addClass('in')
transition ?
that.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) :
2011-11-21 02:19:50 +00:00
that.$element.trigger('shown')
})
2011-08-27 06:57:35 +00:00
return this
}
, hide: function (e) {
e && e.preventDefault()
2011-08-27 06:57:35 +00:00
2011-10-05 04:48:53 +00:00
if ( !this.isShown ) {
return this
}
var that = this
this.isShown = false
2011-08-27 06:57:35 +00:00
escape.call(this)
2011-08-27 06:57:35 +00:00
this.$element
.trigger('hide')
.removeClass('in')
2011-08-27 06:57:35 +00:00
$.support.transition && this.$element.hasClass('fade') ?
2011-11-21 02:19:50 +00:00
hideWithTransition.call(this) :
hideModal.call(this)
2011-08-27 06:57:35 +00:00
return this
}
2011-08-27 06:57:35 +00:00
}
/* MODAL PRIVATE METHODS
* ===================== */
2011-11-21 02:19:50 +00:00
function hideWithTransition() {
var that = this
2011-11-21 02:19:50 +00:00
, timeout = setTimeout(function () {
that.$element.unbind($.support.transition.end)
2011-11-21 02:19:50 +00:00
hideModal.call(that)
}, 500)
this.$element.one($.support.transition.end, function () {
2011-11-21 02:19:50 +00:00
clearTimeout(timeout)
hideModal.call(that)
})
}
function hideModal (that) {
this.$element
.hide()
.trigger('hidden')
backdrop.call(this)
}
2011-11-21 02:19:50 +00:00
function backdrop ( callback ) {
var that = this
, animate = this.$element.hasClass('fade') ? 'fade' : ''
if ( this.isShown && this.settings.backdrop ) {
2011-10-05 04:48:53 +00:00
var doAnimate = $.support.transition && animate
this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
.appendTo(document.body)
2011-10-05 04:48:53 +00:00
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($.support.transition.end, callback) :
2011-10-05 04:48:53 +00:00
callback()
} else if ( !this.isShown && this.$backdrop ) {
this.$backdrop.removeClass('in')
2011-08-27 06:57:35 +00:00
$.support.transition && this.$element.hasClass('fade')?
this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
2011-11-21 02:19:50 +00:00
removeBackdrop.call(this)
} else if ( callback ) {
callback()
2011-08-27 06:57:35 +00:00
}
}
2011-08-27 06:57:35 +00:00
2011-11-21 02:19:50 +00:00
function removeBackdrop() {
this.$backdrop.remove()
this.$backdrop = null
}
function escape() {
var that = this
if ( this.isShown && this.settings.keyboard ) {
2011-10-05 04:48:53 +00:00
$(document).bind('keyup.modal', function ( e ) {
if ( e.which == 27 ) {
that.hide()
}
})
} else if ( !this.isShown ) {
2011-10-05 04:48:53 +00:00
$(document).unbind('keyup.modal')
}
2011-08-27 06:57:35 +00:00
}
/* MODAL PLUGIN DEFINITION
* ======================= */
2011-08-27 07:19:05 +00: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 ( typeof options == 'string' ) {
modal[options]()
} else if ( modal ) {
modal.toggle()
}
return this
2011-08-27 06:57:35 +00:00
}
$.fn.modal.Modal = Modal
$.fn.modal.defaults = {
backdrop: true
, keyboard: true
, show: true
}
2011-11-21 02:19:50 +00:00
/* MODAL DATA- IMPLEMENTATION
* ========================== */
2011-11-21 02:19:50 +00:00
$(document).ready(function () {
$('body').delegate('[data-controls-modal]', 'click.modal.data-api', function (e) {
e.preventDefault()
var $this = $(this)
$('#' + $this.attr('data-controls-modal')).modal( $this.data() )
})
})
2011-10-05 04:48:53 +00:00
}( window.jQuery || window.ender );