Skip to content

Commit a6471b1

Browse files
author
Paul Bakaus
committed
draggable: removed unneeded comment block
sortable: fixed sort callback, merged position fixes from draggables
1 parent 3bec130 commit a6471b1

2 files changed

Lines changed: 77 additions & 76 deletions

File tree

ui/ui.draggable.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, {
123123
//Compute the helpers position
124124
this.position = this._generatePosition(event);
125125
this.positionAbs = this._convertPositionTo("absolute");
126-
127-
/*
128-
* - Position constraining -
129-
* Constrain the position to a mix of grid, containment.
130-
*/
131126

132127
//Call plugins and callbacks and use the resulting position if something is returned
133128
if(!noPropagation) this.position = this._trigger("drag", event) || this.position;

ui/ui.sortable.js

Lines changed: 77 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
105105

106106
//The element's absolute position on the page minus margins
107107
this.offset = this.currentItem.offset();
108-
109108
this.offset = {
110109
top: this.offset.top - this.margins.top,
111110
left: this.offset.left - this.margins.left
@@ -125,13 +124,15 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
125124
relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
126125
});
127126

127+
//Generate the original position
128+
this.originalPosition = this._generatePosition(event);
129+
this.originalPageX = event.pageX;
130+
this.originalPageY = event.pageY;
131+
128132
//Adjust the mouse offset relative to the helper if 'cursorAt' is supplied
129133
if(o.cursorAt)
130134
this._adjustOffsetFromHelper(o.cursorAt);
131135

132-
//Generate the original position
133-
this.originalPosition = this._generatePosition(event);
134-
135136
//Cache the former DOM position
136137
this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] };
137138

@@ -186,7 +187,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
186187
}
187188

188189
//Call the internal plugins
189-
$.ui.plugin.call(this, "sort", [event, this._ui()]);
190+
$.ui.plugin.call(this, "sort", [event, this._uiHash()]);
190191

191192
//Regenerate the absolute position used for position checks
192193
this.positionAbs = this._convertPositionTo("absolute");
@@ -228,7 +229,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
228229
if($.ui.ddmanager) $.ui.ddmanager.drag(this, event);
229230

230231
//Call callbacks
231-
this._trigger('sort', event, this._ui());
232+
this._trigger('sort', event);
232233

233234
this.lastPositionAbs = this.positionAbs;
234235
return false;
@@ -656,8 +657,19 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
656657

