lib/forcegraph/draw.js   A
last analyzed

Complexity

Total Complexity 19
Complexity/F 1.9

Size

Lines of Code 120
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 82
mnd 9
bc 9
fnc 10
dl 0
loc 120
rs 10
bpm 0.9
cpm 1.9
noi 0
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A draw.js ➔ drawHighlightNode 0 8 2
A draw.js ➔ drawDetailNode 0 13 4
A draw.js ➔ setHighlight 0 3 1
A draw.js ➔ drawHighlightLink 0 11 2
A draw.js ➔ setCTX 0 3 1
A draw.js ➔ setTransform 0 3 1
A draw.js ➔ setMaxArea 0 4 1
A draw.js ➔ drawNode 0 23 4
A draw.js ➔ drawLink 0 29 3
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
    if (d.o.is_online) {
57
      ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI);
58
      if (d.o.is_gateway) {
59
        ctx.rect(d.x - 9, d.y - 9, 18, 18);
60
      }
61
      ctx.fillStyle = config.forceGraph.nodeColor;
62
    } else {
63
      ctx.arc(d.x, d.y, 6, 0, 2 * Math.PI);
64
      ctx.fillStyle = config.forceGraph.nodeOfflineColor;
65
    }
66
67
    ctx.fill();
68
69
    drawDetailNode(d);
70
  };
71
72
  self.drawLink = function drawLink(d) {
73
    var zero = transform.invert([0, 0]);
74
    var area = transform.invert([width, height]);
75
    if (d.source.x < zero[0] && d.target.x < zero[0] || d.source.y < zero[1] && d.target.y < zero[1] ||
76
      d.source.x > area[0] && d.target.x > area[0] || d.source.y > area[1] && d.target.y > area[1]) {
77
      return;
78
    }
79
    ctx.beginPath();
80
    ctx.moveTo(d.source.x, d.source.y);
81
    var to = [d.target.x, d.target.y];
82
83
    to = drawHighlightLink(d, to);
84
85
    var grd = ctx.createLinearGradient(d.source.x, d.source.y, d.target.x, d.target.y);
86
    grd.addColorStop(0.45, d.color);
87
    grd.addColorStop(0.55, d.color_to);
88
89
    ctx.lineTo(to[0], to[1]);
90
    ctx.strokeStyle = grd;
91
    if (d.o.type.indexOf('vpn') === 0) {
92
      ctx.globalAlpha = 0.2;
93
      ctx.lineWidth = 1.5;
94
    } else {
95
      ctx.globalAlpha = 0.8;
96
      ctx.lineWidth = 2.5;
97
    }
98
    ctx.stroke();
99
    ctx.globalAlpha = 1;
100
  };
101
102
  self.setCTX = function setCTX(newValue) {
103
    ctx = newValue;
104
  };
105
106
  self.setHighlight = function setHighlight(newValue) {
107
    highlight = newValue;
108
  };
109
110
  self.setTransform = function setTransform(newValue) {
111
    transform = newValue;
112
  };
113
114
  self.setMaxArea = function setMaxArea(newWidth, newHeight) {
115
    width = newWidth;
116
    height = newHeight;
117
  };
118
119
  return self;
120
});
121