@@ -32,19 +32,25 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
32
32
setUpTooltips ( ) ;
33
33
} ;
34
34
35
+ this . $onChanges = function ( ) {
36
+ if ( ctrl . force ) {
37
+ setUpForce ( ) ;
38
+ }
39
+ } ;
40
+
35
41
function setUpTooltips ( ) {
36
42
var canvas = d3 . select ( ctrl . canvas ) ;
37
43
if ( ctrl . tooltipStyle ) {
38
44
ctrl . tooltipStyle = {
39
45
size : ctrl . tooltipStyle . size || 12 ,
40
- font : ctrl . tooltipStyle . font || 'Arial' ,
46
+ font : ctrl . tooltipStyle . font || '"Open Sans", Helvetica, Arial, sans-serif ' ,
41
47
textColor : ctrl . tooltipStyle . textColor || '#FFFFFF' ,
42
48
background : ctrl . tooltipStyle . background || 'rgba(0, 0 , 0, 0.5)'
43
49
} ;
44
50
} else {
45
51
ctrl . tooltipStyle = {
46
52
size : 12 ,
47
- font : 'Arial' ,
53
+ font : '"Open Sans", Helvetica, Arial, sans-serif ' ,
48
54
textColor : '#FFFFFF' ,
49
55
background : 'rgba(0, 0 , 0, 0.5)' ,
50
56
} ;
@@ -56,7 +62,7 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
56
62
if ( d3 . event . defaultPrevented || ctrl . draggedNode ) {
57
63
return ;
58
64
}
59
- ctrl . tooltip = findNode ( ( d3 . event . offsetX - ctrl . transform . x ) / ctrl . transform . k , ( d3 . event . offsetY - ctrl . transform . y ) / ctrl . transform . k ) ;
65
+ ctrl . tooltip = findNode . apply ( this , getRealCoordinates ( d3 . event . offsetX , d3 . event . offsetY ) ) ;
60
66
ctrl . force . on ( 'tick' ) ( ) ;
61
67
}
62
68
}
@@ -70,10 +76,9 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
70
76
if ( d3 . event . defaultPrevented ) {
71
77
return ;
72
78
}
73
- node = findNode ( ( d3 . event . offsetX - ctrl . transform . x ) / ctrl . transform . k , ( d3 . event . offsetY - ctrl . transform . y ) / ctrl . transform . k ) ;
79
+ node = findNode . apply ( this , getRealCoordinates ( d3 . event . offsetX , d3 . event . offsetY ) ) ;
74
80
if ( node ) {
75
81
node . fixed = ! node . fixed ;
76
- ctrl . force . nodes ( ) [ node . index ] = node ;
77
82
ctrl . selectNode ( node ) ;
78
83
ctrl . force . start ( ) ;
79
84
}
@@ -84,15 +89,12 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
84
89
var coords ;
85
90
ctrl . canvas = $element [ 0 ] . querySelector ( 'canvas.topology-graph' ) ;
86
91
coords = ctrl . canvas . getBoundingClientRect ( ) ;
87
- // Store the CSS computed width and the height of the canvas
88
92
ctrl . canvasW = ctrl . canvas . clientWidth ;
89
93
ctrl . canvasH = ctrl . canvas . clientHeight ;
90
- // We need to set to override the default canvas dimensions
91
94
ctrl . canvas . width = ctrl . canvasW ;
92
95
ctrl . canvas . height = ctrl . canvasH ;
93
96
ctrl . canvasX = coords . left ;
94
97
ctrl . canvasY = coords . top ;
95
- // Store the canvas' context
96
98
ctrl . context = ctrl . canvas . getContext ( '2d' ) ;
97
99
}
98
100
@@ -106,41 +108,42 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
106
108
return result ;
107
109
}
108
110
111
+ function getRealCoordinates ( x , y ) {
112
+ return [
113
+ ( x - ctrl . transform . x ) / ctrl . transform . k ,
114
+ ( y - ctrl . transform . y ) / ctrl . transform . k ,
115
+ ] ;
116
+ }
117
+
109
118
function setUpDrag ( ) {
110
119
var drag = d3 . behavior . drag ( ) ;
111
120
var canvas = d3 . select ( ctrl . canvas ) ;
112
121
canvas . call ( drag . on ( 'dragstart' , onDragStart ) . on ( 'drag' , onDrag ) . on ( 'dragend' , onDragEnd ) ) ;
113
122
function onDragStart ( ) {
114
123
ctrl . tooltip = undefined ;
115
124
d3 . event . sourceEvent . stopPropagation ( ) ;
116
- ctrl . draggedNode = findNode ( ( d3 . event . sourceEvent . offsetX - ctrl . transform . x ) / ctrl . transform . k , ( d3 . event . sourceEvent . offsetY - ctrl . transform . y ) / ctrl . transform . k ) ;
125
+ ctrl . draggedNode = findNode . apply ( this , getRealCoordinates ( d3 . event . sourceEvent . offsetX , d3 . event . sourceEvent . offsetY ) ) ;
117
126
}
118
127
function onDrag ( ) {
119
- var newX = ( d3 . event . sourceEvent . offsetX - ctrl . transform . x ) / ctrl . transform . k ;
120
- var newY = ( d3 . event . sourceEvent . offsetY - ctrl . transform . y ) / ctrl . transform . k ;
128
+ var newCoordinates = getRealCoordinates ( d3 . event . sourceEvent . offsetX , d3 . event . sourceEvent . offsetY ) ;
121
129
if ( ctrl . draggedNode ) {
122
130
d3 . event . sourceEvent . stopPropagation ( ) ;
123
- ctrl . draggedNode . px = newX ;
124
- ctrl . draggedNode . py = newY ;
131
+ ctrl . draggedNode . px = newCoordinates [ 0 ] ;
132
+ ctrl . draggedNode . py = newCoordinates [ 1 ] ;
125
133
ctrl . draggedNode . fixed = true ;
126
- ctrl . force . nodes ( ) [ ctrl . draggedNode . index ] = ctrl . draggedNode ;
127
134
ctrl . force . start ( ) ;
128
135
}
129
136
}
130
137
function onDragEnd ( ) {
131
138
d3 . event . sourceEvent . stopPropagation ( ) ;
132
- if ( ctrl . draggedNode ) {
133
- ctrl . force . nodes ( ) [ ctrl . draggedNode . index ] = ctrl . draggedNode ;
134
- ctrl . draggedNode = undefined ;
135
- }
139
+ ctrl . draggedNode = undefined ;
136
140
}
137
141
}
138
142
139
143
function setUpSemanticZoom ( ) {
140
144
var canvas = d3 . select ( ctrl . canvas )
141
145
. call ( ctrl . zoom . on ( "zoom" , semanticZoom ) ) ;
142
146
function semanticZoom ( ) {
143
- var newTransform ;
144
147
var translateX ;
145
148
var translateY ;
146
149
if ( ctrl . draggedNode ) {
@@ -154,12 +157,11 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
154
157
translateY = Math . min ( 0 , Math . max ( ctrl . zoom . translate ( ) [ 1 ] , ctrl . canvasH - ctrl . canvasH * ctrl . zoom . scale ( ) ) ) ;
155
158
}
156
159
ctrl . zoom . translate ( [ translateX , translateY ] ) ;
157
- newTransform = {
160
+ ctrl . transform = {
158
161
x : translateX ,
159
162
y : translateY ,
160
163
k : ctrl . zoom . scale ( ) ,
161
164
} ;
162
- ctrl . transform = newTransform ;
163
165
draw ( ) ;
164
166
}
165
167
}
@@ -192,8 +194,18 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
192
194
. chargeDistance ( 400 )
193
195
. gravity ( 0 )
194
196
. linkDistance ( 100 )
195
- . linkStrength ( 0.4 )
197
+ . linkStrength ( 1 )
196
198
. size ( [ ctrl . canvasW , ctrl . canvasH ] ) ;
199
+
200
+ ctrl . edges . forEach ( function ( edge ) {
201
+ edge . source = _ . findIndex ( ctrl . nodes , function ( node ) {
202
+ return node . id === edge . source ;
203
+ } ) ;
204
+ edge . target = _ . findIndex ( ctrl . nodes , function ( node ) {
205
+ return node . id === edge . target ;
206
+ } ) ;
207
+ } ) ;
208
+
197
209
ctrl . force . nodes ( ctrl . nodes )
198
210
. links ( ctrl . edges )
199
211
. on ( 'tick' , tick )
@@ -205,14 +217,14 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
205
217
}
206
218
207
219
function drawEdges ( ) {
208
- var cx , cy ;
209
220
var quadtree = d3 . geom . quadtree ( ctrl . force . nodes ( ) ) ;
210
221
ctrl . context . strokeStyle = 'rgba(150, 150, 150, 0.6)' ;
211
222
ctrl . context . lineWidth = 1 ;
212
223
ctrl . edges . forEach ( function ( d ) {
213
224
var sourceCoords ;
214
225
var targetCoords ;
215
- quadtree . visit ( collide ( d ) ) ;
226
+ quadtree . visit ( collide ( d . source ) ) ;
227
+ quadtree . visit ( collide ( d . target ) ) ;
216
228
sourceCoords = transformApply ( d . source . x , d . source . y ) ;
217
229
targetCoords = transformApply ( d . target . x , d . target . y ) ;
218
230
ctrl . context . beginPath ( ) ;
@@ -241,7 +253,7 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
241
253
coordinates = transformApply ( node . x , node . y ) ;
242
254
ctrl . context . beginPath ( ) ;
243
255
ctrl . context . fillStyle = node . fill || "#FFFFFF" ;
244
- ctrl . context . strokeStyle = node . boderColor || '#000000' ;
256
+ ctrl . context . strokeStyle = node . borderColor || '#000000' ;
245
257
ctrl . context . lineWidth = 1 ;
246
258
ctrl . context . arc ( coordinates . x , coordinates . y , node . size , 0 , 2 * Math . PI ) ;
247
259
ctrl . context . fill ( ) ;
@@ -320,14 +332,12 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
320
332
ctrl . cachedIcons . unknown . char = window . getComputedStyle ( tmp , ':before' ) . content . replace ( / ' | " / g, '' ) ;
321
333
ctrl . cachedIcons . unknown . font = window . getComputedStyle ( tmp , ':before' ) . fontFamily ;
322
334
ctrl . nodes . forEach ( function ( node ) {
323
- if ( node . iconType === 'fonticon' ) {
324
- if ( ! ctrl . cachedIcons [ node . iconClass ] ) {
325
- ctrl . cachedIcons [ node . iconClass ] = { } ;
326
- tmp . className = 'hidden ' + node . iconClass ;
327
- ctrl . cachedIcons [ node . iconClass ] . char = window . getComputedStyle ( tmp , ':before' ) . content . replace ( / ' | " / g, '' ) ;
328
- ctrl . cachedIcons [ node . iconClass ] . font = window . getComputedStyle ( tmp , ':before' ) . fontFamily ;
329
- }
330
- } else if ( node . iconType === 'fileicon' ) {
335
+ if ( node . iconType === 'fonticon' && ! ctrl . cachedIcons [ node . iconClass ] ) {
336
+ ctrl . cachedIcons [ node . iconClass ] = { } ;
337
+ tmp . className = 'hidden ' + node . iconClass ;
338
+ ctrl . cachedIcons [ node . iconClass ] . char = window . getComputedStyle ( tmp , ':before' ) . content . replace ( / ' | " / g, '' ) ;
339
+ ctrl . cachedIcons [ node . iconClass ] . font = window . getComputedStyle ( tmp , ':before' ) . fontFamily ;
340
+ } else if ( node . iconType === 'fileicon' && ! ctrl . cachedIcons [ node . path ] ) {
331
341
ctrl . cachedIcons [ node . path ] = { } ;
332
342
promises . push ( new Promise ( function ( resolve , reject ) {
333
343
ctrl . cachedIcons [ node . path ] . img = new Image ( ) ;
@@ -338,10 +348,11 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
338
348
} ) ) ;
339
349
}
340
350
} ) ;
351
+ document . body . removeChild ( tmp ) ;
341
352
}
342
353
343
354
function collide ( node ) {
344
- var r = node . size + 16 ,
355
+ var r = node . size + 22 ,
345
356
nx1 = node . x - r ,
346
357
nx2 = node . x + r ,
347
358
ny1 = node . y - r ,
0 commit comments