Skip to content

Resizable: Fix aspectRatio cannot be changed after initialization #1750

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 4 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
22 changes: 22 additions & 0 deletions tests/unit/resizable/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ QUnit.test( "aspectRatio: Resizing can move objects", function( assert ) {
assert.equal( target.position().top, 0, "compare top - no movement" );
} );

QUnit.test( "aspectRatio: aspectRatio can be changed after initialization", function( assert ) {
assert.expect( 4 );

var target = $( "#resizable1" )
.resizable( { aspectRatio: 1 } )
.resizable( "option", "aspectRatio", false );

var handle = ".ui-resizable-e";

testHelper.drag( handle, 80 );

assert.equal( target.width(), 180, "compare width - size change" );
assert.equal( target.height(), 100, "compare height - no size change" );

target.resizable( "option", "aspectRatio", 2 );

testHelper.drag( handle, -40 );

assert.equal( target.width(), 140, "compare width - size change" );
assert.equal( target.height(), 70, "compare height - size change in proper relation" );
} );

QUnit.test( "containment", function( assert ) {
assert.expect( 4 );

Expand Down
3 changes: 3 additions & 0 deletions ui/widgets/resizable.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ $.widget( "ui.resizable", $.ui.mouse, {
this._removeHandles();
this._setupHandles();
break;
case "aspectRatio":
this._aspectRatio = !!value;
Copy link
Member

Choose a reason for hiding this comment

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

You cannot cast to a boolean here, numbers are used to define the ratio.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have two properties this.aspectRatio and this._aspectRatio.
In aspectRatio we have numbers are used to define the ratio. It is set in _mouseStart method https://github.com/jquery/jquery-ui/blob/1.12.1/ui/widgets/resizable.js#L397.
We have numeric aspectRatio even in cases when we do not set aspectRatio in options, because we must know it when resize with shiftKey pressed.
We use _aspectRatio to know save aspect ratio without pressed shiftKey (anytime) or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add tests for setting aspectRatio numeric value. Commit 02796d6

Copy link
Member

Choose a reason for hiding this comment

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

Ah, thanks for pointing that out. Looking at just the diff, I forgot about the two.

break;
default:
break;
}
Expand Down