Skip to content

Commit 6e0a055

Browse files
Ethan Rombamikesherov
Ethan Romba
authored andcommitted
Resizable: Grid now respects min/max dimensions. Fixed #8435 - grid does not respect min/max dimensions
1 parent 48e0aa0 commit 6e0a055

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

tests/unit/resizable/resizable_options.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ test("grid", function() {
117117
equal( target.height(), 120, "compare height");
118118
});
119119

120+
test("grid (min/max dimensions)", function() {
121+
expect(4);
122+
123+
var handle = ".ui-resizable-se", target = $("#resizable1").resizable({ handles: "all", grid: 20, minWidth: 65, minHeight: 65, maxWidth: 135, maxHeight: 135 });
124+
125+
TestHelpers.resizable.drag(handle, 50, 50);
126+
equal( target.width(), 120, "grid should respect maxWidth");
127+
equal( target.height(), 120, "grid should respect maxHeight");
128+
129+
TestHelpers.resizable.drag(handle, -100, -100);
130+
equal( target.width(), 80, "grid should respect minWidth");
131+
equal( target.height(), 80, "grid should respect minHeight");
132+
});
133+
120134
test("grid (wrapped)", function() {
121135
expect(4);
122136

ui/jquery.ui.resizable.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -769,25 +769,39 @@ $.ui.plugin.add("resizable", "grid", {
769769
resize: function() {
770770
var that = $(this).data("resizable"), o = that.options, cs = that.size, os = that.originalSize, op = that.originalPosition, a = that.axis;
771771
o.grid = typeof o.grid == "number" ? [o.grid, o.grid] : o.grid;
772-
var ox = Math.round((cs.width - os.width) / (o.grid[0]||1)) * (o.grid[0]||1), oy = Math.round((cs.height - os.height) / (o.grid[1]||1)) * (o.grid[1]||1);
772+
var gridX = (o.grid[0]||1), gridY = (o.grid[1]||1),
773+
ox = Math.round((cs.width - os.width) / gridX) * gridX, oy = Math.round((cs.height - os.height) / gridY) * gridY,
774+
newWidth = os.width + ox, newHeight = os.height + oy,
775+
isMaxWidth = o.maxWidth && (o.maxWidth < newWidth), isMaxHeight = o.maxHeight && (o.maxHeight < newHeight),
776+
isMinWidth = o.minWidth && (o.minWidth > newWidth), isMinHeight = o.minHeight && (o.minHeight > newHeight);
777+
778+
if (isMinWidth) {
779+
newWidth = newWidth + gridX;
780+
}
781+
if (isMinHeight) {
782+
newHeight = newHeight + gridY;
783+
}
784+
if (isMaxWidth) {
785+
newWidth = newWidth - gridX;
786+
}
787+
if (isMaxHeight) {
788+
newHeight = newHeight - gridY;
789+
}
773790

774791
if (/^(se|s|e)$/.test(a)) {
775-
that.size.width = os.width + ox;
776-
that.size.height = os.height + oy;
777-
}
778-
else if (/^(ne)$/.test(a)) {
779-
that.size.width = os.width + ox;
780-
that.size.height = os.height + oy;
792+
that.size.width = newWidth;
793+
that.size.height = newHeight;
794+
} else if (/^(ne)$/.test(a)) {
795+
that.size.width = newWidth;
796+
that.size.height = newHeight;
781797
that.position.top = op.top - oy;
782-
}
783-
else if (/^(sw)$/.test(a)) {
784-
that.size.width = os.width + ox;
785-
that.size.height = os.height + oy;
798+
} else if (/^(sw)$/.test(a)) {
799+
that.size.width = newWidth;
800+
that.size.height = newHeight;
786801
that.position.left = op.left - ox;
787-
}
788-
else {
789-
that.size.width = os.width + ox;
790-
that.size.height = os.height + oy;
802+
} else {
803+
that.size.width = newWidth;
804+
that.size.height = newHeight;
791805
that.position.top = op.top - oy;
792806
that.position.left = op.left - ox;
793807
}

0 commit comments

Comments
 (0)