diff options
author | Devaev Maxim <[email protected]> | 2020-07-21 15:02:57 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-07-21 15:27:14 +0300 |
commit | 360ff0090346fe2ead3e42d7bc39391325f8a2d3 (patch) | |
tree | 27de4c274acfe1abcc3df6b94d123a2cbb0fd03f /web/share | |
parent | b5ba546481dffa687395df725fb4dd57c7dc8338 (diff) |
refactoring
Diffstat (limited to 'web/share')
-rw-r--r-- | web/share/js/ipmi/main.js | 35 | ||||
-rw-r--r-- | web/share/js/keypad.js | 4 | ||||
-rw-r--r-- | web/share/js/kvm/atx.js | 31 | ||||
-rw-r--r-- | web/share/js/kvm/msd.js | 46 | ||||
-rw-r--r-- | web/share/js/kvm/session.js | 6 | ||||
-rw-r--r-- | web/share/js/kvm/stream.js | 35 | ||||
-rw-r--r-- | web/share/js/kvm/wol.js | 7 | ||||
-rw-r--r-- | web/share/js/tools.js | 22 | ||||
-rw-r--r-- | web/share/js/vnc/main.js | 5 |
9 files changed, 92 insertions, 99 deletions
diff --git a/web/share/js/ipmi/main.js b/web/share/js/ipmi/main.js index 2c48e7d3..a348de11 100644 --- a/web/share/js/ipmi/main.js +++ b/web/share/js/ipmi/main.js @@ -34,34 +34,23 @@ function __loadKvmdInfo() { let http = tools.makeRequest("GET", "/api/info", function() { if (http.readyState === 4) { if (http.status === 200) { - let port = JSON.parse(http.responseText).result.extras.ipmi.port; - let host = window.location.hostname; - let site = `${window.location.protocol}//${window.location.host}`; - $("ipmi-text").innerHTML = ` - <span class="code-comment"># Power on the server if it's off:<br> - $</span> ipmitool -I lanplus -U admin -P admin -H ${host} -p ${port} power on<br> + let ipmi_port = JSON.parse(http.responseText).result.extras.ipmi.port; + let make_item = (comment, ipmi, api) => ` + <span class="code-comment"># ${comment}:<br>$</span> + ipmitool -I lanplus -U admin -P admin -H ${window.location.hostname} -p ${ipmi_port} ${ipmi}<br> <span class="code-comment">$</span> curl -XPOST -HX-KVMD-User:admin -HX-KVMD-Passwd:admin -k \\<br> - ${site}/api/atx/power?action=on<br> + ${window.location.protocol}//${window.location.host}/api/atx${api}<br> + `; + $("ipmi-text").innerHTML = ` + ${make_item("Power on the server if it's off", "power on", "/power?action=on")} <br> - <span class="code-comment"># Soft power off the server if it's on:<br> - $</span> ipmitool -I lanplus -U admin -P admin -H ${host} -p ${port} power soft<br> - <span class="code-comment">$</span> curl -XPOST -HX-KVMD-User:admin -HX-KVMD-Passwd:admin -k \\<br> - ${site}/api/atx/power?action=off<br> + ${make_item("Soft power off the server if it's on", "power soft", "/power?action=off")} <br> - <span class="code-comment"># Hard power off the server if it's on:<br> - $</span> ipmitool -I lanplus -U admin -P admin -H ${host} -p ${port} power off<br> - <span class="code-comment">$</span> curl -XPOST -HX-KVMD-User:admin -HX-KVMD-Passwd:admin -k \\<br> - ${site}/api/atx/power?action=off_hard<br> + ${make_item("Hard power off the server if it's on", "power off", "/power?action=off_hard")} <br> - <span class="code-comment"># Hard reset the server if it's on:<br> - $</span> ipmitool -I lanplus -U admin -P admin -H ${host} -p ${port} power reset<br> - <span class="code-comment">$</span> curl -XPOST -HX-KVMD-User:admin -HX-KVMD-Passwd:admin -k \\<br> - ${site}/api/atx/power?action=reset_hard<br> + ${make_item("Hard reset the server if it's on", "power reset", "/power?action=reset_hard")} <br> - <span class="code-comment"># Check the power status:<br> - $</span> ipmitool -I lanplus -U admin -P admin -H ${host} -p ${port} power status<br> - <span class="code-comment">$</span> curl -HX-KVMD-User:admin -HX-KVMD-Passwd:admin -k \\<br> - ${site}/api/atx + ${make_item("Check the power status", "power status", "")} `; } else if (http.status === 401 || http.status === 403) { document.location.href = "/login"; diff --git a/web/share/js/keypad.js b/web/share/js/keypad.js index 7e4cf366..a3e96813 100644 --- a/web/share/js/keypad.js +++ b/web/share/js/keypad.js @@ -36,7 +36,7 @@ export function Keypad(keys_parent, key_callback) { var __modifiers = {}; var __init__ = function() { - for (let el_key of $$$(keys_parent + " div.key")) { + for (let el_key of $$$(`${keys_parent} div.key`)) { let code = el_key.getAttribute("data-code"); tools.setDefault(__keys, code, []); @@ -54,7 +54,7 @@ export function Keypad(keys_parent, key_callback) { }; } - for (let el_key of $$$(keys_parent + " div.modifier")) { + for (let el_key of $$$(`${keys_parent} div.modifier`)) { let code = el_key.getAttribute("data-code"); tools.setDefault(__modifiers, code, []); diff --git a/web/share/js/kvm/atx.js b/web/share/js/kvm/atx.js index 7c10a496..0614cb95 100644 --- a/web/share/js/kvm/atx.js +++ b/web/share/js/kvm/atx.js @@ -36,30 +36,37 @@ export function Atx() { $("atx-power-led").title = "Power Led"; $("atx-hdd-led").title = "Disk Activity Led"; - tools.setOnClick($("atx-power-button"), () => __clickButton("power", "Are you sure to click the power button?")); - tools.setOnClick($("atx-power-button-long"), () => __clickButton("power_long", "Are you sure to perform the long press of the power button?")); - tools.setOnClick($("atx-reset-button"), () => __clickButton("reset", "Are you sure to reboot the server?")); + for (let args of [ + ["atx-power-button", "power", "Are you sure to click the power button?"], + ["atx-power-button-long", "power_long", "Are you sure to perform the long press of the power button?"], + ["atx-reset-button", "reset", "Are you sure to reboot the server?"], + ]) { + tools.setOnClick($(args[0]), () => __clickButton(args[1], args[2])); + } }; /************************************************************************/ self.setState = function(state) { + let buttons_enabled = false; if (state) { - tools.setFeatureEnabled($("atx-dropdown"), state.enabled); + tools.featureSetEnabled($("atx-dropdown"), state.enabled); + $("atx-power-led").className = (state.busy ? "led-yellow" : (state.leds.power ? "led-green" : "led-gray")); + $("atx-hdd-led").className = (state.leds.hdd ? "led-red" : "led-gray"); + buttons_enabled = !state.busy; + } else { + $("atx-power-led").className = "led-gray"; + $("atx-hdd-led").className = "led-gray"; + } + for (let id of ["atx-power-button", "atx-power-button-long", "atx-reset-button"]) { + wm.switchEnabled($(id), buttons_enabled); } - - $("atx-power-led").className = ((state && state.leds.power) ? "led-green" : "led-gray"); - $("atx-hdd-led").className = ((state && state.leds.hdd) ? "led-red" : "led-gray"); - - wm.switchEnabled($("atx-power-button"), (state && !state.busy)); - wm.switchEnabled($("atx-power-button-long"), (state && !state.busy)); - wm.switchEnabled($("atx-reset-button"), (state && !state.busy)); }; var __clickButton = function(button, confirm_msg) { wm.confirm(confirm_msg).then(function(ok) { if (ok) { - let http = tools.makeRequest("POST", "/api/atx/click?button=" + button, function() { + let http = tools.makeRequest("POST", `/api/atx/click?button=${button}`, function() { if (http.readyState === 4) { if (http.status === 409) { wm.error("Performing another ATX operation for other client.<br>Please try again later"); diff --git a/web/share/js/kvm/msd.js b/web/share/js/kvm/msd.js index 739016c2..c3e5c8ae 100644 --- a/web/share/js/kvm/msd.js +++ b/web/share/js/kvm/msd.js @@ -42,7 +42,7 @@ export function Msd() { $("msd-image-selector").onchange = __selectImage; tools.setOnClick($("msd-remove-image"), __clickRemoveImageButton); - tools.setOnClickRadio("msd-mode-radio", __clickModeRadio); + tools.radioSetOnClick("msd-mode-radio", __clickModeRadio); $("msd-select-new-image-file").onchange = __selectNewImageFile; tools.setOnClick($("msd-select-new-image-button"), () => $("msd-select-new-image-file").click()); @@ -85,7 +85,7 @@ export function Msd() { }; var __clickModeRadio = function() { - __sendParam("cdrom", tools.getRadioValue("msd-mode-radio")); + __sendParam("cdrom", tools.radioGetValue("msd-mode-radio")); }; var __sendParam = function(name, value) { @@ -126,7 +126,7 @@ export function Msd() { var __uploadProgress = function(event) { if(event.lengthComputable) { let percent = Math.round((event.loaded * 100) / event.total); - tools.setProgressPercent($("msd-uploading-progress"), `${percent}%`, percent); + tools.progressSetValue($("msd-uploading-progress"), `${percent}%`, percent); } }; @@ -135,7 +135,7 @@ export function Msd() { __upload_http.upload.onprogress = null; __upload_http.abort(); __upload_http = null; - tools.setProgressPercent($("msd-uploading-progress"), "Aborted", 0); + tools.progressSetValue($("msd-uploading-progress"), "Aborted", 0); }; var __clickConnectButton = function(connect) { @@ -183,8 +183,8 @@ export function Msd() { var __applyState = function() { if (__state) { __toggleMsdFeatures(); - tools.setFeatureEnabled($("msd-dropdown"), __state.enabled); - tools.setFeatureEnabled($("msd-reset-button"), __state.enabled); + tools.featureSetEnabled($("msd-dropdown"), __state.enabled); + tools.featureSetEnabled($("msd-reset-button"), __state.enabled); __showMessageOffline(!__state.online); __showMessageImageBroken(__state.online && __state.drive.image && !__state.drive.image.complete && !__state.storage.uploading); @@ -212,10 +212,10 @@ export function Msd() { let size = __state.storage.size; let used = __state.storage.size - __state.storage.free; $("msd-storage-size").innerHTML = tools.formatSize(size); - tools.setProgressPercent($("msd-storage-progress"), `Storage: ${tools.formatSize(used)} of ${tools.formatSize(size)} used`, used / size * 100); + tools.progressSetValue($("msd-storage-progress"), `Storage: ${tools.formatSize(used)} of ${tools.formatSize(size)} used`, used / size * 100); } else { $("msd-storage-size").innerHTML = "Unavailable"; - tools.setProgressPercent($("msd-storage-progress"), "Storage: unavailable", 0); + tools.progressSetValue($("msd-storage-progress"), "Storage: unavailable", 0); } wm.switchEnabled($("msd-image-selector"), (__state.online && __state.features.multi && !__state.drive.connected && !__state.busy)); @@ -225,7 +225,7 @@ export function Msd() { wm.switchEnabled($("msd-remove-image"), (__state.online && __state.features.multi && __state.drive.image && !__state.drive.connected && !__state.busy)); wm.switchRadioEnabled("msd-mode-radio", (__state.online && __state.features.cdrom && !__state.drive.connected && !__state.busy)); - tools.setRadioValue("msd-mode-radio", `${Number(__state.online && __state.features.cdrom && __state.drive.cdrom)}`); + tools.radioSetValue("msd-mode-radio", `${Number(__state.online && __state.features.cdrom && __state.drive.cdrom)}`); wm.switchEnabled($("msd-connect-button"), (__state.online && (!__state.features.multi || __state.drive.image) && !__state.drive.connected && !__state.busy)); wm.switchEnabled($("msd-disconnect-button"), (__state.online && __state.drive.connected && !__state.busy)); @@ -236,11 +236,11 @@ export function Msd() { wm.switchEnabled($("msd-reset-button"), (__state.enabled && !__state.busy)); - tools.setHiddenVisible($("msd-submenu-new-image"), __image_file); + tools.hiddenSetVisible($("msd-submenu-new-image"), __image_file); $("msd-new-image-name").innerHTML = (__image_file ? __image_file.name : ""); $("msd-new-image-size").innerHTML = (__image_file ? tools.formatSize(__image_file.size) : ""); if (!__upload_http) { - tools.setProgressPercent($("msd-uploading-progress"), "Waiting for upload ...", 0); + tools.progressSetValue($("msd-uploading-progress"), "Waiting for upload ...", 0); } } else { @@ -255,14 +255,14 @@ export function Msd() { $("msd-image-name").innerHTML = ""; $("msd-image-size").innerHTML = ""; $("msd-storage-size").innerHTML = ""; - tools.setProgressPercent($("msd-storage-progress"), "", 0); + tools.progressSetValue($("msd-storage-progress"), "", 0); wm.switchEnabled($("msd-image-selector"), false); $("msd-image-selector").options.length = 1; wm.switchEnabled($("msd-remove-image"), false); wm.switchRadioEnabled("msd-mode-radio", false); - tools.setRadioValue("msd-mode-radio", "0"); + tools.radioSetValue("msd-mode-radio", "0"); wm.switchEnabled($("msd-connect-button"), false); wm.switchEnabled($("msd-disconnect-button"), false); @@ -274,43 +274,43 @@ export function Msd() { wm.switchEnabled($("msd-reset-button"), false); $("msd-select-new-image-file").value = ""; - tools.setHiddenVisible($("msd-submenu-new-image"), false); + tools.hiddenSetVisible($("msd-submenu-new-image"), false); $("msd-new-image-name").innerHTML = ""; $("msd-new-image-size").innerHTML = ""; - tools.setProgressPercent($("msd-uploading-progress"), "", 0); + tools.progressSetValue($("msd-uploading-progress"), "", 0); } }; var __toggleMsdFeatures = function() { for (let el of $$$(".msd-single-storage")) { - tools.setFeatureEnabled(el, !__state.features.multi); + tools.featureSetEnabled(el, !__state.features.multi); } for (let el of $$$(".msd-multi-storage")) { - tools.setFeatureEnabled(el, __state.features.multi); + tools.featureSetEnabled(el, __state.features.multi); } for (let el of $$$(".msd-cdrom-emulation")) { - tools.setFeatureEnabled(el, __state.features.cdrom); + tools.featureSetEnabled(el, __state.features.cdrom); } }; var __showMessageOffline = function(visible) { - tools.setHiddenVisible($("msd-message-offline"), visible); + tools.hiddenSetVisible($("msd-message-offline"), visible); }; var __showMessageImageBroken = function(visible) { - tools.setHiddenVisible($("msd-message-image-broken"), visible); + tools.hiddenSetVisible($("msd-message-image-broken"), visible); }; var __showMessageTooBigForCdrom = function(visible) { - tools.setHiddenVisible($("msd-message-too-big-for-cdrom"), visible); + tools.hiddenSetVisible($("msd-message-too-big-for-cdrom"), visible); }; var __showMessageOutOfStorage = function(visible) { - tools.setHiddenVisible($("msd-message-out-of-storage"), visible); + tools.hiddenSetVisible($("msd-message-out-of-storage"), visible); }; var __showMessageAnotherUserUploads = function(visible) { - tools.setHiddenVisible($("msd-message-another-user-uploads"), visible); + tools.hiddenSetVisible($("msd-message-another-user-uploads"), visible); }; var __setStatus = function(led_cls, msg) { diff --git a/web/share/js/kvm/session.js b/web/share/js/kvm/session.js index 72b68151..06ffcd95 100644 --- a/web/share/js/kvm/session.js +++ b/web/share/js/kvm/session.js @@ -92,11 +92,11 @@ export function Session() { let undervoltage = (flags.undervoltage.now || flags.undervoltage.past); let freq_capped = (flags.freq_capped.now || flags.freq_capped.past); - tools.setHiddenVisible($("hw-health-dropdown"), (undervoltage || freq_capped)); + tools.hiddenSetVisible($("hw-health-dropdown"), (undervoltage || freq_capped)); $("hw-health-undervoltage-led").className = (undervoltage ? (flags.undervoltage.now ? "led-red" : "led-yellow") : "hidden"); $("hw-health-overheating-led").className = (freq_capped ? (flags.freq_capped.now ? "led-red" : "led-yellow") : "hidden"); - tools.setHiddenVisible($("hw-health-message-undervoltage"), undervoltage); - tools.setHiddenVisible($("hw-health-message-overheating"), freq_capped); + tools.hiddenSetVisible($("hw-health-message-undervoltage"), undervoltage); + tools.hiddenSetVisible($("hw-health-message-overheating"), freq_capped); } }; diff --git a/web/share/js/kvm/stream.js b/web/share/js/kvm/stream.js index 646ee88e..4e04aa9d 100644 --- a/web/share/js/kvm/stream.js +++ b/web/share/js/kvm/stream.js @@ -43,27 +43,18 @@ export function Streamer() { var __init__ = function() { $("stream-led").title = "Stream inactive"; - $("stream-quality-slider").min = 5; - $("stream-quality-slider").max = 100; - $("stream-quality-slider").step = 5; - $("stream-quality-slider").value = 80; - tools.setOnUpSlider($("stream-quality-slider"), 1000, __updateQualityValue, (value) => __sendParam("quality", value)); - - $("stream-desired-fps-slider").min = 0; - $("stream-desired-fps-slider").max = 120; - $("stream-desired-fps-slider").step = 1; - $("stream-desired-fps-slider").value = 0; - tools.setOnUpSlider($("stream-desired-fps-slider"), 1000, __updateDesiredFpsValue, (value) => __sendParam("desired_fps", value)); + tools.sliderSetParams($("stream-quality-slider"), 5, 100, 5, 80); + tools.sliderSetOnUp($("stream-quality-slider"), 1000, __updateQualityValue, (value) => __sendParam("quality", value)); + + tools.sliderSetParams($("stream-desired-fps-slider"), 0, 120, 1, 0); + tools.sliderSetOnUp($("stream-desired-fps-slider"), 1000, __updateDesiredFpsValue, (value) => __sendParam("desired_fps", value)); $("stream-resolution-selector").onchange = (() => { wm.switchEnabled($("stream-resolution-selector"), false); __sendParam("resolution", $("stream-resolution-selector").value); }); - $("stream-size-slider").min = 20; - $("stream-size-slider").max = 200; - $("stream-size-slider").step = 5; - $("stream-size-slider").value = 100; + tools.sliderSetParams($("stream-size-slider"), 20, 200, 5, 100); $("stream-size-slider").oninput = () => __resize(); $("stream-size-slider").onchange = () => __resize(); @@ -75,8 +66,8 @@ export function Streamer() { self.setState = function(state) { if (state) { - tools.setFeatureEnabled($("stream-quality"), state.features.quality && (state.streamer === null || state.streamer.encoder.quality > 0)); - tools.setFeatureEnabled($("stream-resolution"), state.features.resolution); + tools.featureSetEnabled($("stream-quality"), state.features.quality && (state.streamer === null || state.streamer.encoder.quality > 0)); + tools.featureSetEnabled($("stream-resolution"), state.features.resolution); } if (state && state.streamer) { @@ -211,7 +202,7 @@ export function Streamer() { }; var __updateQualityValue = function(value) { - $("stream-quality-value").innerHTML = value + "%"; + $("stream-quality-value").innerHTML = `${value}%`; }; var __updateDesiredFpsValue = function(value) { @@ -253,7 +244,7 @@ export function Streamer() { var __resize = function() { let size = $("stream-size-slider").value; - $("stream-size-value").innerHTML = size + "%"; + $("stream-size-value").innerHTML = `${size}%`; __size_factor = size / 100; __applySizeFactor(); }; @@ -281,9 +272,9 @@ export function Streamer() { }; var __applySizeFactor = function() { - let el_stream_image = $("stream-image"); - el_stream_image.style.width = __resolution.width * __size_factor + "px"; - el_stream_image.style.height = __resolution.height * __size_factor + "px"; + let el = $("stream-image"); + el.style.width = __resolution.width * __size_factor + "px"; + el.style.height = __resolution.height * __size_factor + "px"; wm.showWindow($("stream-window"), false); }; diff --git a/web/share/js/kvm/wol.js b/web/share/js/kvm/wol.js index 4833646c..1492d89d 100644 --- a/web/share/js/kvm/wol.js +++ b/web/share/js/kvm/wol.js @@ -42,14 +42,17 @@ export function WakeOnLan() { self.setState = function(state) { if (state) { - tools.setFeatureEnabled($("wol"), state.enabled); + tools.featureSetEnabled($("wol"), state.enabled); __target = state.target; } wm.switchEnabled($("wol-wakeup-button"), (state && state.enabled)); }; var __clickWakeupButton = function() { - let msg = `Are you sure to send Wake-on-LAN packet to the server?<br>Target: <b>${__target.mac}</b> (${__target.ip}:${__target.port})?`; + let msg = ` + Are you sure to send Wake-on-LAN packet to the server?<br> + Target: <b>${__target.mac}</b> (${__target.ip}:${__target.port})? + `; wm.confirm(msg).then(function(ok) { if (ok) { let http = tools.makeRequest("POST", "/api/wol/wakeup", function() { diff --git a/web/share/js/tools.js b/web/share/js/tools.js index 3cf48252..74565932 100644 --- a/web/share/js/tools.js +++ b/web/share/js/tools.js @@ -107,7 +107,7 @@ export var tools = new function() { }; }; - this.setOnUpSlider = function(el, delay, display_callback, execute_callback) { + this.sliderSetOnUp = function(el, delay, display_callback, execute_callback) { el.execution_timer = null; el.activated = false; @@ -134,33 +134,37 @@ export var tools = new function() { }, delay); }; }; + this.sliderSetParams = function(el, min, max, step, value) { + el.min = min; + el.max = max; + el.step = step; + el.value = value; + }; - this.setOnClickRadio = function(name, callback) { + this.radioSetOnClick = function(name, callback) { for (let el of $$$(`input[type="radio"][name="${name}"]`)) { this.setOnClick(el, callback); } }; - - this.getRadioValue = function(name) { + this.radioGetValue = function(name) { return document.querySelector(`input[type="radio"][name="${name}"]:checked`).value; }; - - this.setRadioValue = function(name, value) { + this.radioSetValue = function(name, value) { for (let el of $$$(`input[type="radio"][name="${name}"]`)) { el.checked = (el.value === value); } }; - this.setProgressPercent = function(el, title, percent) { + this.progressSetValue = function(el, title, percent) { el.setAttribute("data-label", title); $(`${el.id}-value`).style.width = `${percent}%`; }; - this.setHiddenVisible = function(el, visible) { + this.hiddenSetVisible = function(el, visible) { el.classList.toggle("hidden", !visible); }; - this.setFeatureEnabled = function(el, enabled) { + this.featureSetEnabled = function(el, enabled) { el.classList.toggle("feature-disabled", !enabled); }; diff --git a/web/share/js/vnc/main.js b/web/share/js/vnc/main.js index 41f310b7..f28427c5 100644 --- a/web/share/js/vnc/main.js +++ b/web/share/js/vnc/main.js @@ -34,11 +34,10 @@ function __loadKvmdInfo() { let http = tools.makeRequest("GET", "/api/info", function() { if (http.readyState === 4) { if (http.status === 200) { - let port = JSON.parse(http.responseText).result.extras.vnc.port; - let host = window.location.hostname; + let vnc_port = JSON.parse(http.responseText).result.extras.vnc.port; $("vnc-text").innerHTML = ` <span class="code-comment"># How to connect using the Linux terminal:<br> - $</span> vncviewer ${host}::${port} + $</span> vncviewer ${window.location.hostname}::${vnc_port} `; } else if (http.status === 401 || http.status === 403) { document.location.href = "/login"; |