Skip to content

Commit a44529f

Browse files
committed
Merge pull request DefinitelyTyped#8497 from WalterNorthwoods/master
updated gojs definition and test files
2 parents d4121d7 + 1cc3795 commit a44529f

File tree

2 files changed

+662
-170
lines changed

2 files changed

+662
-170
lines changed

goJS/goJS-tests.ts

+122-41
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,87 @@
11
// Test file for goJS.d.ts
2-
// This is taken from http://gojs.net/latest/samples/basic.html
2+
// This is taken and adapted from http://gojs.net/latest/samples/basic.html
33

4-
/* Copyright (C) 1998-2015 by Northwoods Software Corporation. */
4+
/* Copyright (C) 1998-2016 by Northwoods Software Corporation. */
55

66
/// <reference path="goJS.d.ts" />
77

8+
class CustomLink extends go.Link {
9+
constructor() {
10+
super();
11+
this.routing = go.Link.Orthogonal;
12+
}
13+
14+
hasCurviness(): boolean {
15+
if (isNaN(this.curviness)) return true;
16+
return super.hasCurviness();
17+
}
18+
19+
computeCurviness(): number {
20+
if (isNaN(this.curviness)) {
21+
var links = this.fromNode.findLinksTo(this.toNode);
22+
if (links.count < 2) return 0;
23+
var i = 0;
24+
while (links.next()) { if (links.value === this) break; i++; }
25+
return 10 * (i - (links.count - 1) / 2);
26+
}
27+
return super.computeCurviness();
28+
}
29+
}
30+
31+
class CustomTreeLayout extends go.TreeLayout {
32+
constructor() {
33+
super();
34+
this.extraProp = 3;
35+
}
36+
37+
extraProp: number;
38+
39+
// override various methods
40+
41+
cloneProtected(copy: CustomTreeLayout): void {
42+
super.cloneProtected(copy);
43+
copy.extraProp = this.extraProp;
44+
}
45+
46+
createNetwork(): CustomTreeNetwork {
47+
return new CustomTreeNetwork();
48+
}
49+
50+
assignTreeVertexValues(v: CustomTreeVertex): void {
51+
super.assignTreeVertexValues(v);
52+
v.someProp = Math.random() * 100;
53+
}
54+
55+
commitNodes(): void {
56+
super.commitNodes();
57+
// ...
58+
}
59+
60+
commitLinks(): void {
61+
super.commitLinks();
62+
this.network.edges.each(e => { e.link.path.strokeWidth = (<CustomTreeEdge>(e)).anotherProp; });
63+
}
64+
}
65+
66+
class CustomTreeNetwork extends go.TreeNetwork {
67+
createVertex(): CustomTreeVertex {
68+
return new CustomTreeVertex();
69+
}
70+
71+
createEdge(): CustomTreeEdge {
72+
return new CustomTreeEdge();
73+
}
74+
}
75+
76+
class CustomTreeVertex extends go.TreeVertex {
77+
someProp: number = 17;
78+
}
79+
80+
class CustomTreeEdge extends go.TreeEdge {
81+
anotherProp: number = 1;
82+
}
83+
84+
885
function init() {
986
var $ = go.GraphObject.make; // for conciseness in defining templates
1087

@@ -20,6 +97,8 @@ function init() {
2097
// allow Ctrl-G to call groupSelection()
2198
"commandHandler.archetypeGroupData": { text: "Group", isGroup: true, color: "blue" },
2299

100+
layout: $(CustomTreeLayout, { angle: 90 }),
101+
23102
// enable undo & redo
24103
"undoManager.isEnabled": true
25104
});
@@ -30,19 +109,19 @@ function init() {
30109

31110
// To simplify this code we define a function for creating a context menu button:
32111
function makeButton(text: string, action: (e: go.InputEvent, obj: go.GraphObject) => void, visiblePredicate?: (obj: go.GraphObject) => boolean) {
33-
if (visiblePredicate === undefined) visiblePredicate = function (o) { return true; };
112+
if (visiblePredicate === undefined) visiblePredicate = o => true;
34113
return $("ContextMenuButton",
35-
$(go.TextBlock, text),
36-
{ click: action },
37-
// don't bother with binding GraphObject.visible if there's no predicate
38-
visiblePredicate ? new go.Binding("visible", "", visiblePredicate).ofObject() : {});
114+
$(go.TextBlock, text),
115+
{ click: action },
116+
// don't bother with binding GraphObject.visible if there's no predicate
117+
visiblePredicate ? new go.Binding("visible", "", visiblePredicate).ofObject() : {});
39118
}
40119

