@@ -4,6 +4,7 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
4
4
edges : '<' ,
5
5
force : '<?' ,
6
6
radius : '<?' ,
7
+ selectedNode : '<?'
7
8
} ,
8
9
templateUrl : 'charts/topology-map.html' ,
9
10
controller : function ( $element ) {
@@ -19,18 +20,31 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
19
20
var transform ;
20
21
var options ;
21
22
var radius = ctrl . radius || 17 ;
22
-
23
- ctrl . scale = 1 ;
23
+ var draggedNode ;
24
24
ctrl . canvas ;
25
25
ctrl . showLabels = false ;
26
26
27
27
this . $onInit = function ( ) {
28
28
setUpCanvas ( ) ;
29
29
setUpForce ( ) ;
30
30
setUpZoom ( ) ;
31
- setUpClick ( ) ;
31
+ setUpDrag ( ) ;
32
+ setUpSelection ( ) ;
32
33
} ;
33
34
35
+ function setUpSelection ( ) {
36
+ var canvas = d3 . select ( ctrl . canvas ) ;
37
+ canvas . on ( 'click' , click ) ;
38
+
39
+ function click ( ) {
40
+ if ( d3 . event . defaultPrevented ) {
41
+ return ;
42
+ }
43
+ console . log ( 'event: ' , d3 . event ) ;
44
+ ctrl . selectedNode = findNode ( ( d3 . event . offsetX - ctrl . translate [ 0 ] ) / ctrl . scale , ( d3 . event . offsetY - ctrl . translate [ 1 ] ) / ctrl . scale ) ;
45
+ }
46
+ }
47
+
34
48
function setUpCanvas ( ) {
35
49
var coords ;
36
50
options = { force : ctrl . force , radius : ctrl . radius } ;
@@ -49,10 +63,46 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
49
63
ctrl . context . font = '17px FontAwesome' ;
50
64
}
51
65
52
- function setUpClick ( ) {
53
- d3 . select ( ctrl . canvas ) . on ( 'click' , function ( ) {
54
- console . log ( 'click: ' , d3 . select ( this ) ) ;
66
+ function findNode ( x , y ) {
67
+ var result = undefined ;
68
+ ctrl . force . nodes ( ) . forEach ( function ( node ) {
69
+ if ( Math . pow ( ( x - node . x ) , 2 ) + Math . pow ( ( y - node . y ) , 2 ) < Math . pow ( node . size , 2 ) ) {
70
+ result = node ;
71
+ ctrl . draggedNode = node ;
72
+ }
55
73
} ) ;
74
+ return result ;
75
+ }
76
+
77
+ function setUpDrag ( ) {
78
+ var drag = d3 . behavior . drag ( ) ;
79
+ var canvas = d3 . select ( ctrl . canvas ) ;
80
+ canvas . call ( drag . on ( 'dragstart' , onDragStart ) . on ( 'drag' , onDrag ) . on ( 'dragend' , onDragEnd ) ) ;
81
+ function onDragStart ( ) {
82
+ d3 . event . sourceEvent . stopPropagation ( ) ;
83
+ ctrl . draggedNode = findNode ( ( d3 . event . sourceEvent . offsetX - ctrl . translate [ 0 ] ) / ctrl . scale , ( d3 . event . sourceEvent . offsetY - ctrl . translate [ 1 ] ) / ctrl . scale ) ;
84
+ }
85
+ function onDrag ( ) {
86
+ var newX = ( d3 . event . sourceEvent . offsetX - ctrl . translate [ 0 ] ) / ctrl . scale ;
87
+ var newY = ( d3 . event . sourceEvent . offsetY - ctrl . translate [ 1 ] ) / ctrl . scale ;
88
+ if ( ctrl . draggedNode ) {
89
+ ctrl . draggedNode . x = newX ;
90
+ ctrl . draggedNode . y = newY ;
91
+ ctrl . draggedNode . px = newX ;
92
+ ctrl . draggedNode . py = newY ;
93
+ ctrl . draggedNode . fixed = true ;
94
+ ctrl . force . nodes ( ) [ ctrl . draggedNode . index ] = ctrl . draggedNode ;
95
+ ctrl . force . start ( ) ;
96
+ }
97
+ }
98
+ function onDragEnd ( ) {
99
+ if ( ctrl . draggedNode ) {
100
+ ctrl . force . nodes ( ) [ ctrl . draggedNode . index ] = ctrl . draggedNode ;
101
+ ctrl . draggedNode = undefined ;
102
+ ctrl . force . on ( 'tick' ) ( ) ;
103
+ //ctrl.force.start();
104
+ }
105
+ }
56
106
}
57
107
58
108
function setUpZoom ( ) {
@@ -68,15 +118,22 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
68
118
function zoomed ( ) {
69
119
var t = zoom . translate ( ) ;
70
120
var s = zoom . scale ( ) ;
71
- ctrl . scale = s ;
72
-
121
+ if ( ctrl . draggedNode ) {
122
+ return ;
123
+ }
73
124
ctrl . context . clearRect ( 0 , 0 , ctrl . canvasW , ctrl . canvasH ) ;
74
-
125
+ ctrl . translate = t ;
126
+ ctrl . scale = s ;
127
+ ctrl . force . stop ( ) ;
75
128
ctrl . context . save ( ) ;
76
129
ctrl . context . translate ( t [ 0 ] , t [ 1 ] ) ;
77
- ctrl . context . scale ( s , s ) ;
78
130
ctrl . force . on ( 'tick' ) ( ) ;
79
131
ctrl . context . restore ( ) ;
132
+ if ( ctrl . scale === 1 ) {
133
+ ctrl . context . translate ( 0 , 0 ) ;
134
+ ctrl . translate = [ 0 , 0 ] ;
135
+ ctrl . force . start ( ) ;
136
+ }
80
137
}
81
138
}
82
139
@@ -93,23 +150,27 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
93
150
. links ( ctrl . edges )
94
151
. on ( 'tick' , tick )
95
152
. start ( ) ;
153
+
96
154
function tick ( ) {
97
155
ctrl . context . clearRect ( 0 , 0 , ctrl . canvasW , ctrl . canvasH ) ;
98
156
drawEdges ( ) ;
99
157
drawNodes ( ) ;
158
+ ctrl . context . save ( ) ;
159
+ ctrl . context . translate ( ctrl . translate [ 0 ] , ctrl . translate [ 1 ] ) ;
160
+ ctrl . context . restore ( ) ;
100
161
if ( ctrl . scale !== 1 ) {
101
162
drawMiniMap ( ) ;
102
163
}
103
164
}
104
165
}
105
166
106
167
function drawEdges ( ) {
107
- ctrl . context . strokeStyle = "#ccc" ;
168
+ ctrl . context . strokeStyle = 'rgba(150, 150, 150, 0.6)' ;
108
169
ctrl . context . beginPath ( ) ;
109
170
ctrl . edges . forEach ( function ( d ) {
110
171
ctrl . context . lineWidth = 1 ;
111
- ctrl . context . moveTo ( d . source . x , d . source . y ) ;
112
- ctrl . context . lineTo ( d . target . x , d . target . y ) ;
172
+ ctrl . context . moveTo ( ctrl . scale * d . source . x , ctrl . scale * d . source . y ) ;
173
+ ctrl . context . lineTo ( ctrl . scale * d . target . x , ctrl . scale * d . target . y ) ;
113
174
} ) ;
114
175
ctrl . context . stroke ( ) ;
115
176
}
@@ -121,19 +182,18 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
121
182
}
122
183
123
184
function drawNodes ( ) {
124
- // draw circle
125
185
ctrl . nodes . forEach ( function ( node ) {
126
186
normalizeNode ( node ) ;
127
187
ctrl . context . beginPath ( ) ;
128
188
ctrl . context . fillStyle = node . fill || "steelblue" ;
129
189
ctrl . context . moveTo ( node . x , node . y ) ;
130
- ctrl . context . arc ( node . x , node . y , 17 , 0 , 2 * Math . PI ) ;
190
+ ctrl . context . arc ( ctrl . scale * node . x , ctrl . scale * node . y , 17 , 0 , 2 * Math . PI ) ;
131
191
ctrl . context . fill ( ) ;
132
192
133
193
if ( node . usage ) {
134
194
ctrl . context . beginPath ( ) ;
135
195
ctrl . context . lineWidth = 5 ;
136
- ctrl . context . arc ( node . x , node . y , node . size , 0 , ( ( node . usage / 100 ) * 2 ) * Math . PI ) ;
196
+ ctrl . context . arc ( ctrl . scale * node . x , ctrl . scale * node . y , node . size , 0 , ( ( node . usage / 100 ) * 2 ) * Math . PI ) ;
137
197
ctrl . context . strokeStyle = '#003300' ;
138
198
ctrl . context . stroke ( ) ;
139
199
}
@@ -143,7 +203,7 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
143
203
ctrl . context . fillStyle = 'white' ;
144
204
ctrl . context . textAlign = 'center' ;
145
205
ctrl . context . textBaseline = 'middle' ;
146
- ctrl . context . fillText ( node . icon || '\uF049' , node . x , node . y ) ;
206
+ ctrl . context . fillText ( node . icon || '\uF049' , ctrl . scale * node . x , ctrl . scale * node . y ) ;
147
207
ctrl . context . fill ( ) ;
148
208
} ) ;
149
209
}
0 commit comments