apply_widget.htm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <% export("cbi_apply_widget", function(redirect_ok, rollback_token) -%>
  2. <script type="text/javascript">//<![CDATA[
  3. var xhr = new XHR(),
  4. uci_apply_auth = { sid: '<%=luci.dispatcher.context.authsession%>', token: '<%=token%>' },
  5. uci_apply_rollback = <%=math.max(luci.config and luci.config.apply and luci.config.apply.rollback or 30, 30)%>,
  6. uci_apply_holdoff = <%=math.max(luci.config and luci.config.apply and luci.config.apply.holdoff or 4, 1)%>,
  7. uci_apply_timeout = <%=math.max(luci.config and luci.config.apply and luci.config.apply.timeout or 5, 1)%>,
  8. uci_apply_display = <%=math.max(luci.config and luci.config.apply and luci.config.apply.display or 1.5, 1)%>,
  9. uci_confirm_auth = <% if rollback_token then %>{ token: '<%=rollback_token%>' }<% else %>null<% end %>,
  10. was_xhr_poll_running = false;
  11. function uci_status_message(type, content) {
  12. if (type) {
  13. var message = showModal('', '');
  14. message.classList.add('alert-message');
  15. DOMTokenList.prototype.add.apply(message.classList, type.split(/\s+/));
  16. if (content)
  17. message.innerHTML = content;
  18. if (!was_xhr_poll_running) {
  19. was_xhr_poll_running = XHR.running();
  20. XHR.halt();
  21. }
  22. }
  23. else {
  24. hideModal();
  25. if (was_xhr_poll_running)
  26. XHR.run();
  27. }
  28. }
  29. function uci_rollback(checked) {
  30. if (checked) {
  31. uci_status_message('warning spinning',
  32. '<p><%:Failed to confirm apply within %ds, waiting for rollback…%></p>'.format(uci_apply_rollback));
  33. var call = function(r, data, duration) {
  34. if (r.status === 204) {
  35. uci_status_message('warning',
  36. '<h4><%:Configuration has been rolled back!%></h4>' +
  37. '<p><%:The device could not be reached within %d seconds after applying the pending changes, which caused the configuration to be rolled back for safety reasons. If you believe that the configuration changes are correct nonetheless, proceed by applying anyway. Alternatively, you can dismiss this warning and edit changes before attempting to apply again, or revert all pending changes to keep the currently working configuration state.%></p>'.format(uci_apply_rollback) +
  38. '<div class="right">' +
  39. '<input type="button" class="btn" onclick="uci_status_message(false)" value="<%:Dismiss%>" /> ' +
  40. '<input type="button" class="btn cbi-button-action important" onclick="uci_revert()" value="<%:Revert changes%>" /> ' +
  41. '<input type="button" class="btn cbi-button-negative important" onclick="uci_apply(false)" value="<%:Apply anyway%>" />' +
  42. '</div>');
  43. return;
  44. }
  45. var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0);
  46. window.setTimeout(function() {
  47. xhr.post('<%=url("admin/uci/confirm")%>', uci_apply_auth, call, uci_apply_timeout * 1000);
  48. }, delay);
  49. };
  50. call({ status: 0 });
  51. }
  52. else {
  53. uci_status_message('warning',
  54. '<h4><%:Device unreachable!%></h4>' +
  55. '<p><%:Could not regain access to the device after applying the configuration changes. You might need to reconnect if you modified network related settings such as the IP address or wireless security credentials.%></p>');
  56. }
  57. }
  58. function uci_confirm(checked, deadline) {
  59. var tt;
  60. var ts = Date.now();
  61. uci_status_message('notice');
  62. var call = function(r, data, duration) {
  63. if (Date.now() >= deadline) {
  64. window.clearTimeout(tt);
  65. uci_rollback(checked);
  66. return;
  67. }
  68. else if (r && (r.status === 200 || r.status === 204)) {
  69. var indicator = document.querySelector('.uci_change_indicator');
  70. if (indicator) indicator.style.display = 'none';
  71. uci_status_message('notice', '<p><%:Configuration has been applied.%></p>');
  72. window.clearTimeout(tt);
  73. window.setTimeout(function() {
  74. <% if redirect_ok then -%>
  75. location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>');
  76. <%- else -%>
  77. window.location = window.location.href.split('#')[0];
  78. <% end %>
  79. }, uci_apply_display * 1000);
  80. return;
  81. }
  82. var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0);
  83. window.setTimeout(function() {
  84. xhr.post('<%=url("admin/uci/confirm")%>', uci_confirm_auth, call, uci_apply_timeout * 1000);
  85. }, delay);
  86. };
  87. var tick = function() {
  88. var now = Date.now();
  89. uci_status_message('notice spinning',
  90. '<p><%:Waiting for configuration to be applied… %ds%></p>'.format(Math.max(Math.floor((deadline - Date.now()) / 1000), 0)));
  91. if (now >= deadline)
  92. return;
  93. tt = window.setTimeout(tick, 1000 - (now - ts));
  94. ts = now;
  95. };
  96. tick();
  97. /* wait a few seconds for the settings to become effective */
  98. window.setTimeout(call, Math.max(uci_apply_holdoff * 1000 - ((ts + uci_apply_rollback * 1000) - deadline), 1));
  99. }
  100. function uci_apply(checked) {
  101. uci_status_message('notice spinning', '<p><%:Starting configuration apply…%></p>');
  102. xhr.post('<%=url("admin/uci")%>/' + (checked ? 'apply_rollback' : 'apply_unchecked'), uci_apply_auth, function(r, tok) {
  103. if (r.status === (checked ? 200 : 204)) {
  104. if (checked && tok !== null && typeof(tok) === 'object' && typeof(tok.token) === 'string')
  105. uci_confirm_auth = tok;
  106. uci_confirm(checked, Date.now() + uci_apply_rollback * 1000);
  107. }
  108. else if (checked && r.status === 204) {
  109. uci_status_message('notice', '<p><%:There are no changes to apply.%></p>');
  110. window.setTimeout(function() {
  111. <% if redirect_ok then -%>
  112. location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>');
  113. <%- else -%>
  114. uci_status_message(false);
  115. <%- end %>
  116. }, uci_apply_display * 1000);
  117. }
  118. else {
  119. uci_status_message('warning', '<p><%_Apply request failed with status <code>%h</code>%></p>'.format(r.responseText || r.statusText || r.status));
  120. window.setTimeout(function() { uci_status_message(false); }, uci_apply_display * 1000);
  121. }
  122. });
  123. }
  124. function uci_revert() {
  125. uci_status_message('notice spinning', '<p><%:Reverting configuration…%></p>');
  126. xhr.post('<%=url("admin/uci/revert")%>', uci_apply_auth, function(r) {
  127. if (r.status === 200) {
  128. uci_status_message('notice', '<p><%:Changes have been reverted.%></p>');
  129. window.setTimeout(function() {
  130. <% if redirect_ok then -%>
  131. location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>');
  132. <%- else -%>
  133. window.location = window.location.href.split('#')[0];
  134. <%- end %>
  135. }, uci_apply_display * 1000);
  136. }
  137. else {
  138. uci_status_message('warning', '<p><%_Revert request failed with status <code>%h</code>%></p>'.format(r.statusText || r.status));
  139. window.setTimeout(function() { uci_status_message(false); }, uci_apply_display * 1000);
  140. }
  141. });
  142. }
  143. //]]></script>
  144. <%- end) %>