Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf51a19

Browse files
authored
Merge pull request gridstack#2629 from adumesny/master
removeAll() trigger Angular's ngOnDestroy
2 parents bc9c436 + 7a7db44 commit bf51a19

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export class AppComponent implements OnInit {
203203
}
204204
public clearGrid() {
205205
if (!this.gridComp) return;
206-
this.gridComp.grid?.removeAll(true);
206+
this.gridComp.grid?.removeAll();
207207
}
208208
public saveGrid() {
209209
this.serializedData = this.gridComp?.grid?.save(false, true) as GridStackOptions || ''; // no content, full options

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ import { BaseWidget, NgCompInputs } from 'gridstack/dist/angular';
1515
export class AComponent extends BaseWidget implements OnDestroy {
1616
@Input() text: string = 'foo'; // test custom input data
1717
public override serialize(): NgCompInputs | undefined { return this.text ? {text: this.text} : undefined; }
18-
ngOnDestroy() {
19-
console.log('Comp A destroyed'); // test to make sure cleanup happens
20-
}
18+
ngOnDestroy() { console.log('Comp A destroyed'); } // test to make sure cleanup happens
2119
}
2220

2321
@Component({
2422
selector: 'app-b',
2523
template: 'Comp B',
2624
})
27-
export class BComponent extends BaseWidget {
25+
export class BComponent extends BaseWidget implements OnDestroy {
26+
ngOnDestroy() { console.log('Comp B destroyed'); }
2827
}
2928

3029
@Component({

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+
- [10.1.1-dev (TBD)](#1011-dev-tbd)
89
- [10.1.1 (2024-03-03)](#1011-2024-03-03)
910
- [10.1.0 (2024-02-04)](#1010-2024-02-04)
1011
- [10.0.1 (2023-12-10)](#1001-2023-12-10)
@@ -107,6 +108,9 @@ Change log
107108
- [v0.1.0 (2014-11-18)](#v010-2014-11-18)
108109

109110
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
111+
## 10.1.1-dev (TBD)
112+
* fix: [#2628](https://github.com/gridstack/gridstack.js/issues/2628) `removeAll()` does not trigger Angular's ngOnDestroy
113+
110114
## 10.1.1 (2024-03-03)
111115
* fix: [#2620](https://github.com/gridstack/gridstack.js/pull/2620) allow resizing with sizeToContent:NUMBER is uses
112116

src/gridstack-engine.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,14 @@ export class GridStackEngine {
554554
return this;
555555
}
556556

557-
public removeAll(removeDOM = true): GridStackEngine {
557+
public removeAll(removeDOM = true, triggerEvent = true): GridStackEngine {
558558
delete this._layouts;
559559
if (!this.nodes.length) return this;
560560
removeDOM && this.nodes.forEach(n => n._removeDOM = true); // let CB remove actual HTML (used to set _id to null, but then we loose layout info)
561-
this.removedNodes = this.nodes;
561+
const removedNodes = this.nodes;
562+
this.removedNodes = triggerEvent ? removedNodes : [];
562563
this.nodes = [];
563-
return this._notify(this.removedNodes);
564+
return this._notify(removedNodes);
564565
}
565566

566567
/** checks if item can be moved (layout constrain) vs moveNode(), returning true if was able to move.

src/gridstack.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ export class GridStack {
12011201
}
12021202
if (!node) return;
12031203

1204-
if (GridStack.addRemoveCB) {
1204+
if (removeDOM && GridStack.addRemoveCB) {
12051205
GridStack.addRemoveCB(this.el, node, false, false);
12061206
}
12071207

@@ -1225,15 +1225,19 @@ export class GridStack {
12251225
/**
12261226
* Removes all widgets from the grid.
12271227
* @param removeDOM if `false` DOM elements won't be removed from the tree (Default? `true`).
1228+
* @param triggerEvent if `false` (quiet mode) element will not be added to removed list and no 'removed' callbacks will be called (Default? true).
12281229
*/
1229-
public removeAll(removeDOM = true): GridStack {
1230+
public removeAll(removeDOM = true, triggerEvent = true): GridStack {
12301231
// always remove our DOM data (circular link) before list gets emptied and drag&drop permanently
12311232
this.engine.nodes.forEach(n => {
1233+
if (removeDOM && GridStack.addRemoveCB) {
1234+
GridStack.addRemoveCB(this.el, n, false, false);
1235+
}
12321236
delete n.el.gridstackNode;
12331237
this._removeDD(n.el);
12341238
});
1235-
this.engine.removeAll(removeDOM);
1236-
this._triggerRemoveEvent();
1239+
this.engine.removeAll(removeDOM, triggerEvent);
1240+
if (triggerEvent) this._triggerRemoveEvent();
12371241
return this;
12381242
}
12391243

0 commit comments

Comments
 (0)