Skip to content

Commit b875069

Browse files
authored
Merge pull request #1641 from adumesny/develop
collision: more fixes
2 parents a7199f5 + 25f880f commit b875069

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

demo/column.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ <h1>column() grid demo (fix cellHeight)</h1>
7272
{x: 0, y: 4, w: 12}
7373
];
7474
let count = 0;
75-
grid.batchUpdate();
76-
addWidget(); addWidget(); addWidget(); addWidget();
77-
grid.commit();
75+
items.forEach(n => n.content = '<button onClick="grid.removeWidget(this.parentNode.parentNode)">X</button><br>' + count++ + (n.text ? n.text : ''));
76+
grid.load(items);
7877

7978
function addWidget() {
8079
let n = items[count] || {

spec/e2e/html/141_1534_swap.html

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ <h1>Swap collision demo</h1>
1818
<a class="btn btn-primary" onclick="toggleFloat()" id="float" href="#"></a>
1919
<a class="btn btn-primary" onclick="toggleMax()" id="max" href="#"></a>
2020
<a class="btn btn-primary" onclick="toggleBigger()" id="big" href="#"></a>
21+
with layouts:
22+
<a class="btn btn-primary" onclick="load(0)" href="#">load 0</a>
23+
<a class="btn btn-primary" onclick="load(1)" href="#">load 1</a>
2124
</div>
2225
<br><br>
2326
<div class="grid-stack"></div>
@@ -28,31 +31,45 @@ <h1>Swap collision demo</h1>
2831
let maxButton = document.getElementById('max');
2932
let bigButton = document.getElementById('big');
3033
let size = 1;
34+
let layout = 0;
3135

3236
let grid = GridStack.init({float: false, cellHeight: 70, maxRow: 0});
3337
addEvents(grid);
3438

35-
let count = 0;
36-
let items = [
39+
let items = [[
3740
{x:0, y:0}, {x:0, y:1}, {x:0, y:2},{x:1, y:0}, {x:1, y:1}, {x:1, y:2, h:2, w:2},
3841
{x:5, y:0}, {x:4, y:1, w:3, locked: false, _content: 'locked'}, {x:5, y:2},
3942
{x:7, y:0}, {x:8, y:0}, {x:9, y:0}, {x:7, y:1, w:3}, {x:8, y:2},
4043
{x:11, y:0}, {x:11, y:1, h:2},
41-
];
42-
items.forEach(n => {n.id = count; n.content = n.content || String(count); count++})
43-
grid.load(items);
44+
], [
45+
{x: 1, y: 0},
46+
{x: 1, y: 1},
47+
{x: 1, y: 2, w: 2},
48+
{x: 0, y: 3, w: 3},
49+
{x: 1, y: 4, h: 2},
50+
]];
51+
items.forEach(layout => {
52+
let count = 0;
53+
layout.forEach(n => {n.id = count; n.content = n.content || String(count); count++})
54+
});
55+
grid.load(items[layout]);
4456

4557
addNewWidget = function() {
4658
let n = {
4759
x: Math.round(12 * Math.random()),
4860
y: Math.round(5 * Math.random()),
4961
w: Math.round(1 + 3 * Math.random()),
5062
h: Math.round(1 + 3 * Math.random()),
51-
content: String(count++)
63+
content: String(grid.engine.nodes.length+1)
5264
};
5365
grid.addWidget(n);
5466
};
5567

68+
load = function(i) {
69+
grid.removeAll();
70+
grid.load(items[i]);
71+
}
72+
5673
toggleFloat = function() {
5774
grid.float(! grid.getFloat());
5875
floatButton.innerHTML = 'float: ' + grid.getFloat();

src/gridstack-engine.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class GridStackEngine {
8383
}
8484

8585
let didMove = false;
86+
let yOffset = 0;
8687
let newOpt: GridStackMoveOpts = {nested: true, pack: false, sanitize: false};
8788
while (collide = collide || this.collide(node, nn)) { // could collide with more than 1 item... so repeat for each
8889
let moved: boolean;
@@ -103,8 +104,9 @@ export class GridStackEngine {
103104
}
104105
didMove = didMove || moved;
105106
} else {
106-
// move collide down *after* us
107-
moved = this.moveNode(collide, {...collide, y: nn.y + nn.h, ...newOpt});
107+
// move collide down *after* where we will be
108+
moved = this.moveNode(collide, {...collide, y: nn.y + nn.h + yOffset, ...newOpt});
109+
if (moved) yOffset += collide.h; // during while loop put next one after this one
108110
}
109111
if (!moved) { return didMove; } // break inf loop if we couldn't move after all (ex: maxRow, fixed)
110112
collide = undefined;
@@ -192,8 +194,9 @@ export class GridStackEngine {
192194
return true;
193195
}
194196

195-
// same size and same row or column
196-
if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y) /*&& Utils.isIntercepted(b, {x: a.x-0.5, y:a.y-0.5, w: a.w+1, h: a.h+1})*/)
197+
// same size and same row/column and touching
198+
if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y)
199+
&& Utils.isIntercepted(b, {x: a.x-0.5, y:a.y-0.5, w: a.w+1, h: a.h+1}))
197200
return _doSwap();
198201
/* different X will be weird (expect vertical swap) and different height overlap, so too complex. user regular layout instead
199202
// else check if swapping would not collide with anything else (requiring a re-layout)

0 commit comments

Comments
 (0)