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