Skip to content

Commit 20f0646

Browse files
dekajpmikesherov
authored andcommitted
Resizable: containment plugin restricts resizing within container
Fixes #7018 Fixes #9107 Closes jquerygh-1122
1 parent bae1a25 commit 20f0646

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

tests/unit/resizable/resizable_options.js

+31
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ test("aspectRatio: 'preserve' (ne)", function() {
123123
equal( target.height(), 70, "compare minHeight");
124124
});
125125

126+
test( "aspectRatio: Resizing can move objects", function() {
127+
expect( 7 );
128+
129+
// http://bugs.jqueryui.com/ticket/7018 - Resizing can move objects
130+
var handleW = ".ui-resizable-w",
131+
handleNW = ".ui-resizable-nw",
132+
target = $( "#resizable1" ).resizable({
133+
aspectRatio: true,
134+
handles: "all",
135+
containment: "parent"
136+
});
137+
138+
$( "#container" ).css({ width: 200, height: 300 });
139+
$( "#resizable1" ).css({ width: 100, height: 100, left: 75, top: 200 });
140+
141+
TestHelpers.resizable.drag( handleW, -20 );
142+
equal( target.width(), 100, "compare width - no size change" );
143+
equal( target.height(), 100, "compare height - no size change" );
144+
equal( target.position().left, 75, "compare left - no movement" );
145+
146+
// http://bugs.jqueryui.com/ticket/9107 - aspectRatio and containment not handled correctly
147+
$( "#container" ).css({ width: 200, height: 300, position: "absolute", left: 100, top: 100 });
148+
$( "#resizable1" ).css({ width: 100, height: 100, left: 0, top: 0 });
149+
150+
TestHelpers.resizable.drag( handleNW, -20, -20 );
151+
equal( target.width(), 100, "compare width - no size change" );
152+
equal( target.height(), 100, "compare height - no size change" );
153+
equal( target.position().left, 0, "compare left - no movement" );
154+
equal( target.position().top, 0, "compare top - no movement" );
155+
});
156+
126157
test( "containment", function() {
127158
expect( 6 );
128159
var element = $( "#resizable1" ).resizable({

ui/jquery.ui.resizable.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,19 @@ $.widget("ui.resizable", $.ui.mouse, {
341341
el = this.helper, props = {},
342342
smp = this.originalMousePosition,
343343
a = this.axis,
344-
prevTop = this.position.top,
345-
prevLeft = this.position.left,
346-
prevWidth = this.size.width,
347-
prevHeight = this.size.height,
348344
dx = (event.pageX-smp.left)||0,
349345
dy = (event.pageY-smp.top)||0,
350346
trigger = this._change[a];
351347

348+
this.prevPosition = {
349+
top: this.position.top,
350+
left: this.position.left
351+
};
352+
this.prevSize = {
353+
width: this.size.width,
354+
height: this.size.height
355+
};
356+
352357
if (!trigger) {
353358
return false;
354359
}
@@ -369,16 +374,16 @@ $.widget("ui.resizable", $.ui.mouse, {
369374
// plugins callbacks need to be called first
370375
this._propagate("resize", event);
371376

372-
if (this.position.top !== prevTop) {
377+
if ( this.position.top !== this.prevPosition.top ) {
373378
props.top = this.position.top + "px";
374379
}
375-
if (this.position.left !== prevLeft) {
380+
if ( this.position.left !== this.prevPosition.left ) {
376381
props.left = this.position.left + "px";
377382
}
378-
if (this.size.width !== prevWidth) {
383+
if ( this.size.width !== this.prevSize.width ) {
379384
props.width = this.size.width + "px";
380385
}
381-
if (this.size.height !== prevHeight) {
386+
if ( this.size.height !== this.prevSize.height ) {
382387
props.height = this.size.height + "px";
383388
}
384389
el.css(props);
@@ -662,7 +667,9 @@ $.widget("ui.resizable", $.ui.mouse, {
662667
position: this.position,
663668
size: this.size,
664669
originalSize: this.originalSize,
665-
originalPosition: this.originalPosition
670+
originalPosition: this.originalPosition,
671+
prevSize: this.prevSize,
672+
prevPosition: this.prevPosition
666673
};
667674
}
668675

@@ -779,7 +786,7 @@ $.ui.plugin.add( "resizable", "containment", {
779786
}
780787
},
781788

782-
resize: function( event ) {
789+
resize: function( event, ui ) {
783790
var woset, hoset, isParent, isOffsetRelative,
784791
that = $( this ).resizable( "instance" ),
785792
o = that.options,
@@ -790,7 +797,8 @@ $.ui.plugin.add( "resizable", "containment", {
790797
top: 0,
791798
left: 0
792799
},
793-
ce = that.containerElement;
800+
ce = that.containerElement,
801+
continueResize = true;
794802

795803
if ( ce[ 0 ] !== document && ( /static/ ).test( ce.css( "position" ) ) ) {
796804
cop = co;
@@ -800,6 +808,7 @@ $.ui.plugin.add( "resizable", "containment", {
800808
that.size.width = that.size.width + ( that._helper ? ( that.position.left - co.left ) : ( that.position.left - cop.left ) );
801809
if ( pRatio ) {
802810
that.size.height = that.size.width / that.aspectRatio;
811+
continueResize = false;
803812
}
804813
that.position.left = o.helper ? co.left : 0;
805814
}
@@ -808,6 +817,7 @@ $.ui.plugin.add( "resizable", "containment", {
808817
that.size.height = that.size.height + ( that._helper ? ( that.position.top - co.top ) : that.position.top );
809818
if ( pRatio ) {
810819
that.size.width = that.size.height * that.aspectRatio;
820+
continueResize = false;
811821
}
812822
that.position.top = that._helper ? co.top : 0;
813823
}
@@ -829,15 +839,24 @@ $.ui.plugin.add( "resizable", "containment", {
829839
that.size.width = that.parentData.width - woset;
830840
if ( pRatio ) {
831841
that.size.height = that.size.width / that.aspectRatio;
842+
continueResize = false;
832843
}
833844
}
834845

835846
if ( hoset + that.size.height >= that.parentData.height ) {
836847
that.size.height = that.parentData.height - hoset;
837848
if ( pRatio ) {
838849
that.size.width = that.size.height * that.aspectRatio;
850+
continueResize = false;
839851
}
840852
}
853+
854+
if ( !continueResize ){
855+
that.position.left = ui.prevPosition.left;
856+
that.position.top = ui.prevPosition.top;
857+
that.size.width = ui.prevSize.width;
858+
that.size.height = ui.prevSize.height;
859+
}
841860
},
842861

843862
stop: function(){

0 commit comments

Comments
 (0)