@@ -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 ;
0 commit comments