Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier for downstream to modify settings #1428

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 76 additions & 47 deletions app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ import * as WebUtil from "./webutil.js";

const PAGE_TITLE = "noVNC";

const LINGUAS = ["cs", "de", "el", "es", "fr", "it", "ja", "ko", "nl", "pl", "pt_BR", "ru", "sv", "tr", "zh_CN", "zh_TW"];

const UI = {

customSettings: {},

connected: false,
desktopName: "",

Expand All @@ -42,20 +46,31 @@ const UI = {
reconnectCallback: null,
reconnectPassword: null,

prime() {
return WebUtil.initSettings().then(() => {
if (document.readyState === "interactive" || document.readyState === "complete") {
return UI.start();
}
async start(options={}) {
UI.customSettings = options.settings || {};
if (UI.customSettings.defaults === undefined) {
UI.customSettings.defaults = {};
}
if (UI.customSettings.mandatory === undefined) {
UI.customSettings.mandatory = {};
}

return new Promise((resolve, reject) => {
document.addEventListener('DOMContentLoaded', () => UI.start().then(resolve).catch(reject));
});
});
},
// Set up translations
try {
await l10n.setup(LINGUAS, "app/locale/");
} catch (err) {
Log.Error("Failed to load translations: " + err);
}

// Render default UI and initialize settings menu
start() {
// Initialize setting storage
await WebUtil.initSettings();

// Wait for the page to load
if (document.readyState !== "interactive" && document.readyState !== "complete") {
await new Promise((resolve, reject) => {
document.addEventListener('DOMContentLoaded', resolve);
});
}

UI.initSettings();

Expand All @@ -70,22 +85,20 @@ const UI = {
}

// Try to fetch version number
fetch('./package.json')
.then((response) => {
if (!response.ok) {
throw Error("" + response.status + " " + response.statusText);
}
return response.json();
})
.then((packageInfo) => {
Array.from(document.getElementsByClassName('noVNC_version')).forEach(el => el.innerText = packageInfo.version);
})
.catch((err) => {
Log.Error("Couldn't fetch package.json: " + err);
Array.from(document.getElementsByClassName('noVNC_version_wrapper'))
.concat(Array.from(document.getElementsByClassName('noVNC_version_separator')))
.forEach(el => el.style.display = 'none');
});
try {
let response = await fetch('./package.json');
if (!response.ok) {
throw Error("" + response.status + " " + response.statusText);
}

let packageInfo = await response.json();
Array.from(document.getElementsByClassName('noVNC_version')).forEach(el => el.innerText = packageInfo.version);
} catch (err) {
Log.Error("Couldn't fetch package.json: " + err);
Array.from(document.getElementsByClassName('noVNC_version_wrapper'))
.concat(Array.from(document.getElementsByClassName('noVNC_version_separator')))
.forEach(el => el.style.display = 'none');
}

// Adapt the interface for touch screen devices
if (isTouchDevice) {
Expand Down Expand Up @@ -120,7 +133,7 @@ const UI = {

document.documentElement.classList.remove("noVNC_loading");

let autoconnect = WebUtil.getConfigVar('autoconnect', false);
let autoconnect = UI.getSetting('autoconnect');
if (autoconnect === 'true' || autoconnect == '1') {
autoconnect = true;
UI.connect();
Expand All @@ -129,8 +142,6 @@ const UI = {
// Show the connect panel on first load unless autoconnecting
UI.openConnectPanel();
}

return Promise.resolve(UI.rfb);
},

initFullscreen() {
Expand Down Expand Up @@ -158,21 +169,24 @@ const UI = {
UI.initSetting('logging', 'warn');
UI.updateLogging();

UI.setupSettingLabels();

/* Populate the controls if defaults are provided in the URL */
UI.initSetting('encrypt', (window.location.protocol === "https:"));
UI.initSetting('password');
UI.initSetting('autoconnect', false);
UI.initSetting('view_clip', false);
UI.initSetting('resize', 'off');
UI.initSetting('quality', 6);
UI.initSetting('compression', 2);
UI.initSetting('shared', true);
UI.initSetting('bell', 'on');
UI.initSetting('view_only', false);
UI.initSetting('show_dot', false);
UI.initSetting('path', 'websockify');
UI.initSetting('repeaterID', '');
UI.initSetting('reconnect', false);
UI.initSetting('reconnect_delay', 5000);

UI.setupSettingLabels();
},
// Adds a link to the label elements on the corresponding input elements
setupSettingLabels() {
Expand Down Expand Up @@ -734,13 +748,22 @@ const UI = {

// Initial page load read/initialization of settings
initSetting(name, defVal) {
// Has the user overridden the default value?
if (name in UI.customSettings.defaults) {
defVal = UI.customSettings.defaults[name];
}
// Check Query string followed by cookie
let val = WebUtil.getConfigVar(name);
if (val === null) {
val = WebUtil.readSetting(name, defVal);
}
WebUtil.setSetting(name, val);
UI.updateSetting(name);
// Has the user forced a value?
if (name in UI.customSettings.mandatory) {
val = UI.customSettings.mandatory[name];
UI.forceSetting(name, val);
}
return val;
},

Expand All @@ -759,9 +782,12 @@ const UI = {
let value = UI.getSetting(name);

const ctrl = document.getElementById('noVNC_setting_' + name);
if (ctrl === null) {
return;
}

if (ctrl.type === 'checkbox') {
ctrl.checked = value;

} else if (typeof ctrl.options !== 'undefined') {
for (let i = 0; i < ctrl.options.length; i += 1) {
if (ctrl.options[i].value === value) {
Expand Down Expand Up @@ -794,7 +820,8 @@ const UI = {
getSetting(name) {
const ctrl = document.getElementById('noVNC_setting_' + name);
let val = WebUtil.readSetting(name);
if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
if (typeof val !== 'undefined' && val !== null &&
ctrl !== null && ctrl.type === 'checkbox') {
if (val.toString().toLowerCase() in {'0': 1, 'no': 1, 'false': 1}) {
val = false;
} else {
Expand All @@ -809,14 +836,22 @@ const UI = {
// disable the labels that belong to disabled input elements.
disableSetting(name) {
const ctrl = document.getElementById('noVNC_setting_' + name);
ctrl.disabled = true;
ctrl.label.classList.add('noVNC_disabled');
if (ctrl !== null) {
ctrl.disabled = true;
if (ctrl.label !== undefined) {
ctrl.label.classList.add('noVNC_disabled');
}
}
},

enableSetting(name) {
const ctrl = document.getElementById('noVNC_setting_' + name);
ctrl.disabled = false;
ctrl.label.classList.remove('noVNC_disabled');
if (ctrl !== null) {
ctrl.disabled = false;
if (ctrl.label !== undefined) {
ctrl.label.classList.remove('noVNC_disabled');
}
}
},

/* ------^-------
Expand Down Expand Up @@ -998,7 +1033,7 @@ const UI = {
const path = UI.getSetting('path');

if (typeof password === 'undefined') {
password = WebUtil.getConfigVar('password');
password = UI.getSetting('password');
UI.reconnectPassword = password;
}

Expand Down Expand Up @@ -1728,7 +1763,7 @@ const UI = {
},

bell(e) {
if (WebUtil.getConfigVar('bell', 'on') === 'on') {
if (UI.getSetting('bell') === 'on') {
const promise = document.getElementById('noVNC_bell').play();
// The standards disagree on the return value here
if (promise) {
Expand Down Expand Up @@ -1759,10 +1794,4 @@ const UI = {
*/
};

// Set up translations
const LINGUAS = ["cs", "de", "el", "es", "fr", "it", "ja", "ko", "nl", "pl", "pt_BR", "ru", "sv", "tr", "zh_CN", "zh_TW"];
l10n.setup(LINGUAS, "app/locale/")
.catch(err => Log.Error("Failed to load translations: " + err))
.then(UI.prime);

export default UI;
1 change: 1 addition & 0 deletions defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions mandatory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
51 changes: 50 additions & 1 deletion vnc.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,56 @@
<link rel="preload" as="image" href="app/images/warning.svg">

<script type="module" crossorigin="anonymous" src="app/error-handler.js"></script>
<script type="module" crossorigin="anonymous" src="app/ui.js"></script>

<script type="module">
import UI from "./app/ui.js";
import * as Log from './core/util/logging.js';

let response;

let defaults = {};
let mandatory = {};

// Default settings will be loaded from defaults.json. Mandatory
// settings will be loaded from mandatory.json, which the user
// cannot change.

try {
response = await fetch('./defaults.json');
if (!response.ok) {
throw Error("" + response.status + " " + response.statusText);
}

defaults = await response.json();
} catch (err) {
Log.Error("Couldn't fetch defaults.json: " + err);
}

try {
response = await fetch('./mandatory.json');
if (!response.ok) {
throw Error("" + response.status + " " + response.statusText);
}

mandatory = await response.json();
} catch (err) {
Log.Error("Couldn't fetch mandatory.json: " + err);
}

// You can also override any defaults you need here:
//
// defaults['host'] = 'vnc.example.com';

// Or force a specific setting, preventing the user from
// changing it:
//
// mandatory['view_only'] = true;

// See docs/EMBEDDING.md for a list of possible settings.

UI.start({ settings: { defaults: defaults,
mandatory: mandatory } });
</script>
</head>

<body>
Expand Down
Loading