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