commands.htm 4.4 KB

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