Skip to content
Permalink
Browse files

draggable: re-implemented containment and grid support (fixes #3607)

  • Loading branch information...
Paul Bakaus
Paul Bakaus committed Jan 8, 2009
1 parent 122786c commit 38a5b20fe77c742c57125e039aa70fe4e4a3677d
Showing with 34 additions and 3 deletions.
  1. +34 −3 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)

0 comments on commit 38a5b20

Please sign in to comment.
You can’t perform that action at this time.