41120
// a context menu is an Adornment with a bunch of buttons in them
42121
var partContextMenu =
43122
$(go.Adornment, "Vertical",
44123
makeButton("Properties",
45-
function (e, obj) { // the OBJ is this Button
124+
(e, obj) => { // the OBJ is this Button
46125
var contextmenu = <go.Adornment>obj.part; // the Button is in the context menu Adornment
47126
var part = contextmenu.adornedPart; // the adornedPart is the Part that the context menu adorns
48127
// now can do something with PART, or with its data, or with the Adornment (the context menu)
@@ -51,29 +130,29 @@ function init() {
51130
else alert(nodeInfo(part.data));
52131
}),
53132
makeButton("Cut",
54-
function (e, obj) { e.diagram.commandHandler.cutSelection(); },
55-
function (o) { return o.diagram.commandHandler.canCutSelection(); }),
133+
(e, obj) => e.diagram.commandHandler.cutSelection(),
134+
o => o.diagram.commandHandler.canCutSelection()),
56135
makeButton("Copy",
57-
function (e, obj) { e.diagram.commandHandler.copySelection(); },
58-
function (o) { return o.diagram.commandHandler.canCopySelection(); }),
136+
(e, obj) => e.diagram.commandHandler.copySelection(),
137+
o => o.diagram.commandHandler.canCopySelection()),
59138
makeButton("Paste",
60-
function (e, obj) { e.diagram.commandHandler.pasteSelection(e.diagram.lastInput.documentPoint); },
61-
function (o) { return o.diagram.commandHandler.canPasteSelection(); }),
139+
(e, obj) => e.diagram.commandHandler.pasteSelection(e.diagram.lastInput.documentPoint),
140+
o => o.diagram.commandHandler.canPasteSelection()),
62141
makeButton("Delete",
63-
function (e, obj) { e.diagram.commandHandler.deleteSelection(); },
64-
function (o) { return o.diagram.commandHandler.canDeleteSelection(); }),
142+
(e, obj) => e.diagram.commandHandler.deleteSelection(),
143+
o => o.diagram.commandHandler.canDeleteSelection()),
65144
makeButton("Undo",
66-
function (e, obj) { e.diagram.commandHandler.undo(); },
67-
function (o) { return o.diagram.commandHandler.canUndo(); }),
145+
(e, obj) => e.diagram.commandHandler.undo(),
146+
o => o.diagram.commandHandler.canUndo()),
68147
makeButton("Redo",
69-
function (e, obj) { e.diagram.commandHandler.redo(); },
70-
function (o) { return o.diagram.commandHandler.canRedo(); }),
148+
(e, obj) => e.diagram.commandHandler.redo(),
149+
o => o.diagram.commandHandler.canRedo()),
71150
makeButton("Group",
72-
function (e, obj) { e.diagram.commandHandler.groupSelection(); },
73-
function (o) { return o.diagram.commandHandler.canGroupSelection(); }),
151+
(e, obj) => e.diagram.commandHandler.groupSelection(),
152+
o => o.diagram.commandHandler.canGroupSelection()),
74153
makeButton("Ungroup",
75-
function (e, obj) { e.diagram.commandHandler.ungroupSelection(); },
76-
function (o) { return o.diagram.commandHandler.canUngroupSelection(); })
154+
(e, obj) => e.diagram.commandHandler.ungroupSelection(),
155+
o => o.diagram.commandHandler.canUngroupSelection())
77156
);
78157

