diff options
Diffstat (limited to 'itjs/loader.js')
-rw-r--r-- | itjs/loader.js | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/itjs/loader.js b/itjs/loader.js new file mode 100644 index 0000000..19f5f9c --- /dev/null +++ b/itjs/loader.js @@ -0,0 +1,207 @@ +/** + * Create loader to request data from server + * + * @param handler Object providing clear()/render() function when data arrives + */ +function it_loader(handler) +{ + /* Clear cache etc. if completely new data */ + this.loader = null; + this.handler = handler; + this.name = "it_loader"; + this.callid = 0; + 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; + + if (this.loader) + this.stop(); + + while ((num > 0) && this.entry[pos]) + { + pos++; + num--; + } + + if (this.attr.eof) + num = Math.min(num, this.end - pos); + + if (num > 0) + { + if (retry) + baseurl += "&retry=" + retry; + + try + { + this.loader = new XMLHttpRequest(); + } + catch (e) + { + var classnames = [ 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ]; + + for (var i in classnames) + { + try + { + this.loader = new ActiveXObject(classnames[i]); + break; + } + catch (e) {} + } + } + + try + { + this.loader.open("GET", baseurl + "&pos=" + pos + "&num=" + num + (query_volatile ? query_volatile : "")); + var me = this; + this.loader.onreadystatechange = function() { me.readyStateChanged(); } + var workingxmlhttp = this.loader.onreadystatechange; + + if (!workingxmlhttp) /* Old Konqueror */ + this.loader = null; + } + catch (e) { } + + if (this.loader) + { + this.starttime = new Date().getTime(); + this.retry = retry; + this.loader.send(null); + } + else + { + + if (!this.iframe) + { + this.iframe = document.createElement("iframe"); + this.iframe.frameBorder = 0; + this.iframe.style.width = this.iframe.style.height = 1; + document.body.appendChild(this.iframe); + } + + this.loader = { starttime: new Date().getTime(), retry: retry }; + var loaderinstance = this.name; + window[loaderinstance] = this; + this.iframe.src = baseurl + "&pos=" + pos + "&num=" + num + '&itjs_call=parent.' + loaderinstance + '.dataReady&itjs_callid=' + ++this.callid; + } + } + else + this.handler.render(this); +}, + +readyStateChanged: function() +{ + var loader = this.loader; // Avoid race conditions + + if (loader && (loader.readyState == 4)) + { + var data = null; + + try + { + if (loader.responseText != "") + data = eval("("+ loader.responseText + ")"); + } + catch (e) + { + 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(e, loader.responseText); + } + + if (data) + this.dataReady(data, this.callid); + } +}, + +retryload: function(p) +{ + this.load(p.baseurl, p.pos, p.num, p.query_volatile, p.retry); +}, + +dataReady: function(data, callid) +{ + var fixkonqueror33gcbug = this.loader; + var loadtime = new Date().getTime() - this.starttime; + + this.loader = null; + + if ((typeof data == "object") && (this.callid == callid)) + { + this.attr = { loadtime: loadtime }; + + 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); + } +}, + +stop: function() +{ + try + { + this.loader.abort(); + } + catch (e) { } + + this.loader = null; +}/* NO COMMA */ + +} |