Skip to content

Commit fc918a4

Browse files
committed
addWidget() while in 1 column fix
* fix #1655 * `addWidget()` while in 1 column now remembers original wanted width (saves in 12 column layout) * removed uneeded _oneColumnMode (check for opts.column === 1) * updated responsive demo to add a 4x1 widget (test while in 1 column, size up back to 12 column)
1 parent e6f64bc commit fc918a4

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

demo/responsive.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ <h1>Responsive grid demo</h1>
2424
<option value="scale">scale</option>
2525
<option value="none">none</option>
2626
</select>
27+
<a onClick="grid.removeAll()" class="btn btn-primary" href="#">Clear</a>
28+
<a onClick="addWidget()" class="btn btn-primary" href="#">Add Widget</a>
2729
</div>
2830
<br/>
2931
<div class="grid-stack">
@@ -58,6 +60,10 @@ <h1>Responsive grid demo</h1>
5860
text.innerHTML = 12;
5961
}
6062
};
63+
64+
function addWidget() {
65+
grid.addWidget({x:0, y:0, w:4, content: '4x1'});
66+
};
6167

6268
let items = [ // our initial 12 column layout loaded first so we can compare
6369
{x: 0, y: 0, w: 2, content: '0'},

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Change log
5656
## 4.1.0-dev
5757

5858
- fix [#1704](https://github.com/gridstack/gridstack.js/issues/1704) scrollbar fix broken in 4.x
59+
- fix [#1655](https://github.com/gridstack/gridstack.js/issues/1655) `addWidget()` while in 1 column now remembers original wanted width
5960
- add [#1682](https://github.com/gridstack/gridstack.js/issues/1682) `addWidget()` now supports nested grids like init/addGrid() does.
6061

6162
## 4.1.0 (2021-4-7)

src/gridstack-engine.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ export class GridStackEngine {
349349
if (node.minH) { node.h = Math.max(node.h, node.minH); }
350350

351351
if (node.w > this.column) {
352+
// if user loaded a larger than allowed widget for current # of columns,
353+
// remember it's full width so we can restore back (1 -> 12 column) #1655
354+
if (this.column < 12) {
355+
node.w = Math.min(12, node.w);
356+
this.cacheOneLayout(node, 12);
357+
}
352358
node.w = this.column;
353359
} else if (node.w < 1) {
354360
node.w = 1;
@@ -691,8 +697,9 @@ export class GridStackEngine {
691697
// we save the original x,y,w (h isn't cached) to see what actually changed to propagate better.
692698
// Note: we don't need to check against out of bound scaling/moving as that will be done when using those cache values.
693699
nodes.forEach(node => {
700+
if (!node._orig) return; // didn't change (newly added ?)
694701
let n = layout.find(l => l._id === node._id);
695-
if (!n) return this; // no cache for new nodes. Will use those values.
702+
if (!n) return; // no cache for new nodes. Will use those values.
696703
let ratio = column / this.column;
697704
// Y changed, push down same amount
698705
// TODO: detect doing item 'swaps' will help instead of move (especially in 1 column mode)
@@ -828,6 +835,21 @@ export class GridStackEngine {
828835
return this;
829836
}
830837

838+
/**
839+
* call to cache the given node layout internally to the given location so we can restore back when column changes size
840+
* @param node single node to cache
841+
* @param column corresponding column index to save it under
842+
*/
843+
public cacheOneLayout(n: GridStackNode, column: number): GridStackEngine {
844+
n._id = n._id || GridStackEngine._idSeq++;
845+
let layout: Layout = {x: n.x, y: n.y, w: n.w, _id: n._id}
846+
this._layouts = this._layouts || [];
847+
this._layouts[column] = this._layouts[column] || [];
848+
let index = this._layouts[column].findIndex(l => l._id === n._id);
849+
index === -1 ? this._layouts[column].push(layout) : this._layouts[column][index] = layout;
850+
return this;
851+
}
852+
831853

832854
/** called to remove all internal values but the _id */
833855
public cleanupNode(node: GridStackNode): GridStackEngine {

src/gridstack.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ export class GridStack {
211211
/** @internal */
212212
private _placeholder: HTMLElement;
213213
/** @internal */
214-
private _oneColumnMode: boolean;
215-
/** @internal */
216214
private _prevColumn: number;
217215
/** @internal */
218216
private _ignoreLayoutsNodeChange: boolean;
@@ -1334,8 +1332,7 @@ export class GridStack {
13341332
let oneColumn = !this.opts.disableOneColumnMode && this.el.clientWidth <= this.opts.minWidth;
13351333
let changedOneColumn = false;
13361334

1337-
if (!this._oneColumnMode !== !oneColumn) { // use ! (negate) so we can check undefined == false vs true
1338-
this._oneColumnMode = oneColumn;
1335+
if ((this.opts.column === 1) !== oneColumn) {
13391336
changedOneColumn = true;
13401337
if (this.opts.animate) { this.setAnimation(false); } // 1 <-> 12 is too radical, turn off animation
13411338
this.column(oneColumn ? 1 : this._prevColumn);

0 commit comments

Comments
 (0)