Completed
Pull Request — develop (#75)
by
unknown
01:26
created

draw.js ➔ ... ➔ drawLink   C

Complexity

Conditions 12
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
nc 5
dl 0
loc 22
rs 5.8703
nop 1

How to fix   Complexity   

Complexity

Complex classes like draw.js ➔ ... ➔ drawLink often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
define(['helper'], function (helper) {
2
  var self = this;
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
15
  var cableColor = '#50b0f0';
16
  var highlightColor = 'rgba(255, 255, 255, 0.2)';
17
18
  var labelColor = '#fff';
19
20
  var NODE_RADIUS = 15;
21
  var LINE_RADIUS = 12;
22
23
  function drawDetailNode(d) {
24
    if (transform.k > 1) {
25
      ctx.beginPath();
26
      helper.positionClients(ctx, d, Math.PI, d.o.node.statistics.clients, 15);
27
      ctx.fillStyle = clientColor;
28
      ctx.fill();
29
      ctx.beginPath();
30
      var name = d.o.node_id;
31
      if (d.o.node && d.o.node.nodeinfo) {
32
        name = d.o.node.nodeinfo.hostname;
33
      }
34
      ctx.textAlign = 'center';
35
      ctx.fillStyle = labelColor;
36
      ctx.fillText(name, d.x, d.y + 20);
37
    }
38
  }
39
40
  function drawHighlightNode(d) {
41
    if (highlight && highlight.type === 'node' && d.o.node === highlight.o) {
42
      ctx.arc(d.x, d.y, NODE_RADIUS * 1.5, 0, 2 * Math.PI);
43
      ctx.fillStyle = highlightColor;
44
      ctx.fill();
45
      ctx.beginPath();
46
    }
47
  }
48
49
  function drawHighlightLink(d, to) {
50
    if (highlight && highlight.type === 'link' && d.o === highlight.o) {
51
      ctx.lineTo(to[0], to[1]);
52
      ctx.strokeStyle = highlightColor;
53
      ctx.lineWidth = LINE_RADIUS * 2;
54
      ctx.lineCap = 'round';
55
      ctx.stroke();
56
      to = [d.source.x, d.source.y];
57
    }
58
    return to;
59
  }
60
61
  self.drawNode = function drawNode(d) {
62
    if (d.x < -width || d.y < -height || width < d.x || height < d.y) {
63
      return;
64
    }
65
    ctx.beginPath();
66
67
    drawHighlightNode(d);
68
69
    ctx.moveTo(d.x + 3, d.y);
70
    ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI);
71
    ctx.fillStyle = nodeColor;
72
    ctx.fill();
73
74
    drawDetailNode(d);
75
  };
76
77
  self.drawLink = function drawLink(d) {
78
    if (d.source.x < -width && d.target.x < -width || d.source.y < -height && d.target.y < -height ||
79
        d.source.x > width && d.target.x > width || d.source.y > height && d.target.y > height) {
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.o.type === 'Kabel' ? cableColor : d.color;
90
    if (d.o.type === 'fastd' || d.o.type === 'L2TP') {
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
  };
99
100
  self.setCTX = function setCTX(newValue) {
101
    ctx = newValue;
102
  };
103
  self.setHighlight = function setHighlight(newValue) {
104
    highlight = newValue;
105
  };
106
  self.setTransform = function setTransform(newValue) {
107
    transform = newValue;
108
  };
109
  self.setMaxArea = function setMaxArea(newWidth, newHeight) {
110
    width = newWidth;
111
    height = newHeight;
112
  };
113
  return self;
114
});
115