Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1 | /*global d3*/ |
| 2 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 3 | d3.selection.prototype.moveToFront = function() { |
| 4 | return this.each(function(){ |
| 5 | this.parentNode.appendChild(this); |
| 6 | }); |
| 7 | }; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 8 | |
| 9 | var colors = [ |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 10 | 'color1', |
| 11 | 'color2', |
| 12 | 'color3', |
| 13 | 'color4', |
| 14 | 'color5', |
| 15 | 'color6', |
| 16 | 'color7', |
| 17 | 'color8', |
| 18 | 'color9', |
| 19 | 'color10', |
| 20 | 'color11', |
| 21 | 'color12', |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 22 | ] |
Paul Greyson | 01a5dff | 2013-03-19 15:50:14 -0700 | [diff] [blame] | 23 | colors.reverse(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 24 | |
| 25 | var controllerColorMap = {}; |
| 26 | |
| 27 | |
| 28 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 29 | function createTopologyView() { |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 30 | var svg = d3.select('#svg-container').append('svg:svg'); |
| 31 | |
| 32 | svg.append("svg:defs").append("svg:marker") |
| 33 | .attr("id", "arrow") |
| 34 | .attr("viewBox", "0 -5 10 10") |
| 35 | .attr("refX", -1) |
| 36 | .attr("markerWidth", 5) |
| 37 | .attr("markerHeight", 5) |
| 38 | .attr("orient", "auto") |
| 39 | .append("svg:path") |
Paul Greyson | 45303ac | 2013-03-23 16:44:01 -0700 | [diff] [blame] | 40 | .attr("d", "M0,-3L10,0L0,3"); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 41 | |
| 42 | return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none'). |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 43 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 46 | function updateHeader(model) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 47 | d3.select('#lastUpdate').text(new Date()); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 48 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 49 | d3.select('#activeFlows').text(model.flows.length); |
| 50 | } |
| 51 | |
| 52 | function toRadians (angle) { |
| 53 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 56 | function createRingsFromModel(model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 57 | var rings = [{ |
| 58 | radius: 3, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 59 | width: 6, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 60 | switches: model.edgeSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 61 | className: 'edge', |
| 62 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 63 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 64 | radius: 2.25, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 65 | width: 12, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 66 | switches: model.aggregationSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 67 | className: 'aggregation', |
| 68 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 69 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 70 | radius: .75, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 71 | width: 18, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 72 | switches: model.coreSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 73 | className: 'core', |
| 74 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 75 | }]; |
| 76 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 77 | |
| 78 | var aggRanges = {}; |
| 79 | |
| 80 | // arrange edge switches at equal increments |
| 81 | var k = 360 / rings[0].switches.length; |
| 82 | rings[0].switches.forEach(function (s, i) { |
| 83 | var angle = k * i; |
| 84 | |
| 85 | rings[0].angles[i] = angle; |
| 86 | |
| 87 | // record the angle for the agg switch layout |
| 88 | var dpid = s.dpid.split(':'); |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 89 | dpid[7] = '01'; // the last component of the agg switch is always '01' |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 90 | var aggdpid = dpid.join(':'); |
| 91 | var aggRange = aggRanges[aggdpid]; |
| 92 | if (!aggRange) { |
| 93 | aggRange = aggRanges[aggdpid] = {}; |
| 94 | aggRange.min = aggRange.max = angle; |
| 95 | } else { |
| 96 | aggRange.max = angle; |
| 97 | } |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 98 | }); |
| 99 | |
| 100 | // arrange aggregation switches to "fan out" to edge switches |
| 101 | k = 360 / rings[1].switches.length; |
| 102 | rings[1].switches.forEach(function (s, i) { |
| 103 | // rings[1].angles[i] = k * i; |
| 104 | var range = aggRanges[s.dpid]; |
| 105 | |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 106 | rings[1].angles[i] = (range.min + range.max)/2; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 107 | }); |
| 108 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 109 | // find the association between core switches and aggregation switches |
| 110 | var aggregationSwitchMap = {}; |
| 111 | model.aggregationSwitches.forEach(function (s, i) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 112 | aggregationSwitchMap[s.dpid] = i; |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 113 | }); |
| 114 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 115 | // put core switches next to linked aggregation switches |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 116 | k = 360 / rings[2].switches.length; |
| 117 | rings[2].switches.forEach(function (s, i) { |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 118 | // rings[2].angles[i] = k * i; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 119 | var associatedAggregationSwitches = model.configuration.association[s.dpid]; |
| 120 | // TODO: go between if there are multiple |
| 121 | var index = aggregationSwitchMap[associatedAggregationSwitches[0]]; |
| 122 | |
| 123 | rings[2].angles[i] = rings[1].angles[index]; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 124 | }); |
| 125 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 126 | // TODO: construct this form initially rather than converting. it works better because |
| 127 | // it allows binding by dpid |
| 128 | var testRings = []; |
| 129 | rings.forEach(function (ring) { |
| 130 | var testRing = []; |
| 131 | ring.switches.forEach(function (s, i) { |
| 132 | var testSwitch = { |
| 133 | dpid: s.dpid, |
| 134 | state: s.state, |
| 135 | radius: ring.radius, |
| 136 | width: ring.width, |
| 137 | className: ring.className, |
| 138 | angle: ring.angles[i], |
| 139 | controller: s.controller |
| 140 | } |
| 141 | testRing.push(testSwitch); |
| 142 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 143 | |
| 144 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 145 | testRings.push(testRing); |
| 146 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 147 | |
| 148 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 149 | // return rings; |
| 150 | return testRings; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | function updateTopology(svg, model) { |
| 154 | |
| 155 | // DRAW THE SWITCHES |
| 156 | var rings = svg.selectAll('.ring').data(createRingsFromModel(model)); |
| 157 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 158 | function ringEnter(data, i) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 159 | if (!data.length) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 163 | // create the nodes |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 164 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 165 | .data(data, function (data) { |
| 166 | return data.dpid; |
| 167 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 168 | .enter().append("svg:g") |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 169 | .classed('nolabel', true) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 170 | .attr("id", function (data, i) { |
| 171 | return data.dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 172 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 173 | .attr("transform", function(data, i) { |
| 174 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 175 | }); |
| 176 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 177 | // add the cirles representing the switches |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 178 | nodes.append("svg:circle") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 179 | .attr("transform", function(data, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 180 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 181 | if (data.scale) { |
| 182 | m = m.scale(data.scale); |
| 183 | } |
| 184 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 185 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 186 | .attr("x", function (data) { |
| 187 | return -data.width / 2; |
| 188 | }) |
| 189 | .attr("y", function (data) { |
| 190 | return -data.width / 2; |
| 191 | }) |
| 192 | .attr("r", function (data) { |
| 193 | return data.width; |
| 194 | }) |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 195 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 196 | // setup the mouseover behaviors |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 197 | function showLabel(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 198 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | function hideLabel(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 202 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | nodes.on('mouseover', showLabel); |
| 206 | nodes.on('mouseout', hideLabel); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 209 | // append switches |
| 210 | rings.enter().append("svg:g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 211 | .attr("class", "ring") |
| 212 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 213 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 214 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 215 | function ringUpdate(data, i) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 216 | nodes = d3.select(this).selectAll("g") |
| 217 | .data(data, function (data) { |
| 218 | return data.dpid; |
| 219 | }) |
| 220 | nodes.select('circle').attr('class', function (data, i) { |
| 221 | if (data.state == 'ACTIVE') { |
| 222 | return data.className + ' ' + controllerColorMap[data.controller]; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 223 | } else { |
| 224 | return data.className + ' ' + 'colorInactive'; |
| 225 | } |
| 226 | }) |
| 227 | } |
| 228 | |
| 229 | // update switches |
| 230 | rings.each(ringUpdate); |
| 231 | |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 232 | |
| 233 | // Now setup the labels |
| 234 | // This is done separately because SVG draws in node order and we want the labels |
| 235 | // always on top |
| 236 | var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model)); |
| 237 | |
| 238 | function labelRingEnter(data, i) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 239 | if (!data.length) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | // create the nodes |
| 244 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 245 | .data(data, function (data) { |
| 246 | return data.dpid; |
| 247 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 248 | .enter().append("svg:g") |
| 249 | .classed('nolabel', true) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 250 | .attr("id", function (data, i) { |
| 251 | return data.dpid + '-label'; |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 252 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 253 | .attr("transform", function(data, i) { |
| 254 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 255 | }); |
| 256 | |
| 257 | // add the text nodes which show on mouse over |
| 258 | nodes.append("svg:text") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 259 | .text(function (data, i) {return data.dpid}) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 260 | .attr("x", 0) |
| 261 | .attr("y", 0) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 262 | .attr("transform", function(data, i) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 263 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 264 | if (data.scale) { |
| 265 | m = m.scale(data.scale); |
| 266 | } |
| 267 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 268 | }) |
| 269 | } |
| 270 | |
| 271 | labelRings.enter().append("svg:g") |
| 272 | .attr("class", "textRing") |
| 273 | .each(labelRingEnter); |
| 274 | |
| 275 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 276 | // switches should not change during operation of the ui so no |
| 277 | // rings.exit() |
| 278 | |
| 279 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 280 | // do mouseover zoom on edge nodes |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 281 | function zoom(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 282 | var g = d3.select(document.getElementById(data.dpid)).select('circle'); |
| 283 | g.transition().duration(100).attr("r", data.width*3); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 284 | // TODO: this doesn't work because the data binding is by index |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 285 | d3.select(this.parentNode).moveToFront(); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 286 | } |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 287 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 288 | svg.selectAll('.edge').on('mouseover', zoom); |
| 289 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 290 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 291 | function unzoom(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 292 | var g = d3.select(document.getElementById(data.dpid)).select('circle'); |
| 293 | g.transition().duration(100).attr("r", data.width); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 294 | } |
| 295 | svg.selectAll('.edge').on('mouseout', unzoom); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 296 | |
| 297 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 298 | // DRAW THE LINKS |
| 299 | var line = d3.svg.line() |
| 300 | .x(function(d) { |
| 301 | return d.x; |
| 302 | }) |
| 303 | .y(function(d) { |
| 304 | return d.y; |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 305 | }); |
| 306 | // .interpolate("basis"); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 307 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 308 | // key on link dpids since these will come/go during demo |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 309 | var links = d3.select('svg').selectAll('.link').data(model.links, function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 310 | return d['src-switch']+'->'+d['dst-switch']; |
| 311 | }); |
| 312 | |
| 313 | // add new links |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 314 | links.enter().append("svg:path") |
| 315 | .attr("class", "link") |
| 316 | .attr("d", function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 317 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 318 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 319 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 320 | |
| 321 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 322 | srcPt.x = src.attr('x'); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 323 | srcPt.y = src.attr('y'); |
| 324 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 325 | |
| 326 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 327 | dstPt.x = dst.attr('x'); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 328 | dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable |
| 329 | dstPt = dstPt.matrixTransform(dst[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 330 | |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 331 | var midPt = document.querySelector('svg').createSVGPoint(); |
| 332 | midPt.x = (srcPt.x + dstPt.x)/2; |
| 333 | midPt.y = (srcPt.y + dstPt.y)/2; |
| 334 | |
| 335 | return line([srcPt, midPt, dstPt]); |
| 336 | }) |
| 337 | .attr("marker-mid", function(d) { return "url(#arrow)"; }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 338 | |
| 339 | // remove old links |
| 340 | links.exit().remove(); |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame^] | 341 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | function updateControllers(model) { |
| 345 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 346 | controllers.enter().append('div') |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 347 | .each(function (c) { |
| 348 | controllerColorMap[c] = colors.pop(); |
| 349 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 350 | }) |
| 351 | .text(function (d) { |
| 352 | return d; |
| 353 | }); |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 354 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 355 | controllers.attr('class', function (d) { |
Paul Greyson | eed3635 | 2013-03-23 11:19:11 -0700 | [diff] [blame] | 356 | var color = 'colorInactive'; |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 357 | if (model.activeControllers.indexOf(d) != -1) { |
| 358 | color = controllerColorMap[d]; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 359 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 360 | var className = 'controller ' + color; |
| 361 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 362 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 363 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 364 | // this should never be needed |
| 365 | // controllers.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 366 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 367 | controllers.on('click', function (c, index) { |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 368 | var allSelected = true; |
| 369 | for (var key in controllerColorMap) { |
| 370 | if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) { |
| 371 | allSelected = false; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | if (allSelected) { |
| 376 | for (var key in controllerColorMap) { |
| 377 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c) |
| 378 | } |
| 379 | } else { |
| 380 | for (var key in controllerColorMap) { |
| 381 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', true) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 386 | // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 387 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 390 | var oldModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 391 | function sync(svg) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 392 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 393 | updateModel(function (newModel) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 394 | console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 395 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 396 | if (true || !oldModel || JSON.stringify(oldModel) != JSON.stringify(newModel)) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 397 | updateControllers(newModel); |
| 398 | updateTopology(svg, newModel); |
| 399 | } else { |
| 400 | console.log('no change'); |
| 401 | } |
| 402 | updateHeader(newModel); |
| 403 | |
| 404 | oldModel = newModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 405 | |
| 406 | // do it again in 1s |
| 407 | setTimeout(function () { |
Paul Greyson | a36a923 | 2013-03-22 22:41:27 -0700 | [diff] [blame] | 408 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 409 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 410 | }); |
| 411 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 412 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 413 | svg = createTopologyView(); |
| 414 | // workaround for Chrome v25 bug |
| 415 | // if executed immediately, the view box transform logic doesn't work properly |
| 416 | // fixed in Chrome v27 |
| 417 | setTimeout(function () { |
| 418 | // workaround for another Chrome v25 bug |
| 419 | // viewbox transform stuff doesn't work in combination with browser zoom |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 420 | // also works in Chrome v27 |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 421 | d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width); |
| 422 | sync(svg); |
| 423 | }, 100); |