Skip to content

Commit 45455ab

Browse files
authored
Merge pull request #1251 from adumesny/typescript
prevent removing another grid's item
2 parents 78599e6 + 1ce2c86 commit 45455ab

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Change log
3636
## 1.1.1-dev (upcoming)
3737

3838
- fix [1229](https://github.com/gridstack/gridstack.js/issues/1229) `staticGrid` no longer disable oneColumnMode
39+
- fix [1250](https://github.com/gridstack/gridstack.js/issues/1250) don't remove item from another grid
3940
- add `getGridItems()` to return list of HTML grid items
4041

4142
## 1.1.1 (2020-03-17)

spec/gridstack-spec.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ describe('gridstack', function() {
88
let gridHTML =
99
'<div class="grid-stack">' +
1010
' <div class="grid-stack-item" data-gs-x="0" data-gs-y="0" data-gs-width="4" data-gs-height="2" id="item1">' +
11-
' <div class="grid-stack-item-content"></div>' +
11+
' <div class="grid-stack-item-content">item 1</div>' +
1212
' </div>' +
1313
' <div class="grid-stack-item" data-gs-x="4" data-gs-y="0" data-gs-width="4" data-gs-height="4" id="item2">' +
14-
' <div class="grid-stack-item-content"></div>' +
14+
' <div class="grid-stack-item-content">item 2</div>' +
1515
' </div>' +
1616
'</div>';
1717
let gridstackHTML =
@@ -1386,6 +1386,42 @@ describe('gridstack', function() {
13861386
});
13871387
});
13881388

1389+
describe('two grids', function() {
1390+
beforeEach(function() {
1391+
document.body.insertAdjacentHTML('afterbegin', gridHTML);
1392+
document.body.insertAdjacentHTML('afterbegin', gridHTML);
1393+
});
1394+
afterEach(function() {
1395+
document.body.removeChild(document.getElementById('gs-cont'));
1396+
});
1397+
it('should not remove incorrect child', function() {
1398+
let grids = GridStack.initAll();
1399+
expect(grids.length).toBe(2);
1400+
expect(grids[0].engine.nodes.length).toBe(2);
1401+
expect(grids[1].engine.nodes.length).toBe(2);
1402+
// should do nothing
1403+
grids[0].removeWidget( grids[1].engine.nodes[0].el );
1404+
expect(grids[0].engine.nodes.length).toBe(2);
1405+
expect(grids[0].el.children.length).toBe(2);
1406+
expect(grids[1].engine.nodes.length).toBe(2);
1407+
expect(grids[1].el.children.length).toBe(2);
1408+
// should empty with no errors
1409+
grids[1].removeAll();
1410+
expect(grids[0].engine.nodes.length).toBe(2);
1411+
expect(grids[0].el.children.length).toBe(2);
1412+
expect(grids[1].engine.nodes.length).toBe(0);
1413+
expect(grids[1].el.children.length).toBe(0);
1414+
});
1415+
it('should remove 1 child', function() {
1416+
let grids = GridStack.initAll();
1417+
grids[1].removeWidget( grids[1].engine.nodes[0].el );
1418+
expect(grids[0].engine.nodes.length).toBe(2);
1419+
expect(grids[0].el.children.length).toBe(2);
1420+
expect(grids[1].engine.nodes.length).toBe(1);
1421+
expect(grids[1].el.children.length).toBe(1);
1422+
});
1423+
});
1424+
13891425
describe('ddPlugin option', function() {
13901426
beforeEach(function() {
13911427
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);

src/gridstack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ export class GridStack {
829829
*/
830830
public removeWidget(els: GridStackElement, detachNode?: boolean): GridStack {
831831
this.getElements(els).forEach(el => {
832+
if (el.parentElement !== this.el) return; // not our child!
832833
let node = el.gridstackNode;
833834
// For Meteor support: https://github.com/gridstack/gridstack.js/pull/272
834835
if (!node) {

0 commit comments

Comments
 (0)