657658
_getParentOffset: function() {
658659

660+
659661
//Get the offsetParent and cache its position
660-
this.offsetParent = this.helper.offsetParent(); var po = this.offsetParent.offset();
662+
this.offsetParent = this.helper.offsetParent();
663+
var po = this.offsetParent.offset();
664+
665+
// This is a special case where we need to modify a offset calculated on start, since the following happened:
666+
// 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
667+
// 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
668+
// the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
669+
if(this.cssPosition == 'absolute' && this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) {
670+
po.left += this.scrollParent.scrollLeft();
671+
po.top += this.scrollParent.scrollTop();
672+
}
661673

662674
if((this.offsetParent[0] == document.body && $.browser.mozilla) //Ugly FF3 fix
663675
|| (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.browser.msie)) //Ugly IE fix
@@ -705,8 +717,8 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
705717
if(o.containment == 'document' || o.containment == 'window') this.containment = [
706718
0 - this.offset.relative.left - this.offset.parent.left,
707719
0 - this.offset.relative.top - this.offset.parent.top,
708-
$(o.containment == 'document' ? document : window).width() - this.offset.relative.left - this.offset.parent.left - this.margins.left - (parseInt(this.currentItem.css("marginRight"),10) || 0),
709-
($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.offset.relative.top - this.offset.parent.top - this.margins.top - (parseInt(this.currentItem.css("marginBottom"),10) || 0)
720+
$(o.containment == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left,
721+
($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
710722
];
711723

712724
if(!(/^(document|window|parent)$/).test(o.containment)) {
@@ -715,10 +727,10 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
715727
var over = ($(ce).css("overflow") != 'hidden');
716728

717729
this.containment = [
718-
co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.relative.left - this.offset.parent.left - this.margins.left,
719-
co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.relative.top - this.offset.parent.top - this.margins.top,
720-
co.left + (over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.offset.relative.left - this.offset.parent.left - this.margins.left,
721-
co.top + (over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.offset.relative.top - this.offset.parent.top - this.margins.top
730+
co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.margins.left,
731+
co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.margins.top,
732+
co.left+(over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - this.helperProportions.width - this.margins.left,
733+
co.top+(over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - this.helperProportions.height - this.margins.top
722734
];
723735
}
724736

@@ -728,69 +740,81 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
728740

729741
if(!pos) pos = this.position;
730742
var mod = d == "absolute" ? 1 : -1;
731-
var scroll = this[(this.cssPosition == 'absolute' ? 'offset' : 'scroll')+'Parent'], scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName);
743+
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);
732744

733745
return {
734746
top: (
735-
pos.top // the calculated relative position
736-
+ this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent
747+
pos.top // The absolute mouse position
748+
+ this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent
737749
+ this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border)
738-
+ ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod
739-
+ this.margins.top * mod //Add the margin (you don't want the margin counting in intersection methods)
750+
- ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod
740751
),
741752
left: (
742-
pos.left // the calculated relative position
743-
+ this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent
753+
pos.left // The absolute mouse position
754+
+ this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent
744755
+ this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border)
745-
+ ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : ( scrollIsRootNode ? 0 : scroll.scrollLeft() ) ) * mod
746-
+ this.margins.left * mod //Add the margin (you don't want the margin counting in intersection methods)
756+
- ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod
747757
)
748758
};
759+
749760
},
750761

751762
_generatePosition: function(event) {
752763

753-
var o = this.options, scroll = this[(this.cssPosition == 'absolute' ? 'offset' : 'scroll')+'Parent'], scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName);
764+
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);
765+
766+
// This is another very weird special case that only happens for relative elements:
767+
// 1. If the css position is relative
768+
// 2. and the scroll parent is the document or similar to the offset parent
769+
// we have to refresh the relative offset during the scroll so there are no jumps
770+
if(this.cssPosition == 'relative' && !(this.scrollParent[0] != document && this.scrollParent[0] != this.offsetParent[0])) {
771+
this.offset.relative = this._getRelativeOffset();
772+
}
773+
774+
var pageX = event.pageX;
775+
var pageY = event.pageY;
776+
777+
/*
778+
* - Position constraining -
779+
* Constrain the position to a mix of grid, containment.
780+
*/
781+
782+
if(this.originalPosition) { //If we are not dragging yet, we won't check for options
783+
784+
if(this.containment) {
785+
if(event.pageX - this.offset.click.left < this.containment[0]) pageX = this.containment[0] + this.offset.click.left;
786+
if(event.pageY - this.offset.click.top < this.containment[1]) pageY = this.containment[1] + this.offset.click.top;
787+
if(event.pageX - this.offset.click.left > this.containment[2]) pageX = this.containment[2] + this.offset.click.left;
788+
if(event.pageY - this.offset.click.top > this.containment[3]) pageY = this.containment[3] + this.offset.click.top;
789+
}
790+
791+
if(o.grid) {
792+
var top = this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1];
793+
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;
794+
795+
var left = this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0];
796+
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;
797+
}
754798

