diff options
author | Thomas BrĂ¼derli | 2008-11-26 10:07:16 +0000 |
---|---|---|
committer | Thomas BrĂ¼derli | 2008-11-26 10:07:16 +0000 |
commit | 643e9f34522a7c07d9f41c0f4f28f7169dad31c5 (patch) | |
tree | 0ed789c656280ea2da227c84c2afb4125f5833cd /itjs/http.js | |
parent | bb671acbe075d93844c6d8ffeef3bcd94019f805 (diff) | |
download | itools-643e9f34522a7c07d9f41c0f4f28f7169dad31c5.tar.gz itools-643e9f34522a7c07d9f41c0f4f28f7169dad31c5.tar.bz2 itools-643e9f34522a7c07d9f41c0f4f28f7169dad31c5.zip |
Add new it_http class to itjs includes
Diffstat (limited to 'itjs/http.js')
-rwxr-xr-x | itjs/http.js | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/itjs/http.js b/itjs/http.js new file mode 100755 index 0000000..8615e98 --- /dev/null +++ b/itjs/http.js @@ -0,0 +1,242 @@ +/** + * Create http loader to request data from server + * + * @param cb Callback function or object with keys 'object', 'method', ['errorhandler'] + */ +function it_http(cb) +{ + this.instance = it_http.instances++; + this.callback = cb ? cb : {}; + this.req = null; + this.scrpt = []; + this.callid = 0; + this.busy = false; + + // register global reference + it_http['__inst' + this.instance] = this; +} + +/* Methods */ +it_http.prototype = { + +/* send GET request and trigger callback */ +get: function(url, callback) +{ + if (typeof callback != 'undefined') + this.callback = callback; + this.send(url, 'GET'); +}, + +/* send POST request with data and trigger callback */ +post: function(url, data, callback) +{ + if (typeof callback != 'undefined') + this.callback = callback; + + var postdata = ''; + if (typeof data == 'object') + { + for (var k in data) + postdata += (postdata ? '&' : '') + it_url_encode(k) + "=" + it_url_encode(data[k]); + } + else + postdata = data; + + this.send(url, 'POST', postdata); +}, + +/* private method that finally sends the request */ +send: function(url, method, postdata) +{ + this.stop(); + + this.busy = true; + this.req = null; + var samehost = (url.indexOf('http://') < 0 || url.indexOf(window.location.hostname) > 0); + + 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=0; i < classnames.length; i++) + { + try + { + this.req = new ActiveXObject(classnames[i]); + break; + } + catch (e) { } + } + } + + try + { + this.req.open(method, url); + var me = this; + this.req.onreadystatechange = function() { me.ready_state_changed(); } + var workingxmlhttp = this.req.onreadystatechange; + + if (!workingxmlhttp) /* Old Konqueror */ + this.req = null; + } + catch (e) { } + } + + this.starttime = new Date().getTime(); + + if (this.req) + { + if (method == "POST") + this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + + this.req.send(postdata); + } + else + { + url += (url.match(/\?/) ? "&" : "?") + "itjs_call=it_http.__inst" + this.instance + "&itjs_callid=" + (++this.callid) + (postdata ? '&' + postdata : ""); + + if (samehost || (window.opera && !window.XMLHttpRequest)) // Opera 7 only works with iframes + { + var scrpt = document.createElement("iframe"); + scrpt.style.width = scrpt.style.height = 1; + url += "&itjs_iframe=1"; + } + else + { + var scrpt = document.createElement("script"); + this.req = { starttime: this.starttime }; + try + { + this.scrpt[this.callid] = scrpt; + if (!document.all) scrpt.src = url; + document.body.appendChild(scrpt); + if (document.all) scrpt.src = url; + } + catch (e) { return false; } + } + } + + return true; +}, + +ready_state_changed: function() +{ + var req = this.req; // Avoid race conditions + + if (req && (req.readyState == 4)) + { + var data = null; + + try + { + if (req.responseText != "") + data = eval("(" + req.responseText + ")"); + } + catch (e) + { + // trigger error handler (if defined) + if (typeof this.callback == 'object' && this.callback.errorhandler) + { + var obj = this.callback.object ? this.callback.object : window; + if (typeof obj[this.callback.errorhandler] == 'function') + obj[this.callback.errorhandler](req.responseText); + } + else + ED(e, req.responseText); + } + + if (data) + this.dataReady(data, this.callid); + + this.unlink(this.callid); + } +}, + +dataReady: function(data, callid) +{ + var fixkonqueror33gcbug = this.req; + var loadtime = new Date().getTime() - this.starttime; + + this.req = null; + + if ((typeof data == "object") && (this.callid == callid)) + { + data.loadtime = loadtime; + + // trigger callback function + if (typeof this.callback == 'function') + this.callback(data); + else if (typeof this.callback == 'object' && this.callback.method) + { + // it_set(data, this.callback); + var obj = this.callback.object ? this.callback.object : window; + if (typeof obj[this.callback.method] == 'function') + obj[this.callback.method](data); + } + } +}, + +stop: function() +{ + try { this.req.abort(); } + catch (e) { } + + this.unlink(this.callid); +}, + +unlink: function(callid) +{ + 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; + } + + this.busy = false; +} /* NO COMMA */ + +} + + +// static properties +it_http.instances = 0; + +// static methods +it_http.get_instance = function() +{ + var inst; + for (var i=0; i < it_http.instances; i++) + if ((inst = it_http['__inst'+i]) && inst.pub && !inst.busy) + return inst; + + inst = new it_http(); + inst.pub = true; + return inst; +} + +/* send GET request and trigger callback */ +it_http.get = function(url, callback) +{ + var inst = it_http.get_instance(); + inst.callback = callback; + inst.get(url); +} + +/* send POST request and trigger callback */ +it_http.post = function(url, postdata, callback) +{ + var inst = it_http.get_instance(); + inst.callback = callback; + inst.post(url, postdata); +} + |