@@ -10,13 +10,20 @@ import { takeUntil } from 'rxjs/operators';
1010import { GridHTMLElement , GridItemHTMLElement , GridStack , GridStackNode , GridStackOptions , GridStackWidget } from 'gridstack' ;
1111
1212import { GridItemCompHTMLElement , GridstackItemComponent } from './gridstack-item.component' ;
13+ import { selectorToComponent } from './dummy.component' ;
1314
1415/** events handlers emitters signature for different events */
1516export type eventCB = { event : Event } ;
1617export type elementCB = { event : Event , el : GridItemHTMLElement } ;
1718export type nodesCB = { event : Event , nodes : GridStackNode [ ] } ;
1819export type droppedCB = { event : Event , previousNode : GridStackNode , newNode : GridStackNode } ;
1920
21+
22+ /** extends to store Ng Component selector, instead/inAddition to content */
23+ export interface NgGridStackWidget extends GridStackWidget {
24+ type ?: string ; // component type to create as content
25+ }
26+
2027/** store element to Ng Class pointer back */
2128export interface GridCompHTMLElement extends GridHTMLElement {
2229 _gridComp ?: GridstackComponent ;
@@ -96,7 +103,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
96103 public ngOnInit ( ) : void {
97104 // inject our own addRemove so we can create GridItemComponent instead of simple divs
98105 const opts : GridStackOptions = this . _options || { } ;
99- opts . addRemoveCB = GridstackComponent . _addRemoveCB ;
106+ opts . addRemoveCB = addRemoveCB ;
100107
101108 // init ourself before any template children are created since we track them below anyway - no need to double create+update widgets
102109 this . loaded = ! ! this . options ?. children ?. length ;
@@ -162,24 +169,31 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
162169 . on ( 'resizestart' , ( event : Event , el : GridItemHTMLElement ) => this . zone . run ( ( ) => this . resizeStartCB . emit ( { event, el} ) ) )
163170 . on ( 'resizestop' , ( event : Event , el : GridItemHTMLElement ) => this . zone . run ( ( ) => this . resizeStopCB . emit ( { event, el} ) ) )
164171 }
172+ }
165173
166- /** called by GS when a new item needs to be created, which we do as a Angular component, or deleted (skip) */
167- private static _addRemoveCB ( parent : GridCompHTMLElement | HTMLElement , w : GridStackWidget | GridStackOptions , add : boolean , isGrid : boolean ) : HTMLElement | undefined {
168- if ( add ) {
169- if ( ! parent ) return ;
170- // create the grid item dynamically - see https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html
171- if ( isGrid ) {
172- const gridItemComp = ( parent . parentElement as GridItemCompHTMLElement ) . _gridItemComp ;
173- const grid = gridItemComp ?. container ?. createComponent ( GridstackComponent ) ?. instance ;
174- if ( grid ) grid . options = w as GridStackOptions ;
175- return grid ?. el ;
176- } else {
177- // TODO: use GridStackWidget to define what type of component to create as child, or do it in GridstackItemComponent template...
178- const gridComp = ( parent as GridCompHTMLElement ) . _gridComp ;
179- const gridItem = gridComp ?. container ?. createComponent ( GridstackItemComponent ) ?. instance ;
180- return gridItem ?. el ;
174+ /** called by GS when a new item needs to be created, which we do as a Angular component, or deleted (skip) */
175+ function addRemoveCB ( parent : GridCompHTMLElement | HTMLElement , w : NgGridStackWidget | GridStackOptions , add : boolean , isGrid : boolean ) : HTMLElement | undefined {
176+ if ( add ) {
177+ if ( ! parent ) return ;
178+ // create the grid item dynamically - see https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html
179+ if ( isGrid ) {
180+ const gridItemComp = ( parent . parentElement as GridItemCompHTMLElement ) . _gridItemComp ;
181+ const grid = gridItemComp ?. container ?. createComponent ( GridstackComponent ) ?. instance ;
182+ if ( grid ) grid . options = w as GridStackOptions ;
183+ return grid ?. el ;
184+ } else {
185+ const gridComp = ( parent as GridCompHTMLElement ) . _gridComp ;
186+ const gridItem = gridComp ?. container ?. createComponent ( GridstackItemComponent ) ?. instance ;
187+
188+ // 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
189+ const type = ( w as NgGridStackWidget ) . type ;
190+ if ( ! w . subGrid && type && selectorToComponent [ type ] ) {
191+ gridItem ?. container ?. createComponent ( selectorToComponent [ type ] ) ;
181192 }
193+
194+ return gridItem ?. el ;
182195 }
183- return ;
184196 }
197+ return ;
185198}
199+
0 commit comments