Skip to content

Commit 7f6a7ab

Browse files
committed
fixed sidebar for nested grids
* fixed nested example to use latest code (remove a bunch) * fixed GS to support subGridOpts
1 parent b4d8def commit 7f6a7ab

File tree

3 files changed

+11
-29
lines changed

3 files changed

+11
-29
lines changed

demo/nested.html

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ <h1>Nested grids demo</h1>
2121
<a class="btn btn-primary" onClick="addNewWidget('.sub1')" href="#">Add Widget Grid1</a>
2222
<a class="btn btn-primary" onClick="addNewWidget('.sub2')" href="#">Add Widget Grid2</a>
2323
<a class="btn btn-primary" onClick="addNested()" href="#">Add Nested Grid</a>
24-
<div class="sidebar" style="height:50px; padding: 0">
25-
<!-- manually force a drop size of 2x2 for nested size -->
26-
<div class="grid-stack-item" gs-w="2" gs-h="2">
27-
<div class="grid-stack-item-content">Drag nested</div>
28-
</div>
29-
</div>
24+
<!-- add .grid-stack-item for acceptWidgets:true -->
25+
<div class="sidebar-item grid-stack-item">Drag nested</div>
3026
</div>
3127
<br />
3228
<span>entire save/re-create:</span>
@@ -78,7 +74,10 @@ <h1>Nested grids demo</h1>
7874
})
7975

8076
// setup drag drop behavior
81-
GridStack.setupDragIn('.sidebar .grid-stack-item', { appendTo: 'body', helper: 'clone' });
77+
let sidebarContent = [
78+
{ w:2, h:2, subGridOpts: { children: [{content: 'nest 1'}, {content: 'nest 2'}]}}
79+
];
80+
GridStack.setupDragIn('.sidebar-item', undefined, sidebarContent);
8281

8382
function addWidget() {
8483
grid.addWidget({x:0, y:100, content:"new item"});
@@ -104,25 +103,6 @@ <h1>Nested grids demo</h1>
104103
return false;
105104
};
106105

107-
// listener on drop event: every time the sidebar item is dropped create a new subgrid
108-
function droppedHandler(event, prevNode, n) {
109-
// if we don't have a prevNode that means it came from toolbar, which today is the only nested case (else check for some node.el.getAttribute or some custom field...)
110-
if (prevNode) return;
111-
112-
// clear the content then make it a subgrid
113-
n.el.querySelector('.grid-stack-item-content').innerHTML = '';
114-
let nodeToAdd = { children: [{content: 'nest 1'}, {content: 'nest 2'}]};
115-
let subgrid = n.grid.makeSubGrid(n.el, nodeToAdd, undefined, false);
116-
117-
// add a listener to the subgrid to allow widgets to be added into this newly created nested widget
118-
subgrid.on('dropped', droppedHandler);
119-
}
120-
// add listener to the main grid and subgrids
121-
grid.on('dropped', droppedHandler);
122-
document.querySelectorAll('.grid-stack-nested').forEach((subGrid) => {
123-
subGrid.gridstack.on('dropped', droppedHandler);
124-
});
125-
126106
//--- end of Drag and Drop Nested widget logic
127107

128108
function save(content = true, full = true) {
@@ -142,7 +122,6 @@ <h1>Nested grids demo</h1>
142122
function load(full = true) {
143123
if (full) {
144124
grid = GridStack.addGrid(document.querySelector('.container-fluid'), options);
145-
grid.on('dropped', droppedHandler);
146125
} else {
147126
grid.load(options);
148127
}

demo/two.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h1>Two grids demo</h1>
2323

2424
<div class="row">
2525
<div class="col-md-8">
26+
<!-- NOTE: add .grid-stack-item for acceptWidgets:true, but not needed for function which handles .sidebar-item -->
2627
<div class="sidebar">
2728
<!-- will size to match content. Also see sidebarContent -->
2829
<div class="sidebar-item">Drag me</div>

src/gridstack.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,7 @@ export class GridStack {
22492249
if (node?.grid === this && !node._isExternal) return false;
22502250

22512251
const wasAdded = !!this.placeholder.parentElement; // skip items not actually added to us because of constrains, but do cleanup #1419
2252-
const wasSidebar = node._isExternal;
2252+
const wasSidebar = el !== helper;
22532253
this.placeholder.remove();
22542254

22552255
// disable animation when replacing a placeholder (already positioned) with actual content
@@ -2296,13 +2296,15 @@ export class GridStack {
22962296
// give the user a chance to alter the widget that will get inserted if new sidebar item
22972297
if (wasSidebar) {
22982298
if (GridStack.addRemoveCB) el = GridStack.addRemoveCB(this.el, node, true, false);
2299-
else if (node.content) el = Utils.createWidgetDivs(undefined, node); // calls GridStack.renderCB()
2299+
else if (node.content || node.subGridOpts) el = Utils.createWidgetDivs(undefined, node); // calls GridStack.renderCB()
23002300
}
23012301
this._prepareElement(el, true, node);
23022302
this.el.appendChild(el);
23032303
if (subGrid) {
23042304
subGrid.parentGridItem = node;
23052305
if (!subGrid.opts.styleInHead) subGrid._updateStyles(true); // re-create sub-grid styles now that we've moved
2306+
} else if (wasSidebar && node.subGridOpts) {
2307+
this.makeSubGrid(node.el, undefined, undefined, !!node.content);
23062308
}
23072309
this._updateContainerHeight();
23082310
this.engine.addedNodes.push(node);

0 commit comments

Comments
 (0)