Position: Extract getWithinInfo method, use that for tests that call getScrollInfo directly, pass within info to that, gets rid of a few more DOM accesses

This commit is contained in:
Jörn Zaefferer 2012-04-13 15:05:43 +02:00
parent c0a5e52f87
commit 7dcfae7da2
2 changed files with 25 additions and 23 deletions

View File

@ -277,7 +277,7 @@ test( "collision: fit, no offset", function() {
collisionTest({ collisionTest({
collision: "fit" collision: "fit"
}, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( within ).width }, "right bottom" ); }, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({ collisionTest2({
collision: "fit" collision: "fit"
@ -292,7 +292,7 @@ test( "collision: fit, with offset", function() {
collisionTest({ collisionTest({
collision: "fit", collision: "fit",
at: "right+2 bottom+3" at: "right+2 bottom+3"
}, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( within ).width }, "right bottom"); }, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom");
collisionTest2({ collisionTest2({
collision: "fit", collision: "fit",
@ -397,7 +397,7 @@ test( "collision: fit, with margin", function() {
collisionTest({ collisionTest({
collision: "fit" collision: "fit"
}, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( within ).width }, "right bottom" ); }, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({ collisionTest2({
collision: "fit" collision: "fit"
@ -410,7 +410,7 @@ test( "collision: fit, with margin", function() {
collisionTest({ collisionTest({
collision: "fit" collision: "fit"
}, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( within ).width }, "right bottom" ); }, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({ collisionTest2({
collision: "fit" collision: "fit"
@ -423,7 +423,7 @@ test( "collision: fit, with margin", function() {
collisionTest({ collisionTest({
collision: "fit" collision: "fit"
}, { top: addTop + of.position().top + of.height() - 15 - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - 15 - $.position.getScrollInfo( within ).width }, "right bottom" ); }, { top: addTop + of.position().top + of.height() - 15 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - 15 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({ collisionTest2({
collision: "fit" collision: "fit"

View File

@ -38,16 +38,28 @@ $.position = {
return w1 - w2; return w1 - w2;
}, },
getScrollInfo: function(within) { getScrollInfo: function( within ) {
var notWindow = within[0] !== window, var overflowX = within.isWindow ? "" : within.element.css( "overflow-x" ),
overflowX = notWindow ? within.css( "overflow-x" ) : "", overflowY = within.isWindow ? "" : within.element.css( "overflow-y" ),
overflowY = notWindow ? within.css( "overflow-y" ) : "",
scrollbarWidth = overflowX === "auto" || overflowX === "scroll" ? $.position.scrollbarWidth() : 0, scrollbarWidth = overflowX === "auto" || overflowX === "scroll" ? $.position.scrollbarWidth() : 0,
scrollbarHeight = overflowY === "auto" || overflowY === "scroll" ? $.position.scrollbarWidth() : 0; scrollbarHeight = overflowY === "auto" || overflowY === "scroll" ? $.position.scrollbarWidth() : 0;
return { return {
height: within.height() < within[0].scrollHeight ? scrollbarHeight : 0, height: within.height < within.element[0].scrollHeight ? scrollbarHeight : 0,
width: within.width() < within[0].scrollWidth ? scrollbarWidth : 0 width: within.width < within.element[0].scrollWidth ? scrollbarWidth : 0
};
},
getWithinInfo: function( element ) {
var withinElement = $( element || window ),
isWindow = $.isWindow( withinElement[0] );
return {
element: withinElement,
isWindow: isWindow,
offset: withinElement.offset(),
scrollLeft: withinElement.scrollLeft(),
scrollTop: withinElement.scrollTop(),
width: isWindow ? withinElement.width() : withinElement.outerWidth(),
height: isWindow ? withinElement.height() : withinElement.outerHeight()
}; };
}, },
getOffsets: function( offsets, width, height ) { getOffsets: function( offsets, width, height ) {
@ -70,18 +82,8 @@ $.fn.position = function( options ) {
options = $.extend( {}, options ); options = $.extend( {}, options );
var target = $( options.of ), var target = $( options.of ),
withinElement = $( options.within || window ), within = $.position.getWithinInfo( options.within ),
isWindow = $.isWindow( withinElement[0] ), scrollInfo = $.position.getScrollInfo( within ),
within = {
element: withinElement,
isWindow: isWindow,
offset: withinElement.offset(),
scrollLeft: withinElement.scrollLeft(),
scrollTop: withinElement.scrollTop(),
width: isWindow ? withinElement.width() : withinElement.outerWidth(),
height: isWindow ? withinElement.height() : withinElement.outerHeight()
},
scrollInfo = $.position.getScrollInfo( withinElement ),
targetElem = target[0], targetElem = target[0],
collision = ( options.collision || "flip" ).split( " " ), collision = ( options.collision || "flip" ).split( " " ),
offsets = {}, offsets = {},