Skip to content

Commit 5f2673e

Browse files
authored
Merge pull request #2821 from adumesny/master
angular support for nested Grid components
2 parents cef5d02 + bb3fac1 commit 5f2673e

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

angular/projects/lib/src/lib/base-widget.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import { NgCompInputs, NgGridStackWidget } from './gridstack.component';
2727
* things that are not mapped directly into @Input() fields for example.
2828
*/
2929
public deserialize(w: NgGridStackWidget) {
30+
// save full description for meta data
31+
this.widgetItem = w;
32+
if (!w) return;
33+
3034
if (w.input) Object.assign(this, w.input);
3135
}
3236
}

angular/projects/lib/src/lib/gridstack-item.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class GridstackItemComponent implements OnDestroy {
5757
return this.el.gridstackNode || this._options || {el: this.el};
5858
}
5959

60-
private _options?: GridStackNode;
60+
protected _options?: GridStackNode;
6161

6262
/** return the native element that contains grid specific fields as well */
6363
public get el(): GridItemCompHTMLElement { return this.elementRef.nativeElement; }
@@ -67,7 +67,7 @@ export class GridstackItemComponent implements OnDestroy {
6767
delete this._options;
6868
}
6969

70-
constructor(private readonly elementRef: ElementRef<GridItemHTMLElement>) {
70+
constructor(protected readonly elementRef: ElementRef<GridItemCompHTMLElement>) {
7171
this.el._gridItemComp = this;
7272
}
7373

angular/projects/lib/src/lib/gridstack.component.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ export type NgCompInputs = {[key: string]: any};
2121

2222
/** extends to store Ng Component selector, instead/inAddition to content */
2323
export interface NgGridStackWidget extends GridStackWidget {
24-
selector?: string; // component type to create as content
25-
input?: NgCompInputs; // serialized data for the component input fields
26-
subGridOpts?: NgGridStackOptions; // nested grid options
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;
2730
}
2831
export interface NgGridStackNode extends GridStackNode {
2932
selector?: string; // component type to create as content
@@ -117,15 +120,15 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
117120
return reflectComponentType(type)!.selector;
118121
}
119122

120-
private _options?: GridStackOptions;
121-
private _grid?: GridStack;
122-
private _sub: Subscription | undefined;
123-
private loaded?: boolean;
123+
protected _options?: GridStackOptions;
124+
protected _grid?: GridStack;
125+
protected _sub: Subscription | undefined;
126+
protected loaded?: boolean;
124127

125128
constructor(
126-
// private readonly zone: NgZone,
127-
// private readonly cd: ChangeDetectorRef,
128-
private readonly elementRef: ElementRef<GridCompHTMLElement>,
129+
// protected readonly zone: NgZone,
130+
// protected readonly cd: ChangeDetectorRef,
131+
protected readonly elementRef: ElementRef<GridCompHTMLElement>,
129132
) {
130133
this.el._gridComp = this;
131134
}
@@ -182,7 +185,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
182185
}
183186

184187
/** get all known events as easy to use Outputs for convenience */
185-
private hookEvents(grid?: GridStack) {
188+
protected hookEvents(grid?: GridStack) {
186189
if (!grid) return;
187190
grid
188191
.on('added', (event: Event, nodes: GridStackNode[]) => { this.checkEmpty(); this.addedCB.emit({event, nodes}); })
@@ -199,7 +202,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
199202
.on('resizestop', (event: Event, el: GridItemHTMLElement) => this.resizeStopCB.emit({event, el}))
200203
}
201204

202-
private unhookEvents(grid?: GridStack) {
205+
protected unhookEvents(grid?: GridStack) {
203206
if (!grid) return;
204207
grid.off('added change disable drag dragstart dragstop dropped enable removed resize resizestart resizestop');
205208
}
@@ -247,7 +250,6 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
247250
// if proper BaseWidget subclass, save it and load additional data
248251
if (childWidget && typeof childWidget.serialize === 'function' && typeof childWidget.deserialize === 'function') {
249252
gridItem.childWidget = childWidget;
250-
childWidget.widgetItem = w;
251253
childWidget.deserialize(w);
252254
}
253255
}
@@ -275,7 +277,7 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
275277

276278
/**
277279
* called for each item in the grid - check if additional information needs to be saved.
278-
* 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(),
279281
* this typically doesn't need to do anything. However your custom Component @Input() are now supported
280282
* using BaseWidget.serialize()
281283
*/

0 commit comments

Comments
 (0)