blob: 39e0f7261214b119044f77b5b480d7421ac7e53c [file] [log] [blame]
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
16
17/*
Simon Hunt0b05d4a2014-10-21 21:50:15 -070018 ONOS network topology viewer - PoC version 1.0
19
20 @author Simon Hunt
21 */
22
23(function (onos) {
24 'use strict';
25
Simon Huntd35961b2014-10-28 08:49:48 -070026 // reference to the framework api
Simon Hunt0b05d4a2014-10-21 21:50:15 -070027 var api = onos.api;
28
Simon Huntd35961b2014-10-28 08:49:48 -070029 // configuration data
Simon Hunt0b05d4a2014-10-21 21:50:15 -070030 var config = {
Simon Huntb4d9d4c2014-10-30 11:27:23 -070031 useLiveData: true,
Simon Hunt73171372014-10-30 09:25:36 -070032 debugOn: false,
33 debug: {
34 showNodeXY: false,
35 showKeyHandler: true
36 },
37 options: {
38 layering: true,
Simon Huntee66e612014-10-30 14:56:31 -070039 collisionPrevention: true,
40 loadBackground: true
Simon Hunt73171372014-10-30 09:25:36 -070041 },
Simon Huntee66e612014-10-30 14:56:31 -070042 backgroundUrl: 'img/us-map.png',
Simon Huntb4d9d4c2014-10-30 11:27:23 -070043 data: {
44 live: {
45 jsonUrl: 'rs/topology/graph',
46 detailPrefix: 'rs/topology/graph/',
47 detailSuffix: ''
48 },
49 fake: {
50 jsonUrl: 'json/network2.json',
51 detailPrefix: 'json/',
52 detailSuffix: '.json'
53 }
54 },
Simon Hunt73171372014-10-30 09:25:36 -070055 iconUrl: {
56 device: 'img/device.png',
57 host: 'img/host.png',
58 pkt: 'img/pkt.png',
59 opt: 'img/opt.png'
60 },
61 mastHeight: 36,
62 force: {
63 note: 'node.class or link.class is used to differentiate',
64 linkDistance: {
65 infra: 200,
66 host: 40
Simon Huntd35961b2014-10-28 08:49:48 -070067 },
Simon Hunt73171372014-10-30 09:25:36 -070068 linkStrength: {
69 infra: 1.0,
70 host: 1.0
Simon Hunt2c9e0c22014-10-23 15:12:58 -070071 },
Simon Hunt73171372014-10-30 09:25:36 -070072 charge: {
73 device: -800,
74 host: -1000
Simon Hunt68ae6652014-10-22 13:58:07 -070075 },
Simon Hunt73171372014-10-30 09:25:36 -070076 ticksWithoutCollisions: 50,
77 marginLR: 20,
78 marginTB: 20,
79 translate: function() {
80 return 'translate(' +
81 config.force.marginLR + ',' +
82 config.force.marginTB + ')';
83 }
84 },
85 labels: {
86 imgPad: 16,
87 padLR: 8,
88 padTB: 6,
89 marginLR: 3,
Simon Hunt69a8d212014-10-30 17:57:35 -070090 marginTB: 2,
91 port: {
Simon Hunt3c29c142014-10-30 21:31:44 -070092 gap: 3,
93 width: 18,
94 height: 14
Simon Hunt69a8d212014-10-30 17:57:35 -070095 }
Simon Hunt73171372014-10-30 09:25:36 -070096 },
97 icons: {
98 w: 32,
99 h: 32,
100 xoff: -12,
101 yoff: -8
102 },
103 constraints: {
104 ypos: {
105 host: 0.05,
106 switch: 0.3,
107 roadm: 0.7
108 }
109 },
110 hostLinkWidth: 1.0,
111 hostRadius: 7,
112 mouseOutTimerDelayMs: 120
113 };
Simon Huntd35961b2014-10-28 08:49:48 -0700114
115 // state variables
116 var view = {},
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700117 network = {},
118 selected = {},
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700119 highlighted = null,
Simon Hunt9a16c822014-10-28 16:09:19 -0700120 hovered = null,
Simon Hunt69a8d212014-10-30 17:57:35 -0700121 viewMode = 'showAll',
122 portLabelsOn = false;
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700123
124
Simon Huntd35961b2014-10-28 08:49:48 -0700125 function debug(what) {
126 return config.debugOn && config.debug[what];
127 }
128
Simon Huntb4d9d4c2014-10-30 11:27:23 -0700129 function urlData() {
130 return config.data[config.useLiveData ? 'live' : 'fake'];
131 }
132
133 function networkJsonUrl() {
134 return urlData().jsonUrl;
135 }
136
Simon Hunt69a8d212014-10-30 17:57:35 -0700137 function safeId(id) {
138 return id.replace(/[^a-z0-9]/gi, '_');
139 }
140
Simon Huntb4d9d4c2014-10-30 11:27:23 -0700141 function detailJsonUrl(id) {
142 var u = urlData(),
Simon Hunt69a8d212014-10-30 17:57:35 -0700143 encId = config.useLiveData ? encodeURIComponent(id) : safeId(id);
Simon Huntb4d9d4c2014-10-30 11:27:23 -0700144 return u.detailPrefix + encId + u.detailSuffix;
145 }
146
147
Simon Huntd35961b2014-10-28 08:49:48 -0700148 // load the topology view of the network
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700149 function loadNetworkView() {
150 // Hey, here I am, calling something on the ONOS api:
151 api.printTime();
152
153 resize();
154
Simon Huntd35961b2014-10-28 08:49:48 -0700155 // go get our network data from the server...
Simon Huntb4d9d4c2014-10-30 11:27:23 -0700156 var url = networkJsonUrl();
157 d3.json(url , function (err, data) {
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700158 if (err) {
159 alert('Oops! Error reading JSON...\n\n' +
Simon Huntb4d9d4c2014-10-30 11:27:23 -0700160 'URL: ' + url + '\n\n' +
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700161 'Error: ' + err.message);
162 return;
163 }
Simon Huntd35961b2014-10-28 08:49:48 -0700164// console.log("here is the JSON data...");
165// console.log(data);
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700166
167 network.data = data;
168 drawNetwork();
169 });
170
Simon Huntd35961b2014-10-28 08:49:48 -0700171 // while we wait for the data, set up the handlers...
172 setUpClickHandler();
173 setUpRadioButtonHandler();
174 setUpKeyHandler();
175 $(window).on('resize', resize);
176 }
177
178 function setUpClickHandler() {
179 // click handler for "selectable" objects
180 $(document).on('click', '.select-object', function () {
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700181 // when any object of class "select-object" is clicked...
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700182 var obj = network.lookup[$(this).data('id')];
183 if (obj) {
184 selectObject(obj);
185 }
186 // stop propagation of event (I think) ...
187 return false;
188 });
Simon Huntd35961b2014-10-28 08:49:48 -0700189 }
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700190
Simon Huntd35961b2014-10-28 08:49:48 -0700191 function setUpRadioButtonHandler() {
192 d3.selectAll('#displayModes .radio').on('click', function () {
193 var id = d3.select(this).attr('id');
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700194 if (id !== viewMode) {
195 radioButton('displayModes', id);
196 viewMode = id;
Simon Huntf967d512014-10-28 20:34:29 -0700197 doRadioAction(id);
198 }
199 });
200 }
201
202 function doRadioAction(id) {
203 showAllLayers();
204 if (id === 'showPkt') {
205 showPacketLayer();
206 } else if (id === 'showOpt') {
207 showOpticalLayer();
208 }
209 }
210
211 function showAllLayers() {
212 network.node.classed('inactive', false);
213 network.link.classed('inactive', false);
214 }
215
216 function showPacketLayer() {
217 network.node.each(function(d) {
218 // deactivate nodes that are not hosts or switches
219 if (d.class === 'device' && d.type !== 'switch') {
220 d3.select(this).classed('inactive', true);
221 }
222 });
223
224 network.link.each(function(lnk) {
225 // deactivate infrastructure links that have opt's as endpoints
226 if (lnk.source.type === 'roadm' || lnk.target.type === 'roadm') {
227 d3.select(this).classed('inactive', true);
228 }
229 });
230 }
231
232 function showOpticalLayer() {
233 network.node.each(function(d) {
234 // deactivate nodes that are not optical devices
235 if (d.type !== 'roadm') {
236 d3.select(this).classed('inactive', true);
237 }
238 });
239
240 network.link.each(function(lnk) {
241 // deactivate infrastructure links that have opt's as endpoints
242 if (lnk.source.type !== 'roadm' || lnk.target.type !== 'roadm') {
243 d3.select(this).classed('inactive', true);
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700244 }
245 });
246 }
247
Simon Huntd35961b2014-10-28 08:49:48 -0700248 function setUpKeyHandler() {
249 d3.select('body')
250 .on('keydown', function () {
251 processKeyEvent();
252 if (debug('showKeyHandler')) {
253 network.svg.append('text')
254 .attr('x', 5)
255 .attr('y', 15)
256 .style('font-size', '20pt')
257 .text('keyCode: ' + d3.event.keyCode +
258 ' applied to : ' + contextLabel())
259 .transition().duration(2000)
260 .style('font-size', '2pt')
261 .style('fill-opacity', 0.01)
262 .remove();
263 }
264 });
265 }
266
Simon Hunt9a16c822014-10-28 16:09:19 -0700267 function contextLabel() {
268 return hovered === null ? "(nothing)" : hovered.id;
269 }
270
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700271 function radioButton(group, id) {
272 d3.selectAll("#" + group + " .radio").classed("active", false);
273 d3.select("#" + group + " #" + id).classed("active", true);
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700274 }
275
Simon Huntd35961b2014-10-28 08:49:48 -0700276 function processKeyEvent() {
277 var code = d3.event.keyCode;
278 switch (code) {
Simon Huntee66e612014-10-30 14:56:31 -0700279 case 66: // B
280 toggleBackground();
281 break;
Thomas Vachuska1de66012014-10-30 03:03:30 -0700282 case 71: // G
283 cycleLayout();
284 break;
Simon Huntd35961b2014-10-28 08:49:48 -0700285 case 76: // L
286 cycleLabels();
287 break;
288 case 80: // P
289 togglePorts();
Simon Hunt9a16c822014-10-28 16:09:19 -0700290 break;
291 case 85: // U
292 unpin();
293 break;
Simon Huntd35961b2014-10-28 08:49:48 -0700294 }
295
296 }
297
Simon Huntee66e612014-10-30 14:56:31 -0700298 function toggleBackground() {
299 var bg = d3.select('#bg'),
300 vis = bg.style('visibility'),
301 newvis = (vis === 'hidden') ? 'visible' : 'hidden';
302 bg.style('visibility', newvis);
303 }
304
Thomas Vachuska1de66012014-10-30 03:03:30 -0700305 function cycleLayout() {
306 config.options.layering = !config.options.layering;
307 network.force.resume();
308 }
309
Simon Huntd35961b2014-10-28 08:49:48 -0700310 function cycleLabels() {
Simon Hunt9a16c822014-10-28 16:09:19 -0700311 console.log('Cycle Labels - context = ' + contextLabel());
Simon Huntd35961b2014-10-28 08:49:48 -0700312 }
313
314 function togglePorts() {
Simon Hunt69a8d212014-10-30 17:57:35 -0700315 portLabelsOn = !portLabelsOn;
316 var portVis = portLabelsOn ? 'visible' : 'hidden';
317 d3.selectAll('.port').style('visibility', portVis);
Simon Hunt3c29c142014-10-30 21:31:44 -0700318 d3.selectAll('.portText').style('visibility', portVis);
Simon Hunt9a16c822014-10-28 16:09:19 -0700319 }
320
321 function unpin() {
322 if (hovered) {
323 hovered.fixed = false;
Simon Huntf967d512014-10-28 20:34:29 -0700324 findNodeFromData(hovered).classed('fixed', false);
Simon Hunt9a16c822014-10-28 16:09:19 -0700325 network.force.resume();
326 }
327 console.log('Unpin - context = ' + contextLabel());
Simon Huntd35961b2014-10-28 08:49:48 -0700328 }
329
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700330
331 // ========================================================
332
333 function drawNetwork() {
334 $('#view').empty();
335
336 prepareNodesAndLinks();
337 createLayout();
338 console.log("\n\nHere is the augmented network object...");
Simon Hunt9a16c822014-10-28 16:09:19 -0700339 console.log(network);
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700340 }
341
342 function prepareNodesAndLinks() {
343 network.lookup = {};
344 network.nodes = [];
345 network.links = [];
346
347 var nw = network.forceWidth,
348 nh = network.forceHeight;
349
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700350 function yPosConstraintForNode(n) {
351 return config.constraints.ypos[n.type || 'host'];
352 }
353
354 // Note that both 'devices' and 'hosts' get mapped into the nodes array
355
356 // first, the devices...
357 network.data.devices.forEach(function(n) {
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700358 var ypc = yPosConstraintForNode(n),
Simon Hunt3ab76a82014-10-22 13:07:32 -0700359 ix = Math.random() * 0.6 * nw + 0.2 * nw,
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700360 iy = ypc * nh,
361 node = {
Thomas Vachuska5f998492014-10-31 00:46:11 -0700362 id: n.id,
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700363 labels: n.labels,
364 class: 'device',
365 icon: 'device',
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700366 type: n.type,
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700367 x: ix,
368 y: iy,
369 constraint: {
370 weight: 0.7,
371 y: iy
372 }
373 };
374 network.lookup[n.id] = node;
375 network.nodes.push(node);
376 });
377
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700378 // then, the hosts...
379 network.data.hosts.forEach(function(n) {
380 var ypc = yPosConstraintForNode(n),
381 ix = Math.random() * 0.6 * nw + 0.2 * nw,
382 iy = ypc * nh,
383 node = {
Thomas Vachuska5f998492014-10-31 00:46:11 -0700384 id: n.id,
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700385 labels: n.labels,
386 class: 'host',
387 icon: 'host',
388 type: n.type,
389 x: ix,
390 y: iy,
391 constraint: {
392 weight: 0.7,
393 y: iy
394 }
395 };
396 network.lookup[n.id] = node;
397 network.nodes.push(node);
398 });
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700399
400
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700401 // now, process the explicit links...
Simon Hunt6f376a32014-10-28 12:38:30 -0700402 network.data.links.forEach(function(lnk) {
403 var src = network.lookup[lnk.src],
404 dst = network.lookup[lnk.dst],
Simon Hunt69a8d212014-10-30 17:57:35 -0700405 id = src.id + "-" + dst.id;
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700406
407 var link = {
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700408 class: 'infra',
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700409 id: id,
Simon Hunt6f376a32014-10-28 12:38:30 -0700410 type: lnk.type,
411 width: lnk.linkWidth,
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700412 source: src,
Simon Hunt69a8d212014-10-30 17:57:35 -0700413 srcPort: lnk.srcPort,
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700414 target: dst,
Simon Hunt69a8d212014-10-30 17:57:35 -0700415 tgtPort: lnk.dstPort,
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700416 strength: config.force.linkStrength.infra
417 };
418 network.links.push(link);
419 });
420
421 // finally, infer host links...
422 network.data.hosts.forEach(function(n) {
423 var src = network.lookup[n.id],
424 dst = network.lookup[n.cp.device],
Simon Hunt69a8d212014-10-30 17:57:35 -0700425 id = src.id + "-" + dst.id;
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700426
427 var link = {
428 class: 'host',
429 id: id,
430 type: 'hostLink',
431 width: config.hostLinkWidth,
432 source: src,
433 target: dst,
434 strength: config.force.linkStrength.host
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700435 };
436 network.links.push(link);
437 });
438 }
439
440 function createLayout() {
441
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700442 var cfg = config.force;
443
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700444 network.force = d3.layout.force()
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700445 .size([network.forceWidth, network.forceHeight])
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700446 .nodes(network.nodes)
447 .links(network.links)
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700448 .linkStrength(function(d) { return cfg.linkStrength[d.class]; })
449 .linkDistance(function(d) { return cfg.linkDistance[d.class]; })
450 .charge(function(d) { return cfg.charge[d.class]; })
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700451 .on('tick', tick);
452
453 network.svg = d3.select('#view').append('svg')
454 .attr('width', view.width)
455 .attr('height', view.height)
456 .append('g')
Simon Huntae968a62014-10-22 14:54:41 -0700457 .attr('transform', config.force.translate());
Simon Hunt3ab76a82014-10-22 13:07:32 -0700458// .attr('id', 'zoomable')
Simon Hunt3ab76a82014-10-22 13:07:32 -0700459// .call(d3.behavior.zoom().on("zoom", zoomRedraw));
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700460
Thomas Vachuska8cd66a52014-10-30 11:53:07 -0700461 network.svg.append('svg:image')
462 .attr({
463 id: 'bg',
464 width: view.width,
465 height: view.height,
Simon Huntee66e612014-10-30 14:56:31 -0700466 'xlink:href': config.backgroundUrl
467 })
468 .style('visibility',
469 config.options.loadBackground ? 'visible' : 'hidden');
Thomas Vachuska8cd66a52014-10-30 11:53:07 -0700470
Simon Hunt3ab76a82014-10-22 13:07:32 -0700471// function zoomRedraw() {
472// d3.select("#zoomable").attr("transform",
473// "translate(" + d3.event.translate + ")"
474// + " scale(" + d3.event.scale + ")");
475// }
476
Simon Hunt3ab76a82014-10-22 13:07:32 -0700477 // TODO: move glow/blur stuff to util script
478 var glow = network.svg.append('filter')
479 .attr('x', '-50%')
480 .attr('y', '-50%')
481 .attr('width', '200%')
482 .attr('height', '200%')
483 .attr('id', 'blue-glow');
484
485 glow.append('feColorMatrix')
486 .attr('type', 'matrix')
487 .attr('values', '0 0 0 0 0 ' +
488 '0 0 0 0 0 ' +
489 '0 0 0 0 .7 ' +
490 '0 0 0 1 0 ');
491
492 glow.append('feGaussianBlur')
493 .attr('stdDeviation', 3)
494 .attr('result', 'coloredBlur');
495
496 glow.append('feMerge').selectAll('feMergeNode')
497 .data(['coloredBlur', 'SourceGraphic'])
498 .enter().append('feMergeNode')
499 .attr('in', String);
500
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700501 // TODO: legend (and auto adjust on scroll)
Simon Hunt3ab76a82014-10-22 13:07:32 -0700502// $('#view').on('scroll', function() {
503//
504// });
505
506
Simon Hunt6f376a32014-10-28 12:38:30 -0700507 // TODO: move drag behavior into separate method.
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700508 // == define node drag behavior...
Simon Hunt3ab76a82014-10-22 13:07:32 -0700509 network.draggedThreshold = d3.scale.linear()
510 .domain([0, 0.1])
511 .range([5, 20])
512 .clamp(true);
513
514 function dragged(d) {
515 var threshold = network.draggedThreshold(network.force.alpha()),
516 dx = d.oldX - d.px,
517 dy = d.oldY - d.py;
518 if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) {
519 d.dragged = true;
520 }
521 return d.dragged;
522 }
523
524 network.drag = d3.behavior.drag()
525 .origin(function(d) { return d; })
526 .on('dragstart', function(d) {
527 d.oldX = d.x;
528 d.oldY = d.y;
529 d.dragged = false;
530 d.fixed |= 2;
531 })
532 .on('drag', function(d) {
533 d.px = d3.event.x;
534 d.py = d3.event.y;
535 if (dragged(d)) {
536 if (!network.force.alpha()) {
537 network.force.alpha(.025);
538 }
539 }
540 })
541 .on('dragend', function(d) {
542 if (!dragged(d)) {
543 selectObject(d, this);
544 }
545 d.fixed &= ~6;
Simon Hunt9a16c822014-10-28 16:09:19 -0700546
547 // once we've finished moving, pin the node in position,
Simon Huntf967d512014-10-28 20:34:29 -0700548 // if it is a device (not a host)
Simon Hunt9a16c822014-10-28 16:09:19 -0700549 if (d.class === 'device') {
550 d.fixed = true;
Simon Huntf967d512014-10-28 20:34:29 -0700551 d3.select(this).classed('fixed', true)
Simon Hunt9a16c822014-10-28 16:09:19 -0700552 }
Simon Hunt3ab76a82014-10-22 13:07:32 -0700553 });
554
555 $('#view').on('click', function(e) {
556 if (!$(e.target).closest('.node').length) {
557 deselectObject();
558 }
559 });
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700560
Simon Hunt69a8d212014-10-30 17:57:35 -0700561 // ...............................................................
562
563 // add links to the display
564 network.link = network.svg.append('g').attr('id', 'links')
565 .selectAll('.link')
566 .data(network.force.links(), function(d) {return d.id})
567 .enter().append('line')
568 .attr('class', function(d) {return 'link ' + d.class});
569
570 network.linkSrcPort = network.svg.append('g')
571 .attr({
572 id: 'srcPorts',
573 class: 'portLayer'
574 });
575 network.linkTgtPort = network.svg.append('g')
576 .attr({
577 id: 'tgtPorts',
578 class: 'portLayer'
579 });
580
Simon Hunt3c29c142014-10-30 21:31:44 -0700581 var portVis = portLabelsOn ? 'visible' : 'hidden',
582 pw = config.labels.port.width,
583 ph = config.labels.port.height;
Simon Hunt69a8d212014-10-30 17:57:35 -0700584
585 network.link.filter('.infra').each(function(d, i) {
586 network.linkSrcPort.append('rect').attr({
Thomas Vachuska5f998492014-10-31 00:46:11 -0700587 id: 'srcPort-' + safeId(d.id),
Simon Hunt69a8d212014-10-30 17:57:35 -0700588 class: 'port',
Simon Hunt3c29c142014-10-30 21:31:44 -0700589 width: pw,
590 height: ph,
Thomas Vachuska5f998492014-10-31 00:46:11 -0700591 Xx: i * 20,
592 Xy: 0,
593 rx: 4,
594 ry: 4
595 }).style('visibility', portVis);
Simon Hunt69a8d212014-10-30 17:57:35 -0700596
597 network.linkTgtPort.append('rect').attr({
Thomas Vachuska5f998492014-10-31 00:46:11 -0700598 id: 'tgtPort-' + safeId(d.id),
Simon Hunt69a8d212014-10-30 17:57:35 -0700599 class: 'port',
Simon Hunt3c29c142014-10-30 21:31:44 -0700600 width: pw,
601 height: ph,
Thomas Vachuska5f998492014-10-31 00:46:11 -0700602 Xx: i * 20,
603 Xy: 20,
604 rx: 4,
605 ry: 4
606 }).style('visibility', portVis);
Simon Hunt69a8d212014-10-30 17:57:35 -0700607
Simon Hunt3c29c142014-10-30 21:31:44 -0700608 network.linkSrcPort.append('text').attr({
Thomas Vachuska5f998492014-10-31 00:46:11 -0700609 id: 'srcText-' + safeId(d.id),
Simon Hunt3c29c142014-10-30 21:31:44 -0700610 class: 'portText',
Thomas Vachuska5f998492014-10-31 00:46:11 -0700611 Xx: i * 20,
612 Xy: 0
Simon Hunt3c29c142014-10-30 21:31:44 -0700613 }).text(d.srcPort)
614 .style('visibility', portVis);
615
616 network.linkTgtPort.append('text').attr({
Thomas Vachuska5f998492014-10-31 00:46:11 -0700617 id: 'tgtText-' + safeId(d.id),
Simon Hunt3c29c142014-10-30 21:31:44 -0700618 class: 'portText',
Thomas Vachuska5f998492014-10-31 00:46:11 -0700619 Xx: i * 20,
620 Xy: 20
Simon Hunt3c29c142014-10-30 21:31:44 -0700621 }).text(d.tgtPort)
622 .style('visibility', portVis);
Simon Hunt69a8d212014-10-30 17:57:35 -0700623 });
624
625 // ...............................................................
Simon Hunt1c5f8b62014-10-22 14:43:01 -0700626
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700627 // add nodes to the display
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700628 network.node = network.svg.selectAll('.node')
629 .data(network.force.nodes(), function(d) {return d.id})
630 .enter().append('g')
Simon Hunt3ab76a82014-10-22 13:07:32 -0700631 .attr('class', function(d) {
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700632 var cls = 'node ' + d.class;
633 if (d.type) {
634 cls += ' ' + d.type;
635 }
636 return cls;
Simon Hunt3ab76a82014-10-22 13:07:32 -0700637 })
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700638 .attr('transform', function(d) {
639 return translate(d.x, d.y);
640 })
Simon Hunt3ab76a82014-10-22 13:07:32 -0700641 .call(network.drag)
642 .on('mouseover', function(d) {
Simon Hunt6f376a32014-10-28 12:38:30 -0700643 // TODO: show tooltip
Simon Hunt9a16c822014-10-28 16:09:19 -0700644 if (network.mouseoutTimeout) {
645 clearTimeout(network.mouseoutTimeout);
646 network.mouseoutTimeout = null;
Simon Hunt3ab76a82014-10-22 13:07:32 -0700647 }
Simon Hunt9a16c822014-10-28 16:09:19 -0700648 hoverObject(d);
Simon Hunt3ab76a82014-10-22 13:07:32 -0700649 })
650 .on('mouseout', function(d) {
Simon Hunt6f376a32014-10-28 12:38:30 -0700651 // TODO: hide tooltip
Simon Hunt9a16c822014-10-28 16:09:19 -0700652 if (network.mouseoutTimeout) {
653 clearTimeout(network.mouseoutTimeout);
654 network.mouseoutTimeout = null;
Simon Hunt3ab76a82014-10-22 13:07:32 -0700655 }
Simon Hunt9a16c822014-10-28 16:09:19 -0700656 network.mouseoutTimeout = setTimeout(function() {
657 hoverObject(null);
658 }, config.mouseOutTimerDelayMs);
Simon Hunt3ab76a82014-10-22 13:07:32 -0700659 });
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700660
Simon Hunt6f376a32014-10-28 12:38:30 -0700661
662 // deal with device nodes first
663 network.nodeRect = network.node.filter('.device')
664 .append('rect')
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700665 .attr({
666 rx: 5,
667 ry: 5,
668 width: 100,
669 height: 12
670 });
671 // note that width/height are adjusted to fit the label text
Simon Hunt6f376a32014-10-28 12:38:30 -0700672 // then padded, and space made for the icon.
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700673
Simon Hunt6f376a32014-10-28 12:38:30 -0700674 network.node.filter('.device').each(function(d) {
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700675 var node = d3.select(this),
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700676 icon = iconUrl(d);
677
678 node.append('text')
679 // TODO: add label cycle behavior
680 .text(d.id)
681 .attr('dy', '1.1em');
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700682
683 if (icon) {
684 var cfg = config.icons;
685 node.append('svg:image')
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700686 .attr({
687 width: cfg.w,
688 height: cfg.h,
689 'xlink:href': icon
690 });
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700691 // note, icon relative positioning (x,y) is done after we have
692 // adjusted the bounds of the rectangle...
693 }
Simon Hunt68ae6652014-10-22 13:58:07 -0700694
Simon Huntd35961b2014-10-28 08:49:48 -0700695 // debug function to show the modelled x,y coordinates of nodes...
696 if (debug('showNodeXY')) {
697 node.select('rect').attr('fill-opacity', 0.5);
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700698 node.append('circle')
699 .attr({
700 class: 'debug',
701 cx: 0,
702 cy: 0,
703 r: '3px'
704 });
705 }
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700706 });
707
Simon Hunt6f376a32014-10-28 12:38:30 -0700708 // now process host nodes
709 network.nodeCircle = network.node.filter('.host')
710 .append('circle')
711 .attr({
712 r: config.hostRadius
713 });
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700714
Simon Hunt6f376a32014-10-28 12:38:30 -0700715 network.node.filter('.host').each(function(d) {
716 var node = d3.select(this),
717 icon = iconUrl(d);
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700718
Simon Hunt6f376a32014-10-28 12:38:30 -0700719 // debug function to show the modelled x,y coordinates of nodes...
720 if (debug('showNodeXY')) {
721 node.select('circle').attr('fill-opacity', 0.5);
722 node.append('circle')
723 .attr({
724 class: 'debug',
725 cx: 0,
726 cy: 0,
727 r: '3px'
728 });
729 }
730 });
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700731
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700732 // this function is scheduled to happen soon after the given thread ends
733 setTimeout(function() {
Simon Hunt69a8d212014-10-30 17:57:35 -0700734 var lab = config.labels,
735 portGap = lab.port.gap,
736 midW = portGap + lab.port.width/ 2,
737 midH = portGap + lab.port.height / 2;
738
Simon Hunt6f376a32014-10-28 12:38:30 -0700739 // post process the device nodes, to pad their size to fit the
740 // label text and attach the icon to the right location.
741 network.node.filter('.device').each(function(d) {
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700742 // for every node, recompute size, padding, etc. so text fits
743 var node = d3.select(this),
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700744 text = node.select('text'),
Simon Hunt69a8d212014-10-30 17:57:35 -0700745 box = adjustRectToFitText(node);
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700746
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700747 // now make the computed adjustment
Simon Hunt1c5f8b62014-10-22 14:43:01 -0700748 node.select('rect')
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700749 .attr(box);
Simon Hunt1c5f8b62014-10-22 14:43:01 -0700750
751 node.select('image')
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700752 .attr('x', box.x + config.icons.xoff)
753 .attr('y', box.y + config.icons.yoff);
Simon Hunt1c219892014-10-22 16:32:39 -0700754
Simon Hunt69a8d212014-10-30 17:57:35 -0700755 var bounds = boundsFromBox(box),
756 portBounds = {
757 x1: bounds.x1 - midW,
758 x2: bounds.x2 + midW,
759 y1: bounds.y1 - midH,
760 y2: bounds.y2 + midH
761 };
Simon Hunt5cd0e8f2014-10-27 16:18:40 -0700762
763 // todo: clean up extent and edge work..
Simon Hunt1c219892014-10-22 16:32:39 -0700764 d.extent = {
765 left: bounds.x1 - lab.marginLR,
766 right: bounds.x2 + lab.marginLR,
767 top: bounds.y1 - lab.marginTB,
768 bottom: bounds.y2 + lab.marginTB
769 };
770
771 d.edge = {
772 left : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x1, bounds.y2),
773 right : new geo.LineSegment(bounds.x2, bounds.y1, bounds.x2, bounds.y2),
774 top : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x2, bounds.y1),
775 bottom : new geo.LineSegment(bounds.x1, bounds.y2, bounds.x2, bounds.y2)
776 };
777
Simon Hunt69a8d212014-10-30 17:57:35 -0700778 d.portEdge = {
779 left : new geo.LineSegment(
780 portBounds.x1, portBounds.y1, portBounds.x1, portBounds.y2
781 ),
782 right : new geo.LineSegment(
783 portBounds.x2, portBounds.y1, portBounds.x2, portBounds.y2
784 ),
785 top : new geo.LineSegment(
786 portBounds.x1, portBounds.y1, portBounds.x2, portBounds.y1
787 ),
788 bottom : new geo.LineSegment(
789 portBounds.x1, portBounds.y2, portBounds.x2, portBounds.y2
790 )
791 };
792
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700793 });
794
795 network.numTicks = 0;
796 network.preventCollisions = false;
797 network.force.start();
Simon Hunt1c219892014-10-22 16:32:39 -0700798 for (var i = 0; i < config.force.ticksWithoutCollisions; i++) {
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700799 network.force.tick();
800 }
801 network.preventCollisions = true;
802 $('#view').css('visibility', 'visible');
803 });
804
Simon Hunt6f376a32014-10-28 12:38:30 -0700805
806 // returns the newly computed bounding box of the rectangle
807 function adjustRectToFitText(n) {
808 var text = n.select('text'),
809 box = text.node().getBBox(),
810 lab = config.labels;
811
Simon Hunt9a16c822014-10-28 16:09:19 -0700812 // not sure why n.data() returns an array of 1 element...
813 var data = n.data()[0];
814
Simon Hunt6f376a32014-10-28 12:38:30 -0700815 text.attr('text-anchor', 'middle')
816 .attr('y', '-0.8em')
817 .attr('x', lab.imgPad/2)
818 ;
819
Simon Hunt6f376a32014-10-28 12:38:30 -0700820 // translate the bbox so that it is centered on [x,y]
821 box.x = -box.width / 2;
822 box.y = -box.height / 2;
823
824 // add padding
825 box.x -= (lab.padLR + lab.imgPad/2);
826 box.width += lab.padLR * 2 + lab.imgPad;
827 box.y -= lab.padTB;
828 box.height += lab.padTB * 2;
829
830 return box;
831 }
832
833 function boundsFromBox(box) {
834 return {
835 x1: box.x,
836 y1: box.y,
837 x2: box.x + box.width,
838 y2: box.y + box.height
839 };
840 }
841
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700842 }
843
Simon Hunt68ae6652014-10-22 13:58:07 -0700844 function iconUrl(d) {
Thomas Vachuska1de66012014-10-30 03:03:30 -0700845 return 'img/' + d.type + '.png';
846// return config.iconUrl[d.icon];
Simon Hunt68ae6652014-10-22 13:58:07 -0700847 }
848
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700849 function translate(x, y) {
850 return 'translate(' + x + ',' + y + ')';
851 }
852
Simon Hunt6f376a32014-10-28 12:38:30 -0700853 // prevents collisions amongst device nodes
Simon Hunt1c219892014-10-22 16:32:39 -0700854 function preventCollisions() {
Simon Hunt6f376a32014-10-28 12:38:30 -0700855 var quadtree = d3.geom.quadtree(network.nodes),
856 hrad = config.hostRadius;
Simon Hunt1c219892014-10-22 16:32:39 -0700857
858 network.nodes.forEach(function(n) {
Simon Hunt6f376a32014-10-28 12:38:30 -0700859 var nx1, nx2, ny1, ny2;
860
861 if (n.class === 'device') {
862 nx1 = n.x + n.extent.left;
863 nx2 = n.x + n.extent.right;
864 ny1 = n.y + n.extent.top;
Simon Hunt1c219892014-10-22 16:32:39 -0700865 ny2 = n.y + n.extent.bottom;
866
Simon Hunt6f376a32014-10-28 12:38:30 -0700867 } else {
868 nx1 = n.x - hrad;
869 nx2 = n.x + hrad;
870 ny1 = n.y - hrad;
871 ny2 = n.y + hrad;
872 }
873
Simon Hunt1c219892014-10-22 16:32:39 -0700874 quadtree.visit(function(quad, x1, y1, x2, y2) {
875 if (quad.point && quad.point !== n) {
Simon Hunt6f376a32014-10-28 12:38:30 -0700876 // check if the rectangles/circles intersect
Simon Hunt1c219892014-10-22 16:32:39 -0700877 var p = quad.point,
Simon Hunt6f376a32014-10-28 12:38:30 -0700878 px1, px2, py1, py2, ix;
879
880 if (p.class === 'device') {
881 px1 = p.x + p.extent.left;
882 px2 = p.x + p.extent.right;
883 py1 = p.y + p.extent.top;
884 py2 = p.y + p.extent.bottom;
885
886 } else {
887 px1 = p.x - hrad;
888 px2 = p.x + hrad;
889 py1 = p.y - hrad;
890 py2 = p.y + hrad;
891 }
892
893 ix = (px1 <= nx2 && nx1 <= px2 && py1 <= ny2 && ny1 <= py2);
894
Simon Hunt1c219892014-10-22 16:32:39 -0700895 if (ix) {
896 var xa1 = nx2 - px1, // shift n left , p right
897 xa2 = px2 - nx1, // shift n right, p left
898 ya1 = ny2 - py1, // shift n up , p down
899 ya2 = py2 - ny1, // shift n down , p up
900 adj = Math.min(xa1, xa2, ya1, ya2);
901
902 if (adj == xa1) {
903 n.x -= adj / 2;
904 p.x += adj / 2;
905 } else if (adj == xa2) {
906 n.x += adj / 2;
907 p.x -= adj / 2;
908 } else if (adj == ya1) {
909 n.y -= adj / 2;
910 p.y += adj / 2;
911 } else if (adj == ya2) {
912 n.y += adj / 2;
913 p.y -= adj / 2;
914 }
915 }
916 return ix;
917 }
918 });
919
920 });
921 }
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700922
923 function tick(e) {
924 network.numTicks++;
925
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700926 if (config.options.layering) {
Simon Hunt68ae6652014-10-22 13:58:07 -0700927 // adjust the y-coord of each node, based on y-pos constraints
928 network.nodes.forEach(function (n) {
929 var z = e.alpha * n.constraint.weight;
930 if (!isNaN(n.constraint.y)) {
931 n.y = (n.constraint.y * z + n.y * (1 - z));
932 }
933 });
934 }
Simon Hunt0b05d4a2014-10-21 21:50:15 -0700935
Simon Hunt2c9e0c22014-10-23 15:12:58 -0700936 if (config.options.collisionPrevention && network.preventCollisions) {
Simon Hunt1c219892014-10-22 16:32:39 -0700937 preventCollisions();
938 }
939
Simon Hunt69a8d212014-10-30 17:57:35 -0700940 var portHalfW = config.labels.port.width / 2,
941 portHalfH = config.labels.port.height / 2;
942
Simon Huntd35961b2014-10-28 08:49:48 -0700943 // clip visualization of links at bounds of nodes...
944 network.link.each(function(d) {
Simon Hunt69a8d212014-10-30 17:57:35 -0700945 var xs = d.source.x,
946 ys = d.source.y,
947 xt = d.target.x,
948 yt = d.target.y,
949 line = new geo.LineSegment(xs, ys, xt, yt),
950 e, ix,
951 exs, eys, ext, eyt,
952 pxs, pys, pxt, pyt;
Simon Hunt1c219892014-10-22 16:32:39 -0700953
Simon Hunt69a8d212014-10-30 17:57:35 -0700954 if (d.class === 'host') {
955 // no adjustment for source end of link, since hosts are dots
956 exs = xs;
957 eys = ys;
958
959 } else {
Simon Huntd35961b2014-10-28 08:49:48 -0700960 for (e in d.source.edge) {
961 ix = line.intersect(d.source.edge[e].offset(xs, ys));
Simon Hunt1c219892014-10-22 16:32:39 -0700962 if (ix.in1 && ix.in2) {
Simon Hunt69a8d212014-10-30 17:57:35 -0700963 exs = ix.x;
964 eys = ix.y;
965
966 // also pick off the port label intersection
967 ix = line.intersect(d.source.portEdge[e].offset(xs, ys));
968 pxs = ix.x;
969 pys = ix.y;
Simon Huntd35961b2014-10-28 08:49:48 -0700970 break;
971 }
972 }
Simon Hunt69a8d212014-10-30 17:57:35 -0700973 }
Simon Huntd35961b2014-10-28 08:49:48 -0700974
Simon Hunt69a8d212014-10-30 17:57:35 -0700975 for (e in d.target.edge) {
976 ix = line.intersect(d.target.edge[e].offset(xt, yt));
977 if (ix.in1 && ix.in2) {
978 ext = ix.x;
979 eyt = ix.y;
980
981 // also pick off the port label intersection
982 ix = line.intersect(d.target.portEdge[e].offset(xt, yt));
983 pxt = ix.x;
984 pyt = ix.y;
985 break;
Simon Hunt1c219892014-10-22 16:32:39 -0700986 }
Simon Hunt69a8d212014-10-30 17:57:35 -0700987 }
Simon Hunt1c219892014-10-22 16:32:39 -0700988
Simon Hunt69a8d212014-10-30 17:57:35 -0700989 // adjust the endpoints of the link's line to match rectangles
Thomas Vachuska5f998492014-10-31 00:46:11 -0700990 var sid = safeId(d.id);
Simon Hunt69a8d212014-10-30 17:57:35 -0700991 d3.select(this)
992 .attr('x1', exs)
993 .attr('y1', eys)
994 .attr('x2', ext)
995 .attr('y2', eyt);
996
Thomas Vachuska5f998492014-10-31 00:46:11 -0700997 d3.select('#srcPort-' + sid)
Simon Hunt69a8d212014-10-30 17:57:35 -0700998 .attr('x', pxs - portHalfW)
999 .attr('y', pys - portHalfH);
1000
Thomas Vachuska5f998492014-10-31 00:46:11 -07001001 d3.select('#tgtPort-' + sid)
Simon Hunt69a8d212014-10-30 17:57:35 -07001002 .attr('x', pxt - portHalfW)
1003 .attr('y', pyt - portHalfH);
1004
Simon Hunt3c29c142014-10-30 21:31:44 -07001005 // TODO: fit label rect to size of port number.
Thomas Vachuska5f998492014-10-31 00:46:11 -07001006 d3.select('#srcText-' + sid)
Simon Hunt3c29c142014-10-30 21:31:44 -07001007 .attr('x', pxs - 5)
1008 .attr('y', pys + 3);
1009
Thomas Vachuska5f998492014-10-31 00:46:11 -07001010 d3.select('#tgtText-' + sid)
Simon Hunt3c29c142014-10-30 21:31:44 -07001011 .attr('x', pxt - 5)
1012 .attr('y', pyt + 3);
1013
Simon Hunt69a8d212014-10-30 17:57:35 -07001014 });
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001015
Simon Huntd35961b2014-10-28 08:49:48 -07001016 // position each node by translating the node (group) by x,y
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001017 network.node
1018 .attr('transform', function(d) {
1019 return translate(d.x, d.y);
1020 });
1021
1022 }
1023
1024 // $('#docs-close').on('click', function() {
1025 // deselectObject();
1026 // return false;
1027 // });
1028
1029 // $(document).on('click', '.select-object', function() {
1030 // var obj = graph.data[$(this).data('name')];
1031 // if (obj) {
1032 // selectObject(obj);
1033 // }
1034 // return false;
1035 // });
1036
Simon Hunt6f376a32014-10-28 12:38:30 -07001037 function findNodeFromData(d) {
1038 var el = null;
1039 network.node.filter('.' + d.class).each(function(n) {
1040 if (n.id === d.id) {
1041 el = d3.select(this);
1042 }
1043 });
1044 return el;
1045 }
1046
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001047 function selectObject(obj, el) {
1048 var node;
1049 if (el) {
1050 node = d3.select(el);
1051 } else {
1052 network.node.each(function(d) {
1053 if (d == obj) {
1054 node = d3.select(el = this);
1055 }
1056 });
1057 }
1058 if (!node) return;
1059
1060 if (node.classed('selected')) {
1061 deselectObject();
Simon Huntc586e212014-10-28 21:24:08 -07001062 flyinPane(null);
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001063 return;
1064 }
1065 deselectObject(false);
1066
1067 selected = {
1068 obj : obj,
1069 el : el
1070 };
1071
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001072 node.classed('selected', true);
Simon Huntc586e212014-10-28 21:24:08 -07001073 flyinPane(obj);
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001074 }
1075
1076 function deselectObject(doResize) {
1077 // Review: logic of 'resize(...)' function.
1078 if (doResize || typeof doResize == 'undefined') {
1079 resize(false);
1080 }
Simon Hunt9a16c822014-10-28 16:09:19 -07001081
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001082 // deselect all nodes in the network...
1083 network.node.classed('selected', false);
1084 selected = {};
Simon Huntc586e212014-10-28 21:24:08 -07001085 flyinPane(null);
1086 }
1087
1088 function flyinPane(obj) {
1089 var pane = d3.select('#flyout'),
Simon Huntcc267562014-10-29 10:22:17 -07001090 url;
Simon Huntc586e212014-10-28 21:24:08 -07001091
1092 if (obj) {
Simon Huntcc267562014-10-29 10:22:17 -07001093 // go get details of the selected object from the server...
Simon Huntb4d9d4c2014-10-30 11:27:23 -07001094 url = detailJsonUrl(obj.id);
Simon Huntcc267562014-10-29 10:22:17 -07001095 d3.json(url, function (err, data) {
1096 if (err) {
1097 alert('Oops! Error reading JSON...\n\n' +
1098 'URL: ' + url + '\n\n' +
1099 'Error: ' + err.message);
1100 return;
1101 }
1102// console.log("JSON data... " + url);
1103// console.log(data);
1104
1105 displayDetails(data, pane);
1106 });
1107
1108 } else {
1109 // hide pane
1110 pane.transition().duration(750)
1111 .style('right', '-320px')
1112 .style('opacity', 0.0);
1113 }
1114 }
1115
1116 function displayDetails(data, pane) {
1117 $('#flyout').empty();
1118
Thomas Vachuska1de66012014-10-30 03:03:30 -07001119 var title = pane.append("h2"),
1120 table = pane.append("table"),
Simon Huntcc267562014-10-29 10:22:17 -07001121 tbody = table.append("tbody");
1122
Thomas Vachuska1de66012014-10-30 03:03:30 -07001123 $('<img src="img/' + data.type + '.png">').appendTo(title);
1124 $('<span>').attr('class', 'icon').text(data.id).appendTo(title);
1125
1126
Simon Huntcc267562014-10-29 10:22:17 -07001127 // TODO: consider using d3 data bind to TR/TD
1128
1129 data.propOrder.forEach(function(p) {
Thomas Vachuska1de66012014-10-30 03:03:30 -07001130 if (p === '-') {
1131 addSep(tbody);
1132 } else {
1133 addProp(tbody, p, data.props[p]);
1134 }
Simon Huntcc267562014-10-29 10:22:17 -07001135 });
1136
Thomas Vachuska1de66012014-10-30 03:03:30 -07001137 function addSep(tbody) {
1138 var tr = tbody.append('tr');
1139 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
1140 }
1141
Simon Huntcc267562014-10-29 10:22:17 -07001142 function addProp(tbody, label, value) {
1143 var tr = tbody.append('tr');
1144
1145 tr.append('td')
1146 .attr('class', 'label')
1147 .text(label + ' :');
1148
1149 tr.append('td')
1150 .attr('class', 'value')
1151 .text(value);
Simon Huntc586e212014-10-28 21:24:08 -07001152 }
1153
Simon Huntcc267562014-10-29 10:22:17 -07001154 // show pane
Simon Huntc586e212014-10-28 21:24:08 -07001155 pane.transition().duration(750)
Simon Huntcc267562014-10-29 10:22:17 -07001156 .style('right', '20px')
1157 .style('opacity', 1.0);
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001158 }
1159
1160 function highlightObject(obj) {
1161 if (obj) {
1162 if (obj != highlighted) {
1163 // TODO set or clear "inactive" class on nodes, based on criteria
1164 network.node.classed('inactive', function(d) {
1165 // return (obj !== d &&
1166 // d.relation(obj.id));
1167 return (obj !== d);
1168 });
1169 // TODO: same with links
1170 network.link.classed('inactive', function(d) {
1171 return (obj !== d.source && obj !== d.target);
1172 });
1173 }
1174 highlighted = obj;
1175 } else {
1176 if (highlighted) {
1177 // clear the inactive flag (no longer suppressed visually)
1178 network.node.classed('inactive', false);
1179 network.link.classed('inactive', false);
1180 }
1181 highlighted = null;
1182
1183 }
1184 }
1185
Simon Hunt9a16c822014-10-28 16:09:19 -07001186 function hoverObject(obj) {
1187 if (obj) {
1188 hovered = obj;
1189 } else {
1190 if (hovered) {
1191 hovered = null;
1192 }
1193 }
1194 }
1195
1196
Simon Huntc586e212014-10-28 21:24:08 -07001197 function resize() {
Simon Hunt0b05d4a2014-10-21 21:50:15 -07001198 view.height = window.innerHeight - config.mastHeight;
1199 view.width = window.innerWidth;
1200 $('#view')
1201 .css('height', view.height + 'px')
1202 .css('width', view.width + 'px');
1203
1204 network.forceWidth = view.width - config.force.marginLR;
1205 network.forceHeight = view.height - config.force.marginTB;
1206 }
1207
1208 // ======================================================================
1209 // register with the UI framework
1210
1211 api.addView('network', {
1212 load: loadNetworkView
1213 });
1214
1215
1216}(ONOS));
1217