Completed
Pull Request — develop (#125)
by Xaver
01:05
created

nodelist.js ➔ ... ➔ V.h(ꞌaꞌ).on.click   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
define(['sorttable', 'snabbdom', 'helper'], function (SortTable, V, helper) {
2
  'use strict';
3
  V = V.default;
4
5
  function getUptime(now, d) {
6
    if (d.flags.online && 'uptime' in d.statistics) {
7
      return Math.round(d.statistics.uptime);
8
    } else if (!d.flags.online && 'lastseen' in d) {
9
      return Math.round(-(now.unix() - d.lastseen.unix()));
10
    }
11
    return 0;
12
  }
13
14
  function showUptime(uptime) {
15
    var s = '';
16
    uptime /= 3600;
17
18
    if (uptime !== undefined) {
19
      if (Math.abs(uptime) >= 24) {
20
        s = Math.round(uptime / 24) + 'd';
21
      } else {
22
        s = Math.round(uptime) + 'h';
23
      }
24
    }
25
26
    return s;
27
  }
28
29
  var headings = [{
30
    name: ''
31
  }, {
32
    name: 'node.nodes',
33
    sort: function (a, b) {
34
      return a.nodeinfo.hostname.localeCompare(b.nodeinfo.hostname);
35
    },
36
    reverse: false
37
  }, {
38
    name: 'node.uptime',
39
    class: 'ion-time',
40
    sort: function (a, b) {
41
      return a.uptime - b.uptime;
42
    },
43
    reverse: true
44
  }, {
45
    name: 'node.links',
46
    class: 'ion-share-alt',
47
    sort: function (a, b) {
48
      return a.meshlinks - b.meshlinks;
49
    },
50
    reverse: true
51
  }, {
52
    name: 'node.clients',
53
    class: 'ion-people',
54
    sort: function (a, b) {
55
      return ('clients' in a.statistics ? a.statistics.clients : -1) -
56
        ('clients' in b.statistics ? b.statistics.clients : -1);
57
    },
58
    reverse: true
59
  }];
60
61
  return function (router) {
62
    function renderRow(d) {
63
      var td0Content = [];
64
      var td1Content = [];
65
      var aClass = ['hostname', d.flags.online ? 'online' : 'offline'];
66
67
      td1Content.push(V.h('a', {
68
        props: {
69
          className: aClass.join(' '),
70
          href: router.generateLink({ node: d.nodeinfo.node_id })
71
        }, on: {
72
          click: function (e) {
73
            router.fullUrl({ node: d.nodeinfo.node_id }, e);
74
          }
75
        }
76
      }, d.nodeinfo.hostname));
77
78
      if (helper.hasLocation(d)) {
79
        td0Content.push(V.h('span', { props: { className: 'icon ion-location' } }));
80
      }
81
82
      var td0 = V.h('td', td0Content);
83
      var td1 = V.h('td', td1Content);
84
      var td2 = V.h('td', showUptime(d.uptime));
85
      var td3 = V.h('td', d.meshlinks.toString());
86
      var td4 = V.h('td', Number('clients' in d.statistics ? d.statistics.clients : 0).toFixed(0));
87
88
      return V.h('tr', [td0, td1, td2, td3, td4]);
89
    }
90
91
    var table = new SortTable(headings, 1, renderRow);
92
93
    this.render = function render(d) {
94
      var h2 = document.createElement('h2');
95
      h2.textContent = _.t('node.all');
96
      d.appendChild(h2);
97
      table.el.elm.classList.add('node-list');
98
      d.appendChild(table.el.elm);
99
    };
100
101
    this.setData = function setData(d) {
102
      var data = d.nodes.all.map(function (e) {
103
        var n = Object.create(e);
104
        n.uptime = getUptime(d.now, e);
105
        n.meshlinks = e.meshlinks || 0;
106
        return n;
107
      });
108
109
      table.setData(data);
110
    };
111
  };
112
});
113