diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-02-07 19:10:34 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-07-07 15:25:49 +0200 |
commit | fda4ba598c0ba1c06f5de43554b40cf0d415d047 (patch) | |
tree | d8158138283846efcd93726eec778790ebcd78ce /modules/luci-base | |
parent | 1dd910148eaf7b9ed7130d1a067465dd43940da3 (diff) |
luci-base: replace uci change pages with client side modal dialog
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/ui.js | 340 | ||||
-rw-r--r-- | modules/luci-base/luasrc/controller/admin/uci.lua | 49 | ||||
-rw-r--r-- | modules/luci-base/luasrc/view/admin_uci/changelog.htm | 66 | ||||
-rw-r--r-- | modules/luci-base/luasrc/view/admin_uci/changes.htm | 45 | ||||
-rw-r--r-- | modules/luci-base/luasrc/view/admin_uci/revert.htm | 33 | ||||
-rw-r--r-- | modules/luci-base/luasrc/view/cbi/apply_widget.htm | 172 | ||||
-rw-r--r-- | modules/luci-base/luasrc/view/footer.htm | 13 | ||||
-rw-r--r-- | modules/luci-base/luasrc/view/header.htm | 22 |
8 files changed, 378 insertions, 362 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/ui.js b/modules/luci-base/htdocs/luci-static/resources/ui.js index 5914b5ee69..c80acdfd23 100644 --- a/modules/luci-base/htdocs/luci-static/resources/ui.js +++ b/modules/luci-base/htdocs/luci-static/resources/ui.js @@ -1,4 +1,5 @@ 'use strict'; +'require uci'; var modalDiv = null, tooltipDiv = null, @@ -1048,6 +1049,8 @@ return L.Class.extend({ document.addEventListener('blur', this.hideTooltip.bind(this), true); document.addEventListener('luci-loaded', this.tabs.init.bind(this.tabs)); + document.addEventListener('luci-loaded', this.changes.init.bind(this.changes)); + document.addEventListener('uci-loaded', this.changes.init.bind(this.changes)); }, /* Modal dialog */ @@ -1318,6 +1321,343 @@ return L.Class.extend({ } }), + /* UCI Changes */ + changes: L.Class.singleton({ + init: function() { + if (!L.env.sessionid) + return; + + return uci.changes().then(L.bind(this.renderChangeIndicator, this)); + }, + + setIndicator: function(n) { + var i = document.querySelector('.uci_change_indicator'); + if (i == null) { + var poll = document.getElementById('xhr_poll_status'); + i = poll.parentNode.insertBefore(E('a', { + 'href': '#', + 'class': 'uci_change_indicator label notice', + 'click': L.bind(this.displayChanges, this) + }), poll); + } + + if (n > 0) { + L.dom.content(i, [ _('Unsaved Changes'), ': ', n ]); + i.classList.add('flash'); + i.style.display = ''; + } + else { + i.classList.remove('flash'); + i.style.display = 'none'; + } + }, + + renderChangeIndicator: function(changes) { + var n_changes = 0; + + for (var config in changes) + if (changes.hasOwnProperty(config)) + n_changes += changes[config].length; + + this.changes = changes; + this.setIndicator(n_changes); + }, + + changeTemplates: { + 'add-3': '<ins>uci add %0 <strong>%3</strong> # =%2</ins>', + 'set-3': '<ins>uci set %0.<strong>%2</strong>=%3</ins>', + 'set-4': '<var><ins>uci set %0.%2.%3=<strong>%4</strong></ins></var>', + 'remove-2': '<del>uci del %0.<strong>%2</strong></del>', + 'remove-3': '<var><del>uci del %0.%2.<strong>%3</strong></del></var>', + 'order-3': '<var>uci reorder %0.%2=<strong>%3</strong></var>', + 'list-add-4': '<var><ins>uci add_list %0.%2.%3=<strong>%4</strong></ins></var>', + 'list-del-4': '<var><del>uci del_list %0.%2.%3=<strong>%4</strong></del></var>', + 'rename-3': '<var>uci rename %0.%2=<strong>%3</strong></var>', + 'rename-4': '<var>uci rename %0.%2.%3=<strong>%4</strong></var>' + }, + + displayChanges: function() { + var list = E('div', { 'class': 'uci-change-list' }), + dlg = L.ui.showModal(_('Configuration') + ' / ' + _('Changes'), [ + E('div', { 'class': 'cbi-section' }, [ + E('strong', _('Legend:')), + E('div', { 'class': 'uci-change-legend' }, [ + E('div', { 'class': 'uci-change-legend-label' }, [ + E('ins', ' '), ' ', _('Section added') ]), + E('div', { 'class': 'uci-change-legend-label' }, [ + E('del', ' '), ' ', _('Section removed') ]), + E('div', { 'class': 'uci-change-legend-label' }, [ + E('var', {}, E('ins', ' ')), ' ', _('Option changed') ]), + E('div', { 'class': 'uci-change-legend-label' }, [ + E('var', {}, E('del', ' ')), ' ', _('Option removed') ])]), + E('br'), list, + E('div', { 'class': 'right' }, [ + E('input', { + 'type': 'button', + 'class': 'btn', + 'click': L.ui.hideModal, + 'value': _('Dismiss') + }), ' ', + E('input', { + 'type': 'button', + 'class': 'cbi-button cbi-button-positive important', + 'click': L.bind(this.apply, this, true), + 'value': _('Save & Apply') + }), ' ', + E('input', { + 'type': 'button', + 'class': 'cbi-button cbi-button-reset', + 'click': L.bind(this.revert, this), + 'value': _('Revert') + })])]) + ]); + + for (var config in this.changes) { + if (!this.changes.hasOwnProperty(config)) + continue; + + list.appendChild(E('h5', '# /etc/config/%s'.format(config))); + + for (var i = 0, added = null; i < this.changes[config].length; i++) { + var chg = this.changes[config][i], + tpl = this.changeTemplates['%s-%d'.format(chg[0], chg.length)]; + + list.appendChild(E(tpl.replace(/%([01234])/g, function(m0, m1) { + switch (+m1) { + case 0: + return config; + + case 2: + if (added != null && chg[1] == added[0]) + return '@' + added[1] + '[-1]'; + else + return chg[1]; + + case 4: + return "'" + chg[3].replace(/'/g, "'\"'\"'") + "'"; + + default: + return chg[m1-1]; + } + }))); + + if (chg[0] == 'add') + added = [ chg[1], chg[2] ]; + } + } + + list.appendChild(E('br')); + dlg.classList.add('uci-dialog'); + }, + + displayStatus: function(type, content) { + if (type) { + var message = L.ui.showModal('', ''); + + message.classList.add('alert-message'); + DOMTokenList.prototype.add.apply(message.classList, type.split(/\s+/)); + + if (content) + L.dom.content(message, content); + + if (!this.was_polling) { + this.was_polling = L.Request.poll.active(); + L.Request.poll.stop(); + } + } + else { + L.ui.hideModal(); + + if (this.was_polling) + L.Request.poll.start(); + } + }, + + rollback: function(checked) { + if (checked) { + this.displayStatus('warning spinning', + E('p', _('Failed to confirm apply within %ds, waiting for rollback…') + .format(L.env.apply_rollback))); + + var call = function(r, data, duration) { + if (r.status === 204) { + L.ui.changes.displayStatus('warning', [ + E('h4', _('Configuration has been rolled back!')), + E('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, perform an unchecked configuration apply. 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.').format(L.env.apply_rollback)), + E('div', { 'class': 'right' }, [ + E('input', { + 'type': 'button', + 'class': 'btn', + 'click': L.bind(L.ui.changes.displayStatus, L.ui.changes, false), + 'value': _('Dismiss') + }), ' ', + E('input', { + 'type': 'button', + 'class': 'btn cbi-button-action important', + 'click': L.bind(L.ui.changes.revert, L.ui.changes), + 'value': _('Revert changes') + }), ' ', + E('input', { + 'type': 'button', + 'class': 'btn cbi-button-negative important', + 'click': L.bind(L.ui.changes.apply, L.ui.changes, false), + 'value': _('Apply unchecked') + }) + ]) + ]); + + return; + } + + var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0); + window.setTimeout(function() { + L.Request.request(L.url('admin/uci/confirm'), { + method: 'post', + timeout: L.env.apply_timeout * 1000, + query: { sid: L.env.sessionid, token: L.env.token } + }).then(call); + }, delay); + }; + + call({ status: 0 }); + } + else { + this.displayStatus('warning', [ + E('h4', _('Device unreachable!')), + E('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.')) + ]); + } + }, + + confirm: function(checked, deadline, override_token) { + var tt; + var ts = Date.now(); + + this.displayStatus('notice'); + + if (override_token) + this.confirm_auth = { token: override_token }; + + var call = function(r, data, duration) { + if (Date.now() >= deadline) { + window.clearTimeout(tt); + L.ui.changes.rollback(checked); + return; + } + else if (r && (r.status === 200 || r.status === 204)) { + document.dispatchEvent(new CustomEvent('uci-applied')); + + L.ui.changes.setIndicator(0); + L.ui.changes.displayStatus('notice', + E('p', _('Configuration has been applied.'))); + + window.clearTimeout(tt); + window.setTimeout(function() { + //L.ui.changes.displayStatus(false); + window.location = window.location.href.split('#')[0]; + }, L.env.apply_display * 1000); + + return; + } + + var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0); + window.setTimeout(function() { + L.Request.request(L.url('admin/uci/confirm'), { + method: 'post', + timeout: L.env.apply_timeout * 1000, + query: L.ui.changes.confirm_auth + }).then(call); + }, delay); + }; + + var tick = function() { + var now = Date.now(); + + L.ui.changes.displayStatus('notice spinning', + E('p', _('Waiting for configuration to get applied… %ds') + .format(Math.max(Math.floor((deadline - Date.now()) / 1000), 0)))); + + if (now >= deadline) + return; + + tt = window.setTimeout(tick, 1000 - (now - ts)); + ts = now; + }; + + tick(); + + /* wait a few seconds for the settings to become effective */ + window.setTimeout(call, Math.max(L.env.apply_holdoff * 1000 - ((ts + L.env.apply_rollback * 1000) - deadline), 1)); + }, + + apply: function(checked) { + this.displayStatus('notice spinning', + E('p', _('Starting configuration apply…'))); + + L.Request.request(L.url('admin/uci', checked ? 'apply_rollback' : 'apply_unchecked'), { + method: 'post', + query: { sid: L.env.sessionid, token: L.env.token } + }).then(function(r) { + if (r.status === (checked ? 200 : 204)) { + var tok = null; try { tok = r.json(); } catch(e) {} + if (checked && tok !== null && typeof(tok) === 'object' && typeof(tok.token) === 'string') + L.ui.changes.confirm_auth = tok; + + L.ui.changes.confirm(checked, Date.now() + L.env.apply_rollback * 1000); + } + else if (checked && r.status === 204) { + L.ui.changes.displayStatus('notice', + E('p', _('There are no changes to apply'))); + + window.setTimeout(function() { + L.ui.changes.displayStatus(false); + }, L.env.apply_display * 1000); + } + else { + L.ui.changes.displayStatus('warning', + E('p', _('Apply request failed with status <code>%h</code>%>') + .format(r.responseText || r.statusText || r.status))); + + window.setTimeout(function() { + L.ui.changes.displayStatus(false); + }, L.env.apply_display * 1000); + } + }); + }, + + revert: function() { + this.displayStatus('notice spinning', + E('p', _('Reverting configuration…'))); + + L.Request.request(L.url('admin/uci/revert'), { + method: 'post', + query: { sid: L.env.sessionid, token: L.env.token } + }).then(function(r) { + if (r.status === 200) { + document.dispatchEvent(new CustomEvent('uci-reverted')); + + L.ui.changes.setIndicator(0); + L.ui.changes.displayStatus('notice', + E('p', _('Changes have been reverted.'))); + + window.setTimeout(function() { + //L.ui.changes.displayStatus(false); + window.location = window.location.href.split('#')[0]; + }, L.env.apply_display * 1000); + } + else { + L.ui.changes.displayStatus('warning', + E('p', _('Revert request failed with status <code>%h</code>') + .format(r.statusText || r.status))); + + window.setTimeout(function() { + L.ui.changes.displayStatus(false); + }, L.env.apply_display * 1000); + } + }); + } + }), + addValidator: function(field, type, optional /*, ... */) { if (type == null) return; diff --git a/modules/luci-base/luasrc/controller/admin/uci.lua b/modules/luci-base/luasrc/controller/admin/uci.lua index 1d955dd982..6b19c62f81 100644 --- a/modules/luci-base/luasrc/controller/admin/uci.lua +++ b/modules/luci-base/luasrc/controller/admin/uci.lua @@ -1,5 +1,5 @@ -- Copyright 2008 Steven Barth <steven@midlink.org> --- Copyright 2010-2015 Jo-Philipp Wich <jow@openwrt.org> +-- Copyright 2010-2019 Jo-Philipp Wich <jo@mein.io> -- Licensed to the public under the Apache License 2.0. module("luci.controller.admin.uci", package.seeall) @@ -9,8 +9,7 @@ function index() or table.concat(luci.dispatcher.context.request, "/") entry({"admin", "uci"}, nil, _("Configuration")) - entry({"admin", "uci", "changes"}, post_on({ trigger_apply = true }, "action_changes"), _("Changes"), 40).query = {redir=redir} - entry({"admin", "uci", "revert"}, post("action_revert"), _("Revert"), 30).query = {redir=redir} + entry({"admin", "uci", "revert"}, post("action_revert"), nil) local node local authen = function(checkpass, allowed_users) @@ -31,34 +30,6 @@ function index() end -function action_changes() - local uci = require "luci.model.uci" - local changes = uci:changes() - - luci.template.render("admin_uci/changes", { - changes = next(changes) and changes, - timeout = timeout, - trigger_apply = luci.http.formvalue("trigger_apply") and true or false - }) -end - -function action_revert() - local uci = require "luci.model.uci" - local changes = uci:changes() - - -- Collect files to be reverted - local r, tbl - for r, tbl in pairs(changes) do - uci:revert(r) - end - - luci.template.render("admin_uci/revert", { - changes = next(changes) and changes, - trigger_revert = true - }) -end - - local function ubus_state_to_http(errstr) local map = { ["Invalid command"] = 400, @@ -107,3 +78,19 @@ function action_confirm() local _, errstr = uci:confirm(token) ubus_state_to_http(errstr) end + +function action_revert() + local uci = require "luci.model.uci" + local changes = uci:changes() + + -- Collect files to be reverted + local _, errstr, r, tbl + for r, tbl in pairs(changes) do + _, errstr = uci:revert(r) + if errstr then + break + end + end + + ubus_state_to_http(errstr or "OK") +end diff --git a/modules/luci-base/luasrc/view/admin_uci/changelog.htm b/modules/luci-base/luasrc/view/admin_uci/changelog.htm deleted file mode 100644 index 9d6267cf31..0000000000 --- a/modules/luci-base/luasrc/view/admin_uci/changelog.htm +++ /dev/null @@ -1,66 +0,0 @@ -<%# - Copyright 2010 Jo-Philipp Wich <jo@mein.io> - Licensed to the public under the Apache License 2.0. --%> - -<% export("uci_changelog", function(changes) -%> -<div class="cbi-section"> - <strong><%:Legend:%></strong> - <div class="uci-change-legend"> - <div class="uci-change-legend-label"><ins> </ins> <%:Section added%></div> - <div class="uci-change-legend-label"><del> </del> <%:Section removed%></div> - <div class="uci-change-legend-label"><var><ins> </ins></var> <%:Option changed%></div> - <div class="uci-change-legend-label"><var><del> </del></var> <%:Option removed%></div> - <br style="clear:both" /> - </div> - <br /> - - <div class="uci-change-list"><% - local util = luci.util - local tpl = { - ["add-3"] = "<ins>uci add %0 <strong>%3</strong> # =%2</ins>", - ["set-3"] = "<ins>uci set %0.<strong>%2</strong>=%3</ins>", - ["set-4"] = "<var><ins>uci set %0.%2.%3=<strong>%4</strong></ins></var>", - ["remove-2"] = "<del>uci del %0.<strong>%2</strong></del>", - ["remove-3"] = "<var><del>uci del %0.%2.<strong>%3</strong></del></var>", - ["order-3"] = "<var>uci reorder %0.%2=<strong>%3</strong></var>", - ["list-add-4"] = "<var><ins>uci add_list %0.%2.%3=<strong>%4</strong></ins></var>", - ["list-del-4"] = "<var><del>uci del_list %0.%2.%3=<strong>%4</strong></del></var>", - ["rename-3"] = "<var>uci rename %0.%2=<strong>%3</strong></var>", - ["rename-4"] = "<var>uci rename %0.%2.%3=<strong>%4</strong></var>" - } - - local conf, deltas - for conf, deltas in util.kspairs(changes) do - write("<h3># /etc/config/%s</h3>" % conf) - - local _, delta, added - for _, delta in pairs(deltas) do - local t = tpl["%s-%d" %{ delta[1], #delta }] - - write(t:gsub("%%(%d)", function(n) - if n == "0" then - return conf - elseif n == "2" then - if added and delta[2] == added[1] then - return "@%s[-1]" % added[2] - else - return delta[2] - end - elseif n == "4" then - return util.shellquote(delta[4]) - else - return delta[tonumber(n)] - end - end)) - - if delta[1] == "add" then - added = { delta[2], delta[3] } - end - end - - write("<br />") - end - %></div> -</div> -<%- end) %> diff --git a/modules/luci-base/luasrc/view/admin_uci/changes.htm b/modules/luci-base/luasrc/view/admin_uci/changes.htm deleted file mode 100644 index 43bd7c23fb..0000000000 --- a/modules/luci-base/luasrc/view/admin_uci/changes.htm +++ /dev/null @@ -1,45 +0,0 @@ -<%# - Copyright 2008 Steven Barth <steven@midlink.org> - Copyright 2008-2018 Jo-Philipp Wich <jo@mein.io> - Licensed to the public under the Apache License 2.0. --%> - -<%+header%> - -<%- - local node, redir_url = luci.dispatcher.lookup(luci.http.formvalue("redir")) - export("redirect", redir_url or url("admin/uci/changes")) - - include("admin_uci/changelog") --%> - -<h2 name="content"><%:Configuration%> / <%:Changes%></h2> - -<% if changes then %> - <%- uci_changelog(changes) -%> -<% else %> - <p><strong><%:There are no pending changes!%></strong></p> -<% end %> - -<div class="alert-message" id="cbi_apply_status" style="display:none"></div> - -<div class="cbi-page-actions"> - <% if redir_url then %> - <form method="get" action="<%=luci.util.pcdata(redir_url)%>"> - <input class="cbi-button cbi-button-link" type="submit" value="<%:Back%>" /> - </form> - <% end %> - - <form method="post" action="<%=url("admin/uci/changes")%>"> - <input type="hidden" name="token" value="<%=token%>" /> - <input type="hidden" name="redir" value="<%=pcdata(luci.http.formvalue("redir"))%>" /> - <input class="cbi-button cbi-button-save" type="submit" name="trigger_apply" value="<%:Save & Apply%>" /> - </form> - <form method="post" action="<%=url("admin/uci/revert")%>"> - <input type="hidden" name="token" value="<%=token%>" /> - <input type="hidden" name="redir" value="<%=pcdata(luci.http.formvalue("redir"))%>" /> - <input class="cbi-button cbi-button-reset" type="submit" value="<%:Revert%>" /> - </form> -</div> - -<%+footer%> diff --git a/modules/luci-base/luasrc/view/admin_uci/revert.htm b/modules/luci-base/luasrc/view/admin_uci/revert.htm deleted file mode 100644 index d8fd3de01e..0000000000 --- a/modules/luci-base/luasrc/view/admin_uci/revert.htm +++ /dev/null @@ -1,33 +0,0 @@ -<%# - Copyright 2008 Steven Barth <steven@midlink.org> - Copyright 2008-2018 Jo-Philipp Wich <jo@mein.io> - Licensed to the public under the Apache License 2.0. --%> - -<%+header%> - -<%- - local node, redir_url = luci.dispatcher.lookup(luci.http.formvalue("redir")) - export("redirect", redir_url or url("admin/uci/changes")) - - include("admin_uci/changelog") --%> - -<h2 name="content"><%:Configuration%> / <%:Revert%></h2> - -<% if changes then %> - <p><strong><%:The following changes have been reverted%>:</strong></p> - <%- uci_changelog(changes) -%> -<% else %> - <p><strong><%:There are no pending changes to revert!%></strong></p> -<% end %> - -<% if redir_url then %> - <div class="cbi-page-actions"> - <form class="inline" method="get" action="<%=luci.util.pcdata(redir_url)%>"> - <input class="cbi-button cbi-button-link" style="margin:0" type="submit" value="<%:Back%>" /> - </form> - </div> -<% end %> - -<%+footer%> diff --git a/modules/luci-base/luasrc/view/cbi/apply_widget.htm b/modules/luci-base/luasrc/view/cbi/apply_widget.htm deleted file mode 100644 index 0f9667390e..0000000000 --- a/modules/luci-base/luasrc/view/cbi/apply_widget.htm +++ /dev/null @@ -1,172 +0,0 @@ -<% export("cbi_apply_widget", function(redirect_ok, rollback_token) -%> -<script type="text/javascript">//<![CDATA[ - var xhr = new XHR(), - uci_apply_auth = { sid: '<%=luci.dispatcher.context.authsession%>', token: '<%=token%>' }, - uci_apply_rollback = <%=math.max(luci.config and luci.config.apply and luci.config.apply.rollback or 30, 30)%>, - uci_apply_holdoff = <%=math.max(luci.config and luci.config.apply and luci.config.apply.holdoff or 4, 1)%>, - uci_apply_timeout = <%=math.max(luci.config and luci.config.apply and luci.config.apply.timeout or 5, 1)%>, - uci_apply_display = <%=math.max(luci.config and luci.config.apply and luci.config.apply.display or 1.5, 1)%>, - uci_confirm_auth = <% if rollback_token then %>{ token: '<%=rollback_token%>' }<% else %>null<% end %>, - was_xhr_poll_running = false; - - function uci_status_message(type, content) { - if (type) { - var message = showModal('', ''); - - message.classList.add('alert-message'); - DOMTokenList.prototype.add.apply(message.classList, type.split(/\s+/)); - - if (content) - message.innerHTML = content; - - if (!was_xhr_poll_running) { - was_xhr_poll_running = XHR.running(); - XHR.halt(); - } - } - else { - hideModal(); - - if (was_xhr_poll_running) - XHR.run(); - } - } - - function uci_rollback(checked) { - if (checked) { - uci_status_message('warning spinning', - '<p><%:Failed to confirm apply within %ds, waiting for rollback…%></p>'.format(uci_apply_rollback)); - - var call = function(r, data, duration) { - if (r.status === 204) { - uci_status_message('warning', - '<h4><%:Configuration has been rolled back!%></h4>' + - '<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) + - '<div class="right">' + - '<input type="button" class="btn" onclick="uci_status_message(false)" value="<%:Dismiss%>" /> ' + - '<input type="button" class="btn cbi-button-action important" onclick="uci_revert()" value="<%:Revert changes%>" /> ' + - '<input type="button" class="btn cbi-button-negative important" onclick="uci_apply(false)" value="<%:Apply anyway%>" />' + - '</div>'); - - return; - } - - var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0); - window.setTimeout(function() { - xhr.post('<%=url("admin/uci/confirm")%>', uci_apply_auth, call, uci_apply_timeout * 1000); - }, delay); - }; - - call({ status: 0 }); - } - else { - uci_status_message('warning', - '<h4><%:Device unreachable!%></h4>' + - '<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>'); - } - } - - function uci_confirm(checked, deadline) { - var tt; - var ts = Date.now(); - - uci_status_message('notice'); - - var call = function(r, data, duration) { - if (Date.now() >= deadline) { - window.clearTimeout(tt); - uci_rollback(checked); - return; - } - else if (r && (r.status === 200 || r.status === 204)) { - var indicator = document.querySelector('.uci_change_indicator'); - if (indicator) indicator.style.display = 'none'; - - uci_status_message('notice', '<p><%:Configuration has been applied.%></p>'); - - window.clearTimeout(tt); - window.setTimeout(function() { - <% if redirect_ok then -%> - location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>'); - <%- else -%> - window.location = window.location.href.split('#')[0]; - <% end %> - }, uci_apply_display * 1000); - - return; - } - - var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0); - window.setTimeout(function() { - xhr.post('<%=url("admin/uci/confirm")%>', uci_confirm_auth, call, uci_apply_timeout * 1000); - }, delay); - }; - - var tick = function() { - var now = Date.now(); - - uci_status_message('notice spinning', - '<p><%:Waiting for configuration to be applied… %ds%></p>'.format(Math.max(Math.floor((deadline - Date.now()) / 1000), 0))); - - if (now >= deadline) - return; - - tt = window.setTimeout(tick, 1000 - (now - ts)); - ts = now; - }; - - tick(); - - /* wait a few seconds for the settings to become effective */ - window.setTimeout(call, Math.max(uci_apply_holdoff * 1000 - ((ts + uci_apply_rollback * 1000) - deadline), 1)); - } - - function uci_apply(checked) { - uci_status_message('notice spinning', '<p><%:Starting configuration apply…%></p>'); - - xhr.post('<%=url("admin/uci")%>/' + (checked ? 'apply_rollback' : 'apply_unchecked'), uci_apply_auth, function(r, tok) { - if (r.status === (checked ? 200 : 204)) { - if (checked && tok !== null && typeof(tok) === 'object' && typeof(tok.token) === 'string') - uci_confirm_auth = tok; - - uci_confirm(checked, Date.now() + uci_apply_rollback * 1000); - } - else if (checked && r.status === 204) { - uci_status_message('notice', '<p><%:There are no changes to apply.%></p>'); - window.setTimeout(function() { - <% if redirect_ok then -%> - location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>'); - <%- else -%> - uci_status_message(false); - <%- end %> - }, uci_apply_display * 1000); - } - else { - uci_status_message('warning', '<p><%_Apply request failed with status <code>%h</code>%></p>'.format(r.responseText || r.statusText || r.status)); - window.setTimeout(function() { uci_status_message(false); }, uci_apply_display * 1000); - } - }); - } - - function uci_revert() { - uci_status_message('notice spinning', '<p><%:Reverting configuration…%></p>'); - - xhr.post('<%=url("admin/uci/revert")%>', uci_apply_auth, function(r) { - if (r.status === 200) { - uci_status_message('notice', '<p><%:Changes have been reverted.%></p>'); - window.setTimeout(function() { - <% if redirect_ok then -%> - location.href = decodeURIComponent('<%=luci.util.urlencode(redirect_ok)%>'); - <%- else -%> - window.location = window.location.href.split('#')[0]; - <%- end %> - }, uci_apply_display * 1000); - } - else { - uci_status_message('warning', '<p><%_Revert request failed with status <code>%h</code>%></p>'.format(r.statusText || r.status)); - window.setTimeout(function() { uci_status_message(false); }, uci_apply_display * 1000); - } - }); - } -//]]></script> -<%- end) %> diff --git a/modules/luci-base/luasrc/view/footer.htm b/modules/luci-base/luasrc/view/footer.htm index 1667d3aa9a..ba14ec8678 100644 --- a/modules/luci-base/luasrc/view/footer.htm +++ b/modules/luci-base/luasrc/view/footer.htm @@ -1,6 +1,6 @@ <%# Copyright 2008 Steven Barth <steven@midlink.org> - Copyright 2008 Jo-Philipp Wich <jow@openwrt.org> + Copyright 2008-2019 Jo-Philipp Wich <jo@mein.io> Licensed to the public under the Apache License 2.0. -%> @@ -8,18 +8,15 @@ local is_rollback_pending, rollback_time_remaining, rollback_session, rollback_token = luci.model.uci:rollback_pending() if is_rollback_pending or trigger_apply or trigger_revert then - include("cbi/apply_widget") - cbi_apply_widget(redirect, rollback_token) %> - <div class="alert-message" id="cbi_apply_status" style="display:none"></div> <script type="text/javascript"> - document.addEventListener("DOMContentLoaded", function() { + document.addEventListener("luci-loaded", function() { <% if trigger_apply then -%> - uci_apply(true); + L.ui.changes.apply(true); <%- elseif trigger_revert then -%> - uci_revert(); + L.ui.changes.revert(); <%- else -%> - uci_confirm(true, Date.now() + <%=rollback_time_remaining%> * 1000); + L.ui.changes.confirm(true, Date.now() + <%=rollback_time_remaining%> * 1000, <%=luci.http.write_json(rollback_token)%>); <%- end %> }); </script> diff --git a/modules/luci-base/luasrc/view/header.htm b/modules/luci-base/luasrc/view/header.htm index 70839c5d82..1ef0e5b013 100644 --- a/modules/luci-base/luasrc/view/header.htm +++ b/modules/luci-base/luasrc/view/header.htm @@ -1,6 +1,6 @@ <%# Copyright 2008 Steven Barth <steven@midlink.org> - Copyright 2008 Jo-Philipp Wich <jow@openwrt.org> + Copyright 2008-2019 Jo-Philipp Wich <jo@mein.io> Licensed to the public under the Apache License 2.0. -%> @@ -9,17 +9,25 @@ include("themes/" .. theme .. "/header") luci.dispatcher.context.template_header_sent = true end + + local applyconf = luci.config and luci.config.apply %> <script type="text/javascript" src="<%=resource%>/promis.min.js"></script> <script type="text/javascript" src="<%=resource%>/luci.js"></script> <script type="text/javascript"> L = new LuCI(<%= luci.http.write_json({ - token = token, - resource = resource, - scriptname = luci.http.getenv("SCRIPT_NAME"), - pathinfo = luci.http.getenv("PATH_INFO"), - requestpath = luci.dispatcher.context.requestpath, - pollinterval = luci.config.main.pollinterval or 5 + token = token, + resource = resource, + scriptname = luci.http.getenv("SCRIPT_NAME"), + pathinfo = luci.http.getenv("PATH_INFO"), + requestpath = luci.dispatcher.context.requestpath, + pollinterval = luci.config.main.pollinterval or 5, + sessionid = luci.dispatcher.context.authsession, + apply_rollback = math.max(applyconf and applyconf.rollback or 30, 30), + apply_holdoff = math.max(applyconf and applyconf.holdoff or 4, 1), + apply_timeout = math.max(applyconf and applyconf.timeout or 5, 1), + apply_display = math.max(applyconf and applyconf.display or 1.5, 1), + rollback_token = rollback_token }) %>); </script> |