Skip to content

Commit d9f94f2

Browse files
committed
prevent removing another grid's item
fix #1251
1 parent 6ea7ecb commit d9f94f2

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Change log
3737

3838
- fix [1229](https://github.com/gridstack/gridstack.js/issues/1229) `staticGrid` no longer disable oneColumnMode
3939
- fix [1195](https://github.com/gridstack/gridstack.js/issues/1195) options broken with ember hash helper - thanks [@btecu](https://github.com/btecu)
40+
- fix [1250](https://github.com/gridstack/gridstack.js/issues/1250) don't remove item from another grid
4041

4142
## 1.1.1 (2020-03-17)
4243

spec/gridstack-spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,42 @@ describe('gridstack', function() {
13291329
});
13301330
});
13311331

1332+
describe('two grids', function() {
1333+
beforeEach(function() {
1334+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
1335+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
1336+
});
1337+
afterEach(function() {
1338+
document.body.removeChild(document.getElementById('gs-cont'));
1339+
});
1340+
it('should not remove incorrect child', function() {
1341+
let grids = GridStack.initAll();
1342+
expect(grids.length).toBe(2);
1343+
expect(grids[0].engine.nodes.length).toBe(2);
1344+
expect(grids[1].engine.nodes.length).toBe(2);
1345+
// should do nothing
1346+
grids[0].removeWidget( grids[1].engine.nodes[0].el );
1347+
expect(grids[0].engine.nodes.length).toBe(2);
1348+
expect(grids[0].el.children.length).toBe(2);
1349+
expect(grids[1].engine.nodes.length).toBe(2);
1350+
expect(grids[1].el.children.length).toBe(2);
1351+
// should empty with no errors
1352+
grids[1].removeAll();
1353+
expect(grids[0].engine.nodes.length).toBe(2);
1354+
expect(grids[0].el.children.length).toBe(2);
1355+
expect(grids[1].engine.nodes.length).toBe(0);
1356+
expect(grids[1].el.children.length).toBe(0);
1357+
});
1358+
it('should remove 1 child', function() {
1359+
let grids = GridStack.initAll();
1360+
grids[1].removeWidget( grids[1].engine.nodes[0].el );
1361+
expect(grids[0].engine.nodes.length).toBe(2);
1362+
expect(grids[0].el.children.length).toBe(2);
1363+
expect(grids[1].engine.nodes.length).toBe(1);
1364+
expect(grids[1].el.children.length).toBe(1);
1365+
});
1366+
});
1367+
13321368
describe('grid.compact', function() {
13331369
beforeEach(function() {
13341370
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);

src/gridstack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,7 @@
15171517
if (!node) {
15181518
node = this.engine.getNodeDataByDOMEl(el.get(0));
15191519
}
1520+
if (!node || node.el.parentElement !== this.el) return; // not our child!
15201521
// remove our DOM data (circular link) and drag&drop permanently
15211522
el.removeData('_gridstack_node');
15221523
this.dd.draggable(el, 'destroy').resizable(el, 'destroy');

0 commit comments

Comments
 (0)