Skip to content

Commit 56dce99

Browse files
authored
Merge pull request #1379 from adumesny/develop
better window resize handler fix
2 parents 91c2d7f + cfaa590 commit 56dce99

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

demo/nested.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ <h1>Nested grids demo</h1>
6060
disableOneColumnMode: true, // nested are small, but still want N columns
6161
margin: 1
6262
};
63-
GridStack.init({cellHeight: 70}, '.grid-stack.top');
63+
let grid0 = GridStack.init({cellHeight: 70}, '.grid-stack.top');
6464
let grid1 = GridStack.init(nestOptions, '.grid-stack.nested1');
6565
nestOptions.dragOut = false;
6666
let grid2 = GridStack.init(nestOptions, '.grid-stack.nested2');

src/gridstack.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export class GridStack {
144144
private _styles: GridCSSStyleSheet;
145145
/** @internal flag to keep cells square during resize */
146146
private _isAutoCellHeight: boolean;
147+
/** @internal track event binding to window resize so we can remove */
148+
private _windowResizeBind: () => GridStack;
147149

148150
/**
149151
* Construct a grid item from the given element and options
@@ -302,13 +304,12 @@ export class GridStack {
302304

303305
this._updateContainerHeight();
304306

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();
308308

309309
this._setupDragIn();
310310
this._setupRemoveDrop();
311311
this._setupAcceptWidget();
312+
this._updateWindowResizeEvent();
312313
};
313314

314315

@@ -451,6 +452,7 @@ export class GridStack {
451452
if (update) {
452453
this._updateStyles();
453454
}
455+
this._resizeNestedGrids(this.el);
454456
return this;
455457
}
456458

@@ -539,7 +541,7 @@ export class GridStack {
539541
* @param removeDOM if `false` grid and items elements will not be removed from the DOM (Optional. Default `true`).
540542
*/
541543
public destroy(removeDOM = true): GridStack {
542-
window.removeEventListener('resize', this._onResizeHandler);
544+
this._updateWindowResizeEvent(true);
543545
this.disable();
544546
if (!removeDOM) {
545547
this.removeAll(removeDOM);
@@ -1333,7 +1335,7 @@ export class GridStack {
13331335

13341336
// if we re-sized a nested grid item, let the children resize as well
13351337
if (event.type === 'resizestop') {
1336-
target.querySelectorAll('.grid-stack').forEach((el: GridHTMLElement) => el.gridstack._onResizeHandler());
1338+
this._resizeNestedGrids(target);
13371339
}
13381340
}
13391341

@@ -1361,6 +1363,16 @@ export class GridStack {
13611363
return this;
13621364
}
13631365

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+
13641376
/** @internal */
13651377
private _prepareElement(el: GridItemHTMLElement, triggerAddEvent = false): GridStack {
13661378
el.classList.add(this.opts.itemClass);
@@ -1471,10 +1483,10 @@ export class GridStack {
14711483
}
14721484

14731485
/**
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)
14761488
*/
1477-
private _onResizeHandler(): GridStack {
1489+
public onParentResize(): GridStack {
14781490
// make the cells content (minus margin) square again
14791491
if (this._isAutoCellHeight) {
14801492
Utils.throttle(() => {
@@ -1488,11 +1500,30 @@ export class GridStack {
14881500
if (this._oneColumnMode) { return this }
14891501
this._oneColumnMode = true;
14901502
this.column(1);
1503+
this._resizeNestedGrids(this.el);
14911504
} else {
14921505
if (!this._oneColumnMode) { return this }
14931506
delete this._oneColumnMode;
14941507
this.column(this._prevColumn);
1508+
this._resizeNestedGrids(this.el);
14951509
}
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+
14961527
return this;
14971528
}
14981529

0 commit comments

Comments
 (0)