Skip to content

Commit b9c8cec

Browse files
authored
Merge pull request #2083 from adumesny/master
fix removeWidget() after it's gone from DOM
2 parents 00d089c + 6c0b236 commit b9c8cec

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

demo/serialization.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,45 @@ <h1>Serialization demo</h1>
4444
{x: 1, y: 3, id: '4'}
4545
];
4646
serializedData.forEach((n, i) =>
47-
n.content = `<button onClick="grid.removeWidget(this.parentNode.parentNode)">X</button><br> ${i}<br> ${n.content ? n.content : ''}`);
47+
n.content = `<button onClick="removeWidget(this.parentElement.parentElement)">X</button><br> ${i}<br> ${n.content ? n.content : ''}`);
4848
let serializedFull;
4949

5050
// 2.x method - just saving list of widgets with content (default)
51-
loadGrid = function() {
51+
function loadGrid() {
5252
grid.load(serializedData, true); // update things
5353
}
5454

5555
// 2.x method
56-
saveGrid = function() {
56+
function saveGrid() {
5757
delete serializedFull;
5858
serializedData = grid.save();
5959
document.querySelector('#saved-data').value = JSON.stringify(serializedData, null, ' ');
6060
}
6161

6262
// 3.1 full method saving the grid options + children (which is recursive for nested grids)
63-
saveFullGrid = function() {
63+
function saveFullGrid() {
6464
serializedFull = grid.save(true, true);
6565
serializedData = serializedFull.children;
6666
document.querySelector('#saved-data').value = JSON.stringify(serializedFull, null, ' ');
6767
}
6868

6969
// 3.1 full method to reload from scratch - delete the grid and add it back from JSON
70-
loadFullGrid = function() {
70+
function loadFullGrid() {
7171
if (!serializedFull) return;
7272
grid.destroy(true); // nuke everything
7373
grid = GridStack.addGrid(document.querySelector('#gridCont'), serializedFull)
7474
}
7575

76-
clearGrid = function() {
76+
function clearGrid() {
7777
grid.removeAll();
7878
}
7979

80+
function removeWidget(el) {
81+
// TEST removing from DOM first like Angular/React/Vue would do
82+
el.remove();
83+
grid.removeWidget(el, false);
84+
}
85+
8086
loadGrid();
8187
</script>
8288
</body>

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+
- [7.0.2 (TBD)](#702-tbd)
89
- [7.0.1 (2022-10-14)](#701-2022-10-14)
910
- [7.0.0 (2022-10-09)](#700-2022-10-09)
1011
- [6.0.3 (2022-10-08)](#603-2022-10-08)
@@ -74,6 +75,9 @@ Change log
7475

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

78+
## 7.0.2 (TBD)
79+
* fixed [#2081](https://github.com/gridstack/gridstack.js/issues/2081) removeWidget() after it's gone from DOM
80+
7781
## 7.0.1 (2022-10-14)
7882
* fixed [#2073](https://github.com/gridstack/gridstack.js/issues/2073) SSR (server side rendering) isTouch issue (introduced in v6)
7983
* fixed - removing last item delete sub-grid that are not auto-generated (nested.html vs nested_advanced.html)

src/dd-touch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { DDManager } from './dd-manager';
1010
* should we use this instead ? (what we had for always showing resize handles)
1111
* /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
1212
*/
13-
export const isTouch: boolean = typeof window !== 'undefined' && typeof document !== 'undefined' &&
13+
export const isTouch: boolean = typeof window !== 'undefined' && typeof document !== 'undefined' &&
1414
( 'ontouchstart' in document
1515
|| 'ontouchstart' in window
1616
// || !!window.TouchEvent // true on Windows 10 Chrome desktop so don't use this

src/gridstack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ export class GridStack {
10101010
*/
10111011
public removeWidget(els: GridStackElement, removeDOM = true, triggerEvent = true): GridStack {
10121012
GridStack.getElements(els).forEach(el => {
1013-
if (el.parentElement !== this.el) return; // not our child!
1013+
if (el.parentElement && el.parentElement !== this.el) return; // not our child!
10141014
let node = el.gridstackNode;
10151015
// For Meteor support: https://github.com/gridstack/gridstack.js/pull/272
10161016
if (!node) {

0 commit comments

Comments
 (0)