@@ -10,13 +10,20 @@ import { takeUntil } from 'rxjs/operators';
10
10
import { GridHTMLElement , GridItemHTMLElement , GridStack , GridStackNode , GridStackOptions , GridStackWidget } from 'gridstack' ;
11
11
12
12
import { GridItemCompHTMLElement , GridstackItemComponent } from './gridstack-item.component' ;
13
+ import { selectorToComponent } from './dummy.component' ;
13
14
14
15
/** events handlers emitters signature for different events */
15
16
export type eventCB = { event : Event } ;
16
17
export type elementCB = { event : Event , el : GridItemHTMLElement } ;
17
18
export type nodesCB = { event : Event , nodes : GridStackNode [ ] } ;
18
19
export type droppedCB = { event : Event , previousNode : GridStackNode , newNode : GridStackNode } ;
19
20
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
+
20
27
/** store element to Ng Class pointer back */
21
28
export interface GridCompHTMLElement extends GridHTMLElement {
22
29
_gridComp ?: GridstackComponent ;
@@ -96,7 +103,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
96
103
public ngOnInit ( ) : void {
97
104
// inject our own addRemove so we can create GridItemComponent instead of simple divs
98
105
const opts : GridStackOptions = this . _options || { } ;
99
- opts . addRemoveCB = GridstackComponent . _addRemoveCB ;
106
+ opts . addRemoveCB = addRemoveCB ;
100
107
101
108
// init ourself before any template children are created since we track them below anyway - no need to double create+update widgets
102
109
this . loaded = ! ! this . options ?. children ?. length ;
@@ -162,24 +169,31 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
162
169
. on ( 'resizestart' , ( event : Event , el : GridItemHTMLElement ) => this . zone . run ( ( ) => this . resizeStartCB . emit ( { event, el} ) ) )
163
170
. on ( 'resizestop' , ( event : Event , el : GridItemHTMLElement ) => this . zone . run ( ( ) => this . resizeStopCB . emit ( { event, el} ) ) )
164
171
}
172
+ }
165
173
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 ] ) ;
181
192
}
193
+
194
+ return gridItem ?. el ;
182
195
}
183
- return ;
184
196
}
197
+ return ;
185
198
}
199
+
0 commit comments