Skip to content

Commit cec6b98

Browse files
committed
removeNode() uses internal _id
1 parent c0f875b commit cec6b98

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

angular/projects/demo/src/app/app.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ export class AppComponent implements OnInit {
3535
];
3636
public gridOptions: GridStackOptions = {
3737
margin: 5,
38-
float: true,
38+
// float: true,
3939
minRow: 1,
4040
}
41+
private sub0: NgGridStackWidget[] = [{x:0, y:0, selector:'app-a'}, {x:1, y:0, content:'plain html'}, {x:0, y:1, selector:'app-b'} ];
4142
public gridOptionsFull: NgGridStackOptions = {
4243
...this.gridOptions,
43-
children: [{x:0, y:0, selector:'app-a'}, {x:1, y:0, selector:'app-b'}, {x:2, y:0, content:'plain html'}],
44+
children: this.sub0,
4445
}
4546

4647
// nested grid options
@@ -70,8 +71,8 @@ export class AppComponent implements OnInit {
7071

7172
constructor() {
7273
// give them content and unique id to make sure we track them during changes below...
73-
[...this.items, ...this.subChildren, ...this.sub1, ...this.sub2].forEach((w: NgGridStackWidget) => {
74-
if (!w.selector && !w.subGridOpts) w.content = `item ${ids}`;
74+
[...this.items, ...this.subChildren, ...this.sub1, ...this.sub2, ...this.sub0].forEach((w: NgGridStackWidget) => {
75+
if (!w.selector && !w.content && !w.subGridOpts) w.content = `item ${ids}`;
7576
w.id = String(ids++);
7677
});
7778
}

doc/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +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+
- [8.2.0-dev (TBD)](#820-dev-tbd)
89
- [8.2.0 (2023-05-24)](#820-2023-05-24)
910
- [8.1.2 (2023-05-22)](#812-2023-05-22)
1011
- [8.1.1 (2023-05-13)](#811-2023-05-13)
@@ -88,6 +89,9 @@ Change log
8889

8990
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
9091

92+
## 8.2.0-dev (TBD)
93+
* fix: make sure `removeNode()` uses internal _id (unique) and not node itself (since we clone those often)
94+
9195
## 8.2.0 (2023-05-24)
9296
* feat: `makeWidget()` now take optional `GridStackWidget` for sizing
9397
* fix: make sure `GridStack.saveCB` is call in `removeWidget()`

src/gridstack-engine.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,14 @@ export class GridStackEngine {
126126

127127
/** return the nodes that intercept the given node. Optionally a different area can be used, as well as a second node to skip */
128128
public collide(skip: GridStackNode, area = skip, skip2?: GridStackNode): GridStackNode {
129-
return this.nodes.find(n => n !== skip && n !== skip2 && Utils.isIntercepted(n, area));
129+
const skipId = skip._id;
130+
const skip2Id = skip2?._id;
131+
return this.nodes.find(n => n._id !== skipId && n._id !== skip2Id && Utils.isIntercepted(n, area));
130132
}
131133
public collideAll(skip: GridStackNode, area = skip, skip2?: GridStackNode): GridStackNode[] {
132-
return this.nodes.filter(n => n !== skip && n !== skip2 && Utils.isIntercepted(n, area));
134+
const skipId = skip._id;
135+
const skip2Id = skip2?._id;
136+
return this.nodes.filter(n => n._id !== skipId && n._id !== skip2Id && Utils.isIntercepted(n, area));
133137
}
134138

135139
/** does a pixel coverage collision based on where we started, returning the node that has the most coverage that is >50% mid line */
@@ -522,7 +526,7 @@ export class GridStackEngine {
522526
}
523527

524528
public removeNode(node: GridStackNode, removeDOM = true, triggerEvent = false): GridStackEngine {
525-
if (!this.nodes.find(n => n === node)) {
529+
if (!this.nodes.find(n => n._id === node._id)) {
526530
// TEST console.log(`Error: GridStackEngine.removeNode() node._id=${node._id} not found!`)
527531
return this;
528532
}
@@ -531,7 +535,7 @@ export class GridStackEngine {
531535
}
532536
if (removeDOM) node._removeDOM = true; // let CB remove actual HTML (used to set _id to null, but then we loose layout info)
533537
// don't use 'faster' .splice(findIndex(),1) in case node isn't in our list, or in multiple times.
534-
this.nodes = this.nodes.filter(n => n !== node);
538+
this.nodes = this.nodes.filter(n => n._id !== node._id);
535539
return this._packNodes()
536540
._notify([node]);
537541
}
@@ -564,7 +568,7 @@ export class GridStackEngine {
564568
column: this.column,
565569
float: this.float,
566570
nodes: this.nodes.map(n => {
567-
if (n === node) {
571+
if (n._id === node._id) {
568572
clonedNode = {...n};
569573
return clonedNode;
570574
}

0 commit comments

Comments
 (0)