From c318a10c14eba2eab616ab875a23722ef7655185 Mon Sep 17 00:00:00 2001 From: Paul Spooren Date: Sun, 24 Feb 2019 22:24:24 +0100 Subject: [PATCH] luci-app-attendedsysupgrade: refactor and format * Reuse `request_dict` between requests. * used vim Autoformat plugin to make the javascript code nice. * add board_name as it eventually replaces board/model in requests Signed-off-by: Paul Spooren --- .../resources/attendedsysupgrade.js | 680 +++++++++--------- 1 file changed, 358 insertions(+), 322 deletions(-) diff --git a/applications/luci-app-attendedsysupgrade/root/www/luci-static/resources/attendedsysupgrade.js b/applications/luci-app-attendedsysupgrade/root/www/luci-static/resources/attendedsysupgrade.js index e23414f70..a5fb0a1cb 100644 --- a/applications/luci-app-attendedsysupgrade/root/www/luci-static/resources/attendedsysupgrade.js +++ b/applications/luci-app-attendedsysupgrade/root/www/luci-static/resources/attendedsysupgrade.js @@ -1,382 +1,418 @@ -function $(s) { return document.getElementById(s.substring(1)); } +function $(s) { + return document.getElementById(s.substring(1)); +} -function show(s) { $(s).style.display = 'block'; } +function show(s) { + $(s).style.display = 'block'; +} -function hide(s) { $(s).style.display = 'none'; } +function hide(s) { + $(s).style.display = 'none'; +} function set_server() { - hide("#status_box"); - data.url = $("#server").value; - ubus_call("uci", "set", { "config": "attendedsysupgrade", "section": "server", values: { "url": data.url } }) - ubus_call("uci", "commit", { "config": "attendedsysupgrade" }) - var server_button = $("#server") - server_button.type = 'button'; - server_button.className = 'cbi-button cbi-button-edit'; - server_button.parentElement.removeChild($("#button_set")); - server_button.onclick = edit_server; + hide("#status_box"); + data.url = $("#server").value; + ubus_call("uci", "set", { + "config": "attendedsysupgrade", + "section": "server", + values: { + "url": data.url + } + }) + ubus_call("uci", "commit", { + "config": "attendedsysupgrade" + }) + var server_button = $("#server") + server_button.type = 'button'; + server_button.className = 'cbi-button cbi-button-edit'; + server_button.parentElement.removeChild($("#button_set")); + server_button.onclick = edit_server; } function edit_server() { - $("#server").type = 'text'; - $("#server").onkeydown = function(event) { - if(event.key === 'Enter') { - set_server(); - return false; - } - } - $("#server").className = ''; - $("#server").onclick = null; - - var button_set = document.createElement("input"); - button_set.type = "button"; - button_set.value = "Save"; - button_set.name = "button_set"; - button_set.id = "button_set"; - button_set.className = 'cbi-button cbi-button-save'; - button_set.onclick = set_server - $("#server").parentElement.appendChild(button_set); + $("#server").type = 'text'; + $("#server").onkeydown = function(event) { + if (event.key === 'Enter') { + set_server(); + return false; + } + } + $("#server").className = ''; + $("#server").onclick = null; + + var button_set = document.createElement("input"); + button_set.type = "button"; + button_set.value = "Save"; + button_set.name = "button_set"; + button_set.id = "button_set"; + button_set.className = 'cbi-button cbi-button-save'; + button_set.onclick = set_server + $("#server").parentElement.appendChild(button_set); } function edit_packages() { - data.edit_packages = true - hide("#edit_button"); - $("#edit_packages").value = data.packages.join("\n"); - show("#edit_packages"); + data.edit_packages = true + hide("#edit_button"); + $("#edit_packages").value = data.packages.join("\n"); + show("#edit_packages"); } // requests to the upgrade server -function server_request(request_dict, path, callback) { - request_dict.distro = data.release.distribution; - request_dict.target = data.release.target; - var request = new XMLHttpRequest(); - request.open("POST", data.url + "/" + path, true); - request.setRequestHeader("Content-type", "application/json"); - request.send(JSON.stringify(request_dict)); - request.onerror = function(e) { - set_status("danger", "upgrade server down") - show("#server_div"); - } - request.addEventListener('load', function(event) { - callback(request) - }); +function server_request(path, callback) { + var request = new XMLHttpRequest(); + request.open("POST", data.url + "/" + path, true); + request.setRequestHeader("Content-type", "application/json"); + request.send(JSON.stringify(request_dict)); + request.onerror = function(e) { + set_status("danger", "upgrade server down") + show("#server_div"); + } + request.addEventListener('load', function(event) { + callback(request) + }); } // initial setup, get system information function setup() { - ubus_call("rpc-sys", "packagelist", {}, "packages"); - ubus_call("system", "board", {}, "release"); - ubus_call("system", "board", {}, "board_name"); - ubus_call("system", "board", {}, "model"); - ubus_call("system", "info", {}, "memory"); - uci_get({ "config": "attendedsysupgrade", "section": "server", "option": "url" }) - uci_get({ "config": "attendedsysupgrade", "section": "client", "option": "upgrade_packages" }) - uci_get({ "config": "attendedsysupgrade", "section": "client", "option": "advanced_mode" }) - uci_get({ "config": "attendedsysupgrade", "section": "client", "option": "auto_search" }) - setup_ready(); + ubus_call("rpc-sys", "packagelist", {}, "packages"); + ubus_call("system", "board", {}, "release"); + ubus_call("system", "board", {}, "board_name"); + ubus_call("system", "board", {}, "model"); + ubus_call("system", "info", {}, "memory"); + uci_get({ + "config": "attendedsysupgrade", + "section": "server", + "option": "url" + }) + uci_get({ + "config": "attendedsysupgrade", + "section": "client", + "option": "upgrade_packages" + }) + uci_get({ + "config": "attendedsysupgrade", + "section": "client", + "option": "advanced_mode" + }) + uci_get({ + "config": "attendedsysupgrade", + "section": "client", + "option": "auto_search" + }) + setup_ready(); } function setup_ready() { - // checks if a async ubus calls have finished - if(ubus_counter != ubus_closed) { - setTimeout(setup_ready, 300) - } else { - if(data.auto_search == 1) { - upgrade_check(); - } else { - show("#upgrade_button"); - show("#server_div"); - $("#server").value = data.url; - } - } + // checks if a async ubus calls have finished + if (ubus_counter != ubus_closed) { + setTimeout(setup_ready, 300) + } else { + if (data.auto_search == 1) { + upgrade_check(); + } else { + show("#upgrade_button"); + show("#server_div"); + $("#server").value = data.url; + } + } } function uci_get(option) { - // simple wrapper to get a uci value store in data.