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

Skip to content

Commit c3f7648

Browse files
authored
Merge pull request gridstack#2643 from adumesny/master
sort() no longer takes column
2 parents ea44523 + e8add28 commit c3f7648

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Change log
110110
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
111111
## 10.1.1-dev (TBD)
112112
* fix: [#2628](https://github.com/gridstack/gridstack.js/issues/2628) `removeAll()` does not trigger Angular's ngOnDestroy
113+
* 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)
114+
* 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.
113115

114116
## 10.1.1 (2024-03-03)
115117
* fix: [#2620](https://github.com/gridstack/gridstack.js/pull/2620) allow resizing with sizeToContent:NUMBER is uses

doc/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ gridstack.js API
6666
- [`update(el: GridStackElement, opts: GridStackWidget)`](#updateel-gridstackelement-opts-gridstackwidget)
6767
- [`willItFit(x, y, width, height, autoPosition)`](#willitfitx-y-width-height-autoposition)
6868
- [Utils](#utils)
69-
- [`GridStack.Utils.sort(nodes[, dir[, width]])`](#gridstackutilssortnodes-dir-width)
69+
- [`GridStack.Utils.sort(nodes[, dir])`](#gridstackutilssortnodes-dir)
7070

7171
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
7272

@@ -628,10 +628,9 @@ else {
628628

629629
## Utils
630630

631-
### `GridStack.Utils.sort(nodes[, dir[, width]])`
631+
### `GridStack.Utils.sort(nodes[, dir])`
632632

633633
Sorts array of nodes
634634

635635
- `nodes` - array to sort
636-
- `dir` - `1` for asc, `-1` for desc (optional)
637-
- `width` - width of the grid. If `undefined` the width will be calculated automatically (optional).
636+
- `dir` - `1` for ascending, `-1` for descending (optional)

src/gridstack-engine.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ export class GridStackEngine {
299299
public get float(): boolean { return this._float || false; }
300300

301301
/** sort the nodes array from first to last, or reverse. Called during collision/placement to force an order */
302-
public sortNodes(dir: 1 | -1 = 1, column = this.column): GridStackEngine {
303-
this.nodes = Utils.sort(this.nodes, dir, column);
302+
public sortNodes(dir: 1 | -1 = 1): GridStackEngine {
303+
this.nodes = Utils.sort(this.nodes, dir);
304304
return this;
305305
}
306306

@@ -812,14 +812,14 @@ export class GridStackEngine {
812812
// simpler shortcuts layouts
813813
const doCompact = layout === 'compact' || layout === 'list';
814814
if (doCompact) {
815-
this.sortNodes(1, prevColumn); // sort with original layout once and only once (new column will affect order otherwise)
815+
this.sortNodes(1); // sort with original layout once and only once (new column will affect order otherwise)
816816
}
817817

818818
// cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data IFF we're sizing down (see below)
819819
if (column < prevColumn) this.cacheLayout(this.nodes, prevColumn);
820820
this.batchUpdate(); // do this EARLY as it will call saveInitial() so we can detect where we started for _dirty and collision
821821
let newNodes: GridStackNode[] = [];
822-
let nodes = doCompact ? this.nodes : Utils.sort(this.nodes, -1, prevColumn); // current column reverse sorting so we can insert last to front (limit collision)
822+
let nodes = doCompact ? this.nodes : Utils.sort(this.nodes, -1); // current column reverse sorting so we can insert last to front (limit collision)
823823

824824
// see if we have cached previous layout IFF we are going up in size (restore) otherwise always
825825
// generate next size down from where we are (looks more natural as you gradually size down).
@@ -891,7 +891,7 @@ export class GridStackEngine {
891891
}
892892

893893
// finally re-layout them in reverse order (to get correct placement)
894-
newNodes = Utils.sort(newNodes, -1, column);
894+
newNodes = Utils.sort(newNodes, -1);
895895
this._inColumnResize = true; // prevent cache update
896896
this.nodes = []; // pretend we have no nodes to start with (add() will use same structures) to simplify layout
897897
newNodes.forEach(node => {

src/gridstack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ export class GridStack {
701701

702702
// if passed list has coordinates, use them (insert from end to beginning for conflict resolution) else keep widget order
703703
const haveCoord = items.some(w => w.x !== undefined || w.y !== undefined);
704-
if (haveCoord) items = Utils.sort(items, -1, column);
704+
if (haveCoord) items = Utils.sort(items, -1);
705705
this._insertNotAppend = haveCoord; // if we create in reverse order...
706706

707707
// if we're loading a layout into for example 1 column and items don't fit, make sure to save

src/utils.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,13 @@ export class Utils {
145145
/**
146146
* Sorts array of nodes
147147
* @param nodes array to sort
148-
* @param dir 1 for asc, -1 for desc (optional)
149-
* @param column number of columns in the grid. If undefined columns will be calculated automatically (optional).
148+
* @param dir 1 for ascending, -1 for descending (optional)
150149
**/
151-
static sort(nodes: GridStackNode[], dir: 1 | -1 = 1, column?: number): GridStackNode[] {
152-
column = column || nodes.reduce((col, n) => Math.max(n.x + n.w, col), 0) || 12;
150+
static sort(nodes: GridStackNode[], dir: 1 | -1 = 1): GridStackNode[] {
151+
const und = 10000;
153152
return nodes.sort((a, b) => {
154-
let diffY = dir * (a.y - b.y);
155-
if (diffY === 0) return dir * column * (a.x - b.x);
153+
let diffY = dir * ((a.y ?? und) - (b.y ?? und));
154+
if (diffY === 0) return dir * ((a.x ?? und) - (b.x ?? und));
156155
return diffY;
157156
});
158157
}

0 commit comments

Comments
 (0)