applications/luci-olsr-services: Update using javascript/json
[project/luci.git] / applications / luci-olsr-services / luasrc / view / freifunk-services / services.htm
1 <%#
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 -%>
13
14 <%
15 local fs  = require "luci.fs"
16 local utl = require "luci.util"
17 local i = 1
18
19 -- check if nameservice plugin is enabled and where to find the services file
20 local has_services = false
21
22 local uci = require "luci.model.uci".cursor()
23 uci:foreach("olsrd", "LoadPlugin", function(s)
24         if s.library == "olsrd_nameservice.so.0.3" then
25                 if s.services_file and fs.access(s.services_file) then
26                         has_services = true
27                         services_file = s.services_file
28                 else
29                         if fs.access("/var/run/services_olsr") then
30                                 has_services = true
31                                 services_file = "/var/run/services_olsr"
32                         end
33                 end
34         end
35 end)
36
37 function fetch_services()
38         local rawdata = fs.readfile(services_file)
39
40         if #rawdata == 0 then
41                 return nil
42         end
43
44         local data = {}
45
46         local tables = utl.split(utl.trim(rawdata), "\n", nil, true)
47         -- remove the first 3 lines
48         table.remove(tables,1)
49         table.remove(tables,1)
50         table.remove(tables,1)
51
52         -- store last line in last_update and remove it, then remove another empty line at the end
53         last_update=table.remove(tables)
54         table.remove(tables)
55
56         for i, tbl in ipairs(tables) do
57                 local lines = utl.split(tbl, "\n", nil, true)
58         end
59         
60         return tables
61 end
62 local table = fetch_services()
63
64 if luci.http.formvalue("status") == "1" then
65         local rv = {}
66         for k, line in ipairs(table) do
67                 local field = utl.split(line, "[#|]", split, true)
68                 local url,descr,origin = pcdata(field[1]),pcdata(field[3]),pcdata(field[4])
69                 local rs = i
70                 rv[#rv+1] = {
71                         url = url,
72                         origin = origin,
73                         descr = descr,
74                         rs = rs
75                         }
76                 if i == 1 then i = 0 elseif i == 0 then i = 1 end
77         end
78         luci.http.prepare_content("application/json")
79         luci.http.write_json(rv)
80         return
81 end
82
83 %>
84
85 <% if has_services then %>
86
87 <%+header%>
88
89 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
90 <script type="text/javascript">//<![CDATA[
91
92         XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
93                 function(x, info)
94                 {
95                 var tbody = document.getElementById('olsr_services');
96                         if (tbody)
97                         {
98                                 var s = '';
99                                 for (var idx = 0; idx < info.length; idx++)
100                                 {
101                                 var service = info[idx];
102                                 s += String.format(
103                                         '<tr class="cbi-section-table-row cbi-rowstyle-%s">' +
104                                                 '<td class="cbi-section-table-cell"><a href="%s">%s</a></td>' +
105                                                 '<td class="cbi-section-table-cell">%s</td>' +
106                                         '</tr>',
107                                         service.rs, service.url, service.descr,  service.origin || '?'
108                                         );
109                                 }
110                                 tbody.innerHTML = s;
111                         }
112                 }
113         );
114 //]]></script>
115         
116
117
118
119         <h2><a id="content" name="content"><%:Services%></a></h2>
120
121         <fieldset class="cbi-section">
122         <legend><%:Internal services%></legend>
123         <table class="cbi-section-table">
124                 <tr class="cbi-section-table-titles">
125                         <th class="cbi-section-table-cell"><%:Url%></th>
126                         <th class="cbi-section-table-cell"><%:Source%></th>
127                 </tr>
128
129                 <tbody id="olsr_services">
130         <%
131                 for k, line in ipairs(table) do
132                         local field = {}
133                         -- split line at # and |, 1=url, 2=proto, 3=description, 4=source
134                         local field = utl.split(line, "[#|]", split, true)
135                         local url,descr,origin = pcdata(field[1]),pcdata(field[3]),pcdata(field[4])
136                         %>
137
138                         <tr class="cbi-section-table-row cbi-rowstyle-<%=i%>">
139                                 <td class="cbi-section-table-cell"><a href="<%=url%>"><%=descr%></a></td>
140                                 <td class="cbi-section-table-cell"><%=origin%></td>
141                 </tr>
142                         <% if i == 1 then i = 0 elseif i == 0 then i = 1 end
143                 end %>
144                 </tbody>
145         </table>
146         <br />
147         <%=last_update%>
148         </fieldset>
149
150 <%else%>
151         <%:No services can be shown, because olsrd is not running or the olsrd-nameservice Plugin is not loaded.%>
152 <%end%>
153
154 <%+footer%>