modules/admin-full: rework status javascript code to use XHR.poll()
[project/luci.git] / modules / admin-full / luasrc / view / admin_network / iface_overview.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12
13 -%>
14
15 <%-
16         local ntm = require "luci.model.network".init()
17         local fwm = require "luci.model.firewall".init()
18
19         local net
20         local netlist = { }
21         for _, net in ipairs(ntm:get_networks()) do
22                 if net:name() ~= "loopback" then
23                         netlist[#netlist+1] = net:name()
24                 end
25         end
26 -%>
27
28 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
29 <script type="text/javascript">//<![CDATA[
30         function iface_shutdown(id, reconnect) {
31                 if (!reconnect && !confirm(String.format('<%_Really shutdown interface "%s" ?\nYou might lose access to this router if you are connected via this interface.%>', id)))
32                         return;
33
34                 var d = document.getElementById(id + '-ifc-description');
35                 if (d)
36                         d.innerHTML = reconnect
37                                 ? '<em><%:Interface is reconnecting...%></em>'
38                                 : '<em><%:Interface is shutting down...%></em>';
39
40                 var s = document.getElementById('ifc-rc-status');
41                 if (s)
42                 {
43                         s.parentNode.style.display = 'block';
44                         s.innerHTML = '<%:Waiting for router...%>';
45                 }
46
47                 XHR.get('<%=luci.dispatcher.build_url("admin", "network")%>/iface_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, null,
48                         function(x)
49                         {
50                                 if (s)
51                                 {
52                                         s.innerHTML = reconnect
53                                                 ? '<%:Interface reconnected%>'
54                                                 : '<%:Interface shut down%>';
55
56                                         window.setTimeout(function() {
57                                                 s.parentNode.style.display = 'none';
58                                         }, 1000);
59                                 }
60                         }
61                 );
62         }
63
64
65         var iwxhr = new XHR();
66         var wifidevs = <%=luci.http.write_json(netdevs)%>;
67         var arptable = <%=luci.http.write_json(arpcache)%>;
68
69         XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "iface_status", table.concat(netlist, ","))%>', null,
70                 function(x, ifcs)
71                 {
72                         if (ifcs)
73                         {
74                                 for (var idx = 0; idx < ifcs.length; idx++)
75                                 {
76                                         var ifc = ifcs[idx];
77                                         var html = '';
78
79                                         var s = document.getElementById(ifc.id + '-ifc-devices');
80                                         if (s)
81                                         {
82                                                 var stat = String.format(
83                                                         '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" />',
84                                                                 ifc.type,
85                                                                 ifc.is_up ? '' : '_disabled'
86                                                 );
87
88                                                 if (ifc.subdevices && ifc.subdevices.length)
89                                                 {
90                                                         stat += ' <strong>(';
91
92                                                         for (var j = 0; j < ifc.subdevices.length; j++)
93                                                         {
94                                                                 var sif = ifc.subdevices[j];
95
96                                                                 stat += String.format(
97                                                                         '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" title="%q" />',
98                                                                                 sif.type,
99                                                                                 sif.is_up ? '' : '_disabled',
100                                                                                 sif.name
101                                                                 );
102                                                         }
103
104                                                         stat += ')</strong>';
105                                                 }
106
107                                                 stat += String.format(
108                                                         '<br /><small>%s</small>',
109                                                                 ifc.name
110                                                 );
111
112                                                 s.innerHTML = stat;
113                                         }
114
115                                         var d = document.getElementById(ifc.id + '-ifc-description');
116                                         if (d && ifc.ifname)
117                                         {
118                                                 if (ifc.is_up)
119                                                 {
120                                                         html += String.format('<strong><%:Uptime%>:</strong> %t<br />', ifc.uptime);
121                                                 }
122
123                                                 if (ifc.type != 'tunnel')
124                                                 {
125                                                         html += String.format('<strong><%:MAC Address%>:</strong> %s<br />', ifc.macaddr);
126                                                 }
127
128                                                 html += String.format(
129                                                         '<strong><%:RX%></strong>: %.2mB (%d <%:Pkts.%>)<br />' +
130                                                         '<strong><%:TX%></strong>: %.2mB (%d <%:Pkts.%>)<br />',
131                                                                 ifc.rx_bytes, ifc.rx_packets,
132                                                                 ifc.tx_bytes, ifc.tx_packets
133                                                 );
134
135                                                 if (ifc.ipaddrs && ifc.ipaddrs.length)
136                                                 {
137                                                         html += '<strong><%:IPv4%>: </strong>';
138
139                                                         for (var i = 0; i < ifc.ipaddrs.length; i++)
140                                                                 html += String.format(
141                                                                         '%s%s/%d',
142                                                                         i ? ', ' : '',
143                                                                         ifc.ipaddrs[i].addr,
144                                                                         ifc.ipaddrs[i].prefix
145                                                                 );
146
147                                                         html += '<br />';
148                                                 }
149
150                                                 if (ifc.ip6addrs && ifc.ip6addrs.length)
151                                                 {
152                                                         html += '<strong><%:IPv6%>: </strong>';
153
154                                                         for (var i = 0; i < ifc.ip6addrs.length; i++)
155                                                                 html += String.format(
156                                                                         '%s%s/%d',
157                                                                         i ? ', ' : '',
158                                                                         ifc.ip6addrs[i].addr.toUpperCase(),
159                                                                         ifc.ip6addrs[i].prefix
160                                                                 );
161
162                                                         html += '<br />';
163                                                 }
164
165                                                 d.innerHTML = html;
166                                         }
167                                         else if (d)
168                                         {
169                                                 d.innerHTML = '<em><%:Interface not present or not connected yet.%></em>';
170                                         }
171                                 }
172                         }
173                 }
174         );
175 //]]></script>
176
177 <fieldset class="cbi-section" style="display:none">
178         <legend><%:Reconnecting interface%></legend>
179         <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
180         <span id="ifc-rc-status"><%:Waiting for router...%></span>
181 </fieldset>
182
183 <div class="cbi-map">
184         <fieldset class="cbi-section">
185                 <legend><%:Interface Overview%></legend>
186
187                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
188                         <tr class="cbi-section-table-titles">
189                                 <th class="cbi-section-table-cell"><%:Network%></th>
190                                 <th class="cbi-section-table-cell" style="text-align:left"><%:Status%></th>
191                                 <th class="cbi-section-table-cell"><%:Actions%></th>
192                         </tr>
193                         <%
194                                 for i, net in ipairs(netlist) do
195                                         local z = fwm:get_zone_by_network(net)
196                                         local c = z and z:get_color() or "#EEEEEE"
197                                         local t = z and translate("Part of zone %q" % z:name()) or translate("No zone assigned")
198                         %>
199                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=i % 2 + 1%>">
200                                         <td class="cbi-value-field" style="padding:3px">
201                                                 <div style="background-color:#FFFFFF; border:1px solid #CCCCCC; margin:0 10px">
202                                                         <div style="border-bottom:1px solid #CCCCCC; padding:2px; background-color:<%=c%>" title="<%=pcdata(t)%>">
203                                                                 <strong><%=net:upper()%></strong>
204                                                         </div>
205                                                         <div style="padding:2px" id="<%=net%>-ifc-devices">
206                                                                 <img src="<%=resource%>/icons/ethernet_disabled.png" style="width:16px; height:16px" /><br />
207                                                                 <small>?</small>
208                                                         </div>
209                                                 </div>
210                                         </td>
211                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net%>-ifc-description">
212                                                 <em><%:Collecting data...%></em>
213                                         </td>
214                                         <td style="width:340px">
215                                                 <input type="button" class="cbi-button cbi-button-add" style="width:80px; background-image:url(<%=resource%>/cbi/reload.gif)" onclick="iface_shutdown('<%=net%>', true)" title="<%:Reconnect this interface%>" value="<%:Connect%>" />
216                                                 <input type="button" class="cbi-button cbi-button-add" style="width:80px; background-image:url(<%=resource%>/cbi/reset.gif)" onclick="iface_shutdown('<%=net%>', false)" title="<%:Shutdown this interface%>" value="<%:Stop%>" />
217                                                 <input type="button" class="cbi-button cbi-button-add" style="width:80px; background-image:url(<%=resource%>/cbi/edit.gif)" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/network", net)%>'" title="<%:Edit this interface%>" value="<%:Edit%>" />
218                                                 <input type="button" class="cbi-button cbi-button-add" style="width:80px; background-image:url(<%=resource%>/cbi/remove.gif)" onclick="if (confirm('<%:Really delete this interface? The deletion cannot be undone!\nYou might lose access to this router if you are connected via this interface.%>')) location.href='<%=luci.dispatcher.build_url("admin/network/iface_delete", net)%>'" title="<%:Delete this interface%>" value="<%:Delete%>" />
219                                         </td>
220                                 </tr>
221                         <% end %>
222                 </table>
223
224                 <input type="button" class="cbi-button cbi-button-add" value="<%:Add new interface...%>" onclick="location.href='<%=luci.dispatcher.build_url("admin/network/iface_add")%>'" />
225         </fieldset>
226 </div>