r2883 - positionTo: Created $.ui.position hash for handling collision detection. Modified all ex...

r2883 - positionTo: Created $.ui.position hash for handling collision detection. Modified all ex...


Author: scott.gonzalez
Date: Tue Jul 7 11:49:00 2009
New Revision: 2883
Modified:
branches/dev/positionTo/tests/unit/positionTo/positionTo_core.js
branches/dev/positionTo/ui/ui.positionTo.js
Log:
positionTo: Created $.ui.position hash for handling collision detection.
Modified all existing tests to explicitly not use collision detection;
separate tests specifically for collision detection will be added later.
Modified: branches/dev/positionTo/tests/unit/positionTo/positionTo_core.js
==============================================================================
--- branches/dev/positionTo/tests/unit/positionTo/positionTo_core.js    
(original)
+++ branches/dev/positionTo/tests/unit/positionTo/positionTo_core.js    Tue
Jul 7 11:49:00 2009
@@ -7,28 +7,32 @@
    $('#elx').positionTo({
        my: 'left top',
        at: 'left top',
-        of: '#parentx'
+        of: '#parentx',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 40, left: 40 }, 'left top, left top');
    $('#elx').positionTo({
        my: 'left top',
        at: 'left bottom',
-        of: '#parentx'
+        of: '#parentx',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 60, left: 40 }, 'left top, left bottom');
    $('#elx').positionTo({
        my: 'left',
        at: 'bottom',
-        of: '#parentx'
+        of: '#parentx',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 55, left: 50 }, 'left, bottom');
    $('#elx').positionTo({
        my: 'left foo',
        at: 'bar baz',
-        of: '#parentx'
+        of: '#parentx',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 45, left: 50 }, 'left foo, bar baz');
});
@@ -38,7 +42,8 @@
    var result = elements.positionTo({
        my: 'left top',
        at: 'left bottom',
-        of: '#parent'
+        of: '#parent',
+        collision: 'none'
    });
    same(result, elements);
@@ -78,7 +83,8 @@
        el.positionTo({
            my: definition.my,
            at: definition.at,
-            of: '#parent'
+            of: '#parent',
+            collision: 'none'
        });
        same(el.offset(), definition.result, "Position via " +
jsDump.parse({my:definition.my, at:definition.at}));
    });