79158
function nodeInfo(d) { // Tooltip info for a node data object
@@ -120,7 +199,7 @@ function init() {
120199
// this context menu Adornment is shared by all nodes
121200
contextMenu: partContextMenu
122201
}
123-
);
202+
);
124203

125204
// Define the appearance and behavior for Links:
126205

@@ -130,7 +209,7 @@ function init() {
130209

131210
// The link shape and arrowhead have their stroke brush data bound to the "color" property
132211
myDiagram.linkTemplate =
133-
$(go.Link,
212+
$(CustomLink,
134213
{ relinkableFrom: true, relinkableTo: true }, // allow the user to relink existing links
135214
$(go.Shape,
136215
{ strokeWidth: 2 },
@@ -148,17 +227,14 @@ function init() {
148227
// the same context menu Adornment is shared by all links
149228
contextMenu: partContextMenu
150229
}
151-
);
230+
);
152231

153232
// Define the appearance and behavior for Groups:
154233

155234
function groupInfo(adornment: go.Adornment) { // takes the tooltip, not a group node data object
156235
var g = <go.Group>adornment.adornedPart; // get the Group that the tooltip adorns
157236
var mems = g.memberParts.count;
158-
var links = 0;
159-
g.memberParts.each(function (part) {
160-
if (part instanceof go.Link) links++;
161-
});
237+
var links = g.memberParts.filter(p => p instanceof go.Link).count;
162238
return "Group " + g.data.key + ": " + g.data.text + "\n" + mems + " members including " + links + " links";
163239
}
164240

@@ -168,7 +244,8 @@ function init() {
168244
$(go.Group, "Vertical",
169245
{
170246
selectionObjectName: "PANEL", // selection handle goes around shape, not label
171-
ungroupable: true // enable Ctrl-Shift-G to ungroup a selected Group
247+
ungroupable: true, // enable Ctrl-Shift-G to ungroup a selected Group
248+
layoutConditions: go.Part.LayoutStandard & ~go.Part.LayoutNodeSized
172249
},
173250
$(go.TextBlock,
174251
{
@@ -195,7 +272,7 @@ function init() {
195272
// the same context menu Adornment is shared by all groups
196273
contextMenu: partContextMenu
197274
}
198-
);
275+
);
199276

200277
// Define the behavior for the Diagram background:
201278

@@ -209,21 +286,21 @@ function init() {
209286
$(go.Shape, { fill: "#FFFFCC" }),
210287
$(go.TextBlock, { margin: 4 },
211288
new go.Binding("text", "", diagramInfo))
212-
);
289+
);
213290

214291
// provide a context menu for the background of the Diagram, when not over any Part
215292
myDiagram.contextMenu =
216293
$(go.Adornment, "Vertical",
217294
makeButton("Paste",
218-
function (e, obj) { e.diagram.commandHandler.pasteSelection(e.diagram.lastInput.documentPoint); },
219-
function (o) { return o.diagram.commandHandler.canPasteSelection(); }),
295+
(e, obj) => e.diagram.commandHandler.pasteSelection(e.diagram.lastInput.documentPoint),
296+
o => o.diagram.commandHandler.canPasteSelection()),
220297
makeButton("Undo",
221-
function (e, obj) { e.diagram.commandHandler.undo(); },
222-
function (o) { return o.diagram.commandHandler.canUndo(); }),
298+
(e, obj) => e.diagram.commandHandler.undo(),
299+
o => o.diagram.commandHandler.canUndo()),
223300
makeButton("Redo",
224-
function (e, obj) { e.diagram.commandHandler.redo(); },
225-
function (o) { return o.diagram.commandHandler.canRedo(); })
226-
);
301+
(e, obj) => e.diagram.commandHandler.redo(),
302+
o => o.diagram.commandHandler.canRedo())
303+
);
227304

228305
// Create the Diagram's Model:
229306
var nodeDataArray = [
@@ -240,4 +317,8 @@ function init() {
240317
{ from: 3, to: 1, color: "purple" }
241318
];
242319
myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
320+
321+
var img = myDiagram.makeImageData({
322+
scale: 0.4, position: new go.Point(-10, -10)
323+
});
243324
}

0 commit comments

Comments
 (0)