| Conditions | 1 |
| Paths | 368 |
| Total Lines | 114 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 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 |