Skip to content

Commit e44eb1d

Browse files
author
Alain Dumesny
authored
Merge pull request #2342 from adumesny/master
addWidget() addRemoveCB checks if already initialize widget
2 parents da52bd1 + 87a454a commit e44eb1d

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Change log
9191

9292
## 8.2.0-dev (TBD)
9393
* fix: make sure `removeNode()` uses internal _id (unique) and not node itself (since we clone those often)
94+
* fix: after calling `addRemoveCB` make sure we don't makeWidget() (incorrectly) a second time
9495

9596
## 8.2.0 (2023-05-24)
9697
* feat: `makeWidget()` now take optional `GridStackWidget` for sizing

src/gridstack.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ export class GridStack {
410410
return w.el !== undefined || w.x !== undefined || w.y !== undefined || w.w !== undefined || w.h !== undefined || w.content !== undefined ? true : false;
411411
}
412412

413-
let el: HTMLElement;
413+
let el: GridItemHTMLElement;
414414
let node: GridStackNode;
415415
if (typeof els === 'string') {
416416
let doc = document.implementation.createHTMLDocument(''); // IE needs a param
@@ -434,6 +434,10 @@ export class GridStack {
434434

435435
if (!el) return;
436436

437+
// if the caller ended up initializing the widget in addRemoveCB, or we stared with one already, skip the rest
438+
node = el.gridstackNode;
439+
if (node && el.parentElement === this.el && this.engine.nodes.find(n => n._id === node._id)) return el;
440+
437441
// Tempting to initialize the passed in opt with default and valid values, but this break knockout demos
438442
// as the actual value are filled in when _prepareElement() calls el.getAttribute('gs-xyz') before adding the node.
439443
// So make sure we load any DOM attributes that are not specified in passed in options (which override)
@@ -449,23 +453,7 @@ export class GridStack {
449453
this.el.appendChild(el);
450454
}
451455

452-
// similar to makeWidget() that doesn't read attr again and worse re-create a new node and loose any _id
453-
this._prepareElement(el, true, options);
454-
this._updateContainerHeight();
455-
456-
// see if there is a sub-grid to create
457-
if (node.subGridOpts) {
458-
this.makeSubGrid(node.el, node.subGridOpts, undefined, false); // node.subGrid will be used as option in method, no need to pass
459-
}
460-
461-
// if we're adding an item into 1 column (_prevColumn is set only when going to 1) make sure
462-
// we don't override the larger 12 column layout that was already saved. #1985
463-
if (this._prevColumn && this.opts.column === 1) {
464-
this._ignoreLayoutsNodeChange = true;
465-
}
466-
this._triggerAddEvent();
467-
this._triggerChangeEvent();
468-
delete this._ignoreLayoutsNodeChange;
456+
this.makeWidget(el, options);
469457

470458
return el;
471459
}
@@ -982,8 +970,22 @@ export class GridStack {
982970
let el = GridStack.getElement(els);
983971
this._prepareElement(el, true, options);
984972
this._updateContainerHeight();
973+
974+
// see if there is a sub-grid to create
975+
const node = el.gridstackNode;
976+
if (node.subGridOpts) {
977+
this.makeSubGrid(el, node.subGridOpts, undefined, false); // node.subGrid will be used as option in method, no need to pass
978+
}
979+
980+
// if we're adding an item into 1 column (_prevColumn is set only when going to 1) make sure
981+
// we don't override the larger 12 column layout that was already saved. #1985
982+
if (this._prevColumn && this.opts.column === 1) {
983+
this._ignoreLayoutsNodeChange = true;
984+
}
985985
this._triggerAddEvent();
986986
this._triggerChangeEvent();
987+
delete this._ignoreLayoutsNodeChange;
988+
987989
return el;
988990
}
989991

0 commit comments

Comments
 (0)