@@ -657,10 +657,10 @@ export class GridStack {
657657 * @example
658658 * see http://gridstackjs.com/demo/serialization.html
659659 */
660- public load ( layout : GridStackWidget [ ] , addRemove : boolean | AddRemoveFcn = GridStack . addRemoveCB || true ) : GridStack {
660+ public load ( items : GridStackWidget [ ] , addRemove : boolean | AddRemoveFcn = GridStack . addRemoveCB || true ) : GridStack {
661661 // if passed list has coordinates, use them (insert from end to beginning for conflict resolution) else force widget same order
662- const haveCoord = layout . some ( w => w . x !== undefined || w . y !== undefined ) ;
663- let items = haveCoord ? Utils . sort ( layout , - 1 , this . _prevColumn || this . getColumn ( ) ) : layout ;
662+ const haveCoord = items . some ( w => w . x !== undefined || w . y !== undefined ) ;
663+ if ( haveCoord ) items = Utils . sort ( items , - 1 , this . _prevColumn || this . getColumn ( ) ) ;
664664 this . _insertNotAppend = haveCoord ; // if we create in reverse order...
665665
666666 // 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
@@ -681,7 +681,8 @@ export class GridStack {
681681 if ( addRemove ) {
682682 let copyNodes = [ ...this . engine . nodes ] ; // don't loop through array you modify
683683 copyNodes . forEach ( n => {
684- let item = items . find ( w => n . id === w . id ) ;
684+ if ( ! n . id ) return ;
685+ let item = Utils . find ( items , n . id ) ;
685686 if ( ! item ) {
686687 if ( GridStack . addRemoveCB )
687688 GridStack . addRemoveCB ( this . el , n , false , false ) ;
@@ -691,19 +692,16 @@ export class GridStack {
691692 } ) ;
692693 }
693694
694- // now add/update the widgets - starting with an empty list to reduce collision and add no-coord ones at next available spot
695- let copyNodes = this . engine . nodes ;
696- this . engine . nodes = [ ] ;
695+ // now add/update the widgets
697696 items . forEach ( w => {
698- let item = ( w . id !== undefined ) ? copyNodes . find ( n => n . id === w . id ) : undefined ;
697+ let item = Utils . find ( this . engine . nodes , w . id ) ;
699698 if ( item ) {
700699 // check if missing coord, in which case find next empty slot with new (or old if missing) sizes
701700 if ( w . autoPosition || w . x === undefined || w . y === undefined ) {
702701 w . w = w . w || item . w ;
703702 w . h = w . h || item . h ;
704703 this . engine . findEmptyPosition ( w ) ;
705704 }
706- this . engine . nodes . push ( item ) ; // now back to current list...
707705 this . update ( item . el , w ) ;
708706 if ( w . subGridOpts ?. children ) { // update any sub grid as well
709707 let sub = item . el . querySelector ( '.grid-stack' ) as GridHTMLElement ;
@@ -1202,6 +1200,7 @@ export class GridStack {
12021200 if ( ! n ) return ;
12031201 let w = Utils . cloneDeep ( opt ) ; // make a copy we can modify in case they re-use it or multiple items
12041202 delete w . autoPosition ;
1203+ delete w . id ;
12051204
12061205 // move/resize widget if anything changed
12071206 let keys = [ 'x' , 'y' , 'w' , 'h' ] ;
@@ -1244,7 +1243,7 @@ export class GridStack {
12441243 Utils . sanitizeMinMax ( n ) ;
12451244
12461245 // finally move the widget
1247- if ( m ) this . moveNode ( n , m ) ;
1246+ if ( m !== undefined ) this . moveNode ( n , m ) ;
12481247 if ( changed ) { // move will only update x,y,w,h so update the rest too
12491248 this . _writeAttr ( el , n ) ;
12501249 }
0 commit comments