Skip to content

Commit c4b5524

Browse files
authored
Merge pull request #2125 from adumesny/master
more angular updates
2 parents cfe490e + 05c4d5e commit c4b5524

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

demo/angular/src/app/app.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
<p>Pick a sample test case to load:</p>
44
<button (click)="show=0">Simple</button>
55
<button (click)="show=1">ngFor case</button>
6+
<button (click)="show=2">ngFor custom command</button>
67
</div>
78

89
<div class="test-container">
910
<angular-simple-test *ngIf="show===0"></angular-simple-test>
1011
<angular-ng-for-test *ngIf="show===1"></angular-ng-for-test>
12+
<angular-ng-for-cmd-test *ngIf="show===2"></angular-ng-for-cmd-test>
1113
</div>

demo/angular/src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { BrowserModule } from '@angular/platform-browser';
33

44
import { AppComponent } from './app.component';
55
import { AngularNgForTestComponent } from './ngFor';
6+
import { AngularNgForCmdTestComponent } from './ngFor_cmd';
67
import { AngularSimpleComponent } from './simple';
78

89
@NgModule({
910
declarations: [
1011
AppComponent,
1112
AngularSimpleComponent,
1213
AngularNgForTestComponent,
14+
AngularNgForCmdTestComponent,
1315
],
1416
imports: [
1517
BrowserModule

demo/angular/src/app/ngFor.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,14 @@ export class AngularNgForTestComponent implements AfterViewInit {
6060
margin: 5,
6161
float: true,
6262
})
63-
// Optional: called when widgets are changed (moved/resized/added) - update our list to match ?
64-
.on('change added', (event, list) => {
65-
setTimeout(() => { // prevent added items from ExpressionChangedAfterItHasBeenCheckedError
66-
(list as GridStackNode[]).forEach(n => {
67-
const item = this._items.find(i => i.id === n.id);
68-
if (item) Utils.copyPos(item, n);
69-
})
70-
}, 0);
71-
});
63+
.on('change added', (event, list) => this.onChange(list as GridStackNode[]));
64+
65+
// sync initial actual valued rendered (in case init() had to merge conflicts)
66+
this.onChange();
7267

7368
/**
7469
* this is called when the list of items changes - get a list of nodes and
75-
* update the layout accordingly (which will take care of adding/removing/updating items to match DOM done by Angular)
70+
* update the layout accordingly (which will take care of adding/removing items changed by Angular)
7671
*/
7772
this.gridstackItems.changes.subscribe(() => {
7873
const layout: GridStackWidget[] = [];
@@ -84,10 +79,24 @@ export class AngularNgForTestComponent implements AfterViewInit {
8479
})
8580
}
8681

82+
/** Optional: called when given widgets are changed (moved/resized/added) - update our list to match.
83+
* Note this is not strictly necessary as demo works without this
84+
*/
85+
public onChange(list = this.grid.engine.nodes) {
86+
setTimeout(() => // prevent new 'added' items from ExpressionChangedAfterItHasBeenCheckedError. TODO: find cleaner way to sync outside Angular change detection ?
87+
list.forEach(n => {
88+
const item = this._items.find(i => i.id === n.id);
89+
if (item) Utils.copyPos(item, n);
90+
})
91+
, 0);
92+
}
93+
8794
/**
8895
* CRUD operations
8996
*/
9097
public add() {
98+
// new array doesn't seem required. Angular seem to detect changes to content...
99+
// this.items = [...this.items, { x: 3, y: 0, w: 3, id: String(ids++) }];
91100
this.items.push({ x: 3, y: 0, w: 3, id: String(ids++) });
92101
}
93102

@@ -96,13 +105,13 @@ export class AngularNgForTestComponent implements AfterViewInit {
96105
}
97106

98107
public change() {
99-
// this.items[0].w = 2; // this will not update GS internal data, only DOM values even thought array doesn't grow/shrink, so call GS update()
108+
// this.items[0]?.w = 2; // this will not update GS internal data, only DOM values even thought array doesn't grow/shrink. Need to call GS update()
100109
const n = this.grid.engine.nodes[0];
101110
if (n) this.grid.update(n.el!, { w: 2 });
102111
}
103112

104113
// ngFor unique node id to have correct match between our items used and GS
105-
identify(i: number, w: GridStackWidget) {
114+
identify(index: number, w: GridStackWidget) {
106115
return w.id;
107116
}
108117
}

demo/angular/src/app/ngFor_cmd.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { GridStack, GridStackWidget } from 'gridstack';
4242
`,
4343
],
4444
})
45-
export class AngularNgForTestComponent implements AfterViewInit {
45+
export class AngularNgForCmdTestComponent implements AfterViewInit {
4646
@ViewChildren("gridStackItem") gridstackItems!: QueryList<any>;
4747
@Input() public items: GridStackWidget[] = [
4848
{ x: 0, y: 0, w: 1, h: 1 },
@@ -71,13 +71,17 @@ export class AngularNgForTestComponent implements AfterViewInit {
7171
if (widgetToMake.action === "add") {
7272
this.grid.makeWidget(`#${widgetToMake.id}`);
7373
} else if (widgetToMake.action === "remove") {
74-
const removeEl = this.grid
75-
.getGridItems()
76-
.find((el) => el.id === `${widgetToMake.id}`);
77-
this.grid.removeWidget(removeEl!);
74+
const id = String(widgetToMake.id);
75+
// Note: DOM element has been removed by Angular already so look for it through the engine node list
76+
const removeEl = this.grid.engine.nodes.find((n) => n.el?.id === id)?.el;
77+
if (removeEl) this.grid.removeWidget(removeEl);
7878
}
7979
}
8080
);
81+
82+
// TODO: the problem with this code is that our items list does NOT reflect changes made by GS (user directly changing, or conflict)
83+
// and believe the other ngFor example (which does track changes) is also cleaner as it doesn't require special action commands
84+
// and track both changes to happen using zip().
8185
}
8286

8387
/**

demo/index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ <h1>Demos</h1>
3232
<li><a href="web2.html">Website demo 2</a></li>
3333
</ul>
3434
<h1>Angular src Demos</h1>
35-
<p>these are complete Angular projects with multiple options. use `yarn` and `yarn start` in demo/angular sub-project to build</p>
36-
<ul>
35+
<p>these are complete Angular projects with multiple options. use `yarn` and `yarn start` in demo/angular sub-project to run them</p>
36+
<p>You can find the complete source at <a href="https://github.com/gridstack/gridstack.js/tree/master/demo/angular" target="_blank">Angular Demo</a></p>
37+
<ol>
3738
<li><a href="angular/src/app/simple.ts">simple.ts</a></li>
3839
<li><a href="angular/src/app/ngFor.ts">ngFor.ts</a></li>
39-
</ul>
40+
</ol>
4041
<h1>Old v5.1.1 Jquery Demos</h1>
41-
Note: those are no longer supported, and use a old version of the lib.
42+
Note: those are no longer supported, and use an old version of the lib to compare functionality.
4243
<ul>
4344
<li><a href="old_two-jq.html">Two grids</a></li>
4445
<li><a href="old_nested-jq.html">Nested grids</a></li>

0 commit comments

Comments
 (0)