Skip to content

Commit 438e5b3

Browse files
committed
Make it easier for downstream to modify settings
Expose a simple and stable API to override default settings, and force settings that users shouldn't be able to change.
1 parent 84897fd commit 438e5b3

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

app/ui.js

+32-11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const LINGUAS = ["cs", "de", "el", "es", "fr", "it", "ja", "ko", "nl", "pl", "pt
2424

2525
const UI = {
2626

27+
customSettings: {},
28+
2729
connected: false,
2830
desktopName: "",
2931

@@ -44,7 +46,15 @@ const UI = {
4446
reconnectCallback: null,
4547
reconnectPassword: null,
4648

47-
async start() {
49+
async start(options={}) {
50+
UI.customSettings = options.settings || {};
51+
if (UI.customSettings.defaults === undefined) {
52+
UI.customSettings.defaults = {};
53+
}
54+
if (UI.customSettings.mandatory === undefined) {
55+
UI.customSettings.mandatory = {};
56+
}
57+
4858
// Set up translations
4959
try {
5060
await l10n.setup(LINGUAS, "app/locale/");
@@ -159,6 +169,8 @@ const UI = {
159169
UI.initSetting('logging', 'warn');
160170
UI.updateLogging();
161171

172+
UI.setupSettingLabels();
173+
162174
/* Populate the controls if defaults are provided in the URL */
163175
UI.initSetting('encrypt', (window.location.protocol === "https:"));
164176
UI.initSetting('password');
@@ -175,8 +187,6 @@ const UI = {
175187
UI.initSetting('repeaterID', '');
176188
UI.initSetting('reconnect', false);
177189
UI.initSetting('reconnect_delay', 5000);
178-
179-
UI.setupSettingLabels();
180190
},
181191
// Adds a link to the label elements on the corresponding input elements
182192
setupSettingLabels() {
@@ -738,13 +748,22 @@ const UI = {
738748

739749
// Initial page load read/initialization of settings
740750
initSetting(name, defVal) {
751+
// Has the user overridden the default value?
752+
if (name in UI.customSettings.defaults) {
753+
defVal = UI.customSettings.defaults[name];
754+
}
741755
// Check Query string followed by cookie
742756
let val = WebUtil.getConfigVar(name);
743757
if (val === null) {
744758
val = WebUtil.readSetting(name, defVal);
745759
}
746760
WebUtil.setSetting(name, val);
747761
UI.updateSetting(name);
762+
// Has the user forced a value?
763+
if (name in UI.customSettings.mandatory) {
764+
val = UI.customSettings.mandatory[name];
765+
UI.forceSetting(name, val);
766+
}
748767
return val;
749768
},
750769

@@ -817,17 +836,21 @@ const UI = {
817836
// disable the labels that belong to disabled input elements.
818837
disableSetting(name) {
819838
const ctrl = document.getElementById('noVNC_setting_' + name);
820-
ctrl.disabled = true;
821-
if (ctrl.label !== undefined) {
822-
ctrl.label.classList.add('noVNC_disabled');
839+
if (ctrl !== null) {
840+
ctrl.disabled = true;
841+
if (ctrl.label !== undefined) {
842+
ctrl.label.classList.add('noVNC_disabled');
843+
}
823844
}
824845
},
825846

826847
enableSetting(name) {
827848
const ctrl = document.getElementById('noVNC_setting_' + name);
828-
ctrl.disabled = false;
829-
if (ctrl.label !== undefined) {
830-
ctrl.label.classList.remove('noVNC_disabled');
849+
if (ctrl !== null) {
850+
ctrl.disabled = false;
851+
if (ctrl.label !== undefined) {
852+
ctrl.label.classList.remove('noVNC_disabled');
853+
}
831854
}
832855
},
833856

@@ -1771,6 +1794,4 @@ const UI = {
17711794
*/
17721795
};
17731796

1774-
UI.start();
1775-
17761797
export default UI;

vnc.html

+21-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,27 @@
4646
<link rel="preload" as="image" href="app/images/warning.svg">
4747

4848
<script type="module" crossorigin="anonymous" src="app/error-handler.js"></script>
49-
<script type="module" crossorigin="anonymous" src="app/ui.js"></script>
49+
50+
<script type="module">
51+
import UI from "./app/ui.js";
52+
53+
let defaults = {};
54+
let mandatory = {};
55+
56+
// Override any defaults you need here:
57+
//
58+
// defaults['host'] = 'vnc.example.com';
59+
60+
// Or force a specific setting, preventing the user from
61+
// changing it:
62+
//
63+
// mandatory['view_only'] = true;
64+
65+
// See docs/EMBEDDING.md for a list of possible settings.
66+
67+
UI.start({ settings: { defaults: defaults,
68+
mandatory: mandatory } });
69+
</script>
5070
</head>
5171

5272
<body>

0 commit comments

Comments
 (0)