r2852 - + Reverted bulk of parameter parsing code until further discussion takes place (offset pa...

r2852 - + Reverted bulk of parameter parsing code until further discussion takes place (offset pa...


Author: vtGavin
Date: Mon Jun 29 21:52:16 2009
New Revision: 2852
Modified:
branches/dev/positionTo/ui/ui.positionTo.js
Log:
+ Reverted bulk of parameter parsing code until further discussion takes
place (offset parameter now works as it did).
+ Incorporated all of Scott's changes into previous snapshot of positionTo.
All major changes are left intact in some form or another (supports
jQuery.noConflict(), supports multiple items, chainable, uses jQuery 1.4
offset method, etc).
Modified: branches/dev/positionTo/ui/ui.positionTo.js
==============================================================================
--- branches/dev/positionTo/ui/ui.positionTo.js    (original)
+++ branches/dev/positionTo/ui/ui.positionTo.js    Mon Jun 29 21:52:16 2009
@@ -1,5 +1,5 @@
/*
- * jQuery UI positionTo @VERSION
+ * jQuery UI / Utils / positionTo @VERSION
*
* Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT (MIT-LICENSE.txt)
@@ -7,117 +7,139 @@
*
* TODO: create document page for positionTo and add link here
*/
-(function($) {
-
-var horizontalPositions = /left|center|right/,
-    horizontalDefault = 'center',
-    verticalPositions = /top|middle|bottom/,
-    verticalDefault = 'middle';
-
-$.fn.positionTo = function(options) {
+(function($){
+    
+$.fn.positionTo = function(options){
+    
    options = $.extend({
+        my: 'left top',
+        at: 'left bottom',
+        offset: '0 0',
        collisionDetect: 'flip',
        stackFix: true
    }, options);
-    var target = $(options.of),
-        targetProps = {
-            offset: target.offset(),
-            width: target.outerWidth(),
-            height: target.outerHeight()
-        },
-
-//        offset = options.offset.replace(/px/gi, '').split(' '),
-        position = targetProps.offset;
-
-    $.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;
-    });
+    var $targetElem = $(options.of),
+        targetPosition = $targetElem.offset();
-    switch (options.at[0]) {
-        case 'left':
-            break;
-        case 'right':
-            position.left += targetProps.width;
-            break;
-        default:
-            position.left += targetProps.width / 2;
-            break;
-    }
+    targetPosition.right = targetPosition.left + $targetElem.outerWidth();
+    targetPosition.bottom = targetPosition.top + $targetElem.outerHeight();
-    switch (options.at[1]) {
-        case 'top':
-            break;
-        case 'bottom':
-            position.top += targetProps.height;
-            break;
-        default:
-            position.top += targetProps.height / 2;
-            break;
-    }
+    var positionableOffset_x = 0,
+        positionableOffset_y = 0,
+        array_positionableAnchor_chunks = options.my.split(' '),
+        array_targetAnchor_chunks = options.at.split(' '),
+        array_offset_chunks = options.offset.replace(/px/gi, '').split(' ');
+
+    if (array_positionableAnchor_chunks.length == 2)
+    {
+        // now, figure out the horizontal positionable offset
+        switch (array_positionableAnchor_chunks[0])
+        {
+            case 'center':
+                positionableOffset_x -= (this.outerWidth() / 2);
+                break;
-    return this.each(function() {
-        var elem = $(this),
-            elemProps = {
-                width: elem.outerWidth(),
-                height: elem.outerHeight()
-            };
+            case 'right':
+                positionableOffset_x -= this.outerWidth();
+                break;
+        }
-        switch (options.my[0]) {
-            case 'left':
+        // figure out the vertical positionable offset
+        switch(array_positionableAnchor_chunks[1])
+        {
+            case 'middle':
+                positionableOffset_y -= (this.outerHeight() / 2);
                break;
-            case 'right':
-                position.left -= elemProps.width;
+                
+            case 'bottom':
+            positionableOffset_y -= this.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;
-            default:
-                position.left -= elemProps.width / 2;
+
+            case 'right':
+                positionableOffset_x += (targetPosition.right - targetPosition.left);
                break;
        }
-        switch (options.my[1]) {
-            case 'top':
+        // figure out the vertical target offset
+        switch (array_targetAnchor_chunks[1])
+        {
+            case 'middle':
+                positionableOffset_y += (targetPosition.bottom - targetPosition.top) /
2;
                break;
+
            case 'bottom':
-                position.top -= elemProps.height;
-                break;
-            default:
-                position.top -= elemProps.height / 2;
+                positionableOffset_y += (targetPosition.bottom - targetPosition.top);
                break;
        }
+    }
-        // TODO: offset option
-        // TODO: collision option
-        // TODO: by option
-        // TODO: stackfix option
+    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;
+        }
-        elem.offset(position);
+        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;
+        }
+    }
+    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;
+        }
+    }
+    
+    return this.each(function(){
+        // 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...
+        $(this).offset({
+            left: Math.round(targetPosition.left + positionableOffset_x),
+            top: Math.round(targetPosition.top + positionableOffset_y)
+        });
    });
};
-
-
-// the following functionality is planned for jQuery 1.4
-// copied from http://plugins.jquery.com/files/offset.js.txt
+/*
+ * The following offset functionality is planned for jQuery 1.4 and 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 the the browser window by calling setXY
+     * elements, relative to the browser window by calling setXY.
+     *
     * @param {Object} newOffset
     */
    offset : function(newOffset){
@@ -162,7 +184,6 @@
            if(hide) $(el).hide();
        });
    }
-
});
})(jQuery);