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