Skip to content

Draggable: Modified to account resizable handles size in document containment mode, used by ui.dialog. Fixed #9521 ui.Dialog: Resizable dialogs move/resize out of viewport boundary - which results in scrollbar in window #1086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/unit/dialog/dialog.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<script src="../testsuite.js"></script>
<script>
TestHelpers.loadResources({
css: [ "ui.core", "ui.dialog" ],
css: [ "ui.core", "ui.dialog", "ui.resizable" ],
js: [
"ui/jquery.ui.core.js",
"ui/jquery.ui.widget.js",
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/dialog/dialog_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ test( "#7960: resizable handles below modal overlays", function() {
dialog.dialog( "destroy" );
});

test("#9521: Dialog: Resiable dialogs move/resize out of viewpoint boundry", function() {
expect( 2 );
var doc = {},
dialogWithNoResize = {},
dialogWithResize = {},
delta ={},
elementWithNoResize = $( "<div></div>" ).dialog({ resizable:false }),
elementWithResize = $( "<div></div>" ).dialog({ resizable:true }),
handleWithNoResize = $( ".ui-dialog-titlebar", elementWithNoResize.dialog( "widget" )),
handleWithResize = $( ".ui-dialog-titlebar", elementWithResize.dialog( "widget" ));

//Dialog with NO Resize Handles
doc.height = $( document ).height();
doc.width = $( document ).width();
dialogWithNoResize.height = elementWithNoResize.outerHeight();
dialogWithNoResize.width = elementWithNoResize.outerWidth();
delta.width = doc.width - dialogWithNoResize.width;
delta.height = doc.height - dialogWithNoResize.height;

elementWithNoResize.offset({ top: 0, left: 0 });

TestHelpers.dialog.drag( elementWithNoResize, handleWithNoResize, delta.width, delta.height );
equal( TestHelpers.dialog.checkScrollBar(), false );

//Dialog with Resize Handles
elementWithResize.offset({ top: 0, left: 0 });
dialogWithResize.height = elementWithResize.outerHeight();
dialogWithResize.width = elementWithResize.outerWidth();
delta.width = doc.width - dialogWithResize.width;
delta.height = doc.height - dialogWithResize.height;

TestHelpers.dialog.drag( elementWithResize, handleWithResize, delta.width, delta.height );

equal( TestHelpers.dialog.checkScrollBar(), false );
});

asyncTest( "Prevent tabbing out of dialogs", function() {
expect( 3 );

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/dialog/dialog_test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ TestHelpers.dialog = {
actual = { width: widthAfter, height: heightAfter },
expected = { width: widthBefore + dw, height: heightBefore + dh };
deepEqual(actual, expected, "resized[" + 50 + ", " + 50 + "] " + msg);
},
checkScrollBar: function() {
return $( document ).height() > $( window ).height();
}
};
65 changes: 59 additions & 6 deletions ui/jquery.ui.draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,48 @@ $.widget("ui.draggable", $.ui.mouse, {
};
},

_isResizableHandlersAttached: function (){
var handle;
this.resizableHandle = this.resizableHandle || {};
this.resizableHandle.attached = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is unnecessary. You're only using it in one spot, and only to avoid subtracting zero from a number.

this.resizableHandle.type = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is unnecessary. It's only used to perform an unnecessary bitwise operation on a different unnecessary property.

RIGHT: 2, //E
BOTTOM: 4, //S
LEFT: 8, //W
TOP: 16, //N
TOP_LEFT: 32, //NW
TOP_RIGHT: 64, //NE
BOTTOM_RIGHT: 128, //SE
BOTTOM_LEFT: 256, //SW
ALL: 510
};

this.resizableHandle.overflow = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is unnecessary. You're redefining the object right below this.

this.resizableHandle.overflow = {
width: 0,
height: 0
};

if ( this.element.children( " .ui-resizable-e " )[ 0 ] ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this and the two other conditionals should be able to be condensed somehow.

handle = $( this.element.children( " .ui-resizable-e " )[ 0 ] );
this.resizableHandle.attached = this.resizableHandle.attached | this.resizableHandle.type.RIGHT;
this.resizableHandle.overflow.width = handle.outerWidth() + ( parseInt( handle.css( "right" ), 10 ) || 0 ) ;
}

if ( this.element.children( " .ui-resizable-s " )[ 0 ] ){
handle = $( this.element.children( " .ui-resizable-s " )[ 0 ] );
this.resizableHandle.attached = this.resizableHandle.attached | this.resizableHandle.type.BOTTOM;
this.resizableHandle.overflow.height = handle.outerHeight() + ( parseInt( handle.css( "bottom" ), 10 ) || 0 );
}

if ( this.element.children( " .ui-resizable-se " )[ 0 ] ) {
handle = $( this.element.children( " .ui-resizable-se " )[ 0 ] );
this.resizableHandle.attached = this.resizableHandle.attached | this.resizableHandle.type.BOTTOM_RIGHT;
this.resizableHandle.overflow.width = handle.outerWidth() + ( parseInt( handle.css( "right"), 10 ) || 0 );
this.resizableHandle.overflow.height = handle.outerHeight() + ( parseInt( handle.css( "bottom" ),10 ) || 0 );
}
},

_setContainment: function() {

var over, c, ce,
Expand All @@ -407,6 +449,8 @@ $.widget("ui.draggable", $.ui.mouse, {
return;
}

this._isResizableHandlersAttached();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is named awkwardly. It's not returning a Boolean, but begins with is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess i can rename it to _calculateResizableHandleOverflow ?


if ( o.containment === "window" ) {
this.containment = [
$( window ).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
Expand All @@ -418,12 +462,21 @@ $.widget("ui.draggable", $.ui.mouse, {
}

if ( o.containment === "document") {
this.containment = [
0,
0,
$( document ).width() - this.helperProportions.width - this.margins.left,
( $( document ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
];
if ( this.resizableHandle.attached ){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional is unnecessary.

this.containment = [
0,
0,
$( document ).width() - this.helperProportions.width - this.margins.left - this.resizableHandle.overflow.width,
( $( document ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top - this.resizableHandle.overflow.height
];
} else {
this.containment = [
0,
0,
$( document ).width() - this.helperProportions.width - this.margins.left,
( $( document ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
];
}
return;
}

Expand Down