@@ -252,8 +252,6 @@ export class GridStack {
252
252
protected _sizeThrottle : ( ) => void ;
253
253
/** @internal limit auto cell resizing method */
254
254
protected prevWidth : number ;
255
- /** @internal true when loading items to insert first rather than append */
256
- protected _insertNotAppend : boolean ;
257
255
/** @internal extra row added when dragging at the bottom of the grid */
258
256
protected _extraDragRow = 0 ;
259
257
/** @internal true if nested grid should get column count from our width */
@@ -421,7 +419,7 @@ export class GridStack {
421
419
422
420
// load any passed in children as well, which overrides any DOM layout done above
423
421
if ( opts . children ) {
424
- let children = opts . children ;
422
+ const children = opts . children ;
425
423
delete opts . children ;
426
424
if ( children . length ) this . load ( children ) ; // don't load empty
427
425
}
@@ -495,11 +493,7 @@ export class GridStack {
495
493
node = this . engine . prepareNode ( options ) ;
496
494
this . _writeAttr ( el , options ) ;
497
495
498
- if ( this . _insertNotAppend ) {
499
- this . el . prepend ( el ) ;
500
- } else {
501
- this . el . appendChild ( el ) ;
502
- }
496
+ this . el . appendChild ( el ) ;
503
497
504
498
this . makeWidget ( el , options ) ;
505
499
@@ -703,22 +697,16 @@ export class GridStack {
703
697
// make sure size 1x1 (default) is present as it may need to override current sizes
704
698
items . forEach ( n => { n . w = n . w || 1 ; n . h = n . h || 1 } ) ;
705
699
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 ) ;
716
702
717
703
// if we're loading a layout into for example 1 column and items don't fit, make sure to save
718
704
// 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 ) {
720
708
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 ) ;
722
710
}
723
711
724
712
// if given a different callback, temporally set it as global option so creating will use it
@@ -728,19 +716,18 @@ export class GridStack {
728
716
let removed : GridStackNode [ ] = [ ] ;
729
717
this . batchUpdate ( ) ;
730
718
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 ) ;
734
722
735
723
// see if any items are missing from new layout and need to be removed first
736
- if ( addRemove ) {
724
+ if ( ! blank && addRemove ) {
737
725
let copyNodes = [ ...this . engine . nodes ] ; // don't loop through array you modify
738
726
copyNodes . forEach ( n => {
739
727
if ( ! n . id ) return ;
740
728
let item = Utils . find ( items , n . id ) ;
741
729
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 ) ;
744
731
removed . push ( n ) ; // batch keep track
745
732
this . removeWidget ( n . el , true , false ) ;
746
733
}
@@ -749,6 +736,7 @@ export class GridStack {
749
736
750
737
// now add/update the widgets - starting with removing items in the new layout we will reposition
751
738
// to reduce collision and add no-coord ones at next available spot
739
+ this . engine . _loading = true ; // help with collision
752
740
let updateNodes : GridStackWidget [ ] = [ ] ;
753
741
this . engine . nodes = this . engine . nodes . filter ( n => {
754
742
if ( Utils . find ( items , n . id ) ) { updateNodes . push ( n ) ; return false ; } // remove if found from list
@@ -778,28 +766,22 @@ export class GridStack {
778
766
let sub = item . el . querySelector ( '.grid-stack' ) as GridHTMLElement ;
779
767
if ( sub && sub . gridstack ) {
780
768
sub . gridstack . load ( w . subGridOpts . children ) ; // TODO: support updating grid options ?
781
- this . _insertNotAppend = true ; // got reset by above call
782
769
}
783
770
}
784
771
} else if ( addRemove ) {
785
772
this . addWidget ( w ) ;
786
773
}
787
774
} ) ;
788
775
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
794
777
this . engine . removedNodes = removed ;
795
778
this . batchUpdate ( false ) ;
796
779
797
780
// after commit, clear that flag
798
781
delete this . _ignoreLayoutsNodeChange ;
799
- delete this . _insertNotAppend ;
800
782
prevCB ? GridStack . addRemoveCB = prevCB : delete GridStack . addRemoveCB ;
801
783
// 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 ) ;
803
785
return this ;
804
786
}
805
787
0 commit comments