Completed
Pull Request — develop (#218)
by
unknown
32s
created

node.js ➔ ... ➔ showGateway   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
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 gatewayRows = [
39
          V.h('div', [
40
            'IPv4 ',
41
            nodeIdLink(node.gateway)
42
          ])
43
        ];
44
        if (node.gateway6 !== undefined) {
45
          gatewayRows.push(V.h('div', [
46
            'IPv6 ',
47
            nodeIdLink(node.gateway6)
48
          ]));
49
        }
50
        var gatewayValue = [
51
          V.h('span', [
52
            nodeIdLink(node.gateway_nexthop),
53
            V.h('br'),
54
            _.t('node.nexthop')
55
          ]),
56
          V.h('span', { props: { className: 'ion-arrow-right-c' } }),
57
          V.h('span', gatewayRows)
58
        ];
59
60
        return V.h('td', { props: { className: 'gateway' } }, gatewayValue);
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 (nodef['show' + row.value] !== undefined) {
130
            field = nodef['show' + row.value](d);
131
          }
132
133
          if (field) {
134
            if (typeof field !== 'object') {
135
              field = V.h('td', field);
136
            }
137
138
            children.push(V.h('tr', [
139
              V.h('th', _.t(row.name)),
140
              field
141
            ]));
142
          }
143
        });
144
145
        children.push(V.h('tr', [
146
          V.h('th', _.t('node.gateway')),
147
          showGateway(d)
148
        ]));
149
150
        var elNew = V.h('table', children);
151
        table = V.patch(table, elNew);
152
        table.elm.classList.add('attributes');
153
154
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
155
        if (d.neighbours.length > 0) {
156
          tableNeighbour.setData(d.neighbours);
157
          tableNeighbour.el.elm.classList.add('node-links');
158
        }
159
160
        if (config.nodeInfos) {
161
          var img = [];
162
          config.nodeInfos.forEach(function (nodeInfo) {
163
            img.push(V.h('h4', nodeInfo.name));
164
            img.push(showStatImg(nodeInfo, d));
165
          });
166
          images = V.patch(images, V.h('div', img));
167
        }
168
      };
169
170
      self.setData = function setData(data) {
171
        if (data.nodeDict[d.node_id]) {
172
          d = data.nodeDict[d.node_id];
173
        }
174
        self.render();
175
      };
176
      return self;
177
    };
178
  });
179