summaryrefslogtreecommitdiff
path: root/devel-utf8/itjs/loader.js
blob: 9fdb9a8ae8b280806cc21d30a86d85b6566403ba (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
/**
 * Create loader to request data from server
 * Uses it_http class for communication
 *
 * @param handler Object providing clear()/render() function when data arrives
 */
function it_loader(handler)
{
	/* Clear cache etc. if completely new data */
	this.http = null;
	this.handler = handler;
	this.callback = { object:this, method:'dataReady', errorhandler:'onerror' };
	this.clear();
}

/* Methods */
it_loader.prototype =
{

/* Clear cache and initialize handler */
clear: function()
{
	/* Clear cache etc. if completely new data */
	this.entry = new Array();
	this.start = this.end = 0;
	this.attr = { num: 0, loadtime: 0 };

	if (this.handler.clear)
		this.handler.clear();
},

load: function(baseurl, pos, num, query_volatile, retry)
{
	/* Convert to int */
	pos -= 0;
	num -= 0;

	if (isNaN(retry))
		retry = 0;

	if (baseurl != this.baseurl)
	{
		this.clear();
		this.baseurl = baseurl;
		this.start = this.end = pos;
	}

	this.pos = pos;
	this.num = num;
	this.query_volatile = query_volatile;

	this.stop();

	while ((num > 0) && (typeof this.entry[pos] != "undefined"))
	{
		pos++;
		num--;
	}

	if (this.attr.eof)
		num = Math.min(num, this.end - pos);

	if (num > 0)
	{
		this.retry = retry;
		this.http = it_http.get_instance();
		this.http.get(baseurl + (baseurl.match(/\?/) ? "&" : "?") + "pos=" + pos + "&num=" + num + (retry ? "&retry=" + retry : "") + (query_volatile ? query_volatile : ""), this.callback);
	}
	else
		this.handler.render(this);

	return true;
},

/* deprecated: use it_http::post() instead */
post: function(baseurl, data)
{
	this.clear();
	this.http = it_http.get_instance();
	this.http.post(baseurl, data, this.callback);
},

retryload: function(p)
{
	this.load(p.baseurl, p.pos, p.num, p.query_volatile, p.retry);
},

dataReady: function(data)
{
	if ((typeof data == "object"))
	{
		this.attr = {};

		for (var key in data)
		{
			var value = data[key];
			var id = key - 0;

			if (!isNaN(id))
			{
				this.start = Math.min(this.start, id);
				this.end = Math.max(this.end, id + 1);
				this.entry[id] = data[key];
			}
			else
				this.attr[key] = data[key];
		}

		if (this.attr.eof)
			this.attr.num = this.end;	/* Fix bogus # of result value */

		this.handler.render(this);

		if (!this.attr.eof && (this.end < this.pos + this.num))
			this.load(this.baseurl, this.end, this.pos + this.num - this.end);

		it_loader.sequence += "h";
		this.http = null;
	}
},

onerror: function(response)
{
	var retry = this.retry + 1;

	if (retry < 10)
		it_timer({ object: this, method: "retryload", timeout: Math.pow(5, Math.min(retry, 5)), baseurl: this.baseurl, pos: this.pos, num: this.num, query_volatile: this.query_volatile, retry: retry });
	else
		ED(response);
},

stop: function()
{
  if (this.http)
    this.http.stop();
} /* NO COMMA */

}

// static properties
it_loader.sequence = "";