diff options
Diffstat (limited to 'itjs/boot.js')
-rw-r--r-- | itjs/boot.js | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/itjs/boot.js b/itjs/boot.js new file mode 100644 index 0000000..8ca539f --- /dev/null +++ b/itjs/boot.js @@ -0,0 +1,136 @@ +// $Id$ + +var it_boot_status = ""; +var it_panictimer = window.setTimeout("it_panic(it_boot_status)", 31337); +var it_catcherrstart = new Date().getTime(); + +function it_catcherr(msg, url, line) +{ + var stacktrace = ""; + + for (var c = it_catcherr.caller; c != null; c = c.caller) + { + var funcname = c.toString().match(/function (\w*)/)[1]; + stacktrace += (funcname ? funcname : "anon") + "/"; + + if (c.caller == c) // Break simple recursion + { + stacktrace += "*"; + break; + } + } + + it_boot_report(msg, url, line, stacktrace); + + return !window.env || !!window.env.is_live_server; // No env or live server -> suppress error +} + +window.onerror = it_catcherr; + +function it_boot_addparam(url, param) +{ + return url + (url.match(/\?/) ? "&" : "?") + param; +} + +function it_panic(msg) +{ + window.setTimeout("document.location.href = it_boot_addparam(document.location.href, 'static=" + msg + "')", 500); + return it_catcherr('panic ' + msg, '-', -1); +} + + +function it_boot_report(msg, file, line, error) +{ + window.clearTimeout(window.it_domtimer); + window.clearTimeout(window.it_panictimer); + + new Image().src = "/itjs/error.gif/" + escape(msg) + "|" + escape(file) + "|" + line + "|" + (new Date().getTime() - it_catcherrstart) + "|" + escape(error); +} + +function it_boot(file, isretry) +{ + window.it_domtimer = null; + var doc = document; + var dom = doc && (dom = document.getElementById('it_boot_dom')); // HTML has been rendered + var view = dom && doc.defaultView; // We can check if stylesheet is active + var style = view && view.getComputedStyle && view.getComputedStyle(dom, ''); + var css = style && style.getPropertyValue && (style.getPropertyValue("visibility") == "hidden"); // CSS active (inline style on tag) + var stylesheet = style && style.getPropertyValue && (style.getPropertyValue("display") == "none"); // External stylesheet loaded + + if (!(doc || !(it_boot_status = "doc")) || !(dom || !(it_boot_status = "dom")) || (style && (style.length > 0) && !(stylesheet || !(it_boot_status = "stylesheet")))) + { + window.it_domtimer = window.setTimeout("it_boot('" + file + "')" , 42); + + if (style && !css) + it_panic("css"); + + return; + } + + try + { + var loader = new XMLHttpRequest(); + } + catch (e) + { + var classnames = [ 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ]; + + for (var i in classnames) + try { loader = new ActiveXObject(classnames[i]); break; } catch (e) {} + } + + if (loader) + { + loader.open("GET", it_boot_addparam(file, "boot=1" + (isretry ? "&retry=1" : ""))); + loader.onreadystatechange = function() + { + var error = ""; + + if (loader.readyState == 4) + { + if (loader.status < 400) // Opera gives back 304 if from cache + { + try + { + var data = eval("(" + loader.responseText + ")"); + + if (data.code.length == data.len) + { + var code = "try {" + data.code + ";window.it_boot_init()} catch (e) { it_boot_report('Load error', '-', -1, e); }"; // Wrapped in try/catch as Konqueror does not support window.onerror + isretry = true; // No further retry after this point + + if (window.execScript) + window.execScript(code, "javascript"); // IE work-around to get script executed in global scope + else + window.setTimeout(code, 0); // Standard compliant version + } + else + error = "Length mismatch " + data.code.length + " != " + data.len; + } + catch (e) + { + for (i in e) + error += i + "=" + e[i] + ";"; + } + } + else + error = loader.statusText; + + if (error) + { + if (isretry) + it_boot_report('Load error on retry', file, -1, error); + else + it_boot(file, true); + } + } + } + loader.send(null); + } + else + { + var tag = document.createElement("script"); + tag.src = it_boot_addparam(file, 'init=1'); + dom.appendChild(tag); + } +} |