Skip to content

Commit 50d5803

Browse files
authored
Merge pull request #1689 from adumesny/master
addGrid() checks for nested '.grid-stack' div
2 parents 1b88198 + 2323c56 commit 50d5803

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Change log
5555
- fix [#1679](https://github.com/gridstack/gridstack.js/issues/1679) `Resizable: {handles:'w/sw'}` work again in 4.x
5656
- fix [#1658](https://github.com/gridstack/gridstack.js/issues/1658) `enableMove(T/F)` not working correctly
5757
- fix `helper: myFunction` now working for H5 case for `dragInOptions` & `setupDragIn()`
58+
- fix prevent `addGrid()` from creating nested div grid if container already is a '.grid-stack' div
59+
5860
## 4.0.1 (2021-3-20)
5961

6062
- fix [#1669](https://github.com/gridstack/gridstack.js/issues/1669) JQ resize broken

src/gridstack-dd.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ GridStack.prototype._setupAcceptWidget = function(): GridStack {
9494
if (!node) return;
9595

9696
helper = helper || el;
97-
// let left = event.pageX - gridPos.left;
98-
// let top = event.pageY - gridPos.top;
9997
let rec = helper.getBoundingClientRect();
10098
let left = rec.left - gridPos.left;
10199
let top = rec.top - gridPos.top;
@@ -532,7 +530,7 @@ GridStack.prototype._leave = function(node: GridStackNode, el: GridItemHTMLEleme
532530

533531
/** @internal called when item is being dragged/resized */
534532
GridStack.prototype._dragOrResize = function(el: GridItemHTMLElement, event: Event, ui: DDUIData, node: GridStackNode, cellWidth: number, cellHeight: number) {
535-
let p = {...node._orig};
533+
let p = {...node._orig}; // could be undefined (_isExternal) which is ok (drag only set x,y and w,h will default to node value)
536534
let resizing: boolean;
537535

538536
if (event.type === 'drag') {

src/gridstack.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,14 @@ export class GridStack {
160160
public static addGrid(parent: HTMLElement, opt: GridStackOptions = {}): GridStack {
161161
if (!parent) return null;
162162

163-
// create the grid element
164-
let doc = document.implementation.createHTMLDocument();
165-
doc.body.innerHTML = `<div class="grid-stack ${opt.class || ''}"></div>`;
166-
let el = doc.body.children[0] as HTMLElement;
167-
parent.appendChild(el);
163+
// create the grid element, but check if the passed 'parent' already has grid styling and should be used instead
164+
let el = parent;
165+
if (!parent.classList.contains('grid-stack')) {
166+
let doc = document.implementation.createHTMLDocument();
167+
doc.body.innerHTML = `<div class="grid-stack ${opt.class || ''}"></div>`;
168+
el = doc.body.children[0] as HTMLElement;
169+
parent.appendChild(el);
170+
}
168171

169172
// create grid class and load any children
170173
let grid = GridStack.init(opt, el);

0 commit comments

Comments
 (0)