Rework LuCI build system
[project/luci.git] / applications / luci-app-commands / luasrc / view / commands.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2012 Jo-Philipp Wich <jow@openwrt.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 -%>
12
13 <% css = [[
14
15 .commandbox {
16         height: 12em;
17         width: 30%;
18         float: left;
19         height: 12em;
20         margin: 5px;
21         position: relative;
22 }
23
24 .commandbox h3 {
25         font-size: 1.5em !important;
26         line-height: 2em !important;
27         margin: 0 !important;
28 }
29
30 .commandbox input[type="text"] {
31         width: 50% !important;
32 }
33
34 .commandbox div {
35         position: absolute;
36         left: 0;
37         bottom: 1.5em;
38 }
39
40 ]] -%>
41
42 <%+header%>
43
44 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
45 <script type="text/javascript">//<![CDATA[
46         var stxhr = new XHR();
47
48         function command_run(id)
49         {
50                 var args;
51                 var field = document.getElementById(id);
52                 if (field)
53                         args = encodeURIComponent(field.value);
54
55                 var legend = document.getElementById('command-rc-legend');
56                 var output = document.getElementById('command-rc-output');
57
58                 if (legend && output)
59                 {
60                         output.innerHTML =
61                                 '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> ' +
62                                 '<%:Waiting for command to complete...%>'
63                         ;
64
65                         legend.parentNode.style.display = 'block';
66                         legend.style.display = 'inline';
67
68                         stxhr.get('<%=luci.dispatcher.build_url("admin", "system", "commands", "run")%>/' + id + (args ? '/' + args : ''), null,
69                                 function(x, st)
70                                 {
71                                         if (st)
72                                         {
73                                                 if (st.binary)
74                                                         st.stdout = '[<%:Binary data not displayed, download instead.%>]';
75
76                                                 legend.style.display = 'none';
77                                                 output.innerHTML = String.format(
78                                                         '<pre><strong># %h\n</strong>%h<span style="color:red">%h</span></pre>' +
79                                                         '<div class="alert-message warning">%s (<%:Code:%> %d)</div>',
80                                                         st.command, st.stdout, st.stderr,
81                                                         (st.exitcode == 0) ? '<%:Command successful%>' : '<%:Command failed%>',
82                                                         st.exitcode);
83                                         }
84                                         else
85                                         {
86                                                 legend.style.display = 'none';
87                                                 output.innerHTML = '<span class="error"><%:Failed to execute command!%></span>';
88                                         }
89
90                                         location.hash = '#output';
91                                 }
92                         );
93                 }
94         }
95
96         function command_download(id)
97         {
98                 var args;
99                 var field = document.getElementById(id);
100                 if (field)
101                         args = encodeURIComponent(field.value);
102
103                 location.href = '<%=luci.dispatcher.build_url("admin", "system", "commands", "download")%>/' + id + (args ? '/' + args : '');
104         }
105
106         function command_link(id)
107         {
108                 var legend = document.getElementById('command-rc-legend');
109                 var output = document.getElementById('command-rc-output');
110
111                 var args;
112                 var field = document.getElementById(id);
113                 if (field)
114                         args = encodeURIComponent(field.value);
115
116                 if (legend && output)
117                 {
118                         var link = location.protocol + '//' + location.hostname +
119                                    (location.port ? ':' + location.port : '') +
120                                            location.pathname.split(';')[0] + 'command/' +
121                                            id + (args ? '/' + args : '');
122
123                         legend.style.display = 'none';
124                         output.parentNode.style.display = 'block';
125                         output.innerHTML = String.format(
126                                 '<div class="alert-message"><%:Access command with%> <a href="%s">%s</a></div>',
127                                 link, link
128                         );
129
130                         location.hash = '#output';
131                 }
132         }
133
134 //]]></script>
135
136 <%
137         local uci = require "luci.model.uci".cursor()
138         local commands = { }
139
140         uci:foreach("luci", "command", function(s) commands[#commands+1] = s end)
141 %>
142
143 <form method="get" action="<%=pcdata(luci.http.getenv("REQUEST_URI"))%>">
144         <div class="cbi-map">
145                 <h2><a id="content" name="content"><%:Custom Commands%></a></h2>
146
147                 <fieldset class="cbi-section">
148                         <% local _, command; for _, command in ipairs(commands) do %>
149                         <div class="commandbox">
150                                 <h3><%=pcdata(command.name)%></h3>
151                                 <p><%:Command:%> <code><%=pcdata(command.command)%></code></p>
152                                 <% if command.param == "1" then %>
153                                         <p><%:Arguments:%> <input type="text" id="<%=command['.name']%>" /></p>
154                                 <% end %>
155                                 <div>
156                                         <input type="button" value="<%:Run%>" class="cbi-button cbi-button-apply" onclick="command_run('<%=command['.name']%>')" />
157                                         <input type="button" value="<%:Download%>" class="cbi-button cbi-button-download" onclick="command_download('<%=command['.name']%>')" />
158                                         <% if command.public == "1" then %>
159                                                 <input type="button" value="<%:Link%>" class="cbi-button cbi-button-link" onclick="command_link('<%=command['.name']%>')" />
160                                         <% end %>
161                                 </div>
162                         </div>
163                         <% end %>
164
165                         <br style="clear:both" /><br />
166                         <a name="output"></a>
167                 </fieldset>
168         </div>
169
170         <fieldset class="cbi-section" style="display:none">
171                 <legend id="command-rc-legend"><%:Collecting data...%></legend>
172                 <span id="command-rc-output"></span>
173         </fieldset>
174 </form>
175
176 <%+footer%>