]> source.dussan.org Git - jquery-ui.git/commitdiff
Position: Split out dimension parsing.
authorScott González <scott.gonzalez@gmail.com>
Thu, 6 Dec 2012 20:00:42 +0000 (15:00 -0500)
committerScott González <scott.gonzalez@gmail.com>
Thu, 6 Dec 2012 20:00:42 +0000 (15:00 -0500)
ui/jquery.ui.position.js

index a5dc31834385b836e6521e23b9a5afa6fcdc3704..dbfadedd1691b5e5fa006062682bab14390bec52 100644 (file)
@@ -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 );