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

Skip to content

Commit fc1446d

Browse files
committed
load() fix with mix of new item without coordinates
* fix gridstack#2639 * make sure we separate out new item that don't have coordinates to be added afterward
1 parent e8add28 commit fc1446d

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Change log
112112
* fix: [#2628](https://github.com/gridstack/gridstack.js/issues/2628) `removeAll()` does not trigger Angular's ngOnDestroy
113113
* fix: [#2503](https://github.com/gridstack/gridstack.js/issues/2503) Drag and drop a widget on top of a locked widget - Thank you [JakubEleniuk](https://github.com/JakubEleniuk)
114114
* fix: [#2584](https://github.com/gridstack/gridstack.js/issues/2584) wrong sort order during 1 column resize - Thank you [JakubEleniuk](https://github.com/JakubEleniuk) again.
115+
* fix: [#2639](https://github.com/gridstack/gridstack.js/issues/2639) load() with mix of new item without coordinates
115116

116117
## 10.1.1 (2024-03-03)
117118
* fix: [#2620](https://github.com/gridstack/gridstack.js/pull/2620) allow resizing with sizeToContent:NUMBER is uses
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>#2639 load() fix</title>
8+
<link rel="stylesheet" href="../../../demo/demo.css" />
9+
<script src="../../../dist/gridstack-all.js"></script>
10+
</head>
11+
<body>
12+
<div class="container-fluid">
13+
<h1>#2639 load() fix with mix of missing coordinates.</h1>
14+
<div>
15+
<button onClick="addNewWidget()">Add widget using .addWidget()</button>
16+
<button onClick="loadNewWidget()">Add widget using .load()</button>
17+
</div>
18+
<br><br>
19+
<div class="grid-stack"></div>
20+
<textarea readonly rows="20" id="text" style="width: 100%;"></textarea>
21+
</div>
22+
<script src="events.js"></script>
23+
<script type="text/javascript">
24+
saveGrid = function() {
25+
items = grid.save();
26+
document.querySelector('#text').innerHTML = JSON.stringify(items, ' ', 2)
27+
}
28+
29+
var count = 0;
30+
var items = [
31+
{x: 0, y: 0, w: 2, h: 2},
32+
{x: 2, y: 0, w: 8, h: 2},
33+
{x: 0, y: 2, w: 6, h: 2},
34+
{x: 6, y: 2, w: 3, h: 2},
35+
{x: 9, y: 2, w: 3, h: 2},
36+
{x: 0, y: 4, w: 5, h: 3},
37+
].map(w => ({
38+
...w,
39+
id: String(count),
40+
content: String(count++)
41+
}));
42+
43+
var options = { // put in gridstack options here
44+
float: false,
45+
cellHeight: 70,
46+
};
47+
var grid = GridStack.init(options).load(items);
48+
grid.on('change added', saveGrid);
49+
saveGrid();
50+
51+
createNode = function (str) {
52+
return {
53+
id: String(count),
54+
w: 3,
55+
h: 3,
56+
content: `${count++} ${str}`,
57+
};
58+
}
59+
60+
// Gets placed in the next hortizontal open slot
61+
addNewWidget = function () {
62+
grid.addWidget(createNode('added'));
63+
return false;
64+
};
65+
66+
// Gets placed at the bottom of a grid
67+
loadNewWidget = function () {
68+
items.push(createNode('loaded'));
69+
grid.load(items);
70+
return false;
71+
}
72+
</script>
73+
</body>
74+
</html>

src/gridstack.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,12 @@ export class GridStack {
699699
items = Utils.cloneDeep(items); // so we can mod
700700
const column = this.getColumn();
701701

702+
// if we have a mix of new items without coordinates and existing items, separate them out so they can be added after #2639
703+
let addAfter = items.filter(n => (n.x === undefined || n.y === undefined) && !Utils.find(this.engine.nodes, n.id));
704+
if (addAfter.length && addAfter.length !== items.length) {
705+
items = items.filter(n => !Utils.find(addAfter, n.id));
706+
} else addAfter = [];
707+
702708
// if passed list has coordinates, use them (insert from end to beginning for conflict resolution) else keep widget order
703709
const haveCoord = items.some(w => w.x !== undefined || w.y !== undefined);
704710
if (haveCoord) items = Utils.sort(items, -1);
@@ -776,6 +782,11 @@ export class GridStack {
776782
}
777783
});
778784

785+
// finally append any separate ones that didn't have explicit coordinates last so they can find next empty spot
786+
if (addRemove) {
787+
addAfter.forEach(w => this.addWidget(w))
788+
}
789+
779790
this.engine.removedNodes = removed;
780791
this.batchUpdate(false);
781792

0 commit comments

Comments
 (0)