diff options
author | Paul Bakaus <paul.bakaus@googlemail.com> | 2009-01-08 12:37:15 +0000 |
---|---|---|
committer | Paul Bakaus <paul.bakaus@googlemail.com> | 2009-01-08 12:37:15 +0000 |
commit | 38a5b20fe77c742c57125e039aa70fe4e4a3677d (patch) | |
tree | d631e45ddfb1c00a45741be17e4db2b2e3af20c7 /ui/ui.draggable.js | |
parent | 122786c4b2216321f53d10ab094f96f9d1ecec11 (diff) | |
download | jquery-ui-38a5b20fe77c742c57125e039aa70fe4e4a3677d.tar.gz jquery-ui-38a5b20fe77c742c57125e039aa70fe4e4a3677d.zip |
draggable: re-implemented containment and grid support (fixes #3607)
Diffstat (limited to 'ui/ui.draggable.js')
-rw-r--r-- | ui/ui.draggable.js | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/ui/ui.draggable.js b/ui/ui.draggable.js index 82d09c77a..78f06e860 100644 --- a/ui/ui.draggable.js +++ b/ui/ui.draggable.js @@ -96,6 +96,8 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, { //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 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, { 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 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, { 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) |