r1554 - trunk/ui

r1554 - trunk/ui


Author: paul.bakaus
Date: Thu Jan 8 04:37:15 2009
New Revision: 1554
Modified:
trunk/ui/ui.draggable.js
Log:
draggable: re-implemented containment and grid support (fixes #3607)
Modified: trunk/ui/ui.draggable.js
==============================================================================
--- trunk/ui/ui.draggable.js    (original)
+++ trunk/ui/ui.draggable.js    Thu Jan 8 04:37:15 2009
@@ -96,6 +96,8 @@
        //Generate the original position
        this.originalPosition = this._generatePosition(event);
+        this.originalPageX = event.pageX;
+        this.originalPageY = event.pageY;
        //Set a containment if given in the options
        if(o.containment)
@@ -278,7 +280,7 @@
        if(!pos) pos = this.position;
        var mod = d == "absolute" ? 1 : -1;
-        var scroll = this[(this.cssPosition
== 'absolute' ? 'offset' : 'scroll')+'Parent'], scrollIsRootNode = (/(html|
body)/i).test(scroll[0].tagName);
+        var o = this.options, scroll = this.cssPosition == 'absolute'
&& !(this.scrollParent[0] != document &&
$.ui.contains(this.scrollParent[0], this.offsetParent[0])) ?
this.offsetParent : this.scrollParent, scrollIsRootNode = (/(html|
body)/i).test(scroll[0].tagName);
        return {
            top: (
@@ -309,17 +311,46 @@
        if(this.cssPosition == 'relative' && !(this.scrollParent[0] != document
&& this.scrollParent[0] != this.offsetParent[0])) {
            this.offset.relative = this._getRelativeOffset();
        }
+        
+        var pageX = event.pageX;
+        var pageY = event.pageY;
+
+        /*
+         * - Position constraining -
+         * Constrain the position to a mix of grid, containment.
+         */
+        
+        if(this.originalPosition) { //If we are not dragging yet, we won't check
for options
+
+            if(this.containment) {
+                if(event.pageX - this.offset.click.left < this.containment[0]) pageX =
this.containment[0] + this.offset.click.left;
+                if(event.pageY - this.offset.click.top < this.containment[1]) pageY =
this.containment[1] + this.offset.click.top;
+                if(event.pageX - this.offset.click.left > this.containment[2]) pageX =
this.containment[2] + this.offset.click.left;
+                if(event.pageY - this.offset.click.top > this.containment[3]) pageY =
this.containment[3] + this.offset.click.top;
+            }
+            
+            if(o.grid) {
+                var top = this.originalPageY + Math.round((pageY - this.originalPageY)
/ o.grid[1]) * o.grid[1];
+                pageY = this.containment ? (!(top - this.offset.click.top <
this.containment[1] || top - this.offset.click.top > this.containment[3]) ?
top : (!(top - this.offset.click.top < this.containment[1]) ? top -
o.grid[1] : top + o.grid[1])) : top;
+
+                var left = this.originalPageX + Math.round((pageX -
this.originalPageX) / o.grid[0]) * o.grid[0];
+                pageX = this.containment ? (!(left - this.offset.click.left <
this.containment[0] || left - this.offset.click.left >
this.containment[2]) ? left : (!(left - this.offset.click.left <
this.containment[0]) ? left - o.grid[0] : left + o.grid[0])) : left;
+            }
+
+        }
+
+
        var position = {
            top: (
-                event.pageY                                                                // The absolute mouse position
+                pageY                                                                // The absolute mouse position
                - this.offset.click.top                                                    // Click offset (relative to the
element)
                - this.offset.relative.top                                                // Only for relative positioned
nodes: Relative offset from element to offset parent
                - this.offset.parent.top                                                // The offsetParent's offset
without borders (offset + border)
                + ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : (
scrollIsRootNode ? 0 : scroll.scrollTop() ) )
            ),
            left: (
-                event.pageX                                                                // The absolute mouse position
+                pageX                                                                // The absolute mouse position
                - this.offset.click.left                                                // Click offset (relative to the
element)
                - this.offset.relative.left                                                // Only for relative positioned
nodes: Relative offset from element to offset parent
                - this.offset.parent.left                                                // The offsetParent's offset
without borders (offset + border)