node.js ➔ render   F
last analyzed

Complexity

Conditions 17

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 48
rs 1.8
c 0
b 0
f 0
cc 17

How to fix   Complexity   

Complexity

Complex classes like node.js ➔ render 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(['sorttable', 'snabbdom', 'd3-interpolate', 'helper', 'utils/node'],
2
  function (SortTable, V, d3Interpolate, helper, nodef) {
3
    'use strict';
4
    V = V.default;
5
6
    function showStatImg(o, d) {
7
      var subst = {
8
        '{NODE_ID}': d.node_id,
9
        '{NODE_NAME}': d.hostname.replace(/[^a-z0-9\-]/ig, '_'),
10
        '{TIME}': d.lastseen.format('DDMMYYYYHmmss'),
11
        '{LOCALE}': _.locale()
12
      };
13
      return helper.showStat(V, o, subst);
14
    }
15
16
    return function (el, d, linkScale, nodeDict) {
17
      function nodeLink(node) {
18
        return V.h('a', {
19
          props: {
20
            className: node.is_online ? 'online' : 'offline',
21
            href: router.generateLink({ node: node.node_id })
22
          }, on: {
23
            click: function (e) {
24
              router.fullUrl({ node: node.node_id }, e);
25
            }
26
          }
27
        }, node.hostname);
28
      }
29
30
      function nodeIdLink(nodeId) {
31
        if (nodeDict[nodeId]) {
32
          return nodeLink(nodeDict[nodeId]);
33
        }
34
        return nodeId;
35
      }
36
37
      function showGateway(node) {
38
        var gatewayCols = [
39
          V.h('span', [
40
            nodeIdLink(node.gateway_nexthop),
41
            V.h('br'),
42
            _.t('node.nexthop')
43
          ]),
44
          V.h('span', { props: { className: 'ion-arrow-right-c' } }),
45
          V.h('span', [
46
            nodeIdLink(node.gateway),
47
            V.h('br'),
48
            'IPv4'
49
          ])
50
        ];
51
52
        if (node.gateway6 !== undefined) {
53
          gatewayCols.push(V.h('span', [
54
            nodeIdLink(node.gateway6),
55
            V.h('br'),
56
            'IPv6'
57
          ]));
58
        }
59
60
        return V.h('td', { props: { className: 'gateway' } }, gatewayCols);
61
      }
62
63
      function renderNeighbourRow(n) {
64
        var icons = [V.h('span', { props: { className: 'icon ion-' + (n.link.type.indexOf('wifi') === 0 ? 'wifi' : 'share-alt'), title: _.t(n.link.type) } })];
65
        if (helper.hasLocation(n.node)) {
66
          icons.push(V.h('span', { props: { className: 'ion-location', title: _.t('location.location') } }));
67
        }
68
69
        return V.h('tr', [
70
          V.h('td', icons),
71
          V.h('td', nodeLink(n.node)),
72
          V.h('td', n.node.clients),
73
          V.h('td', [V.h('a', {
74
            style: {
75
              color: linkScale((n.link.source_tq + n.link.target_tq) / 2)
76
            },
77
            props: {
78
              title: n.link.source.hostname + ' - ' + n.link.target.hostname,
79
              href: router.generateLink({ link: n.link.id })
80
            }, on: {
81
              click: function (e) {
82
                router.fullUrl({ link: n.link.id }, e);
83
              }
84
            }
85
          }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq))]),
86
          V.h('td', helper.showDistance(n.link))
87
        ]);
88
      }
89
90
      var self = this;
91
      var header = document.createElement('h2');
92
      var table = document.createElement('table');
93
      var images = document.createElement('div');
94
      var neighbours = document.createElement('h3');
95
      var headings = [{
96
        name: '',
97
        sort: function (a, b) {
98
          return a.link.type.localeCompare(b.link.type);
99
        }
100
      }, {
101
        name: 'node.nodes',
102
        sort: function (a, b) {
103
          return a.node.hostname.localeCompare(b.node.hostname);
104
        },
105
        reverse: false
106
      }, {
107
        name: 'node.clients',
108
        class: 'ion-people',
109
        sort: function (a, b) {
110
          return a.node.clients - b.node.clients;
111
        },
112
        reverse: true
113
      }, {
114
        name: 'node.tq',
115
        class: 'ion-connection-bars',
116
        sort: function (a, b) {
117
          return a.link.source_tq - b.link.source_tq;
118
        },
119
        reverse: true
120
      }, {
121
        name: 'node.distance',
122
        class: 'ion-arrow-resize',
123
        sort: function (a, b) {
124
          return (a.link.distance === undefined ? -1 : a.link.distance) -
125
            (b.link.distance === undefined ? -1 : b.link.distance);
126
        },
127
        reverse: true
128
      }];
129
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
130
131
      el.appendChild(header);
132
      el.appendChild(table);
133
      el.appendChild(neighbours);
134
      el.appendChild(tableNeighbour.el);
135
      el.appendChild(images);
136
137
      self.render = function render() {
138
        V.patch(header, V.h('h2', d.hostname));
139
140
        var children = [];
141
142
        config.nodeAttr.forEach(function (row) {
143
          var field = d[row.value];
144
          if (typeof row.value === 'function') {
145
            field = row.value(d, nodeDict);
146
          } else if (nodef['show' + row.value] !== undefined) {
147
            field = nodef['show' + row.value](d);
148
          }
149
150
          if (field) {
151
            if (typeof field !== 'object') {
152
              field = V.h('td', field);
153
            }
154
            children.push(V.h('tr', [
155
              row.name !== undefined ? V.h('th', _.t(row.name)) : null,
156
              field
157
            ]));
158
          }
159
        });
160
161
        children.push(V.h('tr', [
162
          V.h('th', _.t('node.gateway')),
163
          showGateway(d)
164
        ]));
165
166
        var elNew = V.h('table', children);
167
        table = V.patch(table, elNew);
168
        table.elm.classList.add('attributes');
169
170
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
171
        if (d.neighbours.length > 0) {
172
          tableNeighbour.setData(d.neighbours);
173
          tableNeighbour.el.elm.classList.add('node-links');
174
        }
175
176
        if (config.nodeInfos) {
177
          var img = [];
178
          config.nodeInfos.forEach(function (nodeInfo) {
179
            img.push(V.h('h4', nodeInfo.name));
180
            img.push(showStatImg(nodeInfo, d));
181
          });
182
          images = V.patch(images, V.h('div', img));
183
        }
184
      };
185
186
      self.setData = function setData(data) {
187
        if (data.nodeDict[d.node_id]) {
188
          d = data.nodeDict[d.node_id];
189
        }
190
        self.render();
191
      };
192
      return self;
193
    };
194
  });
195