Skip to content

Commit 744f707

Browse files
authored
Merge pull request #2131 from adumesny/master
addWidget() & load() fix for existing element
2 parents 6afb383 + c0693e2 commit 744f707

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ Change log
8080
## 7.1.1-dev (TBD)
8181
* fix [#939](https://github.com/gridstack/gridstack.js/issues/2039) 'prototype' undefined error for dd-gridstack.js
8282
* add [#939](https://github.com/gridstack/gridstack.js/issues/2105) disable/enable are methods now recursive by default
83-
* add better GridStackEventHandlerCallback spelled out types
83+
* add better `GridStackEventHandlerCallback` spelled out types
84+
* add We now have support for [Angular Component wrappers](https://github.com/gridstack/gridstack.js/tree/master/demo/angular/src/app) out of the box included in the build, with docs and demo! Need help to do that for React and Vue.
8485

8586
## 7.1.1 (2022-11-13)
8687
* fix [#939](https://github.com/gridstack/gridstack.js/issues/939) editable elements focus (regression in v6). Thank you [@Gezdy](https://github.com/Gezdy)

src/gridstack.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ export class GridStack {
237237
this.el = el; // exposed HTML element to the user
238238
opts = opts || {}; // handles null/undefined/0
239239

240+
if (!el.classList.contains('grid-stack')) {
241+
this.el.classList.add('grid-stack');
242+
}
243+
240244
// if row property exists, replace minRow and maxRow instead
241245
if (opts.row) {
242246
opts.minRow = opts.maxRow = opts.row;
@@ -408,27 +412,32 @@ export class GridStack {
408412
}
409413

410414
let el: HTMLElement;
415+
let node: GridStackNode;
411416
if (typeof els === 'string') {
412417
let doc = document.implementation.createHTMLDocument(''); // IE needs a param
413418
doc.body.innerHTML = els;
414419
el = doc.body.children[0] as HTMLElement;
415420
} else if (arguments.length === 0 || arguments.length === 1 && isGridStackWidget(els)) {
416-
let content = els ? (els as GridStackWidget).content || '' : '';
417-
options = els;
418-
let doc = document.implementation.createHTMLDocument(''); // IE needs a param
419-
doc.body.innerHTML = `<div class="grid-stack-item ${this.opts.itemClass || ''}"><div class="grid-stack-item-content">${content}</div></div>`;
420-
el = doc.body.children[0] as HTMLElement;
421+
node = options = els;
422+
if (node?.el) {
423+
el = node.el; // re-use element stored in the node
424+
} else {
425+
let content = options?.content || '';
426+
let doc = document.implementation.createHTMLDocument(''); // IE needs a param
427+
doc.body.innerHTML = `<div class="grid-stack-item ${this.opts.itemClass || ''}"><div class="grid-stack-item-content">${content}</div></div>`;
428+
el = doc.body.children[0] as HTMLElement;
429+
}
421430
} else {
422431
el = els as HTMLElement;
423432
}
424433

425434
// Tempting to initialize the passed in opt with default and valid values, but this break knockout demos
426-
// as the actual value are filled in when _prepareElement() calls el.getAttribute('gs-xyz) before adding the node.
435+
// as the actual value are filled in when _prepareElement() calls el.getAttribute('gs-xyz') before adding the node.
427436
// So make sure we load any DOM attributes that are not specified in passed in options (which override)
428437
let domAttr = this._readAttr(el);
429438
options = Utils.cloneDeep(options) || {}; // make a copy before we modify in case caller re-uses it
430439
Utils.defaults(options, domAttr);
431-
let node = this.engine.prepareNode(options);
440+
node = this.engine.prepareNode(options);
432441
this._writeAttr(el, options);
433442

434443
if (this._insertNotAppend) {
@@ -680,7 +689,7 @@ export class GridStack {
680689
if (typeof(addAndRemove) === 'function') {
681690
w = addAndRemove(this, w, true).gridstackNode;
682691
} else {
683-
w = this.addWidget(w).gridstackNode;
692+
w = (w.el ? this.addWidget(w.el, w) : this.addWidget(w)).gridstackNode;
684693
}
685694
}
686695
});
@@ -1375,10 +1384,8 @@ export class GridStack {
13751384

13761385
/** @internal */
13771386
protected _prepareElement(el: GridItemHTMLElement, triggerAddEvent = false, node?: GridStackNode): GridStack {
1378-
if (!node) {
1379-
el.classList.add(this.opts.itemClass);
1380-
node = this._readAttr(el);
1381-
}
1387+
el.classList.add(this.opts.itemClass);
1388+
node = node || this._readAttr(el);
13821389
el.gridstackNode = node;
13831390
node.el = el;
13841391
node.grid = this;

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export type GridStackElement = string | HTMLElement | GridItemHTMLElement;
6969
/** specific and general event handlers for the .on() method */
7070
export type GridStackEventHandler = (event: Event) => void;
7171
export type GridStackElementHandler = (event: Event, el: GridItemHTMLElement) => void;
72-
export type GridStackNodesHandler = (event: Event, node: GridStackNode[]) => void;
72+
export type GridStackNodesHandler = (event: Event, nodes: GridStackNode[]) => void;
7373
export type GridStackDroppedHandler = (event: Event, previousNode: GridStackNode, newNode: GridStackNode) => void;
7474
export type GridStackEventHandlerCallback = GridStackEventHandler | GridStackElementHandler | GridStackNodesHandler | GridStackDroppedHandler;
7575

0 commit comments

Comments
 (0)