Skip to content

Commit

Permalink
Make it easier for downstream to modify settings
Browse files Browse the repository at this point in the history
Expose a simple and stable API to override default settings, and force
settings that users shouldn't be able to change.
  • Loading branch information
CendioOssman committed Aug 8, 2024
1 parent 84897fd commit 5f3ec21
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
43 changes: 32 additions & 11 deletions app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const LINGUAS = ["cs", "de", "el", "es", "fr", "it", "ja", "ko", "nl", "pl", "pt

const UI = {

userSettings: {},

connected: false,
desktopName: "",

Expand All @@ -44,7 +46,15 @@ const UI = {
reconnectCallback: null,
reconnectPassword: null,

async start() {
async start(options={}) {
UI.userSettings = options.settings || {};
if (UI.userSettings.defaults === undefined) {
UI.userSettings.defaults = {};
}
if (UI.userSettings.mandatory === undefined) {
UI.userSettings.mandatory = {};
}

// Set up translations
try {
await l10n.setup(LINGUAS, "app/locale/");
Expand Down Expand Up @@ -159,6 +169,8 @@ 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');
Expand All @@ -175,8 +187,6 @@ const UI = {
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 @@ -738,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.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.mandatory) {
val = UI.userSettings.mandatory[name];
UI.forceSetting(name, val);
}
return val;
},

Expand Down Expand Up @@ -817,17 +836,21 @@ const UI = {
// disable the labels that belong to disabled input elements.
disableSetting(name) {
const ctrl = document.getElementById('noVNC_setting_' + name);
ctrl.disabled = true;
if (ctrl.label !== undefined) {
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;
if (ctrl.label !== undefined) {
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 @@ -1771,6 +1794,4 @@ const UI = {
*/
};

UI.start();

export default UI;
20 changes: 19 additions & 1 deletion vnc.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,25 @@
<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";

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

// 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;

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

<body>
Expand Down

0 comments on commit 5f3ec21

Please sign in to comment.