Completed
Pull Request — develop (#123)
by Xaver
01:18
created

simplenodelist.js ➔ ... ➔ V.h(ꞌaꞌ).onclick   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(['moment', 'virtual-dom', 'helper'], function (moment, V, helper) {
2
  'use strict';
3
4
  return function (nodes, field, router, title) {
5
    var self = this;
6
    var el;
7
    var tbody;
8
9
    self.render = function render(d) {
10
      el = d;
11
    };
12
13
    self.setData = function setData(data) {
14
      var list = data.nodes[nodes];
15
16
      if (list.length === 0) {
17
        tbody = null;
18
        return;
19
      }
20
21
      if (!tbody) {
22
        var h2 = document.createElement('h2');
23
        h2.textContent = title;
24
        el.appendChild(h2);
25
26
        var table = document.createElement('table');
27
        table.classList.add('node-list');
28
        el.appendChild(table);
29
30
        tbody = document.createElement('tbody');
31
        tbody.last = V.h('tbody');
32
        table.appendChild(tbody);
33
      }
34
35
      var items = list.map(function (d) {
36
        var time = moment(d[field]).from(data.now);
37
        var td0Content = [];
38
        var td1Content = [];
39
40
        var aClass = ['hostname', d.flags.online ? 'online' : 'offline'];
41
42
        td1Content.push(V.h('a', {
43
          className: aClass.join(' '),
44
          href: router.generateLink({ node: d.nodeinfo.node_id }),
45
          onclick: function (e) {
46
            router.fullUrl({ node: d.nodeinfo.node_id }, e);
47
          }
48
        }, d.nodeinfo.hostname));
49
50
        if (helper.hasLocation(d)) {
51
          td0Content.push(V.h('span', { className: 'icon ion-location' }));
52
        }
53
54
        var td0 = V.h('td', td0Content);
55
        var td1 = V.h('td', td1Content);
56
        var td2 = V.h('td', time);
57
58
        return V.h('tr', [td0, td1, td2]);
59
      });
60
61
      var tbodyNew = V.h('tbody', items);
62
      tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew));
63
      tbody.last = tbodyNew;
64
    };
65
66
    return self;
67
  };
68
});
69