@@ -88,28 +94,32 @@
    $('#elx').positionTo({
        my: 'left top',
        at: 'left top',
-        of: '#parentx'
+        of: '#parentx',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 40, left: 40 }, 'selector');
    
    $('#elx').positionTo({
        my: 'left top',
        at: 'left bottom',
-        of: $('#parentx')
+        of: $('#parentx'),
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 60, left: 40 }, 'jQuery object');
    
    $('#elx').positionTo({
        my: 'left top',
        at: 'left top',
-        of: $('#parentx')[0]
+        of: $('#parentx')[0],
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 40, left: 40 }, 'DOM element');
    
    $('#elx').positionTo({
        my: 'right bottom',
        at: 'right bottom',
-        of: document
+        of: document,
+        collision: 'none'
    });
    same($('#elx').offset(), {
        top: $(document).height() - 10,
@@ -119,7 +129,8 @@
    $('#elx').positionTo({
        my: 'right bottom',
        at: 'right bottom',
-        of: window
+        of: window,
+        collision: 'none'
    });
    same($('#elx').offset(), {
        top: $(window).height() - 10,
@@ -130,7 +141,8 @@
    $('#elx').positionTo({
        my: 'right bottom',
        at: 'right bottom',
-        of: window
+        of: window,
+        collision: 'none'
    });
    same($('#elx').offset(), {
        top: $(window).height() + 500 - 10,
@@ -142,7 +154,8 @@
    $('#elx').positionTo({
        my: 'left top',
        at: 'left top',
-        of: event
+        of: event,
+        collision: 'none'
    });
    same($('#elx').offset(), {
        top: 300,
@@ -153,7 +166,8 @@
    $('#elx').positionTo({
        my: 'left top',
        at: 'right bottom',
-        of: event
+        of: event,
+        collision: 'none'
    });
    same($('#elx').offset(), {
        top: 600,
@@ -166,7 +180,8 @@
        my: 'left top',
        at: 'left bottom',
        of: '#parentx',
-        offset: '10'
+        offset: '10',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 70, left: 50 }, 'single value');
    
@@ -174,7 +189,8 @@
        my: 'left top',
        at: 'left bottom',
        of: '#parentx',
-        offset: '5 -3'
+        offset: '5 -3',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 57, left: 45 }, 'two values');
    
@@ -182,7 +198,8 @@
        my: 'left top',
        at: 'left bottom',
        of: '#parentx',
-        offset: '5px -3px'
+        offset: '5px -3px',
+        collision: 'none'
    });
    same($('#elx').offset(), { top: 57, left: 45 }, 'with units');
});
@@ -196,7 +213,8 @@
        originalPosition = elems.positionTo({
            my: 'right bottom',
            at: 'rigt bottom',
-            of: '#parentx'
+            of: '#parentx',
+            collision: 'none'
        }).offset();
    
    elems.positionTo({
Modified: branches/dev/positionTo/ui/ui.positionTo.js
==============================================================================
--- branches/dev/positionTo/ui/ui.positionTo.js    (original)
+++ branches/dev/positionTo/ui/ui.positionTo.js    Tue Jul 7 11:49:00 2009
@@ -64,6 +64,13 @@
        collision[1] = collision[0];
    }
+    // normalize offset option
+    offset[0] = parseInt(offset[0], 10) || 0;
+    if (offset.length == 1) {
+        offset[1] = offset[0];
+    }
+    offset[1] = parseInt(offset[1], 10) || 0;
+
    switch (options.at[0]) {
        case 'right':
            basePosition.left += targetWidth;
@@ -82,10 +89,8 @@
            break;
    }
-    if (options.offset) {
-        basePosition.left += parseInt(offset[0], 10) || 0;
-        basePosition.top += parseInt(offset[1] === undefined ? offset[0] :
offset[1], 10) || 0;
-    }
+    basePosition.left += offset[0];
+    basePosition.top += offset[1];
    return this.each(function() {
        var elem = $(this),
@@ -114,31 +119,18 @@
                break;
        }
-        if (collision[0] != "none") {
-            over = position.left + elemWidth - $(window).width() -
elem.scrollParent().scrollLeft();
-            switch(collision[0]) {
-            case 'fit':
-                position.left = over > 0 ? position.left - over : Math.max(0,
position.left);
-                break;
-            case 'flip':
-                myOffset = options.my[0] == 'left' ? -elemWidth : elemWidth;
-                position.left += position.left < 0 ? myOffset + targetWidth : over >
0 ? myOffset - targetWidth : 0;
-                break;
-            }
-        }
-        if (collision[1] != "none") {
-            over = position.top + elemHeight - $(window).height() -
elem.scrollParent().scrollTop();
-            switch(collision[1]) {
-            case 'fit':
-                position.top = over > 0 ? position.top - over : Math.max(0,
position.top);
-                break;
-            case 'flip':
-                myOffset = options.my[1] == 'top' ? -elemHeight : elemHeight;
-                atOffset = options.at[1] == 'top' ? targetHeight : -targetHeight;
-                position.top += position.top < 0 ? myOffset + targetHeight : over >
0 ? myOffset + atOffset : 0;
-                break;
-            }
-        }
+        $.each(['left', 'top'], function(i, dir) {
+            ($.ui.position[collision[i]] &&
+                $.ui.position[collision[i]][dir](position, {
+                    targetWidth: targetWidth,
+                    targetHeight: targetHeight,
+                    elemWidth: elemWidth,
+                    elemHeight: elemHeight,
+                    offset: offset,
+                    my: options.my,
+                    at: options.at
+                }));
+        });
        (options.stackfix && $.fn.stackfix && elem.stackfix());
        // the by function is passed the offset values, not the position values
@@ -148,6 +140,32 @@
    });
};
+$.ui.position = {
+    fit: {
+        left: function(position, data) {
+            var over = position.left + data.elemWidth - $(window).width() -
$(window).scrollLeft();
+            position.left = over > 0 ? position.left - over : Math.max(0,
position.left);
+        },
+        top: function(position, data) {
+            var over = position.top + data.elemHeight - $(window).height() -
$(window).scrollTop();
+            position.top = over > 0 ? position.top - over : Math.max(0,
position.top);
+        }
+    },
+
+    flip: {
+        left: function(position, data) {
+            var over = position.top + data.elemHeight - $(window).width() -
$(window).scrollLeft();
+            myOffset = data.my[0] == 'left' ? -data.elemWidth : data.elemWidth;
+            position.left += position.left < 0 ? myOffset + data.targetWidth : over