diff --git a/doc/CHANGES.md b/doc/CHANGES.md index aef8ae0ff..5af045a40 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -36,6 +36,7 @@ Change log ## 1.1.1-dev (upcoming) - fix [1229](https://github.com/gridstack/gridstack.js/issues/1229) `staticGrid` no longer disable oneColumnMode +- fix [1250](https://github.com/gridstack/gridstack.js/issues/1250) don't remove item from another grid - add `getGridItems()` to return list of HTML grid items ## 1.1.1 (2020-03-17) diff --git a/spec/gridstack-spec.ts b/spec/gridstack-spec.ts index 3f647439d..cccfe77e4 100644 --- a/spec/gridstack-spec.ts +++ b/spec/gridstack-spec.ts @@ -8,10 +8,10 @@ describe('gridstack', function() { let gridHTML = '
' + '
' + - '
' + + '
item 1
' + '
' + '
' + - '
' + + '
item 2
' + '
' + '
'; let gridstackHTML = @@ -1386,6 +1386,42 @@ describe('gridstack', function() { }); }); + describe('two grids', function() { + beforeEach(function() { + document.body.insertAdjacentHTML('afterbegin', gridHTML); + document.body.insertAdjacentHTML('afterbegin', gridHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementById('gs-cont')); + }); + it('should not remove incorrect child', function() { + let grids = GridStack.initAll(); + expect(grids.length).toBe(2); + expect(grids[0].engine.nodes.length).toBe(2); + expect(grids[1].engine.nodes.length).toBe(2); + // should do nothing + grids[0].removeWidget( grids[1].engine.nodes[0].el ); + expect(grids[0].engine.nodes.length).toBe(2); + expect(grids[0].el.children.length).toBe(2); + expect(grids[1].engine.nodes.length).toBe(2); + expect(grids[1].el.children.length).toBe(2); + // should empty with no errors + grids[1].removeAll(); + expect(grids[0].engine.nodes.length).toBe(2); + expect(grids[0].el.children.length).toBe(2); + expect(grids[1].engine.nodes.length).toBe(0); + expect(grids[1].el.children.length).toBe(0); + }); + it('should remove 1 child', function() { + let grids = GridStack.initAll(); + grids[1].removeWidget( grids[1].engine.nodes[0].el ); + expect(grids[0].engine.nodes.length).toBe(2); + expect(grids[0].el.children.length).toBe(2); + expect(grids[1].engine.nodes.length).toBe(1); + expect(grids[1].el.children.length).toBe(1); + }); + }); + describe('ddPlugin option', function() { beforeEach(function() { document.body.insertAdjacentHTML('afterbegin', gridstackHTML); diff --git a/src/gridstack.ts b/src/gridstack.ts index 72c5b240f..bebad1afd 100644 --- a/src/gridstack.ts +++ b/src/gridstack.ts @@ -829,6 +829,7 @@ export class GridStack { */ public removeWidget(els: GridStackElement, detachNode?: boolean): GridStack { this.getElements(els).forEach(el => { + if (el.parentElement !== this.el) return; // not our child! let node = el.gridstackNode; // For Meteor support: https://github.com/gridstack/gridstack.js/pull/272 if (!node) {