Removing unnecessary variables, caching 'within' in tests where its beneficial, and making some other changes based on the code review

This commit is contained in:
David Murdoch 2011-05-16 23:46:21 +00:00
parent f798b046e0
commit e4a42991df
3 changed files with 83 additions and 201 deletions

View File

@ -314,7 +314,8 @@ test( "collision: fit, with offset", function() {
test( "collision: fit, window scrolled", function() {
if ( scrollTopSupport() ) {
var win = $( window );
$( window ).scrollTop( 300 ).scrollLeft( 200 );
win.scrollTop( 300 ).scrollLeft( 200 );
collisionTest({
collision: "fit",
at: "left-100 top-100"
@ -323,7 +324,7 @@ test( "collision: fit, window scrolled", function() {
collision: "fit",
at: "right+100 bottom+100"
}, { top: 300 + win.height() - 10, left: 200 + win.width() - 10 }, "right bottom" );
win.scrollTop( 0 ).scrollLeft( 0 );
}
});

View File

@ -1,14 +1,19 @@
(function( $ ) {
$("#within-container").show();
function scrollTopSupport() {
$( window ).scrollTop( 1 );
return $( window ).scrollTop() === 1;
}
module( "position - within", {
setup: function(){
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"}).show();
}
});
var addTop = -20,
addLeft = -20;
$.fn.addOffsets = function() {
var elOffset = this.offset(),
offset = $("#within-container").offset();
@ -19,17 +24,15 @@ $.fn.addOffsets = function() {
return {top: elOffset.top - offset.top, left: elOffset.left - offset.left };
};
test( "within: my, at, of", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "my, at, of", function() {
var within = $("#within-container");
$( "#elx" ).position({
my: "left top",
at: "left top",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 40, left: addLeft + 40 }, "left top, left top" );
@ -38,7 +41,7 @@ test( "within: my, at, of", function() {
at: "left bottom",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 60, left: addLeft + 40 }, "left top, left bottom" );
@ -47,7 +50,7 @@ test( "within: my, at, of", function() {
at: "bottom",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 55, left: addLeft + 50 }, "left, bottom" );
@ -56,16 +59,12 @@ test( "within: my, at, of", function() {
at: "bar baz",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 45, left: addLeft +50 }, "left foo, bar baz" );
});
test( "within: multiple elements", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "multiple elements", function() {
var elements = $( "#el1, #el2" );
var result = elements.position({
my: "left top",
@ -82,11 +81,7 @@ test( "within: multiple elements", function() {
});
});
test( "within: positions", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "positions", function() {
var definitions = [];
var offsets = {
left: 0,
@ -124,18 +119,15 @@ test( "within: positions", function() {
});
});
test( "within: of", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "of", function() {
var within = $("#within-container");
$( "#elx" ).position({
my: "left top",
at: "left top",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 40, left: addLeft + 40 }, "selector" );
@ -144,7 +136,7 @@ test( "within: of", function() {
at: "left bottom",
of: $( "#parentx"),
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 60, left: addLeft + 40 }, "jQuery object" );
@ -153,85 +145,17 @@ test( "within: of", function() {
at: "left top",
of: $( "#parentx" )[ 0 ],
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 40, left: addLeft + 40 }, "DOM element" );
// these tests are not valid for "within" since of is not contained by within.
/*
$( "#elx" ).position({
my: "right bottom",
at: "right bottom",
of: document,
collision: "none",
within: $("#within-container")
});
same( $( "#elx" ).addOffsets(), {
top: addTop + $( document ).height() - 10,
left: addLeft + $( document ).width() - 10
}, "document" );
$( "#elx" ).position({
my: "right bottom",
at: "right bottom",
of: $( document ),
collision: "none",
within: $("#within-container")
});
same( $( "#elx" ).addOffsets(), {
top: addTop + $( document ).height() - 10,
left: addLeft + $( document ).width() - 10
}, "document as jQuery object" );
$( window ).scrollTop( 0 );
$( "#elx" ).position({
my: "right bottom",
at: "right bottom",
of: window,
collision: "none",
within: $("#within-container")
});
same( $( "#elx" ).addOffsets(), {
top: addTop + $( window ).height() - 10,
left: addLeft + $( window ).width() - 10
}, "window" );
$( "#elx" ).position({
my: "right bottom",
at: "right bottom",
of: $( window ),
collision: "none",
within: $("#within-container")
});
same( $( "#elx" ).addOffsets(), {
top: addTop + $( window ).height() - 10,
left: addLeft + $( window ).width() - 10
}, "window as jQuery object" );
if ( scrollTopSupport() ) {
$( window ).scrollTop( 500 ).scrollLeft( 200 );
$( "#elx" ).position({
my: "right bottom",
at: "right bottom",
of: window,
collision: "none",
within: $("#within-container")
});
same( $( "#elx" ).addOffsets(), {
top: addTop + $( window ).height() + 500 - 10,
left: addLeft + $( window ).width() + 200 - 10
}, "window, scrolled" );
$( window ).scrollTop( 0 ).scrollLeft( 0 );
}*/
var event = $.extend( $.Event( "someEvent" ), { pageX: 200, pageY: 300 } );
$( "#elx" ).position({
my: "left top",
at: "left top",
of: event,
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).offset(), {
top: 300,
@ -244,7 +168,7 @@ test( "within: of", function() {
at: "right bottom",
of: event,
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).offset(), {
top: 600,
@ -253,16 +177,14 @@ test( "within: of", function() {
});
test( "within:offsets", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
var within = $("#within-container");
$( "#elx" ).position({
my: "left top",
at: "left+10 bottom+10",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 70, left: addLeft + 50 }, "offsets in at" );
@ -271,7 +193,7 @@ test( "within:offsets", function() {
at: "left bottom",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 50, left: addLeft + 50 }, "offsets in my" );
@ -280,7 +202,7 @@ test( "within:offsets", function() {
at: "left+50% bottom-10%",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 58, left: addLeft + 50 }, "percentage offsets in at" );
@ -289,17 +211,15 @@ test( "within:offsets", function() {
at: "left bottom",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
});
same( $( "#elx" ).addOffsets(), { top: addTop + 65, left: addLeft + 37 }, "percentage offsets in my" );
});
test( "within: using", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "using", function() {
expect( 6 );
var within = $("#within-container");
var count = 0,
elems = $( "#el1, #el2" ),
@ -309,7 +229,7 @@ test( "within: using", function() {
at: "rigt bottom",
of: "#parentx",
collision: "none",
within: $("#within-container")
within: within
}).addOffsets();
elems.position({
@ -317,13 +237,13 @@ test( "within: using", function() {
at: "left top",
of: "#parentx",
using: function( position ) {
position.top -= $("#within-container").offset().top;
position.left -= $("#within-container").offset().left;
position.top -= within.offset().top;
position.left -= within.offset().left;
same( this, elems[ count ], "correct context for call #" + count );
same( position, expectedPosition, "correct position for call #" + count );
count++;
},
within: $("#within-container")
within: within
});
elems.each(function() {
@ -332,12 +252,15 @@ test( "within: using", function() {
});
function collisionTest( config, result, msg ) {
var within = $("#within-container");
var elem = $( "#elx" ).position( $.extend({
my: "left top",
at: "right bottom",
of: $("#within-container")[0],
within: $("#within-container")
of: within[0],
within: within
}, config ) );
same( elem.addOffsets(), result, msg );
}
@ -348,14 +271,12 @@ function collisionTest2( config, result, msg ) {
}, config ), result, msg );
}
test( "within: collision: fit, no offset", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: fit, no offset", function() {
var within = $("#within-container");
collisionTest({
collision: "fit"
}, { top: addTop + $("#within-container").height() - 10, left: addLeft + $("#within-container").width() - 10 }, "right bottom" );
}, { top: addTop + within.height() - 10, left: addLeft + within.width() - 10 }, "right bottom" );
collisionTest2({
collision: "fit"
@ -363,15 +284,13 @@ test( "within: collision: fit, no offset", function() {
});
test( "within: collision: fit, with offset", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: fit, with offset", function() {
var within = $("#within-container");
collisionTest({
collision: "fit",
at: "right+2 bottom+3"
}, { top: addTop + $("#within-container").height() - 10, left: addLeft + $("#within-container").width() - 10 }, "right bottom");
}, { top: addTop + within.height() - 10, left: addLeft + within.width() - 10 }, "right bottom");
collisionTest2({
collision: "fit",
@ -384,14 +303,10 @@ test( "within: collision: fit, with offset", function() {
}, { top: addTop + 0, left: addLeft + 0 }, "left top, negative offset" );
});
test( "within: collision: fit, within scrolled", function() {
test( "collision: fit, within scrolled", function() {
if ( scrollTopSupport() ) {
$("#within-container").css({"width": "1000px", "height": "800px", "top": "20px", "left": "20px", "position": "relative"});
var win = $("#within-container").css("overflow", "auto");
win.scrollTop( 300 ).scrollLeft( 150 );
var within = $("#within-container").css({"width": "1000px", "height": "800px", "overflow": "auto"});
within.scrollTop( 300 ).scrollLeft( 150 );
collisionTest({
collision: "fit",
@ -400,15 +315,13 @@ test( "within: collision: fit, within scrolled", function() {
collisionTest2({
collision: "fit",
at: "right+100 bottom+100"
}, { top: addTop + win.height() - 10, left: addLeft + win.width() - 10 }, "right bottom" );
win.scrollTop( 0 ).scrollLeft( 0 );
}, { top: addTop + within.height() - 10, left: addLeft + within.width() - 10 }, "right bottom" );
within.scrollTop( 0 ).scrollLeft( 0 );
}
});
test( "within: collision: flip, no offset", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: flip, no offset", function() {
var within = $("#within-container");
collisionTest({
collision: "flip"
@ -416,13 +329,11 @@ test( "within: collision: flip, no offset", function() {
collisionTest2({
collision: "flip"
}, { top: addTop + $("#within-container").height(), left: addLeft + $("#within-container").width() }, "right bottom" );
}, { top: addTop + within.height(), left: addLeft + within.width() }, "right bottom" );
});
test( "within: collision: flip, with offset", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: flip, with offset", function() {
var within = $("#within-container");
collisionTest({
collision: "flip",
@ -432,37 +343,33 @@ test( "within: collision: flip, with offset", function() {
collisionTest2({
collision: "flip",
at: "left+2 top+3"
}, { top: addTop + $("#within-container").height() - 3, left: addLeft + $("#within-container").width() - 2 }, "bottom, positive offset" );
}, { top: addTop + within.height() - 3, left: addLeft + within.width() - 2 }, "bottom, positive offset" );
collisionTest2({
collision: "flip",
at: "left-2 top-3"
}, { top: addTop + $("#within-container").height() + 3, left: addLeft + $("#within-container").width() + 2 }, "right bottom, negative offset" );
}, { top: addTop + within.height() + 3, left: addLeft + within.width() + 2 }, "right bottom, negative offset" );
});
test( "within: collision: none, no offset", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: none, no offset", function() {
var within = $("#within-container");
collisionTest({
collision: "none"
}, { top: addTop + $("#within-container").height(), left: addLeft + $("#within-container").width() }, "left top" );
}, { top: addTop + within.height(), left: addLeft + within.width() }, "left top" );
collisionTest2({
collision: "none"
}, { top: addTop + -10, left: addLeft + -10 }, "moved to the right bottom" );
});
test( "within: collision: none, with offset", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: none, with offset", function() {
var within = $("#within-container");
collisionTest({
collision: "none",
at: "right+2 bottom+3"
}, { top: addTop + $("#within-container").height() + 3, left: addLeft + $("#within-container").width() + 2 }, "right bottom, with offset added" );
}, { top: addTop + within.height() + 3, left: addLeft + within.width() + 2 }, "right bottom, with offset added" );
collisionTest2({
collision: "none",
@ -475,16 +382,14 @@ test( "within: collision: none, with offset", function() {
}, { top: addTop + -13, left: addLeft + -12 }, "left top, negative offset" );
});
test( "within: collision: fit, with margin", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: fit, with margin", function() {
var within = $("#within-container");
$( "#elx" ).css( "margin", 10 );
collisionTest({
collision: "fit"
}, { top: addTop + $("#within-container").height() - 20, left: addLeft + $("#within-container").width() - 20 }, "right bottom" );
}, { top: addTop + within.height() - 20, left: addLeft + within.width() - 20 }, "right bottom" );
collisionTest2({
collision: "fit"
@ -497,7 +402,7 @@ test( "within: collision: fit, with margin", function() {
collisionTest({
collision: "fit"
}, { top: addTop + $("#within-container").height() - 20, left: addLeft + $("#within-container").width() - 20 }, "right bottom" );
}, { top: addTop + within.height() - 20, left: addLeft + within.width() - 20 }, "right bottom" );
collisionTest2({
collision: "fit"
@ -510,24 +415,22 @@ test( "within: collision: fit, with margin", function() {
collisionTest({
collision: "fit"
}, { top: addTop + $("#within-container").height() - 25, left: addLeft + $("#within-container").width() - 25 }, "right bottom" );
}, { top: addTop + within.height() - 25, left: addLeft + within.width() - 25 }, "right bottom" );
collisionTest2({
collision: "fit"
}, { top: addTop + 5, left: addLeft + 5 }, "left top" );
});
test( "within: collision: flip, with margin", function() {
$("#within-container").css({"width": "500px", "height": "500px", "top": "20px", "left": "20px", "position": "relative"});
test( "collision: flip, with margin", function() {
var within = $("#within-container");
$( "#elx" ).css( "margin", 10 );
collisionTest({
collision: "flip",
at: "left top"
}, { top: addTop + $("#within-container").height() - 10, left: addLeft + $("#within-container").width() - 10 }, "left top" );
}, { top: addTop + within.height() - 10, left: addLeft + within.width() - 10 }, "left top" );
collisionTest2({
collision: "flip",
@ -535,22 +438,4 @@ test( "within: collision: flip, with margin", function() {
}, { top: addTop + 0, left: addLeft + 0 }, "right bottom" );
});
//test( "bug #5280: consistent results (avoid fractional values)", function() {
// var wrapper = $( "#bug-5280" ),
// elem = wrapper.children(),
// offset1 = elem.position({
// my: "center",
// at: "center",
// of: wrapper,
// collision: "none"
// }).offset(),
// offset2 = elem.position({
// my: "center",
// at: "center",
// of: wrapper,
// collision: "none"
// }).offset();
// same( offset1, offset2 );
//});
}( jQuery ) );

View File

@ -191,20 +191,18 @@ $.ui.position = {
withinOffset = isWindow ? win.scrollLeft() : within.offset().left,
outerWidth = isWindow ? win.width() : within.outerWidth(),
overLeft = withinOffset - data.collisionPosition.left,
overRight = data.collisionPosition.left + data.collisionWidth - outerWidth - withinOffset,
newLeft;
overRight = data.collisionPosition.left + data.collisionWidth - outerWidth - withinOffset;
// element is wider than window or too far left -> align with left edge
if ( data.collisionWidth > outerWidth || overLeft > 0 ) {
newLeft = position.left + overLeft;
position.left += overLeft;
// too far right -> align with right edge
} else if ( overRight > 0 ) {
newLeft = position.left - overRight;
position.left -= overRight;
// adjust based on position and margin
} else {
newLeft = Math.max( position.left - data.collisionPosition.left, position.left );
position.left = Math.max( position.left - data.collisionPosition.left, position.left );
}
position.left = newLeft;
},
top: function( position, data ) {
var within = data.within,
@ -213,20 +211,18 @@ $.ui.position = {
withinOffset = isWindow ? win.scrollTop() : within.offset().top,
outerHeight = isWindow ? win.height() : within.outerHeight(),
overTop = withinOffset - data.collisionPosition.top,
overBottom = data.collisionPosition.top + data.collisionHeight - outerHeight - withinOffset,
newTop;
overBottom = data.collisionPosition.top + data.collisionHeight - outerHeight - withinOffset;
// element is taller than window or too far up -> align with top edge
if ( data.collisionHeight > outerHeight || overTop > 0 ) {
newTop = position.top + overTop;
position.top += overTop;
// too far down -> align with bottom edge
} else if ( overBottom > 0 ) {
newTop = position.top - overBottom;
position.top -= overBottom;
// adjust based on position and margin
} else {
newTop = Math.max( position.top - data.collisionPosition.top, position.top );
position.top = Math.max( position.top - data.collisionPosition.top, position.top );
}
position.top = newTop;
}
},
flip: {