From a1eb9ca4befdabace88fc78aa59947dbf7c36e20 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Scott=20Gonz=C3=A1lez?= Date: Thu, 6 Dec 2012 15:00:42 -0500 Subject: [PATCH] Position: Split out dimension parsing. --- ui/jquery.ui.position.js | 54 +++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/ui/jquery.ui.position.js b/ui/jquery.ui.position.js index a5dc31834..dbfadedd1 100644 --- a/ui/jquery.ui.position.js +++ b/ui/jquery.ui.position.js @@ -29,10 +29,41 @@ function getOffsets( offsets, width, height ) { parseInt( offsets[ 1 ], 10 ) * ( rpercent.test( offsets[ 1 ] ) ? height / 100 : 1 ) ]; } + function parseCss( element, property ) { return parseInt( $.css( element, property ), 10 ) || 0; } +function getDimensions( elem ) { + var raw = elem[0]; + if ( raw.nodeType === 9 ) { + return { + width: elem.width(), + height: elem.height(), + offset: { top: 0, left: 0 } + }; + } + if ( $.isWindow( raw ) ) { + return { + width: elem.width(), + height: elem.height(), + offset: { top: elem.scrollTop(), left: elem.scrollLeft() } + }; + } + if ( raw.preventDefault ) { + return { + width: 0, + height: 0, + offset: { top: raw.pageY, left: raw.pageX } + }; + } + return { + width: elem.outerWidth(), + height: elem.outerHeight(), + offset: elem.offset() + }; +} + $.position = { scrollbarWidth: function() { if ( cachedScrollbarWidth !== undefined ) { @@ -91,32 +122,21 @@ $.fn.position = function( options ) { // make a copy, we don't want to modify arguments options = $.extend( {}, options ); - var atOffset, targetWidth, targetHeight, targetOffset, basePosition, + var atOffset, targetWidth, targetHeight, targetOffset, basePosition, dimensions, target = $( options.of ), within = $.position.getWithinInfo( options.within ), scrollInfo = $.position.getScrollInfo( within ), - targetElem = target[0], collision = ( options.collision || "flip" ).split( " " ), offsets = {}; - if ( targetElem.nodeType === 9 ) { - targetWidth = target.width(); - targetHeight = target.height(); - targetOffset = { top: 0, left: 0 }; - } else if ( $.isWindow( targetElem ) ) { - targetWidth = target.width(); - targetHeight = target.height(); - targetOffset = { top: target.scrollTop(), left: target.scrollLeft() }; - } else if ( targetElem.preventDefault ) { + dimensions = getDimensions( target ); + if ( target[0].preventDefault ) { // force left top to allow flipping options.at = "left top"; - targetWidth = targetHeight = 0; - targetOffset = { top: targetElem.pageY, left: targetElem.pageX }; - } else { - targetWidth = target.outerWidth(); - targetHeight = target.outerHeight(); - targetOffset = target.offset(); } + targetWidth = dimensions.width; + targetHeight = dimensions.height; + targetOffset = dimensions.offset; // clone to reuse original targetOffset later basePosition = $.extend( {}, targetOffset ); -- 2.39.5