@@ -21,8 +21,12 @@ export type NgCompInputs = {[key: string]: any};
2121
2222/** extends to store Ng Component selector, instead/inAddition to content */
2323export interface NgGridStackWidget extends GridStackWidget {
24- selector ?: string ; // component type to create as content
25- input ?: NgCompInputs ; // serialized data for the component input fields
24+ /** Angular tag selector for this component to create at runtime */
25+ selector ?: string ;
26+ /** serialized data for the component input fields */
27+ input ?: NgCompInputs ;
28+ /** nested grid options */
29+ subGridOpts ?: NgGridStackOptions ;
2630}
2731export interface NgGridStackNode extends GridStackNode {
2832 selector ?: string ; // component type to create as content
@@ -116,15 +120,15 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
116120 return reflectComponentType ( type ) ! . selector ;
117121 }
118122
119- private _options ?: GridStackOptions ;
120- private _grid ?: GridStack ;
121- private _sub : Subscription | undefined ;
122- private loaded ?: boolean ;
123+ protected _options ?: GridStackOptions ;
124+ protected _grid ?: GridStack ;
125+ protected _sub : Subscription | undefined ;
126+ protected loaded ?: boolean ;
123127
124128 constructor (
125- // private readonly zone: NgZone,
126- // private readonly cd: ChangeDetectorRef,
127- private readonly elementRef : ElementRef < GridCompHTMLElement > ,
129+ // protected readonly zone: NgZone,
130+ // protected readonly cd: ChangeDetectorRef,
131+ protected readonly elementRef : ElementRef < GridCompHTMLElement > ,
128132 ) {
129133 this . el . _gridComp = this ;
130134 }
@@ -181,7 +185,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
181185 }
182186
183187 /** get all known events as easy to use Outputs for convenience */
184- private hookEvents ( grid ?: GridStack ) {
188+ protected hookEvents ( grid ?: GridStack ) {
185189 if ( ! grid ) return ;
186190 grid
187191 . on ( 'added' , ( event : Event , nodes : GridStackNode [ ] ) => { this . checkEmpty ( ) ; this . addedCB . emit ( { event, nodes} ) ; } )
@@ -198,7 +202,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
198202 . on ( 'resizestop' , ( event : Event , el : GridItemHTMLElement ) => this . resizeStopCB . emit ( { event, el} ) )
199203 }
200204
201- private unhookEvents ( grid ?: GridStack ) {
205+ protected unhookEvents ( grid ?: GridStack ) {
202206 if ( ! grid ) return ;
203207 grid . off ( 'added change disable drag dragstart dragstop dropped enable removed resize resizestart resizestop' ) ;
204208 }
@@ -214,13 +218,17 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
214218 //
215219 if ( ! host ) return ;
216220 if ( isGrid ) {
217- const container = ( host . parentElement as GridItemCompHTMLElement ) ?. _gridItemComp ?. container ;
218221 // TODO: figure out how to create ng component inside regular Div. need to access app injectors...
219222 // if (!container) {
220223 // const hostElement: Element = host;
221224 // const environmentInjector: EnvironmentInjector;
222225 // grid = createComponent(GridstackComponent, {environmentInjector, hostElement})?.instance;
223226 // }
227+
228+ const gridItemCom = ( host . parentElement as GridItemCompHTMLElement ) ?. _gridItemComp ;
229+ if ( ! gridItemCom ) return ;
230+ // check if gridItem has a child component with 'container' exposed to create under..
231+ const container = ( gridItemCom . childWidget as any ) ?. container || gridItemCom . container ;
224232 const gridRef = container ?. createComponent ( GridstackComponent ) ;
225233 const grid = gridRef ?. instance ;
226234 if ( ! grid ) return ;
@@ -234,17 +242,15 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
234242 if ( ! gridItem ) return ;
235243 gridItem . ref = gridItemRef
236244
237- // IFF we're not a subGrid, define what type of component to create as child, OR you can do it GridstackItemComponent template, but this is more generic
238- if ( ! w . subGridOpts ) {
239- const selector = ( w as NgGridStackWidget ) . selector ;
240- const type = selector ? GridstackComponent . selectorToType [ selector ] : undefined ;
241- if ( type ) {
242- const childWidget = gridItem . container ?. createComponent ( type ) ?. instance as BaseWidget ;
243- // if proper BaseWidget subclass, save it and load additional data
244- if ( childWidget && typeof childWidget . serialize === 'function' && typeof childWidget . deserialize === 'function' ) {
245- gridItem . childWidget = childWidget ;
246- childWidget . deserialize ( w ) ;
247- }
245+ // 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 ;
247+ const type = selector ? GridstackComponent . selectorToType [ selector ] : undefined ;
248+ 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 ) ;
248254 }
249255 }
250256
@@ -271,7 +277,7 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
271277
272278/**
273279 * called for each item in the grid - check if additional information needs to be saved.
274- * Note: since this is options minus gridstack private members using Utils.removeInternalForSave(),
280+ * Note: since this is options minus gridstack protected members using Utils.removeInternalForSave(),
275281 * this typically doesn't need to do anything. However your custom Component @Input() are now supported
276282 * using BaseWidget.serialize()
277283 */
0 commit comments