summaryrefslogtreecommitdiff
path: root/itjs/boot.js
blob: 7f24d9f539874c29bebd34801637074b692aa434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// $Id$

var it_boot_status = "boot";
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;
		}
	}

	if (typeof it_boot.sequence != 'undefined')
		stacktrace += "it_boot=" + it_boot.sequence + ";";
	if (typeof window.it_loader != 'undefined')
		stacktrace += "it_loader=" + it_loader.sequence + ";";

	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)
{
	if (!document.location.href.match(/[?&]static=/))	// Avoid loop
		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);

	var more = "";
	if (error && typeof error == 'object')
		for (var k in error)
			more += k + "=" + error[k] + ";";
	else
		more = error;

	new Image().src = "/itjs/error.gif/" + escape(msg) + "|" + escape(file) + "|" + line + "|" + (new Date().getTime() - it_catcherrstart) + "|" + escape(more);
}

function it_boot_checkcss(style, key, value)
{
	return navigator.userAgent.match(/konqueror/i) || (style && style.getPropertyValue && (style.getPropertyValue(key) == value));
}

function it_boot_init()
{
	window.it_domtimer = null;
	var konqueror = navigator.userAgent.match(/konqueror/i);
	var doc = document;
	var dom = doc && (dom = doc.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 = window.it_boot_checkcss(style, "visibility", "hidden"); // CSS active (inline style on tag)
	var stylesheet = window.it_boot_checkcss(style, "display", "none");	// External stylesheet loaded

	if (!(doc || !(it_boot_status = "doc")) || !(dom || !(it_boot_status = "dom")) || (style && !(stylesheet || !(it_boot_status = "stylesheet"))))
	{
		window.it_domtimer = window.setTimeout("it_boot_init()" , 42);

		if (style && !css)
			it_panic("css");

		return;
	}

	window.clearTimeout(window.it_panictimer); 
	window.it_boot_start();
}

function it_boot(file, isretry)
{
	it_boot.sequence = isretry ? "r" : "b";

	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)
	{
		it_boot.sequence += "l";
		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
				{
					if (loader.responseText.length && loader.responseText.lastIndexOf('it_boot_init()') > 0)
					{
						it_boot.sequence += "e";
						var code = "try {" + loader.responseText + "} catch (e) { it_boot_report('Script exec error', '-', -1, e); }";	// Wrapped in try/catch as Konqueror does not support window.onerror
						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

						window.setTimeout('it_boot.sequence = "";', 50);
					}
					else
						error = "Incomplete script load @ status " + loader.status;
				}
				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 doc = document;
		var dom = doc && (dom = doc.getElementById('it_boot_dom'));	// HTML has been rendered
		it_boot.sequence += "n";

		if (window.opera || (document.all && navigator.platform.indexOf("Mac") >= 0))
			document.write('<sc'+'ript type="text/javascript" src="'+it_boot_addparam(file, 'boot=1')+'"><\/sc'+'ript>');
		else if (dom)
		{
			var tag = doc.createElement("script");
			tag.src = it_boot_addparam(file, 'boot=1');
			dom.appendChild(tag);
		}
		else
			window.it_domtimer = window.setTimeout("it_boot('" + file + "')" , 42);
	}
}