r2854 - positionTo: reverted to r2849.

r2854 - positionTo: reverted to r2849.


Author: scott.gonzalez
Date: Tue Jun 30 18:18:32 2009
New Revision: 2854
Modified:
branches/dev/positionTo/ui/ui.positionTo.js
Log:
positionTo: reverted to r2849.
Modified: branches/dev/positionTo/ui/ui.positionTo.js
==============================================================================
--- branches/dev/positionTo/ui/ui.positionTo.js    (original)
+++ branches/dev/positionTo/ui/ui.positionTo.js    Tue Jun 30 18:18:32 2009
@@ -1,5 +1,5 @@
/*
- * jQuery UI / Utils / positionTo @VERSION
+ * jQuery UI positionTo @VERSION
*
* Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT (MIT-LICENSE.txt)
@@ -7,142 +7,117 @@
*
* TODO: create document page for positionTo and add link here
*/
-(function($){
-    
-$.fn.positionTo = function(options){
-    
+(function($) {
+
+var horizontalPositions = /left|center|right/,
+    horizontalDefault = 'center',
+    verticalPositions = /top|middle|bottom/,
+    verticalDefault = 'middle';
+
+$.fn.positionTo = function(options) {
    options = $.extend({
-        my: 'left top',
-        at: 'left bottom',
-        offset: '0 0',
        collisionDetect: 'flip',
-        by: false,
        stackFix: true
    }, options);
-    var $targetElem = $(options.of),
-        targetPosition = $targetElem.offset();
+    var target = $(options.of),
+        targetProps = {
+            offset: target.offset(),
+            width: target.outerWidth(),
+            height: target.outerHeight()
+        },
-    targetPosition.right = targetPosition.left + $targetElem.outerWidth();
-    targetPosition.bottom = targetPosition.top + $targetElem.outerHeight();
+        offset = options.offset.replace(/px/gi, '').split(' '),
+        position = targetProps.offset;
-    var array_positionableAnchor_chunks = options.my.split(' '),
-        array_targetAnchor_chunks = options.at.split(' '),
-        array_offset_chunks = options.offset.replace(/px/gi, '').split(' ');
-    
-    return this.each(function(){
-        var $elem = $(this),
-            positionableOffset_x = 0,
-            positionableOffset_y = 0;
-    
-        if (array_positionableAnchor_chunks.length == 2)
-        {
-            // now, figure out the horizontal positionable offset
-            switch (array_positionableAnchor_chunks[0])
-            {
-                case 'center':
-                    positionableOffset_x -= ($elem.outerWidth() / 2);
-                    break;
-    
-                case 'right':
-                    positionableOffset_x -= $elem.outerWidth();
-                    break;
-            }
-    
-            // figure out the vertical positionable offset
-            switch(array_positionableAnchor_chunks[1])
-            {
-                case 'middle':
-                    positionableOffset_y -= ($elem.outerHeight() / 2);
-                    break;
-                    
-                case 'bottom':
-                    positionableOffset_y -= $elem.outerHeight();
-                    break;
-            }
-        }
-    
-        if (array_targetAnchor_chunks.length == 2)
-        {
-            // figure out the horizontal target offset
-            switch (array_targetAnchor_chunks[0])
-            {
-                case 'center':
-                    positionableOffset_x += (targetPosition.right - targetPosition.left)
/ 2;
-                    break;
-    
-                case 'right':
-                    positionableOffset_x += (targetPosition.right - targetPosition.left);
-                    break;
-            }
-    
-            // figure out the vertical target offset
-            switch (array_targetAnchor_chunks[1])
-            {
-                case 'middle':
-                    positionableOffset_y += (targetPosition.bottom - targetPosition.top)
/ 2;
-                    break;
-    
-                case 'bottom':
-                    positionableOffset_y += (targetPosition.bottom - targetPosition.top);
-                    break;
-            }
-        }
-    
-        if (array_offset_chunks.length == 2)
-        {
-            var offset_horizontal_px = parseInt(array_offset_chunks[0]);
-            // if: a valid integer was specified for the horizontal offset
-            // then: apply it to the calculated horizontal offset
-            if (!isNaN(offset_horizontal_px))
-            {
-                positionableOffset_x += offset_horizontal_px;
-            }
-    
-            var offset_vertical_px = parseInt(array_offset_chunks[1]);
-            // if: a valid integer was specified for the vertical offset
-            // then: apply it to the calculated vertical offset
-            if (!isNaN(offset_vertical_px))
-            {
-                positionableOffset_y += offset_vertical_px;
-            }
+    $.each(['my', 'at'], function() {
+        var pos = options[this].split(' ');
+        pos = pos.length == 1
+            ? horizontalPositions.test(pos[0])
+                ? pos.concat([verticalDefault])
+                : verticalPositions.test(pos[0])
+                    ? [horizontalDefault].concat(pos)
+                    : [horizontalDefault, verticalDefault]
+            : pos;
+        pos[0] = horizontalPositions.test(pos[0]) ? pos[0] : horizontalDefault;
+        pos[1] = verticalPositions.test(pos[1]) ? pos[1] : verticalDefault;
+        options[this] = pos;
+    });
+
+    switch (options.at[0]) {
+        case 'left':
+            break;
+        case 'right':
+            position.left += targetProps.width;
+            break;
+        default:
+            position.left += targetProps.width / 2;
+            break;
+    }
+
+    switch (options.at[1]) {
+        case 'top':
+            break;
+        case 'bottom':
+            position.top += targetProps.height;
+            break;
+        default:
+            position.top += targetProps.height / 2;
+            break;
+    }
+
+    return this.each(function() {
+        var elem = $(this),
+            elemProps = {
+                width: elem.outerWidth(),
+                height: elem.outerHeight()
+            };
+
+        switch (options.my[0]) {
+            case 'left':
+                break;
+            case 'right':
+                position.left -= elemProps.width;
+                break;
+            default:
+                position.left -= elemProps.width / 2;
+                break;
        }
-        else if (array_offset_chunks.length == 1)
-        {
-            var offset_px = parseInt(array_offset_chunks[0]);
-            // if: a valid integer was specified for the offset
-            // then: apply it to the calculated horizontal and vertical offsets
-            if (!isNaN(offset_px))
-            {
-                positionableOffset_x += offset_px;
-                positionableOffset_y += offset_px;
-            }
+
+        switch (options.my[1]) {
+            case 'top':
+                break;
+            case 'bottom':
+                position.top -= elemProps.height;
+                break;
+            default:
+                position.top -= elemProps.height / 2;
+                break;
        }
-        
-        // TODO: currently rounding calculated left and top offset to the
nearest whole number
-        // as calling $.offset() in Firefox 3.5 strangely toggles between
(X.Y) and (X) given
-        // X as an offset when called successively. should be
investigated...
-        $elem.offset({
-            left: Math.round(targetPosition.left + positionableOffset_x),
-            top: Math.round(targetPosition.top + positionableOffset_y)
-        });
+
+        // TODO: offset option
+        // TODO: collision option
+        // TODO: by option
+        // TODO: stackfix option
+
+        elem.offset(position);
    });
};
-/*
- * The following offset functionality is planned for jQuery 1.4 and copied
from
- * (http://plugins.jquery.com/files/offset.js.txt)
- */
+
+
+// the following functionality is planned for jQuery 1.4
+// copied from http://plugins.jquery.com/files/offset.js.txt
$.fn.extend({
+
    /**
-     * Stores the original version of offset(), so that we don't lose it.
+     * Stores the original version of offset(), so that we don't lose it
     */
-    _offset: $.fn.offset,
+    _offset : $.fn.offset,
    
    /**
     * Set or get the specific left and top position of the matched
-     * elements, relative to the browser window by calling setXY.
-     *
+     * elements, relative the the browser window by calling setXY
     * @param {Object} newOffset
     */
    offset : function(newOffset){
@@ -187,6 +162,7 @@
            if(hide) $(el).hide();
        });
    }
+
});
})(jQuery);