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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

const PAGE_TITLE = "noVNC";

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

const UI = {

userSettings: {},

connected: false,
desktopName: "",

Expand All @@ -41,16 +45,44 @@ const UI = {
reconnectCallback: null,
reconnectPassword: null,

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

return new Promise((resolve, reject) => {
document.addEventListener('DOMContentLoaded', () => UI.start().then(resolve).catch(reject));
});
});
let promise;

// Set up translations
l10n.setup(LINGUAS);
if (l10n.language === "en" || l10n.dictionary !== undefined) {
promise = Promise.resolve();
} else {
promise = WebUtil.fetchJSON('app/locale/' + l10n.language + '.json')
.then((translations) => { l10n.dictionary = translations; })
.catch(err => Log.Error("Failed to load translations: " + err));
}

promise
// Initialize setting storage
.then(WebUtil.initSettings)
// Wait for the page to load
.then(() => {
if (document.readyState === "interactive" || document.readyState === "complete") {
return;
}

return new Promise((resolve, reject) => {
document.addEventListener('DOMContentLoaded', () => UI.start().then(resolve).catch(reject));
});
})
// Finally start the UI
.then(UI.start);

return promise;
},

// Render default UI and initialize settings menu
Expand Down Expand Up @@ -106,7 +138,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 Down Expand Up @@ -155,23 +187,26 @@ const UI = {
}
}

UI.setupSettingLabels();

/* Populate the controls if defaults are provided in the URL */
UI.initSetting('host', window.location.hostname);
UI.initSetting('port', port);
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 @@ -719,13 +754,22 @@ const UI = {

// Initial page load read/initialization of settings
initSetting(name, defVal) {
// Has the user overridden the default value?
if (name in UI.userSettings.defaults) {
defVal = UI.userSettings.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.userSettings.forced) {
val = UI.userSettings.forced[name];
UI.forceSetting(name, val);
}
return val;
},

Expand All @@ -744,9 +788,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 @@ -784,7 +831,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 @@ -799,14 +847,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 @@ -993,7 +1049,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 @@ -1662,7 +1718,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 @@ -1693,16 +1749,4 @@ const UI = {
*/
};

// Set up translations
const LINGUAS = ["cs", "de", "el", "es", "ja", "ko", "nl", "pl", "ru", "sv", "tr", "zh_CN", "zh_TW"];
l10n.setup(LINGUAS);
if (l10n.language === "en" || l10n.dictionary !== undefined) {
UI.prime();
} else {
WebUtil.fetchJSON('app/locale/' + l10n.language + '.json')
.then((translations) => { l10n.dictionary = translations; })
.catch(err => Log.Error("Failed to load translations: " + err))
.then(UI.prime);
}

export default UI;
19 changes: 18 additions & 1 deletion vnc.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,24 @@
<!-- ES2015/ES6 modules polyfill -->
<script nomodule src="vendor/browser-es-module-loader/dist/browser-es-module-loader.js"></script>
<!-- actual script modules -->
<script type="module" crossorigin="anonymous" src="app/ui.js"></script>
<script type="module">
import UI from "./app/ui.js";

let defaults = {};
let forced = {};

// Override any defaults you need here:
//
// defaults['host'] = 'vnc.example.com';

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

UI.run({ settings: { defaults: defaults,
forced: forced } });
</script>
<!-- end scripts -->
</head>

Expand Down