@@ -137,11 +137,16 @@ export class GridStack {
137137
138138 // create the grid element, but check if the passed 'parent' already has grid styling and should be used instead
139139 let el = parent ;
140- if ( ! parent . classList . contains ( 'grid-stack' ) ) {
141- let doc = document . implementation . createHTMLDocument ( '' ) ; // IE needs a param
142- doc . body . innerHTML = `<div class="grid-stack ${ opt . class || '' } "></div>` ;
143- el = doc . body . children [ 0 ] as HTMLElement ;
144- parent . appendChild ( el ) ;
140+ const parentIsGrid = parent . classList . contains ( 'grid-stack' ) ;
141+ if ( ! parentIsGrid || opt . addRemoveCB ) {
142+ if ( opt . addRemoveCB ) {
143+ el = opt . addRemoveCB ( parent , opt , true , true ) ;
144+ } else {
145+ let doc = document . implementation . createHTMLDocument ( '' ) ; // IE needs a param
146+ doc . body . innerHTML = `<div class="grid-stack ${ opt . class || '' } "></div>` ;
147+ el = doc . body . children [ 0 ] as HTMLElement ;
148+ parent . appendChild ( el ) ;
149+ }
145150 }
146151
147152 // create grid class and load any children
@@ -182,7 +187,6 @@ export class GridStack {
182187 /** @internal true if we got created by drag over gesture, so we can removed on drag out (temporary) */
183188 public _isTemp ?: boolean ;
184189
185-
186190 /** @internal create placeholder DIV as needed */
187191 public get placeholder ( ) : HTMLElement {
188192 if ( ! this . _placeholder ) {
@@ -409,7 +413,7 @@ export class GridStack {
409413 if ( node ?. el ) {
410414 el = node . el ; // re-use element stored in the node
411415 } else if ( this . opts . addRemoveCB ) {
412- el = this . opts . addRemoveCB ( this , options , true ) ;
416+ el = this . opts . addRemoveCB ( this . el , options , true , false ) ;
413417 } else {
414418 let content = options ?. content || '' ;
415419 let doc = document . implementation . createHTMLDocument ( '' ) ; // IE needs a param
@@ -443,7 +447,7 @@ export class GridStack {
443447
444448 // see if there is a sub-grid to create
445449 if ( node . subGrid ) {
446- this . makeSubGrid ( node . el , undefined , undefined , false ) ;
450+ this . makeSubGrid ( node . el , undefined , undefined , false ) ; //node.subGrid will be used as option in method, no need to pass
447451 }
448452
449453 // if we're adding an item into 1 column (_prevColumn is set only when going to 1) make sure
@@ -493,26 +497,29 @@ export class GridStack {
493497 }
494498
495499 // if we're converting an existing full item, move over the content to be the first sub item in the new grid
496- // TODO: support this.opts.addRemoveCB for frameworks
497500 let content = node . el . querySelector ( '.grid-stack-item-content' ) as HTMLElement ;
498501 let newItem : HTMLElement ;
499502 let newItemOpt : GridStackNode ;
500503 if ( saveContent ) {
501504 this . _removeDD ( node . el ) ; // remove D&D since it's set on content div
502- let doc = document . implementation . createHTMLDocument ( '' ) ; // IE needs a param
503- doc . body . innerHTML = `<div class="grid-stack-item"></div>` ;
504- newItem = doc . body . children [ 0 ] as HTMLElement ;
505- newItem . appendChild ( content ) ;
506505 newItemOpt = { ...node , x :0 , y :0 } ;
507506 Utils . removeInternalForSave ( newItemOpt ) ;
508507 delete newItemOpt . subGrid ;
509508 if ( node . content ) {
510509 newItemOpt . content = node . content ;
511510 delete node . content ;
512511 }
513- doc . body . innerHTML = `<div class="grid-stack-item-content"></div>` ;
514- content = doc . body . children [ 0 ] as HTMLElement ;
515- node . el . appendChild ( content ) ;
512+ if ( this . opts . addRemoveCB ) {
513+ newItem = this . opts . addRemoveCB ( this . el , newItemOpt , true , false ) ;
514+ } else {
515+ let doc = document . implementation . createHTMLDocument ( '' ) ; // IE needs a param
516+ doc . body . innerHTML = `<div class="grid-stack-item"></div>` ;
517+ newItem = doc . body . children [ 0 ] as HTMLElement ;
518+ newItem . appendChild ( content ) ;
519+ doc . body . innerHTML = `<div class="grid-stack-item-content"></div>` ;
520+ content = doc . body . children [ 0 ] as HTMLElement ;
521+ node . el . appendChild ( content ) ;
522+ }
516523 this . _prepareDragDropByNode ( node ) ; // ... and restore original D&D
517524 }
518525
@@ -526,6 +533,9 @@ export class GridStack {
526533 setTimeout ( ( ) => style . transition = null ) ; // recover animation
527534 }
528535
536+ if ( this . opts . addRemoveCB ) {
537+ ops . addRemoveCB = ops . addRemoveCB || this . opts . addRemoveCB ;
538+ }
529539 let subGrid = node . subGrid = GridStack . addGrid ( content , ops ) ;
530540 if ( nodeToAdd ?. _moving ) subGrid . _isTemp = true ; // prevent re-nesting as we add over
531541 if ( autoColumn ) subGrid . _autoColumn = true ;
@@ -564,6 +574,7 @@ export class GridStack {
564574 pGrid . addWidget ( n . el , n ) ;
565575 } ) ;
566576 pGrid . batchUpdate ( false ) ;
577+ if ( this . parentGridItem ) delete this . parentGridItem . subGrid ;
567578 delete this . parentGridItem ;
568579
569580 // create an artificial event for the original grid now that this one is gone (got a leave, but won't get enter)
@@ -668,7 +679,7 @@ export class GridStack {
668679 let item = items . find ( w => n . id === w . id ) ;
669680 if ( ! item ) {
670681 if ( this . opts . addRemoveCB )
671- this . opts . addRemoveCB ( this , n , false ) ;
682+ this . opts . addRemoveCB ( this . el , n , false , false ) ;
672683 removed . push ( n ) ; // batch keep track
673684 this . removeWidget ( n . el , true , false ) ;
674685 }
@@ -873,6 +884,7 @@ export class GridStack {
873884 }
874885 this . _removeStylesheet ( ) ;
875886 this . el . removeAttribute ( 'gs-current-row' ) ;
887+ if ( this . parentGridItem ) delete this . parentGridItem . subGrid ;
876888 delete this . parentGridItem ;
877889 delete this . opts ;
878890 delete this . _placeholder ;
0 commit comments