-
-
Save anonymous/0d04646ab6cf4df50610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ========================================================= | |
* bootstrap-modal.js v2.0.0 | |
* http://twitter.github.com/bootstrap/javascript.html#modals | |
* ========================================================= | |
* Copyright 2012 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. | |
* ========================================================= */ | |
/* Borrowed from jquery ui | |
* ======================= */ | |
function focusable( element, isTabIndexNotNaN ) { | |
var nodeName = element.nodeName.toLowerCase(); | |
if ( "area" === nodeName ) { | |
var map = element.parentNode, | |
mapName = map.name, | |
img; | |
if ( !element.href || !mapName || map.nodeName.toLowerCase() !== "map" ) { | |
return false; | |
} | |
img = $( "img[usemap=#" + mapName + "]" )[0]; | |
return !!img && visible( img ); | |
} | |
return ( /input|select|textarea|button|object/.test( nodeName ) | |
? !element.disabled | |
: "a" == nodeName | |
? element.href || isTabIndexNotNaN | |
: isTabIndexNotNaN) | |
// the element and all of its ancestors must be visible | |
&& visible( element ); | |
} | |
function visible( element ) { | |
return !$( element ).parents().andSelf().filter(function() { | |
return $.curCSS( this, "visibility" ) === "hidden" || | |
$.expr.filters.hidden( this ); | |
}).length; | |
} | |
$.fn.extend({ | |
_focus: $.fn.focus, | |
focus: function( delay, fn ) { | |
return typeof delay === "number" ? | |
this.each(function() { | |
var elem = this; | |
setTimeout(function() { | |
$( elem ).focus(); | |
if ( fn ) { | |
fn.call( elem ); | |
} | |
}, delay ); | |
}) : | |
this._focus.apply( this, arguments ); | |
} | |
}); | |
$.extend( $.expr[ ":" ], { | |
data: function( elem, i, match ) { | |
return !!$.data( elem, match[ 3 ] ); | |
}, | |
focusable: function( element ) { | |
return focusable( element, !isNaN( $.attr( element, "tabindex" ) ) ); | |
}, | |
tabbable: function( element ) { | |
var tabIndex = $.attr( element, "tabindex" ), | |
isTabIndexNaN = isNaN( tabIndex ); | |
return ( isTabIndexNaN || tabIndex >= 0 ) && focusable( element, !isTabIndexNaN ); | |
} | |
}); | |
!function( $ ){ | |
"use strict" | |
/* MODAL CLASS DEFINITION | |
* ====================== */ | |
var Modal = function ( content, options ) { | |
this.options = $.extend({}, $.fn.modal.defaults, options) | |
this.$element = $(content) | |
.delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this)) | |
} | |
Modal.prototype = { | |
constructor: Modal | |
, toggle: function () { | |
return this[!this.isShown ? 'show' : 'hide']() | |
} | |
, show: function () { | |
var that = this | |
if (this.isShown) return | |
$('body').addClass('modal-open') | |
this.isShown = true | |
this.$element.trigger('show') | |
escape.call(this) | |
backdrop.call(this, function () { | |
var transition = $.support.transition && that.$element.hasClass('fade') | |
!that.$element.parent().length && that.$element.appendTo(document.body) //don't move modals dom position | |
that.$element | |
.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'); setFocus.call(that) }) : | |
that.$element.trigger('shown') | |
setFocus.call(that) | |
}) | |
// prevent tabbing out of the modal | |
this.$element.bind( "keydown", function( event ) { | |
if ( event.keyCode !== 9 ) { | |
return; | |
} | |
var tabbables = $( ":tabbable", this ), | |
first = tabbables.filter( ":first" ), | |
last = tabbables.filter( ":last" ); | |
if ( event.target === last[0] && !event.shiftKey ) { | |
first.focus( 1 ); | |
return false; | |
} else if ( event.target === first[0] && event.shiftKey ) { | |
last.focus( 1 ); | |
return false; | |
} | |
}); | |
} | |
, hide: function ( e ) { | |
e && e.preventDefault() | |
if (!this.isShown) return | |
var that = this | |
this.isShown = false | |
$('body').removeClass('modal-open') | |
escape.call(this) | |
this.$element | |
.trigger('hide') | |
.removeClass('in') | |
$.support.transition && this.$element.hasClass('fade') ? | |
hideWithTransition.call(this) : | |
hideModal.call(this) | |
} | |
} | |
/* MODAL PRIVATE METHODS | |
* ===================== */ | |
function setFocus() { | |
// set focus to the first form input field... | |
var focal = this.$element.find( "input,select,textarea,button,object" ); | |
// ...or to the first link that does not look like a close button | |
if ( !focal.length ) { | |
focal = this.$element.find( "a:not([data-toggle=modal])" ); | |
// ...or heck, to the modal div itself | |
if ( !focal.length ) { | |
focal = this.$element; | |
} | |
} | |
focal.eq( 0 ).focus(); | |
} | |
function hideWithTransition() { | |
var that = this | |
, timeout = setTimeout(function () { | |
that.$element.off($.support.transition.end) | |
hideModal.call(that) | |
}, 500) | |
this.$element.one($.support.transition.end, function () { | |
clearTimeout(timeout) | |
hideModal.call(that) | |
}) | |
} | |
function hideModal( that ) { | |
this.$element | |
.hide() | |
.trigger('hidden') | |
backdrop.call(this) | |
} | |
function backdrop( callback ) { | |
var that = this | |
, animate = this.$element.hasClass('fade') ? 'fade' : '' | |
if (this.isShown && this.options.backdrop) { | |
var doAnimate = $.support.transition && animate | |
this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />') | |
.appendTo(document.body) | |
if (this.options.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) : | |
callback() | |
} else if (!this.isShown && this.$backdrop) { | |
this.$backdrop.removeClass('in') | |
$.support.transition && this.$element.hasClass('fade')? | |
this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) : | |
removeBackdrop.call(this) | |
} else if (callback) { | |
callback() | |
} | |
} | |
function removeBackdrop() { | |
this.$backdrop.remove() | |
this.$backdrop = null | |
} | |
function escape() { | |
var that = this | |
if (this.isShown && this.options.keyboard) { | |
$(document).on('keyup.dismiss.modal', function ( e ) { | |
e.which == 27 && that.hide() | |
}) | |
} else if (!this.isShown) { | |
$(document).off('keyup.dismiss.modal') | |
} | |
} | |
/* MODAL PLUGIN DEFINITION | |
* ======================= */ | |
$.fn.modal = function ( option ) { | |
return this.each(function () { | |
var $this = $(this) | |
, data = $this.data('modal') | |
, options = typeof option == 'object' && option | |
if (!data) $this.data('modal', (data = new Modal(this, options))) | |
if (typeof option == 'string') data[option]() | |
else data.show() | |
}) | |
} | |
$.fn.modal.defaults = { | |
backdrop: true | |
, keyboard: true | |
} | |
$.fn.modal.Constructor = Modal | |
/* MODAL DATA-API | |
* ============== */ | |
$(function () { | |
$('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) { | |
var $this = $(this), href | |
, $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7 | |
, option = $target.data('modal') ? 'toggle' : $.extend({}, $target.data(), $this.data()) | |
e.preventDefault() | |
$target.modal(option) | |
}) | |
}) | |
}( window.jQuery ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment