Skip to content

Commit 7c8790d

Browse files
committed
Resizable: account for padding & border in grids.
Fixes #10437
1 parent 750a8fd commit 7c8790d

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

tests/unit/resizable/resizable_options.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,26 @@ test( "grid - Resizable: can be moved when grid option is set (#9611)", function
299299
equal( target.position().left, oldPosition.left, "compare left" );
300300
});
301301

302+
test( "grid - maintains grid with padding and border when approaching no dimensions", function() {
303+
expect( 2 );
304+
305+
// http://bugs.jqueryui.com/ticket/10437 - Resizable: border with grid option working wrong
306+
var handle = ".ui-resizable-nw",
307+
target = $( "#resizable1" ).css({
308+
padding: 5,
309+
borderWidth: 5,
310+
width: 80,
311+
height: 80
312+
}).resizable({
313+
handles: "all",
314+
grid: 50
315+
});
316+
317+
TestHelpers.resizable.drag( handle, 50, 50 );
318+
equal( target.outerWidth(), 50, "compare width" );
319+
equal( target.outerHeight(), 50, "compare height" );
320+
});
321+
302322
test("ui-resizable-se { handles: 'all', minWidth: 60, minHeight: 60, maxWidth: 100, maxHeight: 100 }", function() {
303323
expect(4);
304324

ui/resizable.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -555,32 +555,56 @@ $.widget("ui.resizable", $.ui.mouse, {
555555
return data;
556556
},
557557

558+
_getPaddingPlusBorderDimensions: function( element ) {
559+
var i = 0,
560+
widths = [],
561+
borders = [
562+
element.css( "borderTopWidth" ),
563+
element.css( "borderRightWidth" ),
564+
element.css( "borderBottomWidth" ),
565+
element.css( "borderLeftWidth" )
566+
],
567+
paddings = [
568+
element.css( "paddingTop" ),
569+
element.css( "paddingRight" ),
570+
element.css( "paddingBottom" ),
571+
element.css( "paddingLeft" )
572+
];
573+
574+
for ( ; i < 4; i++ ) {
575+
widths[ i ] = ( parseInt( borders[ i ], 10 ) || 0 );
576+
widths[ i ] += ( parseInt( paddings[ i ], 10 ) || 0 );
577+
}
578+
579+
return {
580+
height: widths[ 0 ] + widths[ 2 ],
581+
width: widths[ 1 ] + widths[ 3 ]
582+
};
583+
},
584+
558585
_proportionallyResize: function() {
559586

560587
if (!this._proportionallyResizeElements.length) {
561588
return;
562589
}
563590

564-
var i, j, borders, paddings, prel,
591+
var prel,
592+
i = 0,
565593
element = this.helper || this.element;
566594

567-
for ( i=0; i < this._proportionallyResizeElements.length; i++) {
595+
for ( ; i < this._proportionallyResizeElements.length; i++) {
568596

569597
prel = this._proportionallyResizeElements[i];
570598

571-
if (!this.borderDif) {
572-
this.borderDif = [];
573-
borders = [prel.css("borderTopWidth"), prel.css("borderRightWidth"), prel.css("borderBottomWidth"), prel.css("borderLeftWidth")];
574-
paddings = [prel.css("paddingTop"), prel.css("paddingRight"), prel.css("paddingBottom"), prel.css("paddingLeft")];
575-
576-
for ( j = 0; j < borders.length; j++ ) {
577-
this.borderDif[ j ] = ( parseInt( borders[ j ], 10 ) || 0 ) + ( parseInt( paddings[ j ], 10 ) || 0 );
578-
}
599+
// TODO: Seems like a bug to cache this.outerDimensions
600+
// considering that we are in a loop.
601+
if (!this.outerDimensions) {
602+
this.outerDimensions = this._getPaddingPlusBorderDimensions( prel );
579603
}
580604

581605
prel.css({
582-
height: (element.height() - this.borderDif[0] - this.borderDif[2]) || 0,
583-
width: (element.width() - this.borderDif[1] - this.borderDif[3]) || 0
606+
height: (element.height() - this.outerDimensions.height) || 0,
607+
width: (element.width() - this.outerDimensions.width) || 0
584608
});
585609

586610
}
@@ -972,7 +996,8 @@ $.ui.plugin.add("resizable", "ghost", {
972996
$.ui.plugin.add("resizable", "grid", {
973997

974998
resize: function() {
975-
var that = $(this).resizable( "instance" ),
999+
var outerDimensions,
1000+
that = $(this).resizable( "instance" ),
9761001
o = that.options,
9771002
cs = that.size,
9781003
os = that.originalSize,
@@ -993,16 +1018,16 @@ $.ui.plugin.add("resizable", "grid", {
9931018
o.grid = grid;
9941019

9951020
if (isMinWidth) {
996-
newWidth = newWidth + gridX;
1021+
newWidth += gridX;
9971022
}
9981023
if (isMinHeight) {
999-
newHeight = newHeight + gridY;
1024+
newHeight += gridY;
10001025
}
10011026
if (isMaxWidth) {
1002-
newWidth = newWidth - gridX;
1027+
newWidth -= gridX;
10031028
}
10041029
if (isMaxHeight) {
1005-
newHeight = newHeight - gridY;
1030+
newHeight -= gridY;
10061031
}
10071032

10081033
if (/^(se|s|e)$/.test(a)) {
@@ -1017,19 +1042,25 @@ $.ui.plugin.add("resizable", "grid", {
10171042
that.size.height = newHeight;
10181043
that.position.left = op.left - ox;
10191044
} else {
1045+
if ( newHeight - gridY <= 0 || newWidth - gridX <= 0) {
1046+
outerDimensions = that._getPaddingPlusBorderDimensions( this );
1047+
}
1048+
10201049
if ( newHeight - gridY > 0 ) {
10211050
that.size.height = newHeight;
10221051
that.position.top = op.top - oy;
10231052
} else {
1024-
that.size.height = gridY;
1025-
that.position.top = op.top + os.height - gridY;
1053+
newHeight = gridY - outerDimensions.height;
1054+
that.size.height = newHeight;
1055+
that.position.top = op.top + os.height - newHeight;
10261056
}
10271057
if ( newWidth - gridX > 0 ) {
10281058
that.size.width = newWidth;
10291059
that.position.left = op.left - ox;
10301060
} else {
1031-
that.size.width = gridX;
1032-
that.position.left = op.left + os.width - gridX;
1061+
newWidth = gridY - outerDimensions.height;
1062+
that.size.width = newWidth;
1063+
that.position.left = op.left + os.width - newWidth;
10331064
}
10341065
}
10351066
}

0 commit comments

Comments
 (0)