Completed
Pull Request — develop (#105)
by Xaver
01:04
created

lib/forcegraph/draw.js   A

Complexity

Total Complexity 32
Complexity/F 3.2

Size

Lines of Code 119
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 32
c 2
b 0
f 0
nc 1656
mnd 2
bc 18
fnc 10
dl 0
loc 119
rs 9.6
bpm 1.8
cpm 3.2
noi 0
1
define(['helper'], function (helper) {
2
  var self = {};
3
4
  var ctx;
5
  var width;
6
  var height;
7
8
  var transform;
9
10
  var highlight;
11
12
  var nodeColor = '#fff';
13
  var clientColor = '#e6324b';
14
  var highlightColor = 'rgba(255, 255, 255, 0.2)';
15
16
  var labelColor = '#fff';
17
18
  var NODE_RADIUS = 15;
19
  var LINE_RADIUS = 12;
20
21
  function drawDetailNode(d) {
22
    if (transform.k > 1) {
23
      ctx.beginPath();
24
      helper.positionClients(ctx, d, Math.PI, d.o.node.statistics.clients, 15);
25
      ctx.fillStyle = clientColor;
26
      ctx.fill();
27
      ctx.beginPath();
28
      var name = d.o.node_id;
29
      if (d.o.node && d.o.node.nodeinfo) {
30
        name = d.o.node.nodeinfo.hostname;
31
      }
32
      ctx.textAlign = 'center';
33
      ctx.fillStyle = labelColor;
34
      ctx.fillText(name, d.x, d.y + 20);
35
    }
36
  }
37
38
  function drawHighlightNode(d) {
39
    if (highlight && highlight.type === 'node' && d.o.node === highlight.o) {
40
      ctx.arc(d.x, d.y, NODE_RADIUS * 1.5, 0, 2 * Math.PI);
41
      ctx.fillStyle = highlightColor;
42
      ctx.fill();
43
      ctx.beginPath();
44
    }
45
  }
46
47
  function drawHighlightLink(d, to) {
48
    if (highlight && highlight.type === 'link' && d.o === highlight.o) {
49
      ctx.lineTo(to[0], to[1]);
50
      ctx.strokeStyle = highlightColor;
51
      ctx.lineWidth = LINE_RADIUS * 2;
52
      ctx.lineCap = 'round';
53
      ctx.stroke();
54
      to = [d.source.x, d.source.y];
55
    }
56
    return to;
57
  }
58
59
  self.drawNode = function drawNode(d) {
60
    if (d.x < transform.invertX(0) || d.y < transform.invertY(0) || transform.invertX(width) < d.x || transform.invertY(height) < d.y) {
61
      return;
62
    }
63
    ctx.beginPath();
64
65
    drawHighlightNode(d);
66
67
    ctx.moveTo(d.x + 3, d.y);
68
    ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI);
69
    ctx.fillStyle = nodeColor;
70
    ctx.fill();
71
72
    drawDetailNode(d);
73
  };
74
75
  self.drawLink = function drawLink(d) {
76
    var zero = transform.invert([0, 0]);
77
    var area = transform.invert([width, height]);
78
    if (d.source.x < zero[0] && d.target.x < zero[0] || d.source.y < zero[1] && d.target.y < zero[1] ||
79
        d.source.x > area[0] && d.target.x > area[0] || d.source.y > area[1] && d.target.y > area[1]) {
80
      return;
81
    }
82
    ctx.beginPath();
83
    ctx.moveTo(d.source.x, d.source.y);
84
    var to = [d.target.x, d.target.y];
85
86
    to = drawHighlightLink(d, to);
87
88
    ctx.lineTo(to[0], to[1]);
89
    ctx.strokeStyle = d.color;
90
    if (d.o.vpn) {
91
      ctx.globalAlpha = 0.2;
92
      ctx.lineWidth = 1.5;
93
    } else {
94
      ctx.globalAlpha = 0.8;
95
      ctx.lineWidth = 2.5;
96
    }
97
    ctx.stroke();
98
    ctx.globalAlpha = 1;
99
  };
100
101
  self.setCTX = function setCTX(newValue) {
102
    ctx = newValue;
103
  };
104
105
  self.setHighlight = function setHighlight(newValue) {
106
    highlight = newValue;
107
  };
108
109
  self.setTransform = function setTransform(newValue) {
110
    transform = newValue;
111
  };
112
113
  self.setMaxArea = function setMaxArea(newWidth, newHeight) {
114
    width = newWidth;
115
    height = newHeight;
116
  };
117
118
  return self;
119
});
120