01a8e1835a5d03af351a4e4f03406ea1c64a9342
[project/luci.git] / modules / luci-mod-status / htdocs / luci-static / resources / view / status / index.js
1 'use strict';
2 'require fs';
3 'require network';
4
5 function invokeIncludesLoad(includes) {
6 var tasks = [], has_load = false;
7
8 for (var i = 0; i < includes.length; i++) {
9 if (typeof(includes[i].load) == 'function') {
10 tasks.push(includes[i].load());
11 has_load = true;
12 }
13 else {
14 tasks.push(null);
15 }
16 }
17
18 return has_load ? Promise.all(tasks) : Promise.resolve(null);
19 }
20
21 function startPolling(includes, containers) {
22 var step = function() {
23 return network.flushCache().then(function() {
24 return invokeIncludesLoad(includes);
25 }).then(function(results) {
26 for (var i = 0; i < includes.length; i++) {
27 var content = null;
28
29 if (typeof(includes[i].render) == 'function')
30 content = includes[i].render(results ? results[i] : null);
31 else if (includes[i].content != null)
32 content = includes[i].content;
33
34 if (content != null) {
35 containers[i].parentNode.style.display = '';
36 containers[i].parentNode.classList.add('fade-in');
37
38 L.dom.content(containers[i], content);
39 }
40 }
41
42 var ssi = document.querySelector('div.includes');
43 if (ssi) {
44 ssi.style.display = '';
45 ssi.classList.add('fade-in');
46 }
47 });
48 };
49
50 return step().then(function() {
51 L.Poll.add(step);
52 });
53 }
54
55 return L.view.extend({
56 load: function() {
57 return L.resolveDefault(fs.list('/www' + L.resource('view/status/include')), []).then(function(entries) {
58 return Promise.all(entries.filter(function(e) {
59 return (e.type == 'file' && e.name.match(/\.js$/));
60 }).map(function(e) {
61 return 'view.status.include.' + e.name.replace(/\.js$/, '');
62 }).sort().map(function(n) {
63 return L.require(n);
64 }));
65 });
66 },
67
68 render: function(includes) {
69 var rv = E([]), containers = [];
70
71 for (var i = 0; i < includes.length; i++) {
72 var title = null;
73
74 if (includes[i].title != null)
75 title = includes[i].title;
76 else
77 title = String(includes[i]).replace(/^\[ViewStatusInclude\d+_(.+)Class\]$/,
78 function(m, n) { return n.replace(/(^|_)(.)/g,
79 function(m, s, c) { return (s ? ' ' : '') + c.toUpperCase() })
80 });
81
82 var container = E('div');
83
84 rv.appendChild(E('div', { 'class': 'cbi-section', 'style': 'display:none' }, [
85 title != '' ? E('h3', title) : '',
86 container
87 ]));
88
89 containers.push(container);
90 }
91
92 return startPolling(includes, containers).then(function() {
93 return rv;
94 });
95 },
96
97 handleSaveApply: null,
98 handleSave: null,
99 handleReset: null
100 });