@@ -410,7 +410,7 @@ export class GridStack {
410
410
return w . el !== undefined || w . x !== undefined || w . y !== undefined || w . w !== undefined || w . h !== undefined || w . content !== undefined ? true : false ;
411
411
}
412
412
413
- let el : HTMLElement ;
413
+ let el : GridItemHTMLElement ;
414
414
let node : GridStackNode ;
415
415
if ( typeof els === 'string' ) {
416
416
let doc = document . implementation . createHTMLDocument ( '' ) ; // IE needs a param
@@ -434,6 +434,10 @@ export class GridStack {
434
434
435
435
if ( ! el ) return ;
436
436
437
+ // if the caller ended up initializing the widget in addRemoveCB, or we stared with one already, skip the rest
438
+ node = el . gridstackNode ;
439
+ if ( node && el . parentElement === this . el && this . engine . nodes . find ( n => n . _id === node . _id ) ) return el ;
440
+
437
441
// Tempting to initialize the passed in opt with default and valid values, but this break knockout demos
438
442
// as the actual value are filled in when _prepareElement() calls el.getAttribute('gs-xyz') before adding the node.
439
443
// So make sure we load any DOM attributes that are not specified in passed in options (which override)
@@ -449,23 +453,7 @@ export class GridStack {
449
453
this . el . appendChild ( el ) ;
450
454
}
451
455
452
- // similar to makeWidget() that doesn't read attr again and worse re-create a new node and loose any _id
453
- this . _prepareElement ( el , true , options ) ;
454
- this . _updateContainerHeight ( ) ;
455
-
456
- // see if there is a sub-grid to create
457
- if ( node . subGridOpts ) {
458
- this . makeSubGrid ( node . el , node . subGridOpts , undefined , false ) ; // node.subGrid will be used as option in method, no need to pass
459
- }
460
-
461
- // if we're adding an item into 1 column (_prevColumn is set only when going to 1) make sure
462
- // we don't override the larger 12 column layout that was already saved. #1985
463
- if ( this . _prevColumn && this . opts . column === 1 ) {
464
- this . _ignoreLayoutsNodeChange = true ;
465
- }
466
- this . _triggerAddEvent ( ) ;
467
- this . _triggerChangeEvent ( ) ;
468
- delete this . _ignoreLayoutsNodeChange ;
456
+ this . makeWidget ( el , options ) ;
469
457
470
458
return el ;
471
459
}
@@ -982,8 +970,22 @@ export class GridStack {
982
970
let el = GridStack . getElement ( els ) ;
983
971
this . _prepareElement ( el , true , options ) ;
984
972
this . _updateContainerHeight ( ) ;
973
+
974
+ // see if there is a sub-grid to create
975
+ const node = el . gridstackNode ;
976
+ if ( node . subGridOpts ) {
977
+ this . makeSubGrid ( el , node . subGridOpts , undefined , false ) ; // node.subGrid will be used as option in method, no need to pass
978
+ }
979
+
980
+ // if we're adding an item into 1 column (_prevColumn is set only when going to 1) make sure
981
+ // we don't override the larger 12 column layout that was already saved. #1985
982
+ if ( this . _prevColumn && this . opts . column === 1 ) {
983
+ this . _ignoreLayoutsNodeChange = true ;
984
+ }
985
985
this . _triggerAddEvent ( ) ;
986
986
this . _triggerChangeEvent ( ) ;
987
+ delete this . _ignoreLayoutsNodeChange ;
988
+
987
989
return el ;
988
990
}
989
991
0 commit comments