From d7dbc4237268ad0a4aadd210e36b8cd288a5c383 Mon Sep 17 00:00:00 2001 From: Hirokazu Takahashi Date: Mon, 15 Oct 2012 19:04:38 +0900 Subject: [PATCH] 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. --- include/start.js | 2 +- include/vnc.js | 43 ++++++++++++++++++++++++++ include/web-socket-js/web_socket.js | 4 +-- include/websock.js | 47 ++++++++++++----------------- vnc_auto.html | 2 +- 5 files changed, 67 insertions(+), 31 deletions(-) diff --git a/include/start.js b/include/start.js index b5d90b4c..e3d99a1d 100644 --- a/include/start.js +++ b/include/start.js @@ -1 +1 @@ -window.onload = UI.load; +NoVnc.onload = UI.load; diff --git a/include/vnc.js b/include/vnc.js index 435fdc47..e840d361 100644 --- a/include/vnc.js +++ b/include/vnc.js @@ -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