aboutsummaryrefslogtreecommitdiff
path: root/polldance.js
blob: 906aed6f8d36ac060583c662670b5e500521a332 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// don't break if there's no console
if(typeof(console) === "undefined")
{
	console = { log: function() {} };
}

var FO = FO || {};

var fo_jsonp_callback_id = 0;
var fo_jsonp_callbacks = {};

function fo_jsonp_get_callback(cb_id)
{
	if(fo_jsonp_callbacks[cb_id])
	{
		return fo_jsonp_callbacks[cb_id];
	}
	else
	{
		console.log("no callback with id " + cb_id);
		return function(result) {};
	}
}

// FO.Request has onFinished(int code, object result) and onError(int reason) callback members
// error reasons: 0=http, 1=timeout
// note: failed json parse on http body results in empty object {}, not error

FO.Request = function()
{
	if(!(this instanceof FO.Request))
		throw new Error("Constructor called as a function");
}

FO.Request.prototype.start = function(method, url, headers, body)
{
	this._cors = false;
	if("withCredentials" in new XMLHttpRequest())
		this._cors = true;

	var self = this;
	this._timer = setTimeout(function() { self._timeout(); }, 60000);

	if(this._cors)
	{
		this._xhr = new XMLHttpRequest();

		this._xhr.onreadystatechange = function() { self._xhr_readystatechange(); };
		this._xhr.open(method, url, true);

		for(var key in headers)
		{
			if(!headers.hasOwnProperty(key))
				continue;

			this._xhr.setRequestHeader(key, headers[key]);
		}

		this._xhr.send(body);
	}
	else
	{
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement("script");

		script.type = "text/javascript";

		this._cb_id = fo_jsonp_callback_id.toString();
		++fo_jsonp_callback_id;

		this._script_id = "fo-jsonp-script-" + this._cb_id;
		script.id = this._script_id;

		fo_jsonp_callbacks[this._cb_id] = function(result) { self._jsonp_callback(result); };

		var param_list = new Array();

		param_list.push("callback=" + encodeURIComponent("fo_jsonp_get_callback(\"" + this._cb_id + "\")"));
		if(method != "GET")
			param_list.push("method=" + encodeURIComponent(method));
		if(headers)
			param_list.push("headers=" + encodeURIComponent(JSON.stringify(headers)));
		if(body)
			param_list.push("body=" + encodeURIComponent(body));

		var params = param_list.join("&");

		var src;
		if(url.indexOf("?") != -1)
			src = url + "&" + params;
		else
			src = url + "?" + params;

		script.src = src;
		console.log("FO.Request json-p " + this._cb_id + " " + src);
		head.appendChild(script);
	}
}

FO.Request.prototype.abort = function()
{
	clearTimeout(this._timer);
	this._timer = null;

	this._cancelreq();
}

FO.Request.prototype._cancelreq = function()
{
	if(this._cors)
	{
		this._xhr.onreadystatechange = function() {}
		this._xhr.abort();
		this._xhr = null;
	}
	else
	{
		console.log("FO.Request json-p " + this._cb_id + " timeout");

		delete fo_jsonp_callbacks[this._cb_id];
		var script = document.getElementById(this._script_id);
		script.parentNode.removeChild(script);
		this._cb_id = null;
		this._script_id = null;
	}
}

FO.Request.prototype._xhr_readystatechange = function()
{
	if(this._xhr.readyState === 4)
	{
		clearTimeout(this._timer);
		this._timer = null;

		var status = this._xhr.status;
		var responseText = this._xhr.responseText;
		this._xhr = null;

		if(status)
		{
			var result;

			try
			{
				result = JSON.parse(responseText);
			}
			catch(e)
			{
				result = {}
			}

			this._finished(status, result);
		}
		else
			this._error(0);
	}
}

FO.Request.prototype._jsonp_callback = function(result)
{
	console.log("FO.Request json-p " + this._cb_id + " finished");

	clearTimeout(this._timer);
	this._timer = null;

	delete fo_jsonp_callbacks[this._cb_id];
	var script = document.getElementById(this._script_id);
	script.parentNode.removeChild(script);
	this._cb_id = null;
	this._script_id = null;

	this._finished(result.status, result.value);
}

FO.Request.prototype._timeout = function()
{
	this._timer = null;
	this._cancelreq();
	this._error(1);
}

FO.Request.prototype._finished = function(code, result)
{
	if(this.onFinished)
		this.onFinished(code, result);
}

FO.Request.prototype._error = function(reason)
{
	if(this.onError)
		this.onError(reason);
}