@@ -376,9 +376,10 @@ export class GridStackEngine {
376
376
// remember it's position & width so we can restore back (1 -> 12 column) #1655 #1985
377
377
// IFF we're not in the middle of column resizing!
378
378
const saveOrig = this . column === 1 || node . x + node . w > this . column ;
379
- if ( saveOrig && this . column < 12 && ! this . _inColumnResize && ! node . autoPosition && node . _id && this . findCacheLayout ( node , 12 ) === - 1 ) {
379
+ if ( saveOrig && this . column < 12 && ! this . _inColumnResize && node . _id && this . findCacheLayout ( node , 12 ) === - 1 ) {
380
380
let copy = { ...node } ; // need _id + positions
381
- copy . x = Math . min ( 11 , copy . x ) ;
381
+ if ( copy . autoPosition ) { delete copy . x ; delete copy . y ; }
382
+ else copy . x = Math . min ( 11 , copy . x ) ;
382
383
copy . w = Math . min ( 12 , copy . w ) ;
383
384
this . cacheOneLayout ( copy , 12 ) ;
384
385
}
@@ -474,20 +475,23 @@ export class GridStackEngine {
474
475
return this ;
475
476
}
476
477
477
- /** find the first available empty spot for the given node width/height, updating the x,y attributes. return true if found */
478
- public findEmptyPosition ( node : GridStackNode ) : boolean {
479
- this . sortNodes ( ) ;
478
+ /** find the first available empty spot for the given node width/height, updating the x,y attributes. return true if found.
479
+ * optionally you can pass your own existing node list and column count, otherwise defaults to that engine data.
480
+ */
481
+ public findEmptyPosition ( node : GridStackNode , nodeList = this . nodes , column = this . column ) : boolean {
482
+ nodeList = Utils . sort ( nodeList , - 1 , column ) ;
480
483
let found = false ;
481
484
for ( let i = 0 ; ! found ; ++ i ) {
482
- let x = i % this . column ;
483
- let y = Math . floor ( i / this . column ) ;
484
- if ( x + node . w > this . column ) {
485
+ let x = i % column ;
486
+ let y = Math . floor ( i / column ) ;
487
+ if ( x + node . w > column ) {
485
488
continue ;
486
489
}
487
490
let box = { x, y, w : node . w , h : node . h } ;
488
- if ( ! this . nodes . find ( n => Utils . isIntercepted ( box , n ) ) ) {
491
+ if ( ! nodeList . find ( n => Utils . isIntercepted ( box , n ) ) ) {
489
492
node . x = x ;
490
493
node . y = y ;
494
+ delete node . autoPosition ;
491
495
found = true ;
492
496
}
493
497
}
@@ -829,10 +833,15 @@ export class GridStackEngine {
829
833
let j = nodes . findIndex ( n => n . _id === cacheNode . _id ) ;
830
834
if ( j !== - 1 ) {
831
835
// still current, use cache info positions
832
- nodes [ j ] . x = cacheNode . x ;
833
- nodes [ j ] . y = cacheNode . y ;
834
- nodes [ j ] . w = cacheNode . w ;
835
- newNodes . push ( nodes [ j ] ) ;
836
+ if ( cacheNode . autoPosition || isNaN ( cacheNode . x ) || isNaN ( cacheNode . y ) ) {
837
+ this . findEmptyPosition ( cacheNode , newNodes ) ;
838
+ }
839
+ if ( ! cacheNode . autoPosition ) {
840
+ nodes [ j ] . x = cacheNode . x ;
841
+ nodes [ j ] . y = cacheNode . y ;
842
+ nodes [ j ] . w = cacheNode . w ;
843
+ newNodes . push ( nodes [ j ] ) ;
844
+ }
836
845
nodes . splice ( j , 1 ) ;
837
846
}
838
847
} ) ;
@@ -892,14 +901,15 @@ export class GridStackEngine {
892
901
*/
893
902
public cacheOneLayout ( n : GridStackNode , column : number ) : GridStackEngine {
894
903
n . _id = n . _id || GridStackEngine . _idSeq ++ ;
895
- let layout : GridStackNode = { x : n . x , y : n . y , w : n . w , _id : n . _id }
904
+ let l : GridStackNode = { x : n . x , y : n . y , w : n . w , _id : n . _id }
905
+ if ( n . autoPosition ) { delete l . x ; delete l . y ; l . autoPosition = true ; }
896
906
this . _layouts = this . _layouts || [ ] ;
897
907
this . _layouts [ column ] = this . _layouts [ column ] || [ ] ;
898
908
let index = this . findCacheLayout ( n , column ) ;
899
909
if ( index === - 1 )
900
- this . _layouts [ column ] . push ( layout ) ;
910
+ this . _layouts [ column ] . push ( l ) ;
901
911
else
902
- this . _layouts [ column ] [ index ] = layout ;
912
+ this . _layouts [ column ] [ index ] = l ;
903
913
return this ;
904
914
}
905
915
0 commit comments