Skip to content

Commit b0440be

Browse files
committed
insert extra rows, insert below
fix #219 **fixing another 6 years old request** * we now automatically insert extra rows when dragging an item at the bottom below others to make it easier to insert below. * added _extraDragRow to grids driven by dragged item
1 parent 00a8214 commit b0440be

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Change log
5454
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
5555
## 4.0.3-dev
5656

57+
- fix [#219](https://github.com/gridstack/gridstack.js/issues/219) **fixing another 6 years old request** we now automatically insert extra rows
58+
when dragging an item at the bottom below others to make it easier to insert below.
5759
- fix [#1687](https://github.com/gridstack/gridstack.js/issues/1687) more fix for drag between 2 grids with `row / maxRow` broken in 4.x
5860

5961
## 4.0.3 (2021-3-28)

src/gridstack-dd.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
420420
}
421421
}
422422

423+
this._extraDragRow = 0;
423424
this._updateContainerHeight();
424425
this._triggerChangeEvent();
425426

@@ -537,6 +538,19 @@ GridStack.prototype._dragOrResize = function(el: GridItemHTMLElement, event: Eve
537538
let top = ui.position.top + (ui.position.top > node._lastUiPosition.top ? -this.opts.marginBottom : this.opts.marginTop);
538539
p.x = Math.round(left / cellWidth);
539540
p.y = Math.round(top / cellHeight);
541+
542+
// if we're at the bottom hitting something else, grow the grid so cursor doesn't leave when trying to place below others
543+
let prev = this._extraDragRow;
544+
if (this.engine.collide(node, p)) {
545+
let row = this.getRow();
546+
let extra = Math.max(0, (p.y + node.h) - row);
547+
if (this.opts.maxRow && row + extra > this.opts.maxRow) {
548+
extra = Math.max(0, this.opts.maxRow - row);
549+
}
550+
this._extraDragRow = extra;
551+
} else this._extraDragRow = 0;
552+
if (this._extraDragRow !== prev) this._updateContainerHeight();
553+
540554
if (node.x === p.x && node.y === p.y) return; // skip same
541555
// DON'T skip one we tried as we might have failed because of coverage <50% before
542556
// if (node._lastTried && node._lastTried.x === x && node._lastTried.y === y) return;
@@ -570,6 +584,7 @@ GridStack.prototype._dragOrResize = function(el: GridItemHTMLElement, event: Eve
570584
this.engine.cacheRects(cellWidth, cellHeight, this.opts.marginTop, this.opts.marginRight, this.opts.marginBottom, this.opts.marginLeft);
571585
delete node._skipDown;
572586
if (resizing && node.subGrid) { (node.subGrid as GridStack).onParentResize(); }
587+
this._extraDragRow = 0;
573588
this._updateContainerHeight();
574589

575590
let target = event.target as GridItemHTMLElement;

src/gridstack.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ export class GridStack {
228228
private _cellHeightThrottle: () => void;
229229
/** @internal true when loading items to insert first rather than append */
230230
private _insertNotAppend: boolean;
231+
/** @internal extra row added when dragging at the bottom of the grid */
232+
private _extraDragRow = 0;
231233

232234
/**
233235
* Construct a grid item from the given element and options
@@ -1200,7 +1202,7 @@ export class GridStack {
12001202
/** @internal */
12011203
private _updateContainerHeight(): GridStack {
12021204
if (!this.engine || this.engine.batchMode) return this;
1203-
let row = this.getRow(); // checks for minRow already
1205+
let row = this.getRow() + this._extraDragRow; // checks for minRow already
12041206
// check for css min height
12051207
let cssMinHeight = parseInt(getComputedStyle(this.el)['min-height']);
12061208
if (cssMinHeight > 0) {

0 commit comments

Comments
 (0)