Skip to content

Commit 02b8154

Browse files
authored
Merge pull request #1035 from adumesny/bugfix/999
onStartMoving() fix to minHeight (#999)
2 parents 66339a5 + 6b07c28 commit 02b8154

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

demo/two.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
1919
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
2020
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.6.9/shim.min.js"></script>
21-
<script src="../dist/gridstack.js"></script>
22-
<script src="../dist/gridstack.jQueryUI.js"></script>
21+
<script src="../src/gridstack.js"></script>
22+
<script src="../src/gridstack.jQueryUI.js"></script>
2323

2424
<style type="text/css">
2525
#grid1 {

doc/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ Change log
2323

2424
## v0.5.1-dev (work in progress)
2525

26-
- undefined x,y position messes up grid ([#1017](https://github.com/gridstack/gridstack.js/issues/1017)).
26+
- undefined x,y position messes up grid ([#1017](https://github.com/gridstack/gridstack.js/issues/1017))
2727
- changed code to 2 spaces
28+
- fix minHeight during `onStartMoving()` ([#999](https://github.com/gridstack/gridstack.js/issues/999))
2829

2930
## v0.5.1 (2019-11-07)
3031

src/gridstack.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,9 @@
759759

760760
this.opts.isNested = isNested;
761761

762-
isAutoCellHeight = this.opts.cellHeight === 'auto';
762+
isAutoCellHeight = (this.opts.cellHeight === 'auto');
763763
if (isAutoCellHeight) {
764+
// make the cell square initially
764765
self.cellHeight(self.cellWidth(), true);
765766
} else {
766767
this.cellHeight(this.opts.cellHeight, true);
@@ -964,9 +965,11 @@
964965
var cellWidth = self.cellWidth();
965966
var cellHeight = self.cellHeight();
966967
var origNode = el.data('_gridstack_node');
968+
var verticalMargin = self.opts.verticalMargin;
967969

968-
var width = origNode ? origNode.width : (Math.ceil(el.outerWidth() / cellWidth));
969-
var height = origNode ? origNode.height : (Math.ceil(el.outerHeight() / cellHeight));
970+
// height: Each row is cellHeight + verticalMargin, until last one which has no margin below
971+
var width = origNode ? origNode.width : Math.ceil(el.outerWidth() / cellWidth);
972+
var height = origNode ? origNode.height : Math.round((el.outerHeight() + verticalMargin) / (cellHeight + verticalMargin));
970973

971974
draggingElement = el;
972975

@@ -1148,7 +1151,8 @@
11481151
// check for css min height. Each row is cellHeight + verticalMargin, until last one which has no margin below
11491152
var cssMinHeight = parseInt(this.container.css('min-height'));
11501153
if (cssMinHeight > 0) {
1151-
var minHeight = (cssMinHeight + this.opts.verticalMargin) / (this.cellHeight() + this.opts.verticalMargin);
1154+
var verticalMargin = this.opts.verticalMargin;
1155+
var minHeight = Math.round((cssMinHeight + verticalMargin) / (this.cellHeight() + verticalMargin));
11521156
if (height < minHeight) {
11531157
height = minHeight;
11541158
}
@@ -1285,7 +1289,10 @@
12851289
self.grid.cleanNodes();
12861290
self.grid.beginUpdate(node);
12871291
cellWidth = self.cellWidth();
1288-
var strictCellHeight = Math.ceil(o.outerHeight() / o.attr('data-gs-height'));
1292+
var strictCellHeight = self.cellHeight();
1293+
// TODO: cellHeight cannot be set to cellHeight() (i.e. remove strictCellHeight) right here otherwise
1294+
// when sizing up we jump almost right away to next size instead of half way there. Not sure
1295+
// why as we don't use ceil() is many places but round().
12891296
cellHeight = self.container.height() / parseInt(self.container.attr('data-gs-current-height'));
12901297
self.placeholder
12911298
.attr('data-gs-x', o.attr('data-gs-x'))
@@ -1297,9 +1304,12 @@
12971304
node._beforeDragX = node.x;
12981305
node._beforeDragY = node.y;
12991306
node._prevYPix = ui.position.top;
1307+
var minHeight = (node.minHeight || 1);
1308+
var verticalMargin = self.opts.verticalMargin;
13001309

1310+
// mineHeight - Each row is cellHeight + verticalMargin, until last one which has no margin below
13011311
self.dd.resizable(el, 'option', 'minWidth', cellWidth * (node.minWidth || 1));
1302-
self.dd.resizable(el, 'option', 'minHeight', strictCellHeight * (node.minHeight || 1));
1312+
self.dd.resizable(el, 'option', 'minHeight', (strictCellHeight * minHeight) + (minHeight - 1) * verticalMargin);
13031313

13041314
if (event.type == 'resizestart') {
13051315
o.find('.grid-stack-item').trigger('resizestart');
@@ -1723,16 +1733,22 @@
17231733
}
17241734
};
17251735

1736+
/** set/get the current cell height value */
17261737
GridStack.prototype.cellHeight = function(val, noUpdate) {
1738+
// getter - returns the opts stored height else compute it...
17271739
if (typeof val == 'undefined') {
1728-
if (this.opts.cellHeight) {
1740+
if (this.opts.cellHeight && this.opts.cellHeight !== 'auto') {
17291741
return this.opts.cellHeight;
17301742
}
1743+
// compute the height taking margin into account (each row has margin other than last one)
17311744
var o = this.container.children('.' + this.opts.itemClass).first();
1732-
return Math.ceil(o.outerHeight() / o.attr('data-gs-height'));
1745+
var height = o.attr('data-gs-height');
1746+
var verticalMargin = this.opts.verticalMargin;
1747+
return Math.round((o.outerHeight() - (height - 1) * verticalMargin) / height);
17331748
}
1734-
var heightData = Utils.parseHeight(val);
17351749

1750+
// setter - updates the cellHeight value if they changed
1751+
var heightData = Utils.parseHeight(val);
17361752
if (this.opts.cellHeightUnit === heightData.unit && this.opts.cellHeight === heightData.height) {
17371753
return ;
17381754
}
@@ -1742,7 +1758,6 @@
17421758
if (!noUpdate) {
17431759
this._updateStyles();
17441760
}
1745-
17461761
};
17471762

17481763
GridStack.prototype.cellWidth = function() {

0 commit comments

Comments
 (0)