Skip to content

Commit e17a4f6

Browse files
committed
loding nested grids small then growing
* fix #1538 * we now call resize on nested grids when window changes size * we also call nested grid of the currently being resized item as it happens (live) * updated nested.html to show more complete case, with custom column and different oneColumn constrain
1 parent c647501 commit e17a4f6

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

demo/nested.html

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
<title>Nested grids demo (ES6)</title>
88
<link rel="stylesheet" href="demo.css"/>
9+
<link rel="stylesheet" href="../dist/gridstack-extra.min.css"/>
910
<script src="../dist/gridstack-jq.js"></script>
1011
<style type="text/css">
1112
.grid-stack .grid-stack {
@@ -20,32 +21,34 @@
2021
<div class="container-fluid">
2122
<h1>Nested grids demo</h1>
2223
<p>This example uses new v3.1 API to load the entire nested grid from JSON, and shows dragging between nested grid items (pink) vs dragging higher grid items (green)</p>
23-
<p>Note: initial v3.0.0 HTML5 release doesn't support 'dragOut:false' constrain so this uses JQ version for now, otherwise recommend you use h5 version.</p>
24+
<p>Note: initial v3 HTML5 release doesn't support 'dragOut:false' constrain so this uses JQ version for now, otherwise recommend you use h5 version.</p>
2425
<a class="btn btn-primary" onClick="addNewWidget('.nested1')" href="#">Add Widget Grid1</a>
2526
<a class="btn btn-primary" onClick="addNewWidget('.nested2')" href="#">Add Widget Grid2</a>
2627
<br><br>
2728
<!-- grid will be added here -->
2829
</div>
2930

3031
<script type="text/javascript">
31-
let sub1 = [ {w:3}, {w:3}, {w:3}, {w:3}, {w:3}, {w:3}];
32-
let sub2 = [ {w:3}, {x:0, y:1, w:3}];
32+
let sub1 = [ {x:0, y:0}, {x:1, y:0}, {x:2, y:0}, {x:3, y:0}, {x:0, y:1}, {x:1, y:1}];
33+
let sub2 = [ {x:0, y:0}, {x:0, y:1, w:2}];
3334
let count = 0;
3435
[...sub1, ...sub2].forEach(d => d.content = String(count++));
3536
let subOptions = {
37+
cellHeight: 30,
38+
column: 4, // make sure to include gridstack-extra.min.css
3639
itemClass: 'sub', // style sub items differently and use to prevent dragging in/out
3740
acceptWidgets: '.grid-stack-item.sub', // only pink sub items can be inserted, otherwise grid-items causes all sort of issues
38-
disableOneColumnMode: true, // nested are small, but still want N columns
41+
minWidth: 300, // min to go 1 column mode
3942
margin: 1
4043
};
41-
let layout = {cellHeight: 70, children: [
42-
{w:1, content: 'regular item'},
44+
let opts = {cellHeight: 70, children: [
45+
{y:0, content: 'regular item'},
4346
{x:1, w:4, h:4, content: 'nested 1 - can drag items out', subGrid: {children: sub1, dragOut: true, class: 'nested1', ...subOptions}},
4447
{x:5, w:4, h:4, content: 'nested 2 - constrained to parent (default)', subGrid: {children: sub2, class: 'nested2', ...subOptions}},
4548
]};
4649

4750
// create and load it all from JSON above
48-
GridStack.addGrid(document.querySelector('.container-fluid'), layout);
51+
GridStack.addGrid(document.querySelector('.container-fluid'), opts);
4952

5053
addNewWidget = function(selector) {
5154
grid = document.querySelector(selector).gridstack;

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Change log
5454
- new `cellHeight:initial` which makes the cell squares initially, but doesn't change as windows resizes (better performance)
5555
- new grid option `cellHeightThrottle` (100ms) to control throttle of auto sizing triggers
5656
- fix [1600](https://github.com/gridstack/gridstack.js/issues/1600) height too small with `cellHeight:auto` loading in 1 column. Now detect we load at 1 column and size accordingly (default 'auto' could make big 700x700 cells, so explicit px might still be wanted)
57+
- fix [1538](https://github.com/gridstack/gridstack.js/issues/1538) loading nested into small size and sizing back up
5758

5859
## 3.2.0 (2021-1-25)
5960

src/gridstack-dd.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
395395
let y = Math.round(ui.position.top / cellHeight);
396396
let w: number;
397397
let h: number;
398+
let resizing: boolean;
398399

399400
if (event.type === 'drag') {
400401
let distance = ui.position.top - node._prevYPix;
@@ -437,6 +438,7 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
437438
w = Math.round(ui.size.width / cellWidth);
438439
h = Math.round(ui.size.height / cellHeight);
439440
if (w === node.w && h === node.h) return;
441+
resizing = true;
440442
}
441443

442444
if (!this.engine.canMoveNode(node, x, y, w, h)) return;
@@ -445,6 +447,7 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
445447
node._lastTriedW = w;
446448
node._lastTriedH = h;
447449
this.engine.moveNode(node, x, y, w, h);
450+
if (resizing && node.subGrid) { (node.subGrid as GridStack).onParentResize(); }
448451
this._updateContainerHeight();
449452
}
450453

@@ -495,10 +498,12 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
495498

496499
this.engine.endUpdate();
497500

501+
/* doing it on live resize instead
498502
// if we re-sized a nested grid item, let the children resize as well
499503
if (event.type === 'resizestop') {
500-
this._resizeNestedGrids(target);
504+
if (target.gridstackNode.subGrid) {(target.gridstackNode.subGrid as GridStack).onParentResize()}
501505
}
506+
*/
502507
}
503508

504509
GridStackDD.get()

src/gridstack.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,6 @@ export class GridStack {
618618
if (update) {
619619
this._updateStyles(true, this.getRow()); // true = force re-create, for that # of rows
620620
}
621-
this._resizeNestedGrids(this.el);
622621
return this;
623622
}
624623

@@ -1222,16 +1221,6 @@ export class GridStack {
12221221
return this;
12231222
}
12241223

1225-
/** called to resize children nested grids when we/item resizes */
1226-
private _resizeNestedGrids(target: HTMLElement): GridStack {
1227-
target.querySelectorAll('.grid-stack').forEach((el: GridHTMLElement) => {
1228-
if (el.gridstack) {
1229-
el.gridstack.onParentResize();
1230-
}});
1231-
return this;
1232-
}
1233-
1234-
12351224
/** @internal */
12361225
private _prepareElement(el: GridItemHTMLElement, triggerAddEvent = false, node?: GridStackNode): GridStack {
12371226
if (!node) {
@@ -1337,15 +1326,14 @@ export class GridStack {
13371326
*/
13381327
public onParentResize(): GridStack {
13391328
if (!this.el || !this.el.clientWidth) return; // return if we're gone or no size yet (will get called again)
1340-
let changedOneColumn = false;
13411329
let oneColumn = !this.opts.disableOneColumnMode && this.el.clientWidth <= this.opts.minWidth;
1330+
let changedOneColumn = false;
13421331

13431332
if (!this._oneColumnMode !== !oneColumn) { // use ! (negate) so we can check undefined == false vs true
13441333
this._oneColumnMode = oneColumn;
13451334
changedOneColumn = true;
13461335
if (this.opts.animate) { this.setAnimation(false); } // 1 <-> 12 is too radical, turn off animation
13471336
this.column(oneColumn ? 1 : this._prevColumn);
1348-
this._resizeNestedGrids(this.el);
13491337
if (this.opts.animate) { this.setAnimation(true); }
13501338
}
13511339

@@ -1362,6 +1350,11 @@ export class GridStack {
13621350
}
13631351
}
13641352

1353+
// finally update any nested grids
1354+
this.engine.nodes.forEach(n => {
1355+
if (n.subGrid) {(n.subGrid as GridStack).onParentResize()}
1356+
});
1357+
13651358
return this;
13661359
}
13671360

0 commit comments

Comments
 (0)