@@ -376,9 +376,10 @@ export class GridStackEngine {
376376 // remember it's position & width so we can restore back (1 -> 12 column) #1655 #1985
377377 // IFF we're not in the middle of column resizing!
378378 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 ) {
380380 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 ) ;
382383 copy . w = Math . min ( 12 , copy . w ) ;
383384 this . cacheOneLayout ( copy , 12 ) ;
384385 }
@@ -474,20 +475,23 @@ export class GridStackEngine {
474475 return this ;
475476 }
476477
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 ) ;
480483 let found = false ;
481484 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 ) {
485488 continue ;
486489 }
487490 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 ) ) ) {
489492 node . x = x ;
490493 node . y = y ;
494+ delete node . autoPosition ;
491495 found = true ;
492496 }
493497 }
@@ -829,10 +833,15 @@ export class GridStackEngine {
829833 let j = nodes . findIndex ( n => n . _id === cacheNode . _id ) ;
830834 if ( j !== - 1 ) {
831835 // 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+ }
836845 nodes . splice ( j , 1 ) ;
837846 }
838847 } ) ;
@@ -892,14 +901,15 @@ export class GridStackEngine {
892901 */
893902 public cacheOneLayout ( n : GridStackNode , column : number ) : GridStackEngine {
894903 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 ; }
896906 this . _layouts = this . _layouts || [ ] ;
897907 this . _layouts [ column ] = this . _layouts [ column ] || [ ] ;
898908 let index = this . findCacheLayout ( n , column ) ;
899909 if ( index === - 1 )
900- this . _layouts [ column ] . push ( layout ) ;
910+ this . _layouts [ column ] . push ( l ) ;
901911 else
902- this . _layouts [ column ] [ index ] = layout ;
912+ this . _layouts [ column ] [ index ] = l ;
903913 return this ;
904914 }
905915
0 commit comments