Completed
Pull Request — develop (#241)
by Xaver
28s
created

lib/forcegraph/draw.js   A

Complexity

Total Complexity 34
Complexity/F 3.4

Size

Lines of Code 122
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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