@@ -252,8 +252,6 @@ export class GridStack {
252252 protected _sizeThrottle : ( ) => void ;
253253 /** @internal limit auto cell resizing method */
254254 protected prevWidth : number ;
255- /** @internal true when loading items to insert first rather than append */
256- protected _insertNotAppend : boolean ;
257255 /** @internal extra row added when dragging at the bottom of the grid */
258256 protected _extraDragRow = 0 ;
259257 /** @internal true if nested grid should get column count from our width */
@@ -421,7 +419,7 @@ export class GridStack {
421419
422420 // load any passed in children as well, which overrides any DOM layout done above
423421 if ( opts . children ) {
424- let children = opts . children ;
422+ const children = opts . children ;
425423 delete opts . children ;
426424 if ( children . length ) this . load ( children ) ; // don't load empty
427425 }
@@ -495,11 +493,7 @@ export class GridStack {
495493 node = this . engine . prepareNode ( options ) ;
496494 this . _writeAttr ( el , options ) ;
497495
498- if ( this . _insertNotAppend ) {
499- this . el . prepend ( el ) ;
500- } else {
501- this . el . appendChild ( el ) ;
502- }
496+ this . el . appendChild ( el ) ;
503497
504498 this . makeWidget ( el , options ) ;
505499
@@ -703,22 +697,16 @@ export class GridStack {
703697 // make sure size 1x1 (default) is present as it may need to override current sizes
704698 items . forEach ( n => { n . w = n . w || 1 ; n . h = n . h || 1 } ) ;
705699
706- // if we have a mix of new items without coordinates and existing items, separate them out so they can be added after #2639
707- let addAfter = items . filter ( n => ( n . x === undefined || n . y === undefined ) && ! Utils . find ( this . engine . nodes , n . id ) ) ;
708- if ( addAfter . length && addAfter . length !== items . length ) {
709- items = items . filter ( n => ! Utils . find ( addAfter , n . id ) ) ;
710- } else addAfter = [ ] ;
711-
712- // if passed list has coordinates, use them (insert from end to beginning for conflict resolution) else keep widget order
713- const haveCoord = items . some ( w => w . x !== undefined || w . y !== undefined ) ;
714- if ( haveCoord ) items = Utils . sort ( items , - 1 ) ;
715- this . _insertNotAppend = haveCoord ; // if we create in reverse order...
700+ // sort items. those without coord will be appended last
701+ items = Utils . sort ( items ) ;
716702
717703 // if we're loading a layout into for example 1 column and items don't fit, make sure to save
718704 // the original wanted layout so we can scale back up correctly #1471
719- if ( items . some ( n => ( ( n . x || 0 ) + ( n . w || 1 ) ) > column ) ) {
705+ let maxColumn = 0 ;
706+ items . forEach ( n => { maxColumn = Math . max ( maxColumn , ( n . x || 0 ) + n . w ) } ) ;
707+ if ( maxColumn > column ) {
720708 this . _ignoreLayoutsNodeChange = true ; // skip layout update
721- this . engine . cacheLayout ( items , 12 , true ) ; // TODO: 12 is arbitrary. use max value in layout ?
709+ this . engine . cacheLayout ( items , maxColumn , true ) ;
722710 }
723711
724712 // if given a different callback, temporally set it as global option so creating will use it
@@ -728,19 +716,18 @@ export class GridStack {
728716 let removed : GridStackNode [ ] = [ ] ;
729717 this . batchUpdate ( ) ;
730718
731- // if we are blank ( loading into empty like startup) temp remove animation
732- const noAnim = ! this . engine . nodes . length ;
733- if ( noAnim ) this . setAnimation ( false ) ;
719+ // if we are loading from empty temporarily remove animation
720+ const blank = ! this . engine . nodes . length ;
721+ if ( blank ) this . setAnimation ( false ) ;
734722
735723 // see if any items are missing from new layout and need to be removed first
736- if ( addRemove ) {
724+ if ( ! blank && addRemove ) {
737725 let copyNodes = [ ...this . engine . nodes ] ; // don't loop through array you modify
738726 copyNodes . forEach ( n => {
739727 if ( ! n . id ) return ;
740728 let item = Utils . find ( items , n . id ) ;
741729 if ( ! item ) {
742- if ( GridStack . addRemoveCB )
743- GridStack . addRemoveCB ( this . el , n , false , false ) ;
730+ if ( GridStack . addRemoveCB ) GridStack . addRemoveCB ( this . el , n , false , false ) ;
744731 removed . push ( n ) ; // batch keep track
745732 this . removeWidget ( n . el , true , false ) ;
746733 }
@@ -749,6 +736,7 @@ export class GridStack {
749736
750737 // now add/update the widgets - starting with removing items in the new layout we will reposition
751738 // to reduce collision and add no-coord ones at next available spot
739+ this . engine . _loading = true ; // help with collision
752740 let updateNodes : GridStackWidget [ ] = [ ] ;
753741 this . engine . nodes = this . engine . nodes . filter ( n => {
754742 if ( Utils . find ( items , n . id ) ) { updateNodes . push ( n ) ; return false ; } // remove if found from list
@@ -778,28 +766,22 @@ export class GridStack {
778766 let sub = item . el . querySelector ( '.grid-stack' ) as GridHTMLElement ;
779767 if ( sub && sub . gridstack ) {
780768 sub . gridstack . load ( w . subGridOpts . children ) ; // TODO: support updating grid options ?
781- this . _insertNotAppend = true ; // got reset by above call
782769 }
783770 }
784771 } else if ( addRemove ) {
785772 this . addWidget ( w ) ;
786773 }
787774 } ) ;
788775
789- // finally append any separate ones that didn't have explicit coordinates last so they can find next empty spot
790- if ( addRemove ) {
791- addAfter . forEach ( w => this . addWidget ( w ) )
792- }
793-
776+ delete this . engine . _loading ; // done loading
794777 this . engine . removedNodes = removed ;
795778 this . batchUpdate ( false ) ;
796779
797780 // after commit, clear that flag
798781 delete this . _ignoreLayoutsNodeChange ;
799- delete this . _insertNotAppend ;
800782 prevCB ? GridStack . addRemoveCB = prevCB : delete GridStack . addRemoveCB ;
801783 // delay adding animation back
802- if ( noAnim && this . opts ?. animate ) this . setAnimation ( this . opts . animate , true ) ;
784+ if ( blank && this . opts ?. animate ) this . setAnimation ( this . opts . animate , true ) ;
803785 return this ;
804786 }
805787
0 commit comments