Skip to content

Commit dc5254a

Browse files
committed
Draggable: Handle containment set to false after init, and style improvements. Fixes #8962 - Containment doesn't properly update
1 parent f306a82 commit dc5254a

File tree

2 files changed

+58
-42
lines changed

2 files changed

+58
-42
lines changed

tests/unit/draggable/draggable_options.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ test( "containment, account for border", function() {
378378
});
379379

380380
test( "containment, default, switching after initialization", function() {
381-
expect( 2 );
381+
expect( 3 );
382382

383383
var element = $( "#draggable1" ).draggable({ containment: false });
384384

@@ -393,9 +393,8 @@ test( "containment, default, switching after initialization", function() {
393393

394394
TestHelpers.draggable.testDrag( element, element, -100, -100, 0, 0 );
395395

396-
// TODO: Switching back to false does not update to false
397-
// element.draggable( "option", "containment", false );
398-
// TestHelpers.draggable.testDrag( element, element, -100, -100, -100, -100 );
396+
element.draggable( "option", "containment", false );
397+
TestHelpers.draggable.testDrag( element, element, -100, -100, -100, -100 );
399398
});
400399

401400
test( "{ cursor: 'auto' }, default", function() {

ui/jquery.ui.draggable.js

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ $.widget("ui.draggable", $.ui.mouse, {
156156
(o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
157157

158158
//Set a containment if given in the options
159-
if(o.containment) {
160-
this._setContainment();
161-
}
159+
this._setContainment();
162160

163161
//Trigger event + callbacks
164162
if(this._trigger("start", event) === false) {
@@ -383,40 +381,56 @@ $.widget("ui.draggable", $.ui.mouse, {
383381
var over, c, ce,
384382
o = this.options;
385383

386-
if(o.containment === "parent") {
387-
o.containment = this.helper[0].parentNode;
384+
if ( !o.containment ) {
385+
this.containment = null;
386+
return;
388387
}
389-
if(o.containment === "document" || o.containment === "window") {
388+
389+
if ( o.containment === "window" ) {
390390
this.containment = [
391-
o.containment === "document" ? 0 : $(window).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
392-
o.containment === "document" ? 0 : $(window).scrollTop() - this.offset.relative.top - this.offset.parent.top,
393-
(o.containment === "document" ? 0 : $(window).scrollLeft()) + $(o.containment === "document" ? document : window).width() - this.helperProportions.width - this.margins.left,
394-
(o.containment === "document" ? 0 : $(window).scrollTop()) + ($(o.containment === "document" ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
391+
$( window ).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
392+
$( window ).scrollTop() - this.offset.relative.top - this.offset.parent.top,
393+
$( window ).scrollLeft() + $( window ).width() - this.helperProportions.width - this.margins.left,
394+
$( window ).scrollTop() + ( $( window ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
395395
];
396+
return;
396397
}
397398

398-
if(!(/^(document|window|parent)$/).test(o.containment) && o.containment.constructor !== Array) {
399-
c = $(o.containment);
400-
ce = c[0];
401-
402-
if(!ce) {
403-
return;
404-
}
405-
406-
over = ($(ce).css("overflow") !== "hidden");
407-
399+
if ( o.containment === "document") {
408400
this.containment = [
409-
(parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0),
410-
(parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0),
411-
(over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderRightWidth"),10) || 0) - (parseInt($(ce).css("paddingRight"),10) || 0) - this.helperProportions.width - this.margins.left - this.margins.right,
412-
(over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderBottomWidth"),10) || 0) - (parseInt($(ce).css("paddingBottom"),10) || 0) - this.helperProportions.height - this.margins.top - this.margins.bottom
401+
0,
402+
0,
403+
$( document ).width() - this.helperProportions.width - this.margins.left,
404+
( $( document ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
413405
];
414-
this.relative_container = c;
406+
return;
407+
}
415408

416-
} else if(o.containment.constructor === Array) {
409+
if ( o.containment.constructor === Array ) {
417410
this.containment = o.containment;
411+
return;
412+
}
413+
414+
if ( o.containment === "parent" ) {
415+
o.containment = this.helper[ 0 ].parentNode;
418416
}
419417

418+
c = $( o.containment );
419+
ce = c[ 0 ];
420+
421+
if( !ce ) {
422+
return;
423+
}
424+
425+
over = c.css( "overflow" ) !== "hidden";
426+
427+
this.containment = [
428+
( parseInt( c.css( "borderLeftWidth" ), 10 ) || 0 ) + ( parseInt( c.css( "paddingLeft" ), 10 ) || 0 ),
429+
( parseInt( c.css( "borderTopWidth" ), 10 ) || 0 ) + ( parseInt( c.css( "paddingTop" ), 10 ) || 0 ) ,
430+
( over ? Math.max( ce.scrollWidth, ce.offsetWidth ) : ce.offsetWidth ) - ( parseInt( c.css( "borderRightWidth" ), 10 ) || 0 ) - ( parseInt( c.css( "paddingRight" ), 10 ) || 0 ) - this.helperProportions.width - this.margins.left - this.margins.right,
431+
( over ? Math.max( ce.scrollHeight, ce.offsetHeight ) : ce.offsetHeight ) - ( parseInt( c.css( "borderBottomWidth" ), 10 ) || 0 ) - ( parseInt( c.css( "paddingBottom" ), 10 ) || 0 ) - this.helperProportions.height - this.margins.top - this.margins.bottom
432+
];
433+
this.relative_container = c;
420434
},
421435

422436
_convertPositionTo: function(d, pos) {
@@ -469,18 +483,21 @@ $.widget("ui.draggable", $.ui.mouse, {
469483
* Constrain the position to a mix of grid, containment.
470484
*/
471485

472-
if(this.originalPosition) { //If we are not dragging yet, we won't check for options
473-
if(this.containment) {
474-
if (this.relative_container){
475-
co = this.relative_container.offset();
476-
containment = [ this.containment[0] + co.left,
477-
this.containment[1] + co.top,
478-
this.containment[2] + co.left,
479-
this.containment[3] + co.top ];
480-
}
481-
else {
482-
containment = this.containment;
483-
}
486+
// If we are not dragging yet, we won't check for options
487+
if ( this.originalPosition ) {
488+
if ( this.containment ) {
489+
if ( this.relative_container ){
490+
co = this.relative_container.offset();
491+
containment = [
492+
this.containment[ 0 ] + co.left,
493+
this.containment[ 1 ] + co.top,
494+
this.containment[ 2 ] + co.left,
495+
this.containment[ 3 ] + co.top
496+
];
497+
}
498+
else {
499+
containment = this.containment;
500+
}
484501

485502
if(event.pageX - this.offset.click.left < containment[0]) {
486503
pageX = containment[0] + this.offset.click.left;

0 commit comments

Comments
 (0)