Skip to content

Commit 06f1c2c

Browse files
committed
add DiagramComponent.clear method
1 parent 3a91da1 commit 06f1c2c

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

projects/gojs-angular/src/lib/diagram.component.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class DiagramComponent {
1818
/** Link data for diagram. Optional. */
1919
@Input() public linkDataArray: Array<go.ObjectData> = null;
2020
/** Model data for diagram. Optional. */
21-
@Input() public modelData: go.ObjectData = null;
21+
@Input() public modelData: go.ObjectData = null;
2222
/** Diagram div class name. Use this name to style your diagram in CSS. */
2323
@Input() public divClassName: string;
2424
/** Model changed listener function for diagram */
@@ -31,6 +31,8 @@ export class DiagramComponent {
3131
@ViewChild('ngDiagram', { static: true }) public diagramDiv: ElementRef;
3232
/** The Diagram itself */
3333
public diagram: go.Diagram = null;
34+
/** An internal flag used to tell ngOnChanges to treat the next sync operation as a Diagram initialization */
35+
private wasCleared = false;
3436

3537
constructor(public zone: NgZone) { }
3638

@@ -44,7 +46,7 @@ export class DiagramComponent {
4446
this.diagram = this.initDiagram();
4547
if (!(this.diagram instanceof go.Diagram)) {
4648
throw new Error("initDiagram function did not return a go.Diagram");
47-
}
49+
}
4850

4951
// reduces change detection on mouse moves, boosting performance
5052
NgDiagramHelper.makeMouseMoveRunOutsideAngularZone(this.diagram, this.zone);
@@ -62,15 +64,39 @@ export class DiagramComponent {
6264
} // end ngAfterViewInit
6365

6466
/**
65-
* If a change has occured on an @Input property, merge the app-level changes with GoJS
67+
* If a change has occurred on an @Input property, merge the app-level changes with GoJS
6668
*/
6769
public ngOnChanges() {
6870
if (!this.diagram || !this.diagram.model || this.skipsDiagramUpdate) return;
69-
NgDiagramHelper.mergeAppDataWithModel(this);
71+
// if clear was just called, treat this as initial
72+
if (this.wasCleared) {
73+
this.diagram.delayInitialization(() => {
74+
NgDiagramHelper.mergeAppDataWithModel(this, true);
75+
});
76+
this.wasCleared = false;
77+
} else {
78+
NgDiagramHelper.mergeAppDataWithModel(this);
79+
}
7080
} // end ngOnChanges
7181

82+
/**
83+
* Clears the diagram of all nodes, links, and model data.
84+
* Also clears the UndoManager history and clipboard.
85+
* The next state update will be treated as diagram initialization.
86+
*/
87+
public clear(): void {
88+
const diagram = this.diagram;
89+
if (diagram !== null) {
90+
diagram.clear();
91+
this.wasCleared = true;
92+
}
93+
} // end clear function
94+
95+
/**
96+
* Set this.diagram.div to null, removing all event listeners
97+
*/
7298
public ngOnDestroy() {
7399
this.diagram.div = null; // removes event listeners
74-
}
100+
} // end ngOnDestroy function
75101

76102
}

projects/gojs-angular/src/lib/ng-diagram-helper.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import * as go from "gojs";
44
import { DiagramComponent } from "./diagram.component";
55

