scripts' initialization control.

- Initalize scripts in register order.
 - Fire a NoVnc.onload event when all scripts are loaded and initialized.
 - noVNC starts its service when catching a NoVnc.onload event.
This commit is contained in:
Hirokazu Takahashi 2012-10-15 19:04:38 +09:00
parent 8dfd916946
commit d7dbc42372
5 changed files with 67 additions and 31 deletions

View File

@ -1 +1 @@
window.onload = UI.load;
NoVnc.onload = UI.load;

View File

@ -9,6 +9,12 @@
/*jslint evil: true */
/*global window, document, INCLUDE_URI */
var NoVnc = {};
NoVnc.onload = null;
NoVnc.init_scripts = [];
NoVnc.loading = 0;
/*
* Load supporting scripts
*/
@ -21,11 +27,48 @@ function get_INCLUDE_URI() {
*/
function load_scripts(base, files) {
var head = document.getElementsByTagName('head')[0];
function onloadhook () {
if (this.initState) //Already initialized
return;
this.initState = true;
NoVnc.loading--;
initscripts();
if (NoVnc.loading === 0 && !!NoVnc.onload) {
NoVnc.onload();
NoVnc.onload = null;
}
}
function initscripts() {
// Call the initialization routines in register order when all
// the scripts have been loaded.
// Notice: These routines may also call load_scripts to start
// loading other scripts.
while (NoVnc.loading === 0 && NoVnc.init_scripts.length > 0) {
var script = NoVnc.init_scripts[0];
NoVnc.init_scripts.splice(0, 1);
// It is assumed that ABC.js should have _init_ABC() to
// initialize itself.
var f = script.src.split("/");
f = "_init_" + f[f.length -1].split(".")[0].replace(/[\-\+]/,"_");
eval("if (typeof " + f + " !== 'undefined') " + f + "()");
}
}
for (var i=0; i<files.length; i++) {
var script = document.createElement('script');
script.type = 'text/javascript';
NoVnc.loading++;
script.onload = onloadhook;
script.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded')
this.onload();
}
script.initState = false;
script.src = base + files[i];
head.appendChild(script);
NoVnc.init_scripts = NoVnc.init_scripts.concat([script]);
}
}

View File

@ -3,7 +3,7 @@
// Reference: http://dev.w3.org/html5/websockets/
// Reference: http://tools.ietf.org/html/rfc6455
(function() {
function _init_web_socket() {
if (window.WEB_SOCKET_FORCE_FLASH) {
// Keeps going.
@ -388,4 +388,4 @@
});
}
})();
}

View File

@ -25,34 +25,27 @@
// To enable WebSocket emulator debug:
//window.WEB_SOCKET_DEBUG=1;
if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
Websock_native = true;
} else if (window.MozWebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
Websock_native = true;
window.WebSocket = window.MozWebSocket;
} else {
/* no builtin WebSocket so load web_socket.js */
function _init_websock() {
if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
Websock_native = true;
} else if (window.MozWebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
Websock_native = true;
window.WebSocket = window.MozWebSocket;
} else {
/* no builtin WebSocket so load web_socket.js */
Websock_native = false;
(function () {
function get_INCLUDE_URI() {
return (typeof INCLUDE_URI !== "undefined") ?
INCLUDE_URI : "include/";
}
var start = "<script src='" + get_INCLUDE_URI(),
end = "'><\/script>", extra = "";
window.WEB_SOCKET_SWF_LOCATION = get_INCLUDE_URI() +
"web-socket-js/WebSocketMain.swf";
if (Util.Engine.trident) {
Util.Debug("Forcing uncached load of WebSocketMain.swf");
window.WEB_SOCKET_SWF_LOCATION += "?" + Math.random();
}
extra += start + "web-socket-js/swfobject.js" + end;
extra += start + "web-socket-js/web_socket.js" + end;
document.write(extra);
}());
Websock_native = false;
(function () {
window.WEB_SOCKET_SWF_LOCATION = get_INCLUDE_URI() +
"web-socket-js/WebSocketMain.swf";
if (Util.Engine.trident) {
Util.Debug("Forcing uncached load of WebSocketMain.swf");
window.WEB_SOCKET_SWF_LOCATION += "?" + Math.random();
}
load_scripts(get_INCLUDE_URI() + "web-socket-js/",
["swfobject.js", "web_socket.js"]);
}());
}
}

View File

@ -84,7 +84,7 @@
}
}
window.onload = function () {
NoVnc.onload = function () {
var host, port, password, path, token;
$D('sendCtrlAltDelButton').style.display = "inline";