0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-03-02 02:29:24 +01:00

force reflow instead of relying on eventloop (firefox)

listen to window for escape press
This commit is contained in:
Jacob Thornton 2011-09-18 21:03:52 -07:00
parent ba6f4f03da
commit ac8001190a
3 changed files with 158 additions and 151 deletions

View File

@ -12,7 +12,7 @@
<![endif]--> <![endif]-->
<!-- Le javascript --> <!-- Le javascript -->
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="assets/js/google-code-prettify/prettify.js"></script> <script src="assets/js/google-code-prettify/prettify.js"></script>
<script>$(function () { prettyPrint() })</script> <script>$(function () { prettyPrint() })</script>
<script src="../js/bootstrap-modal.js"></script> <script src="../js/bootstrap-modal.js"></script>

21
js/bootstrap-modal.js vendored
View File

@ -85,11 +85,13 @@
.appendTo(document.body) .appendTo(document.body)
.show() .show()
setTimeout(function () { if ($.support.transition && that.$element.hasClass('fade')) {
that.$backdrop[0].offsetWidth // force reflow
}
that.$element that.$element
.addClass('in') .addClass('in')
.trigger('shown') .trigger('shown')
}, 0)
}) })
return this return this
@ -132,6 +134,8 @@
var that = this var that = this
, animate = this.$element.hasClass('fade') ? 'fade' : '' , animate = this.$element.hasClass('fade') ? 'fade' : ''
if ( this.isShown && this.settings.backdrop ) { if ( this.isShown && this.settings.backdrop ) {
var doAnimate = $.support.transition && animate
this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />') this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
.appendTo(document.body) .appendTo(document.body)
@ -139,12 +143,15 @@
this.$backdrop.click($.proxy(this.hide, this)) this.$backdrop.click($.proxy(this.hide, this))
} }
setTimeout(function () { if ( doAnimate ) {
that.$backdrop[0].offsetWidth // force reflow
}
that.$backdrop && that.$backdrop.addClass('in') that.$backdrop && that.$backdrop.addClass('in')
$.support.transition && that.$backdrop.hasClass('fade') ?
doAnimate ?
that.$backdrop.one(transitionEnd, callback) : that.$backdrop.one(transitionEnd, callback) :
callback() callback()
}, 0)
} else if ( !this.isShown && this.$backdrop ) { } else if ( !this.isShown && this.$backdrop ) {
this.$backdrop.removeClass('in') this.$backdrop.removeClass('in')
@ -165,13 +172,13 @@
function escape() { function escape() {
var that = this var that = this
if ( this.isShown && this.settings.keyboard ) { if ( this.isShown && this.settings.keyboard ) {
$('body').bind('keyup.modal', function ( e ) { $(window).bind('keyup.modal', function ( e ) {
if ( e.which == 27 ) { if ( e.which == 27 ) {
that.hide() that.hide()
} }
}) })
} else if ( !this.isShown ) { } else if ( !this.isShown ) {
$('body').unbind('keyup.modal') $(window).unbind('keyup.modal')
} }
} }

View File

@ -9,7 +9,7 @@ $(function () {
test("should return element", function () { test("should return element", function () {
var div = $("<div id='modal-test'></div>") var div = $("<div id='modal-test'></div>")
ok(div.modal() == div, 'document.body returned') ok(div.modal() == div, 'div element returned')
}) })
test("should expose defaults var for settings", function () { test("should expose defaults var for settings", function () {
@ -22,12 +22,12 @@ $(function () {
var div = $("<div id='modal-test'></div>") var div = $("<div id='modal-test'></div>")
div div
.modal() .modal()
.modal("show")
.bind("shown", function () { .bind("shown", function () {
ok($('#modal-test').length, 'modal insterted into dom') ok($('#modal-test').length, 'modal insterted into dom')
start() start()
div.remove() div.remove()
}) })
.modal("show")
}) })
test("should hide modal when hide is called", function () { test("should hide modal when hide is called", function () {
@ -92,14 +92,14 @@ $(function () {
$.support.transition = false $.support.transition = false
var div = $("<div id='modal-test'></div>") var div = $("<div id='modal-test'></div>")
div div
.modal({backdrop:true}) .modal({ backdrop:true })
.modal("show")
.bind("shown", function () { .bind("shown", function () {
equal($('.modal-backdrop').length, 1, 'modal backdrop inserted into dom') equal($('.modal-backdrop').length, 1, 'modal backdrop inserted into dom')
start() start()
div.remove() div.remove()
$('.modal-backdrop').remove() $('.modal-backdrop').remove()
}) })
.modal("show")
}) })
test("should not add backdrop when not desired", function () { test("should not add backdrop when not desired", function () {
@ -108,12 +108,12 @@ $(function () {
var div = $("<div id='modal-test'></div>") var div = $("<div id='modal-test'></div>")
div div
.modal({backdrop:false}) .modal({backdrop:false})
.modal("show")
.bind("shown", function () { .bind("shown", function () {
equal($('.modal-backdrop').length, 0, 'modal backdrop not inserted into dom') equal($('.modal-backdrop').length, 0, 'modal backdrop not inserted into dom')
start() start()
div.remove() div.remove()
}) })
.modal("show")
}) })
test("should close backdrop when clicked", function () { test("should close backdrop when clicked", function () {
@ -122,7 +122,6 @@ $(function () {
var div = $("<div id='modal-test'></div>") var div = $("<div id='modal-test'></div>")
div div
.modal({backdrop:true}) .modal({backdrop:true})
.modal("show")
.bind("shown", function () { .bind("shown", function () {
equal($('.modal-backdrop').length, 1, 'modal backdrop inserted into dom') equal($('.modal-backdrop').length, 1, 'modal backdrop inserted into dom')
$('.modal-backdrop').click() $('.modal-backdrop').click()
@ -130,6 +129,7 @@ $(function () {
start() start()
div.remove() div.remove()
}) })
.modal("show")
}) })
test("should not close backdrop when click disabled", function () { test("should not close backdrop when click disabled", function () {
@ -138,7 +138,6 @@ $(function () {
var div = $("<div id='modal-test'></div>") var div = $("<div id='modal-test'></div>")
div div
.modal({backdrop: 'static'}) .modal({backdrop: 'static'})
.modal("show")
.bind("shown", function () { .bind("shown", function () {
equal($('.modal-backdrop').length, 1, 'modal backdrop inserted into dom') equal($('.modal-backdrop').length, 1, 'modal backdrop inserted into dom')
$('.modal-backdrop').click() $('.modal-backdrop').click()
@ -147,5 +146,6 @@ $(function () {
div.remove() div.remove()
$('.modal-backdrop').remove() $('.modal-backdrop').remove()
}) })
.modal("show")
}) })
}) })