diff options
Diffstat (limited to 'itjs/loader.js')
-rw-r--r-- | itjs/loader.js | 177 |
1 files changed, 21 insertions, 156 deletions
diff --git a/itjs/loader.js b/itjs/loader.js index 90edf1e..9fdb9a8 100644 --- a/itjs/loader.js +++ b/itjs/loader.js @@ -1,16 +1,15 @@ /** * 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.req = null; + this.http = null; this.handler = handler; - this.instance = it_loader.instances++; - this.callid = 0; - this.scrpt = []; + this.callback = { object:this, method:'dataReady', errorhandler:'onerror' }; this.clear(); } @@ -25,8 +24,6 @@ clear: function() this.entry = new Array(); this.start = this.end = 0; this.attr = { num: 0, loadtime: 0 }; - this.method = "GET"; - this.post_data = null; if (this.handler.clear) this.handler.clear(); @@ -65,80 +62,9 @@ load: function(baseurl, pos, num, query_volatile, retry) if (num > 0) { - this.req = null; - var samehost = (baseurl.indexOf('http://') < 0 || baseurl.indexOf(window.location.hostname) > 0); - - if (retry) - baseurl += (baseurl.match(/\?/) ? "&" : "?") + "retry=" + retry; - - if (samehost) // use XMLHTTP request only if on same host - { - try - { - this.req = new XMLHttpRequest(); - } - catch (e) - { - var classnames = [ 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ]; - - for (var i in classnames) - { - try - { - this.req = new ActiveXObject(classnames[i]); - break; - } - catch (e) { } - } - } - - try - { - this.req.open(this.method, baseurl + (baseurl.match(/\?/) ? "&" : "?") + "pos=" + pos + "&num=" + num + (query_volatile ? query_volatile : "")); - var me = this; - this.req.onreadystatechange = function() { me.readyStateChanged(); } - var workingxmlhttp = this.req.onreadystatechange; - - if (!workingxmlhttp) /* Old Konqueror */ - this.req = null; - } - catch (e) { } - } - - this.starttime = new Date().getTime(); this.retry = retry; - - if (this.req) - { - if (this.method == "POST") - this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - - this.req.send(this.post_data); - } - else - { - var req_url = baseurl + (baseurl.match(/\?/) ? "&" : "?") + "pos=" + pos + "&num=" + num + (query_volatile ? query_volatile : "") + (this.post_data ? '&' + this.post_data : "") + "&itjs_call=it_loader.__inst" + this.instance + "&itjs_callid=" + ++this.callid; - - if (samehost || (window.opera && !window.XMLHttpRequest)) // Opera 7 only works with iframes - { - var scrpt = document.createElement("iframe"); - scrpt.style.width = scrpt.style.height = 1; - req_url += "&itjs_iframe=1"; - } - else - var scrpt = document.createElement("script"); - - it_loader['__inst' + this.instance] = this; - this.req = { starttime: this.starttime, retry: retry }; - try - { - this.scrpt[this.callid] = scrpt; - if (!document.all) scrpt.src = req_url; - document.body.appendChild(scrpt); - if (document.all) scrpt.src = req_url; - } - catch (e) { return false; } - } + 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); @@ -146,52 +72,12 @@ load: function(baseurl, pos, num, query_volatile, retry) return true; }, +/* deprecated: use it_http::post() instead */ post: function(baseurl, data) { this.clear(); - this.method = "POST"; - this.baseurl = baseurl; - this.start = this.end = 10; - this.post_data = ""; - - if (typeof data == 'object') - { - for (var k in data) - this.post_data += (this.post_data ? "&" : "") + k + "=" + it_url_encode(data[k]); - } - else - this.post_data = data; - - this.load(baseurl, 0, 1, ""); -}, - -readyStateChanged: function() -{ - var req = this.req; // Avoid race conditions - it_loader.sequence += "r"; - - if (req && (req.readyState == 4)) - { - var data = null; - - try - { - if (req.responseText != "") - data = eval("(" + req.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, req.responseText); - } - - if (data) - this.dataReady(data, this.callid); - } + this.http = it_http.get_instance(); + this.http.post(baseurl, data, this.callback); }, retryload: function(p) @@ -199,17 +85,11 @@ retryload: function(p) this.load(p.baseurl, p.pos, p.num, p.query_volatile, p.retry); }, -dataReady: function(data, callid) +dataReady: function(data) { - var fixkonqueror33gcbug = this.req; - var loadtime = new Date().getTime() - this.starttime; - it_loader.sequence += "e"; - - this.req = null; - - if ((typeof data == "object") && (this.callid == callid)) + if ((typeof data == "object")) { - this.attr = { loadtime: loadtime }; + this.attr = {}; for (var key in data) { @@ -235,42 +115,27 @@ dataReady: function(data, callid) this.load(this.baseurl, this.end, this.pos + this.num - this.end); it_loader.sequence += "h"; + this.http = null; } - - this.unlink(callid); }, -stop: function() +onerror: function(response) { - try - { - this.req.abort(); - } - catch (e) { } + var retry = this.retry + 1; - this.unlink(this.callid); + 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); }, -unlink: function(callid) +stop: function() { - if (it_loader['__inst' + this.instance] && callid == this.callid) - it_loader['__inst' + this.instance] = null; - - if (this.req) - this.req = null; - - if (this.scrpt[callid]) - { - if (!(document.all && String(navigator.userAgent).indexOf('MSIE 5.0') > 0)) - document.body.removeChild(this.scrpt[callid]); - this.scrpt[callid] = null; - } - - it_loader.sequence = ""; + if (this.http) + this.http.stop(); } /* NO COMMA */ } // static properties -it_loader.instances = 0; it_loader.sequence = ""; |