Skip to content

Commit 37523cf

Browse files
authored
Merge pull request #1663 from adumesny/develop
collision: more edge case fixes
2 parents e82d4de + 7aa2d4e commit 37523cf

27 files changed

+141
-214
lines changed

LICENSE

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
The MIT License (MIT)
1+
v4 Copyright (c) 2021 Alain Dumesny
2+
under MIT license
23

4+
previous v3.x and older is:
35
Copyright (c) 2014-2020 Alain Dumesny, Dylan Weiss, Pavel Reznikov
46

7+
The MIT License (MIT)
8+
59
Permission is hereby granted, free of charge, to any person obtaining a copy
610
of this software and associated documentation files (the "Software"), to deal
711
in the Software without restriction, including without limitation the rights

demo/two.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ <h1>Two grids demo</h1>
9595
let grids = GridStack.initAll(options);
9696
grids[1].float(true);
9797

98-
// new 4.x static method instead of setting up options on every grid (never been per grid really but old options still work)
98+
// new 4.x static method instead of setting up options on every grid (never been per grid really) but old options still works
9999
GridStack.setupDragIn('.sidebar .grid-stack-item', { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' });
100-
// GridStack.setupDragIn(); // will now work as well (cache last values)
100+
// GridStack.setupDragIn(); // second call will now work (cache last values)
101101

102102
let items = [
103103
{x: 0, y: 0, w: 2, h: 2},

doc/CHANGES.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Change log
55
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
66
**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*
77

8-
- [3.3.0-dev](#330-dev)
8+
- [4.0.0](#400)
99
- [3.3.0 (2021-2-2)](#330-2021-2-2)
1010
- [3.2.0 (2021-1-25)](#320-2021-1-25)
1111
- [3.1.5 (2021-1-23)](#315-2021-1-23)
@@ -48,9 +48,9 @@ Change log
4848
- [v0.1.0 (2014-11-18)](#v010-2014-11-18)
4949

5050
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
51-
## 3.3.0-dev
51+
## 4.0.0
5252

53-
- fix [#149](https://github.com/gridstack/gridstack.js/issues/149) [#1094](https://github.com/gridstack/gridstack.js/issues/1094) [#1605](https://github.com/gridstack/gridstack.js/issues/1605) re-write of the **collision code - fixing 6 years old most requested request**
53+
- fix [#149](https://github.com/gridstack/gridstack.js/issues/149) [#1094](https://github.com/gridstack/gridstack.js/issues/1094) [#1605](https://github.com/gridstack/gridstack.js/issues/1605) [#1534](https://github.com/gridstack/gridstack.js/issues/1534) re-write of the **collision code - fixing 6 years old most requested request**
5454
1. you can now swap items of the same size (vertical/horizontal) when grid is full, and is the default in `float:false` (top gravity) as it feels more natural. Could add Alt key for swap vs push behavior later.
5555
2. Dragging up and down now behave the same (used to require push WAY down past to swap/append). Also much more efficient collision code.
5656
3. handle mid point of dragged over items (>50%) rather than just a new row/column and check for the most covered item when multiple collide.
@@ -59,6 +59,7 @@ Change log
5959
1. we now remove item when cursor leaves (`acceptWidgets` case using `dropout` event) or shape is outside (re-using same method) and re-insert on cursor enter (since we only get `dropover` event). Should **not be possible to have 2 placeholders** which confuses the grids.
6060
2. major re-write and cleanup of the drag in/out. Vars have been renamed and fully documented as I couldn't understand the legacy buggy code.
6161
3. removed any over trash delay feedback as I don't see the point and could introduce race conditions.
62+
6263
- fix [1617](https://github.com/gridstack/gridstack.js/issues/1617) FireFox DOM order issue. Thanks [@marcel-necker](https://github.com/marcel-necker)
6364
- fix changing column # `column(n)` now resizes `cellHeight:'auto'` to keep square
6465
- add [1616](https://github.com/gridstack/gridstack.js/pull/1616) `drag | resize` events while dragging. Thanks [@MrCorba](https://github.com/MrCorba)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gridstack",
3-
"version": "3.3.0-dev",
3+
"version": "4.0.0",
44
"description": "TypeScript/JS lib for dashboard layout and creation, no external dependencies, with many wrappers (React, Angular, Vue, Ember, knockout...)",
55
"main": "./dist/gridstack.js",
66
"types": "./dist/gridstack.d.ts",

spec/e2e/html/141_1534_swap.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<body>
1414
<div class="container-fluid">
1515
<h1>Swap collision demo</h1>
16+
<p>for bugs 141 <b>1094</b> 1534 1605</p>
1617
<div>
1718
<a class="btn btn-primary" onClick="addNewWidget()" href="#">Add Widget</a>
1819
<a class="btn btn-primary" onclick="toggleFloat()" id="float" href="#"></a>
@@ -21,8 +22,9 @@ <h1>Swap collision demo</h1>
2122
with layouts:
2223
<a class="btn btn-primary" onclick="load(0)" href="#">load 0</a>
2324
<a class="btn btn-primary" onclick="load(1)" href="#">load 1</a>
24-
<a class="btn btn-primary" onclick="load(2)" href="#">load web1</a>
25-
<a class="btn btn-primary" onclick="loadAdvanced()" href="#">load web2</a>
25+
<a class="btn btn-primary" onclick="load(2)" href="#">load 2</a>
26+
<a class="btn btn-primary" onclick="load(3)" href="#">load web1</a>
27+
<a class="btn btn-primary" onclick="load(3); loadAdvanced()" href="#">load web2</a>
2628
</div>
2729
<br><br>
2830
<div class="grid-stack"></div>
@@ -38,17 +40,19 @@ <h1>Swap collision demo</h1>
3840
let grid = GridStack.init({float: false, cellHeight: 70, maxRow: 0});
3941
addEvents(grid);
4042

41-
let items = [[
43+
let items = [[ // load 0
4244
{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*/},
4345
{x:5, y:0}, {x:4, y:1, w:3, locked: false, _content: 'locked'}, {x:5, y:2},
4446
{x:7, y:0}, {x:8, y:0}, {x:9, y:0}, {x:7, y:1, w:3}, {x:8, y:2},
4547
{x:11, y:0}, {x:11, y:1, h:2},
46-
], [
47-
{x:1, y:0}, {x:1, y:1}, {x:1, y:2, w:2}, {x:0, y:3, w:3}, {x:1, y:4, h:2},
48+
], [ // load 1
49+
{x:1, y:0}, {x:1, y:1, w:2}, {x:1, y:2, h:2}, {x:0, y:4, w:3},
4850
{x:3, y:0, w:3, h:3}, {x:6, y:0},
4951
{x:7, y:0, w:3}, {x:8, y:1, w:3},
50-
],[
51-
// web1.html demo
52+
], [ // load 2
53+
{x:1, y:0, w:2}, {x:1, y:1, h:2}, {x:2, y:1}, {x:0, y:3, w:3} , {x:0, y:4},
54+
// {x:5, y:0, w:2}, {x:6, y:1}, {x:4, y:2, w:3} , {x:5, y:4, h:2}, {x:4, y:4},
55+
],[ // web1.html demo
5256
{x:0, y:0, w:4, h:2},
5357
{x:4, y:0, w:4, h:4, id: 'big'},
5458
{x:8, y:0, w:2, h:2},
@@ -84,7 +88,6 @@ <h1>Swap collision demo</h1>
8488
grid.load(items[i]);
8589
}
8690
loadAdvanced = function() {
87-
load(2);
8891
grid.update(grid.engine.nodes[1].el, {locked: true, content:'locked'});
8992
}
9093
toggleFloat = function() {

src/gridstack-dd.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
// gridstack-GridStackDD.get().ts 3.3.0-dev @preserve
2-
3-
/**
4-
* https://gridstackjs.com/
5-
* (c) 2014-2020 Alain Dumesny, Dylan Weiss, Pavel Reznikov
6-
* gridstack.js may be freely distributed under the MIT license.
7-
*/
1+
// gridstack-GridStackDD.get().ts 4.0.0
2+
// (c) 2021 Alain Dumesny - see root license
83
/* eslint-disable @typescript-eslint/no-unused-vars */
94
import { GridStackDDI } from './gridstack-ddi';
105
import { GridItemHTMLElement, GridStackNode, GridStackElement, DDUIData, DDDragInOpt, GridStackPosition } from './types';
@@ -432,8 +427,7 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
432427
} else {
433428
// got removed - restore item back to before dragging position
434429
Utils.removePositioningStyles(target);
435-
Utils.copyPos(node, node._beforeDrag);
436-
delete node._beforeDrag;
430+
Utils.copyPos(node, node._orig);
437431
this._writePosAttr(target, node);
438432
this.engine.addNode(node);
439433
}
@@ -483,14 +477,14 @@ GridStack.prototype._onStartMoving = function(event: Event, ui: DDUIData, node:
483477
node.el = this.placeholder;
484478
node._lastUiPosition = ui.position;
485479
node._prevYPix = ui.position.top;
486-
node._moving = (event.type === 'dragstart');
480+
node._moving = (event.type === 'dragstart'); // 'dropover' are not initially moving so they can go exactly where they enter (will push stuff out of the way)
487481
delete node._lastTried;
488482
delete node._isCursorOutside;
489483

490484
if (event.type === 'dropover' && node._temporaryRemoved) {
491485
// TEST console.log('engine.addNode x=' + node.x);
492-
this.engine.addNode(node); // will add, constrain, update attr and clear _temporaryRemoved
493-
node._moving = true; // lastly mark as moving object
486+
this.engine.addNode(node); // will add, fix collisions, update attr and clear _temporaryRemoved
487+
node._moving = true; // AFTER, mark as moving object (wanted fix location before)
494488
}
495489

496490
// set the min/max resize info
@@ -533,6 +527,8 @@ GridStack.prototype._leave = function(node: GridStackNode, el: GridItemHTMLEleme
533527
// item came from outside (like a toolbar) so nuke any node info
534528
delete node.el;
535529
delete el.gridstackNode;
530+
// and restore all nodes back to original
531+
this.engine.restoreInitial();
536532
}
537533
}
538534

src/gridstack-ddi.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
// gridstack-ddi.ts 3.3.0-dev @preserve
2-
3-
/**
4-
* https://gridstackjs.com/
5-
* (c) 2014-2020 Alain Dumesny, Dylan Weiss, Pavel Reznikov
6-
* gridstack.js may be freely distributed under the MIT license.
7-
*/
8-
/* eslint-disable @typescript-eslint/no-unused-vars */
9-
1+
// gridstack-ddi.ts 4.0.0
2+
// (c) 2021 Alain Dumesny - see root license
103
import { GridItemHTMLElement } from './types';
114

125
/**

0 commit comments

Comments
 (0)