Skip to content

Commit 750a8fd

Browse files
dekajpmikesherov
authored andcommitted
Resizable: Fix containment width with relative parent.
Refs #10140 Closes gh-1303
1 parent 2779212 commit 750a8fd

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

tests/unit/resizable/resizable_options.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ test( "aspectRatio: Resizing can move objects", function() {
155155
});
156156

157157
test( "containment", function() {
158-
expect( 8 );
158+
expect( 4 );
159159

160160
var element = $( "#resizable1" ).resizable({
161161
containment: "#container"
@@ -168,32 +168,70 @@ test( "containment", function() {
168168
TestHelpers.resizable.drag( ".ui-resizable-se", 400, 400 );
169169
equal( element.width(), 300, "constrained width at containment edge" );
170170
equal( element.height(), 200, "constrained height at containment edge" );
171+
});
172+
173+
test( "containment - not immediate parent", function() {
174+
expect( 4 );
171175

172176
// http://bugs.jqueryui.com/ticket/7485 - Resizable: Containment calculation is wrong
173177
// when containment element is not the immediate parent
174-
element = $( "#child" ).resizable({
178+
var element = $( "#child" ).resizable({
175179
containment: "#container2",
176180
handles: "all"
177181
});
178182

179183
TestHelpers.resizable.drag( ".ui-resizable-e", 300, 0 );
180-
equal( element.width(), 400, "element able to resize itself to max allowable width within container" );
184+
equal( element.width(), 400, "Relative, contained within container width" );
181185

182186
TestHelpers.resizable.drag( ".ui-resizable-s", 0, 300 );
183-
equal( element.height(), 400, "element able to resize itself to max allowable height within container" );
187+
equal( element.height(), 400, "Relative, contained within container height" );
188+
189+
$( "#child" ).css( { left: 50, top: 50 } );
190+
$( "#parent" ).css( { left: 50, top: 50 } );
191+
$( "#container2" ).css( { left: 50, top: 50 } );
184192

185-
// http://bugs.jqueryui.com/ticket/10140 - Resizable: Width calculation is wrong
186-
// when containment element is "position: relative"
187193
element = $( "#child" ).resizable({
194+
containment: "#container2",
195+
handles: "all"
196+
});
197+
198+
TestHelpers.resizable.drag( ".ui-resizable-e", 400, 0 );
199+
equal( element.width(), 300, "Relative with Left, contained within container width" );
200+
201+
TestHelpers.resizable.drag( ".ui-resizable-s", 0, 400 );
202+
equal( element.height(), 300, "Relative with Top, contained within container height" );
203+
});
204+
205+
test( "containment - immediate parent", function() {
206+
expect( 4 );
207+
208+
// http://bugs.jqueryui.com/ticket/10140 - Resizable: Width calculation is wrong when containment element is "position: relative"
209+
// when containment element is immediate parent
210+
var element = $( "#child" ).resizable({
188211
containment: "parent",
189212
handles: "all"
190213
});
191214

192-
TestHelpers.resizable.drag( ".ui-resizable-e", 300, 0 );
193-
equal( element.width(), 300, "element able to resize itself to max allowable width within container" );
215+
TestHelpers.resizable.drag( ".ui-resizable-e", 400, 0 );
216+
equal( element.width(), 300, "Relative, contained within container width" );
194217

195-
TestHelpers.resizable.drag( ".ui-resizable-s", 0, 300 );
196-
equal( element.height(), 300, "element able to resize itself to max allowable height within container" );
218+
TestHelpers.resizable.drag( ".ui-resizable-s", 0, 400 );
219+
equal( element.height(), 300, "Relative, contained within container height" );
220+
221+
$( "#child" ).css( { left: 50, top: 50 } );
222+
$( "#parent" ).css( { left: 50, top: 50 } );
223+
$( "#container2" ).css( { left: 50, top: 50 } );
224+
225+
element = $( "#child" ).resizable({
226+
containment: "parent",
227+
handles: "all"
228+
});
229+
230+
TestHelpers.resizable.drag( ".ui-resizable-e", 400, 0 );
231+
equal( element.width(), 250, "Relative with Left, contained within container width" );
232+
233+
TestHelpers.resizable.drag( ".ui-resizable-s", 0, 400 );
234+
equal( element.height(), 250, "Relative with Top, contained within container height" );
197235
});
198236

199237
test("grid", function() {

ui/resizable.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ $.ui.plugin.add( "resizable", "containment", {
774774
},
775775

776776
resize: function( event ) {
777-
var woset, hoset,
777+
var woset, hoset, isParent, isOffsetRelative,
778778
that = $( this ).resizable( "instance" ),
779779
o = that.options,
780780
co = that.containerOffset,
@@ -809,11 +809,19 @@ $.ui.plugin.add( "resizable", "containment", {
809809
that.position.top = that._helper ? co.top : 0;
810810
}
811811

812-
that.offset.left = that.parentData.left + that.position.left;
813-
that.offset.top = that.parentData.top + that.position.top;
812+
isParent = that.containerElement.get( 0 ) === that.element.parent().get( 0 );
813+
isOffsetRelative = /relative|absolute/.test( that.containerElement.css( "position" ) );
814814

815-
woset = Math.abs( ( that._helper ? that.offset.left - cop.left : ( that.offset.left - co.left ) ) + that.sizeDiff.width );
816-
hoset = Math.abs( ( that._helper ? that.offset.top - cop.top : ( that.offset.top - co.top ) ) + that.sizeDiff.height );
815+
if ( isParent && isOffsetRelative ) {
816+
that.offset.left = that.parentData.left + that.position.left;
817+
that.offset.top = that.parentData.top + that.position.top;
818+
} else {
819+
that.offset.left = that.element.offset().left;
820+
that.offset.top = that.element.offset().top;
821+
}
822+
823+
woset = Math.abs( (that._helper ? that.offset.left - cop.left : (that.offset.left - co.left)) + that.sizeDiff.width );
824+
hoset = Math.abs( (that._helper ? that.offset.top - cop.top : (that.offset.top - co.top)) + that.sizeDiff.height );
817825

818826
if ( woset + that.size.width >= that.parentData.width ) {
819827
that.size.width = that.parentData.width - woset;

0 commit comments

Comments
 (0)