Skip to content

Commit 8942d84

Browse files
author
Alain Dumesny
authored
Merge pull request #2397 from adumesny/master
`load()` support re-order loading with autoPosition
2 parents baaef43 + 784ad10 commit 8942d84

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Change log
9595
## 8.3.0-dev (TBD)
9696
* feat [#2378](https://github.com/gridstack/gridstack.js/pull/2378) attribute `DDRemoveOpt.decline` to deny the removal of a specific class.
9797
* fix: dragging onto trash now calls removeWidget() and therefore `GridStack.addRemoveCB` (for component cleanup)
98+
* feat: make `load()` support re-order loading without explicit coordinates (`autoPosition` or missing `x,y`) uses passed order.
9899

99100
## 8.3.0 (2023-06-13)
100101
* feat [#2358](https://github.com/gridstack/gridstack.js/issues/2358) column(N, 'list'|'compact'|...) resizing now support reflowing content as list

src/gridstack.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,14 @@ export class GridStack {
650650
* see http://gridstackjs.com/demo/serialization.html
651651
**/
652652
public load(layout: GridStackWidget[], addRemove: boolean | AddRemoveFcn = GridStack.addRemoveCB || true): GridStack {
653-
let items = GridStack.Utils.sort([...layout], -1, this._prevColumn || this.getColumn()); // make copy before we mod/sort
654-
this._insertNotAppend = true; // since create in reverse order...
653+
// if passed list has coordinates, use them (insert from end to beginning for conflict resolution) else force widget same order
654+
const haveCoord = layout.some(w => w.x !== undefined || w.y !== undefined);
655+
let items = haveCoord ? Utils.sort(layout, -1, this._prevColumn || this.getColumn()) : layout;
656+
this._insertNotAppend = haveCoord; // if we create in reverse order...
655657

656658
// if we're loading a layout into for example 1 column (_prevColumn is set only when going to 1) and items don't fit, make sure to save
657659
// the original wanted layout so we can scale back up correctly #1471
658-
if (this._prevColumn && this._prevColumn !== this.opts.column && items.some(n => (n.x + n.w) > (this.opts.column as number))) {
660+
if (this._prevColumn && this._prevColumn !== this.opts.column && items.some(n => ((n.x || 0) + n.w) > (this.opts.column as number))) {
659661
this._ignoreLayoutsNodeChange = true; // skip layout update
660662
this.engine.cacheLayout(items, this._prevColumn, true);
661663
}
@@ -681,10 +683,19 @@ export class GridStack {
681683
});
682684
}
683685

684-
// now add/update the widgets
686+
// now add/update the widgets - starting with an empty list to reduce collision and add no-coord ones at next available spot
687+
let copyNodes = this.engine.nodes;
688+
this.engine.nodes = [];
685689
items.forEach(w => {
686-
let item = (w.id !== undefined) ? this.engine.nodes.find(n => n.id === w.id) : undefined;
690+
let item = (w.id !== undefined) ? copyNodes.find(n => n.id === w.id) : undefined;
687691
if (item) {
692+
// check if missing coord, in which case find next empty slot with new (or old if missing) sizes
693+
if (w.autoPosition || w.x === undefined || w.y === undefined) {
694+
w.w = w.w || item.w;
695+
w.h = w.h || item.h;
696+
this.engine.findEmptyPosition(w);
697+
}
698+
this.engine.nodes.push(item); // now back to current list...
688699
this.update(item.el, w);
689700
if (w.subGridOpts?.children) { // update any sub grid as well
690701
let sub = item.el.querySelector('.grid-stack') as GridHTMLElement;

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ export class Utils {
137137
static sort(nodes: GridStackNode[], dir: 1 | -1 = 1, column?: number): GridStackNode[] {
138138
column = column || nodes.reduce((col, n) => Math.max(n.x + n.w, col), 0) || 12;
139139
if (dir === -1)
140-
return nodes.sort((a, b) => (b.x + b.y * column)-(a.x + a.y * column));
140+
return nodes.sort((a, b) => ((b.x ?? 1000) + (b.y ?? 1000) * column)-((a.x ?? 1000) + (a.y ?? 1000) * column));
141141
else
142-
return nodes.sort((b, a) => (b.x + b.y * column)-(a.x + a.y * column));
142+
return nodes.sort((b, a) => ((b.x ?? 1000) + (b.y ?? 1000) * column)-((a.x ?? 1000) + (a.y ?? 1000) * column));
143143
}
144144

145145
/**

0 commit comments

Comments
 (0)