@@ -211,7 +211,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
211211/**
212212 * can be used when a new item needs to be created, which we do as a Angular component, or deleted (skip)
213213 **/
214- export function gsCreateNgComponents ( host : GridCompHTMLElement | HTMLElement , w : NgGridStackWidget | GridStackNode , add : boolean , isGrid : boolean ) : HTMLElement | undefined {
214+ export function gsCreateNgComponents ( host : GridCompHTMLElement | HTMLElement , n : NgGridStackNode , add : boolean , isGrid : boolean ) : HTMLElement | undefined {
215215 if ( add ) {
216216 //
217217 // create the component dynamically - see https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html
@@ -225,15 +225,15 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
225225 // grid = createComponent(GridstackComponent, {environmentInjector, hostElement})?.instance;
226226 // }
227227
228- const gridItemCom = ( host . parentElement as GridItemCompHTMLElement ) ?. _gridItemComp ;
229- if ( ! gridItemCom ) return ;
228+ const gridItemComp = ( host . parentElement as GridItemCompHTMLElement ) ?. _gridItemComp ;
229+ if ( ! gridItemComp ) return ;
230230 // check if gridItem has a child component with 'container' exposed to create under..
231- const container = ( gridItemCom . childWidget as any ) ?. container || gridItemCom . container ;
231+ const container = ( gridItemComp . childWidget as any ) ?. container || gridItemComp . container ;
232232 const gridRef = container ?. createComponent ( GridstackComponent ) ;
233233 const grid = gridRef ?. instance ;
234234 if ( ! grid ) return ;
235235 grid . ref = gridRef ;
236- grid . options = w as GridStackOptions ;
236+ grid . options = n ;
237237 return grid . el ;
238238 } else {
239239 const gridComp = ( host as GridCompHTMLElement ) . _gridComp ;
@@ -243,15 +243,30 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
243243 gridItem . ref = gridItemRef
244244
245245 // define what type of component to create as child, OR you can do it GridstackItemComponent template, but this is more generic
246- const selector = ( w as NgGridStackWidget ) . selector ;
246+ const selector = n . selector ;
247247 const type = selector ? GridstackComponent . selectorToType [ selector ] : undefined ;
248248 if ( type ) {
249- const childWidget = gridItem . container ?. createComponent ( type ) ?. instance as BaseWidget ;
250- // if proper BaseWidget subclass, save it and load additional data
251- if ( childWidget && typeof childWidget . serialize === 'function' && typeof childWidget . deserialize === 'function' ) {
252- gridItem . childWidget = childWidget ;
253- childWidget . deserialize ( w ) ;
249+ // shared code to create our selector component
250+ const createComp = ( ) => {
251+ const childWidget = gridItem . container ?. createComponent ( type ) ?. instance as BaseWidget ;
252+ // if proper BaseWidget subclass, save it and load additional data
253+ if ( childWidget && typeof childWidget . serialize === 'function' && typeof childWidget . deserialize === 'function' ) {
254+ gridItem . childWidget = childWidget ;
255+ childWidget . deserialize ( n ) ;
256+ }
254257 }
258+
259+ const lazyLoad = n . lazyLoad || n . grid ?. opts ?. lazyLoad && n . lazyLoad !== false ;
260+ if ( lazyLoad ) {
261+ if ( ! n . visibleObservable ) {
262+ n . visibleObservable = new IntersectionObserver ( ( [ entry ] ) => { if ( entry . isIntersecting ) {
263+ n . visibleObservable ?. disconnect ( ) ;
264+ delete n . visibleObservable ;
265+ createComp ( ) ;
266+ } } ) ;
267+ window . setTimeout ( ( ) => n . visibleObservable ?. observe ( gridItem . el ) ) ; // wait until callee sets position attributes
268+ }
269+ } else createComp ( ) ;
255270 }
256271
257272 return gridItem . el ;
@@ -261,7 +276,6 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
261276 // REMOVE - have to call ComponentRef:destroy() for dynamic objects to correctly remove themselves
262277 // Note: this will destroy all children dynamic components as well: gridItem -> childWidget
263278 //
264- const n = w as GridStackNode ;
265279 if ( isGrid ) {
266280 const grid = ( n . el as GridCompHTMLElement ) ?. _gridComp ;
267281 if ( grid ?. ref ) grid . ref . destroy ( ) ;
0 commit comments