@@ -144,6 +144,8 @@ export class GridStack {
144
144
private _styles : GridCSSStyleSheet ;
145
145
/** @internal flag to keep cells square during resize */
146
146
private _isAutoCellHeight : boolean ;
147
+ /** @internal track event binding to window resize so we can remove */
148
+ private _windowResizeBind : ( ) => GridStack ;
147
149
148
150
/**
149
151
* Construct a grid item from the given element and options
@@ -302,13 +304,12 @@ export class GridStack {
302
304
303
305
this . _updateContainerHeight ( ) ;
304
306
305
- this . _onResizeHandler = this . _onResizeHandler . bind ( this ) ; // so we can properly remove later
306
- window . addEventListener ( 'resize' , this . _onResizeHandler ) ;
307
- this . _onResizeHandler ( ) ;
307
+ this . onParentResize ( ) ;
308
308
309
309
this . _setupDragIn ( ) ;
310
310
this . _setupRemoveDrop ( ) ;
311
311
this . _setupAcceptWidget ( ) ;
312
+ this . _updateWindowResizeEvent ( ) ;
312
313
} ;
313
314
314
315
@@ -451,6 +452,7 @@ export class GridStack {
451
452
if ( update ) {
452
453
this . _updateStyles ( ) ;
453
454
}
455
+ this . _resizeNestedGrids ( this . el ) ;
454
456
return this ;
455
457
}
456
458
@@ -539,7 +541,7 @@ export class GridStack {
539
541
* @param removeDOM if `false` grid and items elements will not be removed from the DOM (Optional. Default `true`).
540
542
*/
541
543
public destroy ( removeDOM = true ) : GridStack {
542
- window . removeEventListener ( 'resize' , this . _onResizeHandler ) ;
544
+ this . _updateWindowResizeEvent ( true ) ;
543
545
this . disable ( ) ;
544
546
if ( ! removeDOM ) {
545
547
this . removeAll ( removeDOM ) ;
@@ -1333,7 +1335,7 @@ export class GridStack {
1333
1335
1334
1336
// if we re-sized a nested grid item, let the children resize as well
1335
1337
if ( event . type === 'resizestop' ) {
1336
- target . querySelectorAll ( '.grid-stack' ) . forEach ( ( el : GridHTMLElement ) => el . gridstack . _onResizeHandler ( ) ) ;
1338
+ this . _resizeNestedGrids ( target ) ;
1337
1339
}
1338
1340
}
1339
1341
@@ -1361,6 +1363,16 @@ export class GridStack {
1361
1363
return this ;
1362
1364
}
1363
1365
1366
+ /** called to resize children nested grids when we/item resizes */
1367
+ private _resizeNestedGrids ( target : HTMLElement ) : GridStack {
1368
+ target . querySelectorAll ( '.grid-stack' ) . forEach ( ( el : GridHTMLElement ) => {
1369
+ if ( el . gridstack ) {
1370
+ el . gridstack . onParentResize ( ) ;
1371
+ } } ) ;
1372
+ return this ;
1373
+ }
1374
+
1375
+
1364
1376
/** @internal */
1365
1377
private _prepareElement ( el : GridItemHTMLElement , triggerAddEvent = false ) : GridStack {
1366
1378
el . classList . add ( this . opts . itemClass ) ;
@@ -1471,10 +1483,10 @@ export class GridStack {
1471
1483
}
1472
1484
1473
1485
/**
1474
- * @internal called when we are being resized - check if the one Column Mode needs to be turned on/off
1475
- * and remember the prev columns we used.
1486
+ * called when we are being resized by the window - check if the one Column Mode needs to be turned on/off
1487
+ * and remember the prev columns we used, as well as check for auto cell height (square)
1476
1488
*/
1477
- private _onResizeHandler ( ) : GridStack {
1489
+ public onParentResize ( ) : GridStack {
1478
1490
// make the cells content (minus margin) square again
1479
1491
if ( this . _isAutoCellHeight ) {
1480
1492
Utils . throttle ( ( ) => {
@@ -1488,11 +1500,30 @@ export class GridStack {
1488
1500
if ( this . _oneColumnMode ) { return this }
1489
1501
this . _oneColumnMode = true ;
1490
1502
this . column ( 1 ) ;
1503
+ this . _resizeNestedGrids ( this . el ) ;
1491
1504
} else {
1492
1505
if ( ! this . _oneColumnMode ) { return this }
1493
1506
delete this . _oneColumnMode ;
1494
1507
this . column ( this . _prevColumn ) ;
1508
+ this . _resizeNestedGrids ( this . el ) ;
1495
1509
}
1510
+
1511
+ return this ;
1512
+ }
1513
+
1514
+ /** add or remove the window size event handler */
1515
+ private _updateWindowResizeEvent ( forceRemove = false ) : GridStack {
1516
+ const workTodo = ( this . _isAutoCellHeight || ! this . opts . disableOneColumnMode ) ;
1517
+
1518
+ // only add event if we're not nested (parent will call us) and we're auto sizing cells or supporting oneColumn (i.e. doing work)
1519
+ if ( workTodo && ! forceRemove && ! this . opts . _isNested && ! this . _windowResizeBind ) {
1520
+ this . _windowResizeBind = this . onParentResize . bind ( this ) ; // so we can properly remove later
1521
+ window . addEventListener ( 'resize' , this . _windowResizeBind ) ;
1522
+ } else if ( ( forceRemove || ! workTodo ) && this . _windowResizeBind ) {
1523
+ window . removeEventListener ( 'resize' , this . _windowResizeBind ) ;
1524
+ delete this . _windowResizeBind ; // remove link to us so we can free
1525
+ }
1526
+
1496
1527
return this ;
1497
1528
}
1498
1529
0 commit comments