Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c60dfd7

Browse files
authored
Merge pull request gridstack#2502 from adumesny/master
doContentResize called in load()
2 parents 5f2e453 + 62d02ca commit c60dfd7

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

demo/sizeToContent.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ <h1>Cell sizeToContent options demo</h1>
2121
<p>new 9.x feature that size the items to fit their content height as to not have scroll bars
2222
(unless `sizeToContent:false` in C: case). Defaulting to different initial size (see code) to show grow/shrink behavior.</p>
2323
<div>
24+
<a onClick="clearGrid()" class="btn btn-primary" href="#">clear</a>
25+
<a onClick="load()" class="btn btn-primary" href="#">load</a>
2426
column:
2527
<a onClick="column(8)" class="btn btn-primary" href="#">8</a>
2628
<a onClick="column(12)" class="btn btn-primary" href="#">12</a>
@@ -46,14 +48,22 @@ <h1>Cell sizeToContent options demo</h1>
4648
let grid = GridStack.init(opts);
4749
let text ='some very large content that will normally not fit in the window.'
4850
text = text + text;
51+
let count = 0;
4952
let items = [
5053
{x:0, y:0, w:2, content: `<div>A no h: ${text}</div>`},
5154
{x:2, y:0, w:1, h:2, content: '<div>B: shrink h=2</div>'}, // make taller than needed upfront
5255
{x:3, y:0, w:2, sizeToContent: false, content: `<div>C: WILL SCROLL. ${text}</div>`}, // prevent this from fitting testing
5356
{x:0, y:1, w:3, content: `<div>D no h: ${text} ${text}</div>`},
5457
{x:3, y:1, w:2, sizeToContent:3, content: `<div>E sizeToContent=3 <button onClick="more()">more</button><button onClick="less()">less</button><div id="dynContent">${text} ${text} ${text}</div></div>`} ];
58+
items.forEach(n => n.id = String(count++));
5559
grid.load(items);
5660

61+
function clearGrid() {
62+
grid.removeAll();
63+
}
64+
function load() {
65+
grid.load(items);
66+
}
5767
function column(n) {
5868
grid.column(n, 'none');
5969
}
@@ -85,6 +95,10 @@ <h1>Cell sizeToContent options demo</h1>
8595
let el = cont.parentElement.parentElement.parentElement;
8696
grid.resizeToContent(el);
8797
}
98+
99+
// TEST
100+
// grid.update(grid.engine.nodes[0].el, {x:7});
101+
// load();
88102
</script>
89103
</body>
90104
</html>

src/gridstack.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ export class GridStack {
693693
}
694694

695695
// now add/update the widgets
696+
let widthChanged = false;
696697
items.forEach(w => {
697698
let item = Utils.find(this.engine.nodes, w.id);
698699
if (item) {
@@ -702,6 +703,7 @@ export class GridStack {
702703
w.h = w.h || item.h;
703704
this.engine.findEmptyPosition(w);
704705
}
706+
widthChanged = widthChanged || (w.w !== undefined && w.w !== item.w);
705707
this.update(item.el, w);
706708
if (w.subGridOpts?.children) { // update any sub grid as well
707709
let sub = item.el.querySelector('.grid-stack') as GridHTMLElement;
@@ -716,6 +718,7 @@ export class GridStack {
716718
});
717719

718720
this.engine.removedNodes = removed;
721+
this.doContentResize(widthChanged, true); // we only need to wait for animation if we changed any widths
719722
this.batchUpdate(false);
720723

721724
// after commit, clear that flag
@@ -1220,12 +1223,13 @@ export class GridStack {
12201223
// check for content changing
12211224
if (w.content !== undefined) {
12221225
const itemContent = el.querySelector('.grid-stack-item-content');
1223-
if (!itemContent || itemContent.innerHTML === w.content) return;
1224-
itemContent.innerHTML = w.content;
1225-
// restore any sub-grid back
1226-
if (n.subGrid?.el) {
1227-
itemContent.appendChild(n.subGrid.el);
1228-
if (!n.subGrid.opts.styleInHead) n.subGrid._updateStyles(true); // force create
1226+
if (itemContent && itemContent.innerHTML !== w.content) {
1227+
itemContent.innerHTML = w.content;
1228+
// restore any sub-grid back
1229+
if (n.subGrid?.el) {
1230+
itemContent.appendChild(n.subGrid.el);
1231+
if (!n.subGrid.opts.styleInHead) n.subGrid._updateStyles(true); // force create
1232+
}
12291233
}
12301234
delete w.content;
12311235
}

0 commit comments

Comments
 (0)