summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-olsr-services/htdocs/luci-static/resources/view/freifunk-services/services.js
blob: 57bd83d93fe9dcf68f2ed0de901c0d7c8b60cdf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
'require rpc';
'require view';
'require poll';

var getOlsrd4Services = rpc.declare({
    object: 'olsr-services',
    method: 'services4',
    expect: {}
});

var getOlsrd6Services = rpc.declare({
    object: 'olsr-services',
    method: 'services6',
    expect: {}
});

function createTableData(servicesArray) {
    var tableData = [];
    servicesArray.forEach(function (service) {
        var sourceUrl = service.isIpv6 ? '[' + service.source + ']' : service.source;
        tableData.push(
            [
                E('a', { 'href': service.url }, service.description),
                service.protocol,
                E('a', { 'href': 'http://' + sourceUrl + '/cgi-bin-status.html' }, service.source)
            ]
        );
    });
    return tableData;
}

function extractServiceInformation(results) {
    var servicesArray = [];
    results.forEach(function(result) {
        if (result.configured && result.services != "") {
            var isIpv6 = result.source == "olsrd6";
            var services = result.services.split('\n');
            services.forEach(function (service) {
                var source = service.split('#')[1];
                var serviceRawDescription = service.replace(/\t/g, '').split('#')[0].split('|');
                var url = serviceRawDescription[0];
                var protocol = serviceRawDescription[1];
                var description = serviceRawDescription[2];
                servicesArray.push({ "source": source, "url": url, "protocol": protocol, "description": description, "isIpv6": isIpv6 });
            });
        }
    });
    return servicesArray;
}

return view.extend({
    handleSaveApply: null,
    handleSave: null,
    handleReset: null,
    render: function (data) {
        poll.add(function () {
            Promise.all([getOlsrd4Services(), getOlsrd6Services()]).then(function (results) {
                var servicesArray = extractServiceInformation(results);
                cbi_update_table("#olsr_services", createTableData(servicesArray));
            });
        }, 30);
        return E([], {}, [
            E('h2', { 'name': 'content' }, [_('Services')]),
            E('legend', {}, [_('Internal services')]),
            E('fieldset', { 'class': 'cbi-section' }, [
                E('table', { 'id': 'olsr_services' }, [
                    E('tr', { 'class' : 'tr table-titles'}, [
                        E('td', { 'class' : 'th'}, _('Url')),
                        E('td', { 'class' : 'th'}, _('Protocol')),
                        E('td', { 'class' : 'th'}, _('Source'))
                    ]),
                ])
            ]),
        ]);
    }
});