66
/**
7-
* An interface to allow methods defined below to accept Palette or Diagram Components,
7+
* An interface to allow methods defined below to accept Palette or Diagram Components,
88
* without requiring DiagramComponent or PaletteComponent directly in this file
99
* (that would create a circular dependency)
10-
*/
10+
*/
1111
export interface IDiagramOrPaletteComponent {
1212
modelChange: EventEmitter<go.IncrementalData>,
1313
zone: NgZone,
@@ -25,9 +25,10 @@ export class NgDiagramHelper{
2525
/**
2626
* Ensures mousemove event listeners on a diagram's canvas are run outside NgZone.
2727
* This way, change detection isn't triggered on each mousemove, improving performance.
28+
*
2829
* If some state-alteration must happen on a mousemove event inside the diagram, use zone.run() to make sure the event triggers angular change detection.
2930
* Used by DiagramComponent, PaletteComponent, and OverviewComponent in their ngAfterViewInit lifecycle hooks
30-
* @param diagram
31+
* @param diagram
3132
* @param zone
3233
*/
3334
public static makeMouseMoveRunOutsideAngularZone(diagram: go.Diagram, zone: NgZone) {
@@ -45,10 +46,10 @@ export class NgDiagramHelper{
4546

4647
/**
4748
* Initialize a given diagram's model with given node / link / model data
48-
* @param diagram
49-
* @param nodeDataArray
50-
* @param linkDataArray
51-
* @param modelData
49+
* @param diagram
50+
* @param nodeDataArray
51+
* @param linkDataArray
52+
* @param modelData
5253
*/
5354
public static initializeModel(diagram: go.Diagram | go.Palette, nodeDataArray: Array<go.ObjectData>, linkDataArray: Array<go.ObjectData>, modelData: go.ObjectData) {
5455
diagram.delayInitialization(() => {
@@ -74,7 +75,7 @@ export class NgDiagramHelper{
7475
var diagram = null;
7576
if (!(component.hasOwnProperty("diagram")) && !(component.hasOwnProperty("palette"))) return;
7677
if (component.hasOwnProperty("diagram")) diagram = component["diagram"];
77-
if (component.hasOwnProperty("palette")) diagram = component["palette"];
78+
if (component.hasOwnProperty("palette")) diagram = component["palette"];
7879
component.modelChangedListener = (e: go.ChangedEvent) => {
7980
if (e.isTransactionFinished && e.model && !e.model.isReadOnly && component.modelChange) {
8081
// this must be done within a NgZone.run block, so changes are detected in the parent component
@@ -89,28 +90,27 @@ export class NgDiagramHelper{
8990

9091
/**
9192
* Merge the app-level node / link / model data of a supplied Diagram|Palette Component with its underlying Diagram|Palette model data
92-
* @param component
93+
* @param component
94+
* @param isInit Whether or not to treat this update as a Diagram initialization
9395
*/
94-
public static mergeAppDataWithModel(component: IDiagramOrPaletteComponent) {
96+
public static mergeAppDataWithModel(component: IDiagramOrPaletteComponent, isInit?: boolean) {
9597
var diagram = null;
9698
if (component.hasOwnProperty("diagram")) diagram = component["diagram"];
97-
if (component.hasOwnProperty("palette")) diagram = component["palette"];
98-
// don't need model change listener while performing known data updates
99-
const mcl = component instanceof DiagramComponent ? component.modelChangedListener : null;
100-
if (mcl !== null) diagram.model.removeChangedListener(mcl);
99+
if (component.hasOwnProperty("palette")) diagram = component["palette"];
101100

102-
diagram.model.startTransaction('update data');
103-
// update modelData first, in case bindings on nodes / links depend on model data
104-
diagram.model.assignAllDataProperties(diagram.model.modelData, component.modelData);
105-
// merge node / link data
106-
diagram.model.mergeNodeDataArray(component.nodeDataArray);
107-
if (component.linkDataArray && diagram.model instanceof go.GraphLinksModel) {
108-
diagram.model.mergeLinkDataArray(component.linkDataArray);
109-
}
110-
diagram.model.commitTransaction('update data');
101+
diagram.model.commit((m: go.Model) => {
102+
if (isInit) diagram.model.modelData = {};
103+
// update modelData first, in case bindings on nodes / links depend on model data
104+
diagram.model.assignAllDataProperties(diagram.model.modelData, component.modelData);
105+
// merge node / link data
106+
if (isInit) diagram.model.nodeDataArray = [];
107+
diagram.model.mergeNodeDataArray(component.nodeDataArray);
108+
if (component.linkDataArray && diagram.model instanceof go.GraphLinksModel) {
109+
if (isInit) diagram.model.linkDataArray = [];
110+
diagram.model.mergeLinkDataArray(component.linkDataArray);
111+
}
112+
}, isInit ? null : 'update data');
111113

112-
// reset the model change listener
113-
if (mcl !== null) diagram.model.addChangedListener(mcl);
114114
}
115115

116-
}
116+
}

src/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</head>
1212

1313
<body>
14-
<app-root></app-root>
14+
<app2-root></app2-root>
1515
</body>
1616

17-
</html>
17+
</html>

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { enableProdMode } from '@angular/core';
22
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
33

4-
import { AppModule } from './app/app.module';
4+
import { AppModule } from './appv2/app.module';
55
import { environment } from './environments/environment';
66

77
if (environment.production) {

0 commit comments

Comments
 (0)