Completed
Pull Request — develop (#217)
by Xaver
26s
created

node.js ➔ ... ➔ config.nodeAttr.forEach   B

Complexity

Conditions 6
Paths 15

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
nc 15
dl 0
loc 18
rs 8.8571
nop 1
1
define(['sorttable', 'snabbdom', 'd3-interpolate', 'moment', 'helper', 'utils/node'],
2
  function (SortTable, V, d3Interpolate, moment, 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
          V.h('span', [
51
            nodeIdLink(node.gateway6),
52
            V.h('br'),
53
            'IPv6'
54
          ])
55
        ];
56
57
        return V.h('td', { props: { className: 'gateway' } }, gatewayCols);
58
      }
59
60
      function renderNeighbourRow(n) {
61
        var icons = '';
62
        if (helper.hasLocation(n.node)) {
63
          icons = V.h('span', { props: { className: 'ion-location' } });
64
        }
65
66
        return V.h('tr', [
67
          V.h('td', icons),
68
          V.h('td', nodeLink(n.node)),
69
          V.h('td', n.node.clients),
70
          V.h('td', { style: { color: linkScale((n.link.source_tq + n.link.target_tq) / 2) } }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq)),
71
          V.h('td', helper.showDistance(n.link))
72
        ]);
73
      }
74
75
      var self = this;
76
      var header = document.createElement('h2');
77
      var table = document.createElement('table');
78
      var images = document.createElement('div');
79
      var neighbours = document.createElement('h3');
80
      var headings = [{
81
        name: ''
82
      }, {
83
        name: 'node.nodes',
84
        sort: function (a, b) {
85
          return a.node.hostname.localeCompare(b.node.hostname);
86
        },
87
        reverse: false
88
      }, {
89
        name: 'node.clients',
90
        class: 'ion-people',
91
        sort: function (a, b) {
92
          return a.node.clients - b.node.clients;
93
        },
94
        reverse: true
95
      }, {
96
        name: 'node.tq',
97
        class: 'ion-connection-bars',
98
        sort: function (a, b) {
99
          return a.link.source_tq - b.link.source_tq;
100
        },
101
        reverse: true
102
      }, {
103
        name: 'node.distance',
104
        class: 'ion-arrow-resize',
105
        sort: function (a, b) {
106
          return (a.link.distance === undefined ? -1 : a.link.distance) -
107
            (b.link.distance === undefined ? -1 : b.link.distance);
108
        },
109
        reverse: true
110
      }];
111
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
112
113
      el.appendChild(header);
114
      el.appendChild(table);
115
      el.appendChild(neighbours);
116
      el.appendChild(tableNeighbour.el);
117
      el.appendChild(images);
118
119
      self.render = function render() {
120
        V.patch(header, V.h('h2', d.hostname));
121
122
        var children = [];
123
124
        config.nodeAttr.forEach(function (row) {
125
          var field = d[row.value];
126
          if (typeof row.value === 'function') {
127
            field = row.value(d, nodeDict);
128
          } else if (nodef['show' + row.value] !== undefined) {
129
            field = nodef['show' + row.value](d);
130
          }
131
132
          if (field) {
133
            if (typeof field !== 'object') {
134
              field = V.h('td', field);
135
            }
136
            children.push(V.h('tr', [
137
              row.name !== undefined ? V.h('th', _.t(row.name)) : null,
138
              field
139
            ]));
140
          }
141
        });
142
143
        children.push(V.h('tr', [
144
          V.h('th', _.t('node.gateway')),
145
          showGateway(d)
146
        ]));
147
148
        var elNew = V.h('table', children);
149
        table = V.patch(table, elNew);
150
        table.elm.classList.add('attributes');
151
152
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
153
        if (d.neighbours.length > 0) {
154
          tableNeighbour.setData(d.neighbours);
155
          tableNeighbour.el.elm.classList.add('node-links');
156
        }
157
158
        if (config.nodeInfos) {
159
          var img = [];
160
          config.nodeInfos.forEach(function (nodeInfo) {
161
            img.push(V.h('h4', nodeInfo.name));
162
            img.push(showStatImg(nodeInfo, d));
163
          });
164
          images = V.patch(images, V.h('div', img));
165
        }
166
      };
167
168
      self.setData = function setData(data) {
169
        if (data.nodeDict[d.node_id]) {
170
          d = data.nodeDict[d.node_id];
171
        }
172
        self.render();
173
      };
174
      return self;
175
    };
176
  });
177