Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42a6bb8

Browse files
authored
demo Angular - updated code to Ang13+ (gridstack#2183)
* ComponentFactoryResolver is no longer needed * select="[empty-content]" *ngIf="isEmpty" is cleaner * added bug caveat to readme.
1 parent 22357ed commit 42a6bb8

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

demo/angular/src/app/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ to build the demo, go to demo/angular and run `yarn` + `yarn start` and Navigate
8383

8484
- This wrapper needs v7.2+ to run as it needs the latest changes
8585
- Code isn't compiled into a lib YET. You'll need to copy those files. Let me know (slack) if you are using it...
86+
- BUG: content doesn't appear on new widget until widget is moved around (or another created that pushes it). Need to force angular detection changes...
8687

8788
## ngFor Caveats
8889
- This wrapper handles well ngFor loops, but if you're using a trackBy function (as I would recommend) and no element id change after an update,

demo/angular/src/app/app.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export class AppComponent {
5252
* TEST dynamic grid operations - uses grid API directly (since we don't track structure that gets out of sync)
5353
*/
5454
public add(gridComp: GridstackComponent) {
55+
// TODO: BUG the content doesn't appear until widget is moved around (or another created). Need to force
56+
// angular detection changes...
5557
gridComp.grid?.addWidget({x:3, y:0, w:2, content:`item ${ids}`, id:String(ids++)});
5658
}
5759
public delete(gridComp: GridstackComponent) {

demo/angular/src/app/gridstack.component.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2022 Alain Dumesny - see GridStack root license
44
*/
55

6-
import { AfterContentInit, ChangeDetectionStrategy, Component, ComponentFactoryResolver, ContentChildren, ElementRef, EventEmitter, Input,
6+
import { AfterContentInit, ChangeDetectionStrategy, Component, ContentChildren, ElementRef, EventEmitter, Input,
77
NgZone, OnDestroy, OnInit, Output, QueryList, ViewChild, ViewContainerRef } from '@angular/core';
88
import { Subject } from 'rxjs';
99
import { takeUntil } from 'rxjs/operators';
@@ -24,7 +24,7 @@ export type droppedCB = {event: Event, previousNode: GridStackNode, newNode: Gri
2424
selector: 'gridstack',
2525
template: `
2626
<!-- content to show when when grid is empty, like instructions on how to add widgets -->
27-
<ng-content select="[no-item-content]" *ngIf="showEmpty"></ng-content>
27+
<ng-content select="[empty-content]" *ngIf="isEmpty"></ng-content>
2828
<!-- where dynamic items go -->
2929
<ng-template #container></ng-template>
3030
<!-- where template items go -->
@@ -48,7 +48,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
4848
public get options(): GridStackOptions { return this._grid?.opts || this._options || {}; }
4949

5050
/** true while ng-content with 'no-item-content' should be shown when last item is removed from a grid */
51-
@Input() public showEmpty?: boolean;
51+
@Input() public isEmpty?: boolean;
5252

5353
/** individual list of GridStackEvent callbacks handlers as output
5454
* otherwise use this.grid.on('name1 name2 name3', callback) to handle multiple at once
@@ -82,7 +82,6 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
8282
constructor(
8383
private readonly ngZone: NgZone,
8484
private readonly elementRef: ElementRef<GridHTMLElement>,
85-
private readonly resolver: ComponentFactoryResolver,
8685
) {
8786
}
8887

@@ -135,22 +134,22 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
135134
/** check if the grid is empty, if so show alternative content */
136135
public checkEmpty() {
137136
if (!this.grid) return;
138-
this.showEmpty = !this.grid.engine.nodes.length;
137+
this.isEmpty = !this.grid.engine.nodes.length;
139138
}
140139

141140
/** get all known events as easy to use Outputs for convenience */
142141
private hookEvents(grid?: GridStack) {
143142
if (!grid) return;
144143
grid
145-
.on('added', (event: Event, nodes: GridStackNode[]) => this.ngZone.run(() => {this.added.emit({event, nodes}); this.checkEmpty(); }))
144+
.on('added', (event: Event, nodes: GridStackNode[]) => this.ngZone.run(() => { this.checkEmpty(); this.added.emit({event, nodes}); }))
146145
.on('change', (event: Event, nodes: GridStackNode[]) => this.ngZone.run(() => this.changeGS.emit({event, nodes})))
147146
.on('disable', (event: Event) => this.ngZone.run(() => this.disable.emit({event})))
148147
.on('drag', (event: Event, el: GridItemHTMLElement) => this.ngZone.run(() => this.drag.emit({event, el})))
149148
.on('dragstart', (event: Event, el: GridItemHTMLElement) => this.ngZone.run(() => this.dragstart.emit({event, el})))
150149
.on('dragstop', (event: Event, el: GridItemHTMLElement) => this.ngZone.run(() => this.dragstop.emit({event, el})))
151150
.on('dropped', (event: Event, previousNode: GridStackNode, newNode: GridStackNode) => this.ngZone.run(() => this.dropped.emit({event, previousNode, newNode})))
152151
.on('enable', (event: Event) => this.ngZone.run(() => this.enable.emit({event})))
153-
.on('removed', (event: Event, nodes: GridStackNode[]) => this.ngZone.run(() => {this.removed.emit({event, nodes}); this.checkEmpty(); }))
152+
.on('removed', (event: Event, nodes: GridStackNode[]) => this.ngZone.run(() => { this.checkEmpty(); this.removed.emit({event, nodes}); }))
154153
.on('resize', (event: Event, el: GridItemHTMLElement) => this.ngZone.run(() => this.resize.emit({event, el})))
155154
.on('resizestart', (event: Event, el: GridItemHTMLElement) => this.ngZone.run(() => this.resizestart.emit({event, el})))
156155
.on('resizestop', (event: Event, el: GridItemHTMLElement) => this.ngZone.run(() => this.resizestop.emit({event, el})))
@@ -161,12 +160,10 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
161160
if (add) {
162161
if (!this.container) return;
163162
// create the grid item dynamically - see https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html
164-
// and https://netbasal.com/dynamically-creating-components-with-angular-a7346f4a982d#.irxd1nulp
165-
const factory = this.resolver.resolveComponentFactory(GridstackItemComponent);
166-
const gridItem = this.container.createComponent(factory).instance;
167-
return gridItem.el;
163+
const gridItem = this.container.createComponent(GridstackItemComponent)?.instance;
164+
return gridItem?.el;
168165
}
169-
// if (this.outsideAddRemove) this.outsideAddRemove(g, w, add);
166+
// if (this.outsideAddRemove) this.outsideAddRemove(g, w, add); // TODO: ?
170167
return;
171168
}
172169
}

0 commit comments

Comments
 (0)