1
1
// 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
3
3
4
- /* Copyright (C) 1998-2015 by Northwoods Software Corporation. */
4
+ /* Copyright (C) 1998-2016 by Northwoods Software Corporation. */
5
5
6
6
/// <reference path="goJS.d.ts" />
7
7
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
+
8
85
function init ( ) {
9
86
var $ = go . GraphObject . make ; // for conciseness in defining templates
10
87
@@ -20,6 +97,8 @@ function init() {
20
97
// allow Ctrl-G to call groupSelection()
21
98
"commandHandler.archetypeGroupData" : { text : "Group" , isGroup : true , color : "blue" } ,
22
99
100
+ layout : $ ( CustomTreeLayout , { angle : 90 } ) ,
101
+
23
102
// enable undo & redo
24
103
"undoManager.isEnabled" : true
25
104
} ) ;
@@ -30,19 +109,19 @@ function init() {
30
109
31
110
// To simplify this code we define a function for creating a context menu button:
32
111
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 ;
34
113
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 ( ) : { } ) ;
39
118
}
40
119
41
120
// a context menu is an Adornment with a bunch of buttons in them
42
121
var partContextMenu =
43
122
$ ( go . Adornment , "Vertical" ,
44
123
makeButton ( "Properties" ,
45
- function ( e , obj ) { // the OBJ is this Button
124
+ ( e , obj ) => { // the OBJ is this Button
46
125
var contextmenu = < go . Adornment > obj . part ; // the Button is in the context menu Adornment
47
126
var part = contextmenu . adornedPart ; // the adornedPart is the Part that the context menu adorns
48
127
// now can do something with PART, or with its data, or with the Adornment (the context menu)
@@ -51,29 +130,29 @@ function init() {
51
130
else alert ( nodeInfo ( part . data ) ) ;
52
131
} ) ,
53
132
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 ( ) ) ,
56
135
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 ( ) ) ,
59
138
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 ( ) ) ,
62
141
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 ( ) ) ,
65
144
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 ( ) ) ,
68
147
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 ( ) ) ,
71
150
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 ( ) ) ,
74
153
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 ( ) )
77
156
) ;
78
157
79
158
function nodeInfo ( d ) { // Tooltip info for a node data object
@@ -120,7 +199,7 @@ function init() {
120
199
// this context menu Adornment is shared by all nodes
121
200
contextMenu : partContextMenu
122
201
}
123
- ) ;
202
+ ) ;
124
203
125
204
// Define the appearance and behavior for Links:
126
205
@@ -130,7 +209,7 @@ function init() {
130
209
131
210
// The link shape and arrowhead have their stroke brush data bound to the "color" property
132
211
myDiagram . linkTemplate =
133
- $ ( go . Link ,
212
+ $ ( CustomLink ,
134
213
{ relinkableFrom : true , relinkableTo : true } , // allow the user to relink existing links
135
214
$ ( go . Shape ,
136
215
{ strokeWidth : 2 } ,
@@ -148,17 +227,14 @@ function init() {
148
227
// the same context menu Adornment is shared by all links
149
228
contextMenu : partContextMenu
150
229
}
151
- ) ;
230
+ ) ;
152
231
153
232
// Define the appearance and behavior for Groups:
154
233
155
234
function groupInfo ( adornment : go . Adornment ) { // takes the tooltip, not a group node data object
156
235
var g = < go . Group > adornment . adornedPart ; // get the Group that the tooltip adorns
157
236
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 ;
162
238
return "Group " + g . data . key + ": " + g . data . text + "\n" + mems + " members including " + links + " links" ;
163
239
}
164
240
@@ -168,7 +244,8 @@ function init() {
168
244
$ ( go . Group , "Vertical" ,
169
245
{
170
246
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
172
249
} ,
173
250
$ ( go . TextBlock ,
174
251
{
@@ -195,7 +272,7 @@ function init() {
195
272
// the same context menu Adornment is shared by all groups
196
273
contextMenu : partContextMenu
197
274
}
198
- ) ;
275
+ ) ;
199
276
200
277
// Define the behavior for the Diagram background:
201
278
@@ -209,21 +286,21 @@ function init() {
209
286
$ ( go . Shape , { fill : "#FFFFCC" } ) ,
210
287
$ ( go . TextBlock , { margin : 4 } ,
211
288
new go . Binding ( "text" , "" , diagramInfo ) )
212
- ) ;
289
+ ) ;
213
290
214
291
// provide a context menu for the background of the Diagram, when not over any Part
215
292
myDiagram . contextMenu =
216
293
$ ( go . Adornment , "Vertical" ,
217
294
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 ( ) ) ,
220
297
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 ( ) ) ,
223
300
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
+ ) ;
227
304
228
305
// Create the Diagram's Model:
229
306
var nodeDataArray = [
@@ -240,4 +317,8 @@ function init() {
240
317
{ from : 3 , to : 1 , color : "purple" }
241
318
] ;
242
319
myDiagram . model = new go . GraphLinksModel ( nodeDataArray , linkDataArray ) ;
320
+
321
+ var img = myDiagram . makeImageData ( {
322
+ scale : 0.4 , position : new go . Point ( - 10 , - 10 )
323
+ } ) ;
243
324
}
0 commit comments