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 | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame^] | 126 | |
| 127 | |
| 128 | |
| 129 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 130 | return rings; |
| 131 | } |
| 132 | |
| 133 | function updateTopology(svg, model) { |
| 134 | |
| 135 | // DRAW THE SWITCHES |
| 136 | var rings = svg.selectAll('.ring').data(createRingsFromModel(model)); |
| 137 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 138 | function ringEnter(data, i) { |
| 139 | if (!data.switches.length) { |
| 140 | return; |
| 141 | } |
| 142 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 143 | // create the nodes |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 144 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 145 | .data(d3.range(data.switches.length).map(function() { |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 146 | return data; |
| 147 | })) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 148 | .enter().append("svg:g") |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 149 | .classed('nolabel', true) |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 150 | .attr("id", function (_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 151 | return data.switches[i].dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 152 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 153 | .attr("transform", function(_, i) { |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 154 | 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] | 155 | }); |
| 156 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 157 | // add the cirles representing the switches |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 158 | nodes.append("svg:circle") |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 159 | .attr("transform", function(_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 160 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 161 | if (data.scale) { |
| 162 | m = m.scale(data.scale); |
| 163 | } |
| 164 | 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] | 165 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 166 | .attr("x", -data.width / 2) |
| 167 | .attr("y", -data.width / 2) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 168 | .attr("r", data.width) |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 169 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 170 | // setup the mouseover behaviors |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 171 | function showLabel(data, index) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 172 | d3.select(document.getElementById(data.switches[index].dpid + '-label')).classed('nolabel', false); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | function hideLabel(data, index) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 176 | d3.select(document.getElementById(data.switches[index].dpid + '-label')).classed('nolabel', true); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | nodes.on('mouseover', showLabel); |
| 180 | nodes.on('mouseout', hideLabel); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 183 | // append switches |
| 184 | rings.enter().append("svg:g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 185 | .attr("class", "ring") |
| 186 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 187 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 188 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 189 | function ringUpdate(data, i) { |
| 190 | nodes = d3.select(this).selectAll("circle"); |
| 191 | nodes.attr('class', function (_, i) { |
| 192 | if (data.switches[i].state == 'ACTIVE') { |
| 193 | return data.className + ' ' + controllerColorMap[data.switches[i].controller]; |
| 194 | } else { |
| 195 | return data.className + ' ' + 'colorInactive'; |
| 196 | } |
| 197 | }) |
| 198 | } |
| 199 | |
| 200 | // update switches |
| 201 | rings.each(ringUpdate); |
| 202 | |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 203 | |
| 204 | // Now setup the labels |
| 205 | // This is done separately because SVG draws in node order and we want the labels |
| 206 | // always on top |
| 207 | var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model)); |
| 208 | |
| 209 | function labelRingEnter(data, i) { |
| 210 | if (!data.switches.length) { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | // create the nodes |
| 215 | var nodes = d3.select(this).selectAll("g") |
| 216 | .data(d3.range(data.switches.length).map(function() { |
| 217 | return data; |
| 218 | })) |
| 219 | .enter().append("svg:g") |
| 220 | .classed('nolabel', true) |
| 221 | .attr("id", function (_, i) { |
| 222 | return data.switches[i].dpid + '-label'; |
| 223 | }) |
| 224 | .attr("transform", function(_, i) { |
| 225 | return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")"; |
| 226 | }); |
| 227 | |
| 228 | // add the text nodes which show on mouse over |
| 229 | nodes.append("svg:text") |
| 230 | .text(function (d, i) {return d.switches[i].dpid}) |
| 231 | .attr("x", 0) |
| 232 | .attr("y", 0) |
| 233 | .attr("transform", function(_, i) { |
| 234 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 235 | if (data.scale) { |
| 236 | m = m.scale(data.scale); |
| 237 | } |
| 238 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 239 | }) |
| 240 | } |
| 241 | |
| 242 | labelRings.enter().append("svg:g") |
| 243 | .attr("class", "textRing") |
| 244 | .each(labelRingEnter); |
| 245 | |
| 246 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 247 | // switches should not change during operation of the ui so no |
| 248 | // rings.exit() |
| 249 | |
| 250 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 251 | // do mouseover zoom on edge nodes |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame^] | 252 | function zoom(data, index) { |
| 253 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 254 | g.transition().duration(100).attr("r", g.data()[0].width*3); |
| 255 | // TODO: this doesn't work because the data binding is by index |
| 256 | // d3.select(this.parentNode).moveToFront(); |
| 257 | } |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 258 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame^] | 259 | svg.selectAll('.edge').on('mouseover', zoom); |
| 260 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 261 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame^] | 262 | function unzoom(data, index) { |
| 263 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 264 | g.transition().duration(100).attr("r", g.data()[0].width); |
| 265 | } |
| 266 | svg.selectAll('.edge').on('mouseout', unzoom); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 267 | |
| 268 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 269 | // DRAW THE LINKS |
| 270 | var line = d3.svg.line() |
| 271 | .x(function(d) { |
| 272 | return d.x; |
| 273 | }) |
| 274 | .y(function(d) { |
| 275 | return d.y; |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 276 | }); |
| 277 | // .interpolate("basis"); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 278 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 279 | // key on link dpids since these will come/go during demo |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 280 | var links = d3.select('svg').selectAll('.link').data(model.links, function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 281 | return d['src-switch']+'->'+d['dst-switch']; |
| 282 | }); |
| 283 | |
| 284 | // add new links |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 285 | links.enter().append("svg:path") |
| 286 | .attr("class", "link") |
| 287 | .attr("d", function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 288 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 289 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 290 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 291 | |
| 292 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 293 | srcPt.x = src.attr('x'); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 294 | srcPt.y = src.attr('y'); |
| 295 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 296 | |
| 297 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 298 | dstPt.x = dst.attr('x'); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 299 | dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable |
| 300 | dstPt = dstPt.matrixTransform(dst[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 301 | |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 302 | var midPt = document.querySelector('svg').createSVGPoint(); |
| 303 | midPt.x = (srcPt.x + dstPt.x)/2; |
| 304 | midPt.y = (srcPt.y + dstPt.y)/2; |
| 305 | |
| 306 | return line([srcPt, midPt, dstPt]); |
| 307 | }) |
| 308 | .attr("marker-mid", function(d) { return "url(#arrow)"; }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 309 | |
| 310 | // remove old links |
| 311 | links.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | function updateControllers(model) { |
| 315 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 316 | controllers.enter().append('div') |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 317 | .each(function (c) { |
| 318 | controllerColorMap[c] = colors.pop(); |
| 319 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 320 | }) |
| 321 | .text(function (d) { |
| 322 | return d; |
| 323 | }); |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 324 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 325 | controllers.attr('class', function (d) { |
Paul Greyson | eed3635 | 2013-03-23 11:19:11 -0700 | [diff] [blame] | 326 | var color = 'colorInactive'; |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 327 | if (model.activeControllers.indexOf(d) != -1) { |
| 328 | color = controllerColorMap[d]; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 329 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 330 | var className = 'controller ' + color; |
| 331 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 332 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 333 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 334 | // this should never be needed |
| 335 | // controllers.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 336 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 337 | controllers.on('click', function (c, index) { |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 338 | var allSelected = true; |
| 339 | for (var key in controllerColorMap) { |
| 340 | if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) { |
| 341 | allSelected = false; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | if (allSelected) { |
| 346 | for (var key in controllerColorMap) { |
| 347 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c) |
| 348 | } |
| 349 | } else { |
| 350 | for (var key in controllerColorMap) { |
| 351 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', true) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 356 | // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 357 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 360 | var oldModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 361 | function sync(svg) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 362 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 363 | updateModel(function (newModel) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 364 | console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 365 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame^] | 366 | if (true || !oldModel || JSON.stringify(oldModel) != JSON.stringify(newModel)) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 367 | updateControllers(newModel); |
| 368 | updateTopology(svg, newModel); |
| 369 | } else { |
| 370 | console.log('no change'); |
| 371 | } |
| 372 | updateHeader(newModel); |
| 373 | |
| 374 | oldModel = newModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 375 | |
| 376 | // do it again in 1s |
| 377 | setTimeout(function () { |
Paul Greyson | a36a923 | 2013-03-22 22:41:27 -0700 | [diff] [blame] | 378 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 379 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 380 | }); |
| 381 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 382 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 383 | svg = createTopologyView(); |
| 384 | // workaround for Chrome v25 bug |
| 385 | // if executed immediately, the view box transform logic doesn't work properly |
| 386 | // fixed in Chrome v27 |
| 387 | setTimeout(function () { |
| 388 | // workaround for another Chrome v25 bug |
| 389 | // viewbox transform stuff doesn't work in combination with browser zoom |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 390 | // also works in Chrome v27 |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 391 | d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width); |
| 392 | sync(svg); |
| 393 | }, 100); |