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

node.js ➔ ... ➔ showGateway   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 25
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
        ];
51
52
        if (node.gatewayCols !== undefined) {
53
          gatewayRows.push(V.h('span', [
0 ignored issues
show
Bug introduced by
The variable gatewayRows seems to be never declared. If this is a global, consider adding a /** global: gatewayRows */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

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