1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/bootstrap-modal.js

219 lines
5.1 KiB
JavaScript
Raw Normal View History

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