GUI -- Refactoring updateNodes() to make the code more readable.
Change-Id: Ib375b5573a4670a67dd94a6532caf078be33beb0
Showing
1 changed file
with
64 additions
and
60 deletions
... | @@ -164,11 +164,6 @@ | ... | @@ -164,11 +164,6 @@ |
164 | } | 164 | } |
165 | 165 | ||
166 | 166 | ||
167 | - function updateNodes() { | ||
168 | - $log.debug('TODO updateNodes()...'); | ||
169 | - // TODO... | ||
170 | - } | ||
171 | - | ||
172 | function fStart() { | 167 | function fStart() { |
173 | $log.debug('TODO fStart()...'); | 168 | $log.debug('TODO fStart()...'); |
174 | // TODO... | 169 | // TODO... |
... | @@ -428,8 +423,8 @@ | ... | @@ -428,8 +423,8 @@ |
428 | s = d.el.classed('selected'), | 423 | s = d.el.classed('selected'), |
429 | c = devBaseColor(d), | 424 | c = devBaseColor(d), |
430 | a = instColor(d.master, o), | 425 | a = instColor(d.master, o), |
431 | - g, r, | 426 | + icon = d.el.select('g.deviceIcon'), |
432 | - icon = d.el.select('g.deviceIcon'); | 427 | + g, r; |
433 | 428 | ||
434 | if (s) { | 429 | if (s) { |
435 | g = c.glyph; | 430 | g = c.glyph; |
... | @@ -442,10 +437,8 @@ | ... | @@ -442,10 +437,8 @@ |
442 | r = c.rect; | 437 | r = c.rect; |
443 | } | 438 | } |
444 | 439 | ||
445 | - icon.select('use') | 440 | + icon.select('use').style('fill', g); |
446 | - .style('fill', g); | 441 | + icon.select('rect').style('fill', r); |
447 | - icon.select('rect') | ||
448 | - .style('fill', r); | ||
449 | } | 442 | } |
450 | 443 | ||
451 | function instColor(id, online) { | 444 | function instColor(id, online) { |
... | @@ -459,17 +452,8 @@ | ... | @@ -459,17 +452,8 @@ |
459 | .data(network.nodes, function (d) { return d.id; }); | 452 | .data(network.nodes, function (d) { return d.id; }); |
460 | 453 | ||
461 | // operate on existing nodes... | 454 | // operate on existing nodes... |
462 | - node.filter('.device').each(function (d) { | 455 | + node.filter('.device').each(deviceExisting); |
463 | - var node = d.el; | 456 | + node.filter('.host').each(hostExisting); |
464 | - node.classed('online', d.online); | ||
465 | - updateDeviceLabel(d); | ||
466 | - positionNode(d, true); | ||
467 | - }); | ||
468 | - | ||
469 | - node.filter('.host').each(function (d) { | ||
470 | - updateHostLabel(d); | ||
471 | - positionNode(d, true); | ||
472 | - }); | ||
473 | 457 | ||
474 | // operate on entering nodes: | 458 | // operate on entering nodes: |
475 | var entering = node.enter() | 459 | var entering = node.enter() |
... | @@ -486,15 +470,53 @@ | ... | @@ -486,15 +470,53 @@ |
486 | .transition() | 470 | .transition() |
487 | .attr('opacity', 1); | 471 | .attr('opacity', 1); |
488 | 472 | ||
489 | - // augment device nodes... | 473 | + // augment nodes... |
490 | - entering.filter('.device').each(function (d) { | 474 | + entering.filter('.device').each(deviceEnter); |
475 | + entering.filter('.host').each(hostEnter); | ||
476 | + | ||
477 | + // operate on both existing and new nodes: | ||
478 | + updateDeviceColors(); | ||
479 | + | ||
480 | + // operate on exiting nodes: | ||
481 | + // Note that the node is removed after 2 seconds. | ||
482 | + // Sub element animations should be shorter than 2 seconds. | ||
483 | + var exiting = node.exit() | ||
484 | + .transition() | ||
485 | + .duration(2000) | ||
486 | + .style('opacity', 0) | ||
487 | + .remove(); | ||
488 | + | ||
489 | + // node specific.... | ||
490 | + exiting.filter('.host').each(hostExit); | ||
491 | + exiting.filter('.device').each(deviceExit); | ||
492 | + | ||
493 | + // finally, resume the force layout | ||
494 | + fResume(); | ||
495 | + } | ||
496 | + | ||
497 | + // ========================== | ||
498 | + // updateNodes - subfunctions | ||
499 | + | ||
500 | + function deviceExisting(d) { | ||
501 | + var node = d.el; | ||
502 | + node.classed('online', d.online); | ||
503 | + updateDeviceLabel(d); | ||
504 | + positionNode(d, true); | ||
505 | + } | ||
506 | + | ||
507 | + function hostExisting(d) { | ||
508 | + updateHostLabel(d); | ||
509 | + positionNode(d, true); | ||
510 | + } | ||
511 | + | ||
512 | + function deviceEnter(d) { | ||
491 | var node = d3.select(this), | 513 | var node = d3.select(this), |
492 | glyphId = d.type || 'unknown', | 514 | glyphId = d.type || 'unknown', |
493 | label = trimLabel(deviceLabel(d)), | 515 | label = trimLabel(deviceLabel(d)), |
516 | + devCfg = deviceIconConfig, | ||
494 | noLabel = !label, | 517 | noLabel = !label, |
495 | box, dx, dy, icon; | 518 | box, dx, dy, icon; |
496 | 519 | ||
497 | - // provide ref to element from backing data.... | ||
498 | d.el = node; | 520 | d.el = node; |
499 | 521 | ||
500 | node.append('rect').attr({ rx: 5, ry: 5 }); | 522 | node.append('rect').attr({ rx: 5, ry: 5 }); |
... | @@ -503,58 +525,43 @@ | ... | @@ -503,58 +525,43 @@ |
503 | node.select('rect').attr(box); | 525 | node.select('rect').attr(box); |
504 | 526 | ||
505 | icon = is.addDeviceIcon(node, glyphId); | 527 | icon = is.addDeviceIcon(node, glyphId); |
506 | - d.iconDim = icon.dim; | ||
507 | 528 | ||
508 | if (noLabel) { | 529 | if (noLabel) { |
509 | dx = -icon.dim/2; | 530 | dx = -icon.dim/2; |
510 | dy = -icon.dim/2; | 531 | dy = -icon.dim/2; |
511 | } else { | 532 | } else { |
512 | box = adjustRectToFitText(node); | 533 | box = adjustRectToFitText(node); |
513 | - dx = box.x + iconConfig.xoff; | 534 | + dx = box.x + devCfg.xoff; |
514 | - dy = box.y + iconConfig.yoff; | 535 | + dy = box.y + devCfg.yoff; |
515 | } | 536 | } |
516 | 537 | ||
517 | icon.attr('transform', sus.translate(dx, dy)); | 538 | icon.attr('transform', sus.translate(dx, dy)); |
518 | - }); | 539 | + } |
519 | 540 | ||
520 | - // augment host nodes... | 541 | + function hostEnter(d) { |
521 | - entering.filter('.host').each(function (d) { | 542 | + var node = d3.select(this); |
522 | - var node = d3.select(this), | 543 | + |
523 | - cfg = config.icons.host, | 544 | + //cfg = config.icons.host, |
524 | - r = cfg.radius[d.type] || cfg.defaultRadius, | 545 | + //r = cfg.radius[d.type] || cfg.defaultRadius, |
525 | - textDy = r + 10, | 546 | + //textDy = r + 10, |
526 | //TODO: iid = iconGlyphUrl(d), | 547 | //TODO: iid = iconGlyphUrl(d), |
527 | - _dummy; | 548 | + // _dummy; |
528 | 549 | ||
529 | - // provide ref to element from backing data.... | ||
530 | d.el = node; | 550 | d.el = node; |
531 | 551 | ||
532 | //TODO: showHostVis(node); | 552 | //TODO: showHostVis(node); |
533 | 553 | ||
534 | node.append('circle').attr('r', r); | 554 | node.append('circle').attr('r', r); |
535 | - if (iid) { | 555 | + //if (iid) { |
536 | //TODO: addHostIcon(node, r, iid); | 556 | //TODO: addHostIcon(node, r, iid); |
537 | - } | 557 | + //} |
538 | node.append('text') | 558 | node.append('text') |
539 | .text(hostLabel) | 559 | .text(hostLabel) |
540 | - .attr('dy', textDy) | 560 | + //.attr('dy', textDy) |
541 | .attr('text-anchor', 'middle'); | 561 | .attr('text-anchor', 'middle'); |
542 | - }); | 562 | + } |
543 | - | ||
544 | - // operate on both existing and new nodes, if necessary | ||
545 | - updateDeviceColors(); | ||
546 | - | ||
547 | - // operate on exiting nodes: | ||
548 | - // Note that the node is removed after 2 seconds. | ||
549 | - // Sub element animations should be shorter than 2 seconds. | ||
550 | - var exiting = node.exit() | ||
551 | - .transition() | ||
552 | - .duration(2000) | ||
553 | - .style('opacity', 0) | ||
554 | - .remove(); | ||
555 | 563 | ||
556 | - // host node exits.... | 564 | + function hostExit(d) { |
557 | - exiting.filter('.host').each(function (d) { | ||
558 | var node = d.el; | 565 | var node = d.el; |
559 | node.select('use') | 566 | node.select('use') |
560 | .style('opacity', 0.5) | 567 | .style('opacity', 0.5) |
... | @@ -575,10 +582,9 @@ | ... | @@ -575,10 +582,9 @@ |
575 | .transition() | 582 | .transition() |
576 | .duration(1500) | 583 | .duration(1500) |
577 | .attr('r', 0); | 584 | .attr('r', 0); |
578 | - }); | 585 | + } |
579 | 586 | ||
580 | - // device node exits.... | 587 | + function deviceExit(d) { |
581 | - exiting.filter('.device').each(function (d) { | ||
582 | var node = d.el; | 588 | var node = d.el; |
583 | node.select('use') | 589 | node.select('use') |
584 | .style('opacity', 0.5) | 590 | .style('opacity', 0.5) |
... | @@ -590,8 +596,6 @@ | ... | @@ -590,8 +596,6 @@ |
590 | .style('stroke-fill', '#555') | 596 | .style('stroke-fill', '#555') |
591 | .style('fill', '#888') | 597 | .style('fill', '#888') |
592 | .style('opacity', 0.5); | 598 | .style('opacity', 0.5); |
593 | - }); | ||
594 | - fResume(); | ||
595 | } | 599 | } |
596 | 600 | ||
597 | 601 | ... | ... |
-
Please register or login to post a comment