@@ -20,8 +20,12 @@ import * as WebUtil from "./webutil.js";
2020
2121const PAGE_TITLE = "noVNC" ;
2222
23+ const LINGUAS = [ "cs" , "de" , "el" , "es" , "fr" , "it" , "ja" , "ko" , "nl" , "pl" , "pt_BR" , "ru" , "sv" , "tr" , "zh_CN" , "zh_TW" ] ;
24+
2325const UI = {
2426
27+ customSettings : { } ,
28+
2529 connected : false ,
2630 desktopName : "" ,
2731
@@ -42,20 +46,31 @@ const UI = {
4246 reconnectCallback : null ,
4347 reconnectPassword : null ,
4448
45- prime ( ) {
46- return WebUtil . initSettings ( ) . then ( ( ) => {
47- if ( document . readyState === "interactive" || document . readyState === "complete" ) {
48- return UI . start ( ) ;
49- }
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+ }
5057
51- return new Promise ( ( resolve , reject ) => {
52- document . addEventListener ( 'DOMContentLoaded' , ( ) => UI . start ( ) . then ( resolve ) . catch ( reject ) ) ;
53- } ) ;
54- } ) ;
55- } ,
58+ // Set up translations
59+ try {
60+ await l10n . setup ( LINGUAS , "app/locale/" ) ;
61+ } catch ( err ) {
62+ Log . Error ( "Failed to load translations: " + err ) ;
63+ }
5664
57- // Render default UI and initialize settings menu
58- start ( ) {
65+ // Initialize setting storage
66+ await WebUtil . initSettings ( ) ;
67+
68+ // Wait for the page to load
69+ if ( document . readyState !== "interactive" && document . readyState !== "complete" ) {
70+ await new Promise ( ( resolve , reject ) => {
71+ document . addEventListener ( 'DOMContentLoaded' , resolve ) ;
72+ } ) ;
73+ }
5974
6075 UI . initSettings ( ) ;
6176
@@ -70,22 +85,20 @@ const UI = {
7085 }
7186
7287 // Try to fetch version number
73- fetch ( './package.json' )
74- . then ( ( response ) => {
75- if ( ! response . ok ) {
76- throw Error ( "" + response . status + " " + response . statusText ) ;
77- }
78- return response . json ( ) ;
79- } )
80- . then ( ( packageInfo ) => {
81- Array . from ( document . getElementsByClassName ( 'noVNC_version' ) ) . forEach ( el => el . innerText = packageInfo . version ) ;
82- } )
83- . catch ( ( err ) => {
84- Log . Error ( "Couldn't fetch package.json: " + err ) ;
85- Array . from ( document . getElementsByClassName ( 'noVNC_version_wrapper' ) )
86- . concat ( Array . from ( document . getElementsByClassName ( 'noVNC_version_separator' ) ) )
87- . forEach ( el => el . style . display = 'none' ) ;
88- } ) ;
88+ try {
89+ let response = await fetch ( './package.json' ) ;
90+ if ( ! response . ok ) {
91+ throw Error ( "" + response . status + " " + response . statusText ) ;
92+ }
93+
94+ let packageInfo = await response . json ( ) ;
95+ Array . from ( document . getElementsByClassName ( 'noVNC_version' ) ) . forEach ( el => el . innerText = packageInfo . version ) ;
96+ } catch ( err ) {
97+ Log . Error ( "Couldn't fetch package.json: " + err ) ;
98+ Array . from ( document . getElementsByClassName ( 'noVNC_version_wrapper' ) )
99+ . concat ( Array . from ( document . getElementsByClassName ( 'noVNC_version_separator' ) ) )
100+ . forEach ( el => el . style . display = 'none' ) ;
101+ }
89102
90103 // Adapt the interface for touch screen devices
91104 if ( isTouchDevice ) {
@@ -120,7 +133,7 @@ const UI = {
120133
121134 document . documentElement . classList . remove ( "noVNC_loading" ) ;
122135
123- let autoconnect = WebUtil . getConfigVar ( 'autoconnect' , false ) ;
136+ let autoconnect = UI . getSetting ( 'autoconnect' ) ;
124137 if ( autoconnect === 'true' || autoconnect == '1' ) {
125138 autoconnect = true ;
126139 UI . connect ( ) ;
@@ -129,8 +142,6 @@ const UI = {
129142 // Show the connect panel on first load unless autoconnecting
130143 UI . openConnectPanel ( ) ;
131144 }
132-
133- return Promise . resolve ( UI . rfb ) ;
134145 } ,
135146
136147 initFullscreen ( ) {
@@ -158,23 +169,26 @@ const UI = {
158169 UI . initSetting ( 'logging' , 'warn' ) ;
159170 UI . updateLogging ( ) ;
160171
172+ UI . setupSettingLabels ( ) ;
173+
161174 /* Populate the controls if defaults are provided in the URL */
162175 UI . initSetting ( 'host' , '' ) ;
163176 UI . initSetting ( 'port' , 0 ) ;
164177 UI . initSetting ( 'encrypt' , ( window . location . protocol === "https:" ) ) ;
178+ UI . initSetting ( 'password' ) ;
179+ UI . initSetting ( 'autoconnect' , false ) ;
165180 UI . initSetting ( 'view_clip' , false ) ;
166181 UI . initSetting ( 'resize' , 'off' ) ;
167182 UI . initSetting ( 'quality' , 6 ) ;
168183 UI . initSetting ( 'compression' , 2 ) ;
169184 UI . initSetting ( 'shared' , true ) ;
185+ UI . initSetting ( 'bell' , 'on' ) ;
170186 UI . initSetting ( 'view_only' , false ) ;
171187 UI . initSetting ( 'show_dot' , false ) ;
172188 UI . initSetting ( 'path' , 'websockify' ) ;
173189 UI . initSetting ( 'repeaterID' , '' ) ;
174190 UI . initSetting ( 'reconnect' , false ) ;
175191 UI . initSetting ( 'reconnect_delay' , 5000 ) ;
176-
177- UI . setupSettingLabels ( ) ;
178192 } ,
179193 // Adds a link to the label elements on the corresponding input elements
180194 setupSettingLabels ( ) {
@@ -736,13 +750,22 @@ const UI = {
736750
737751 // Initial page load read/initialization of settings
738752 initSetting ( name , defVal ) {
753+ // Has the user overridden the default value?
754+ if ( name in UI . customSettings . defaults ) {
755+ defVal = UI . customSettings . defaults [ name ] ;
756+ }
739757 // Check Query string followed by cookie
740758 let val = WebUtil . getConfigVar ( name ) ;
741759 if ( val === null ) {
742760 val = WebUtil . readSetting ( name , defVal ) ;
743761 }
744762 WebUtil . setSetting ( name , val ) ;
745763 UI . updateSetting ( name ) ;
764+ // Has the user forced a value?
765+ if ( name in UI . customSettings . mandatory ) {
766+ val = UI . customSettings . mandatory [ name ] ;
767+ UI . forceSetting ( name , val ) ;
768+ }
746769 return val ;
747770 } ,
748771
@@ -761,9 +784,12 @@ const UI = {
761784 let value = UI . getSetting ( name ) ;
762785
763786 const ctrl = document . getElementById ( 'noVNC_setting_' + name ) ;
787+ if ( ctrl === null ) {
788+ return ;
789+ }
790+
764791 if ( ctrl . type === 'checkbox' ) {
765792 ctrl . checked = value ;
766-
767793 } else if ( typeof ctrl . options !== 'undefined' ) {
768794 for ( let i = 0 ; i < ctrl . options . length ; i += 1 ) {
769795 if ( ctrl . options [ i ] . value === value ) {
@@ -796,7 +822,8 @@ const UI = {
796822 getSetting ( name ) {
797823 const ctrl = document . getElementById ( 'noVNC_setting_' + name ) ;
798824 let val = WebUtil . readSetting ( name ) ;
799- if ( typeof val !== 'undefined' && val !== null && ctrl . type === 'checkbox' ) {
825+ if ( typeof val !== 'undefined' && val !== null &&
826+ ctrl !== null && ctrl . type === 'checkbox' ) {
800827 if ( val . toString ( ) . toLowerCase ( ) in { '0' : 1 , 'no' : 1 , 'false' : 1 } ) {
801828 val = false ;
802829 } else {
@@ -811,14 +838,22 @@ const UI = {
811838 // disable the labels that belong to disabled input elements.
812839 disableSetting ( name ) {
813840 const ctrl = document . getElementById ( 'noVNC_setting_' + name ) ;
814- ctrl . disabled = true ;
815- ctrl . label . classList . add ( 'noVNC_disabled' ) ;
841+ if ( ctrl !== null ) {
842+ ctrl . disabled = true ;
843+ if ( ctrl . label !== undefined ) {
844+ ctrl . label . classList . add ( 'noVNC_disabled' ) ;
845+ }
846+ }
816847 } ,
817848
818849 enableSetting ( name ) {
819850 const ctrl = document . getElementById ( 'noVNC_setting_' + name ) ;
820- ctrl . disabled = false ;
821- ctrl . label . classList . remove ( 'noVNC_disabled' ) ;
851+ if ( ctrl !== null ) {
852+ ctrl . disabled = false ;
853+ if ( ctrl . label !== undefined ) {
854+ ctrl . label . classList . remove ( 'noVNC_disabled' ) ;
855+ }
856+ }
822857 } ,
823858
824859/* ------^-------
@@ -1000,7 +1035,7 @@ const UI = {
10001035 const path = UI . getSetting ( 'path' ) ;
10011036
10021037 if ( typeof password === 'undefined' ) {
1003- password = WebUtil . getConfigVar ( 'password' ) ;
1038+ password = UI . getSetting ( 'password' ) ;
10041039 UI . reconnectPassword = password ;
10051040 }
10061041
@@ -1730,7 +1765,7 @@ const UI = {
17301765 } ,
17311766
17321767 bell ( e ) {
1733- if ( WebUtil . getConfigVar ( 'bell' , 'on ') === 'on' ) {
1768+ if ( UI . getSetting ( 'bell' ) === 'on' ) {
17341769 const promise = document . getElementById ( 'noVNC_bell' ) . play ( ) ;
17351770 // The standards disagree on the return value here
17361771 if ( promise ) {
@@ -1761,10 +1796,4 @@ const UI = {
17611796 */
17621797} ;
17631798
1764- // Set up translations
1765- const LINGUAS = [ "cs" , "de" , "el" , "es" , "fr" , "it" , "ja" , "ko" , "nl" , "pl" , "pt_BR" , "ru" , "sv" , "tr" , "zh_CN" , "zh_TW" ] ;
1766- l10n . setup ( LINGUAS , "app/locale/" )
1767- . catch ( err => Log . Error ( "Failed to load translations: " + err ) )
1768- . then ( UI . prime ) ;
1769-
17701799export default UI ;
0 commit comments