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