755-
var position = {
799+
}
800+
801+
return {
756802
top: (
757-
event.pageY // The absolute mouse position
803+
pageY // The absolute mouse position
758804
- this.offset.click.top // Click offset (relative to the element)
759805
- this.offset.relative.top // Only for relative positioned nodes: Relative offset from element to offset parent
760806
- this.offset.parent.top // The offsetParent's offset without borders (offset + border)
761807
+ ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) )
762808
),
763809
left: (
764-
event.pageX // The absolute mouse position
810+
pageX // The absolute mouse position
765811
- this.offset.click.left // Click offset (relative to the element)
766812
- this.offset.relative.left // Only for relative positioned nodes: Relative offset from element to offset parent
767813
- this.offset.parent.left // The offsetParent's offset without borders (offset + border)
768-
+ ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : ( scrollIsRootNode ? 0 : scroll.scrollLeft() ) )
814+
+ ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() )
769815
)
770816
};
771-
772-
if(!this.originalPosition) return position; //If we are not dragging yet, we won't check for options
773-
774-
/*
775-
* - Position constraining -
776-
* Constrain the position to a mix of grid, containment.
777-
*/
778-
if(this.containment) {
779-
if(position.left < this.containment[0]) position.left = this.containment[0];
780-
if(position.top < this.containment[1]) position.top = this.containment[1];
781-
if(position.left + this.helperProportions.width > this.containment[2]) position.left = this.containment[2] - this.helperProportions.width;
782-
if(position.top + this.helperProportions.height > this.containment[3]) position.top = this.containment[3] - this.helperProportions.height;
783-
}
784-
785-
if(o.grid) {
786-
var top = this.originalPosition.top + Math.round((position.top - this.originalPosition.top) / o.grid[1]) * o.grid[1];
787-
position.top = this.containment ? (!(top < this.containment[1] || top > this.containment[3]) ? top : (!(top < this.containment[1]) ? top - o.grid[1] : top + o.grid[1])) : top;
788-
789-
var left = this.originalPosition.left + Math.round((position.left - this.originalPosition.left) / o.grid[0]) * o.grid[0];
790-
position.left = this.containment ? (!(left < this.containment[0] || left > this.containment[2]) ? left : (!(left < this.containment[0]) ? left - o.grid[0] : left + o.grid[0])) : left;
791-
}
792-
793-
return position;
817+
794818
},
795819

796820
_rearrange: function(event, i, a, hardRefresh) {
@@ -870,14 +894,14 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
870894
},
871895

872896
_trigger: function(type, event, inst, noPropagation) {
873-
$.ui.plugin.call(this, type, [event, this._ui(inst)]);
874-
if(!noPropagation) $.widget.prototype._trigger.call(this, type, event, this._ui(inst));
897+
$.ui.plugin.call(this, type, [event, this._uiHash(inst)]);
898+
if(!noPropagation) $.widget.prototype._trigger.call(this, type, event, this._uiHash(inst));
875899
if(event.returnValue === false) this.cancel();
876900
},
877901

878902
plugins: {},
879903

880-
_ui: function(inst) {
904+
_uiHash: function(inst) {
881905
var self = inst || this;
882906
return {
883907
helper: self.helper,
@@ -982,24 +1006,6 @@ $.ui.plugin.add("sortable", "scroll", {
9821006
if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour)
9831007
$.ui.ddmanager.prepareOffsets(i, event);
9841008

985-
986-
987-
//This is a special case where we need to modify a offset calculated on start, since the following happened:
988-
// 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
989-
// 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
990-
// the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
991-
if(scrolled !== false && i.cssPosition == 'absolute' && i.scrollParent[0] != document && $.ui.contains(i.scrollParent[0], i.offsetParent[0])) {
992-
i.offset.parent = i._getParentOffset();
993-
}
994-
995-
// This is another very weird special case that only happens for relative elements:
996-
// 1. If the css position is relative
997-
// 2. and the scroll parent is the document or similar to the offset parent
998-
// we have to refresh the relative offset during the scroll so there are no jumps
999-
if(scrolled !== false && i.cssPosition == 'relative' && !(i.scrollParent[0] != document && i.scrollParent[0] != i.offsetParent[0])) {
1000-
i.offset.relative = i._getRelativeOffset();
1001-
}
1002-
10031009
}
10041010
});
10051011

0 commit comments

Comments
 (0)