cyrilleinvalides/choupas/www/admin/editor/scripts/xhr2.js

146 lines
2.8 KiB
JavaScript
Executable File

function xhrQuery(url, method) {
this.url = url;
this.method = method;
this.headers = new Array();
this.posts = new Array();
this.cookies = new Array();
this.callback = null;
this.async = true;
this.reset = function () {
this.headers = new Array();
this.postData = new Array();
this.cookies = new Array();
this.callback = null;
this.async = true;
}
this.addHeader = function (name, value) {
var a = new Array();
a['name'] = name;
a['value'] = value;
this.headers[this.headers.length] = a;
return a;
}
this.addCookie = function (name, value) {
var a = new Array();
a['name'] = name;
a['value'] = encodeURIComponent(value);
this.cookies[this.cookies.length] = a;
return a;
}
this.addPost = function (name, value) {
var a = new Array();
a['name'] = name;
a['value'] = encodeURIComponent(value);
this.posts[this.posts.length] = a;
return a;
}
this.onError = function (status, statusText) {
alert(status + " : " + statusText);
}
}
function xhrResult(xhr) {
this.xml = xhr.responseXML;
this.text = xhr.responseText
this.status = xhr.status;
this.statusText = xhr.statusText;
this.headerStr = xhr.getAllResponseHeaders();
}
function xhrProcessor() {
this.queue = new Array('null');
this.current = 1;
this.busy = false;
this.xhr = null;
var self = this;
/* this.enqueue = function (query) {
var pos = this.queue.length;
this.queue[pos] = query;
//setTimeout("10", this.process);
return pos;
}
this.dequeue = function (i) {
var query = this.queue[i];
this.queue[i] = null;
return query;
}
*/
this.process = function (query) {
//if (this.busy || this.queue.length <= this.current) return false;
this.busy = this.current;
this.xhr = new XMLHttpRequest();
// var query = this.queue[this.current];
for (var i in query.headers) this.xhr.setRequestHeader(query.headers[i]['name'], query.headers[i]['value']);
var data = this.linearize(query.posts);
var cook = this.linearize(query.cookies);
if (cook) this.xhr.setRequestHeader('Cookies', cook);
// processing...
this.xhr.onreadystatechange = function() {
if ( this.readyState == 4 ) {
self.busy = false;
var xhrs = this;
var result = new xhrResult( xhrs );
if (query.callback) return query.callback(result);
return true;
}
}
this.xhr.open(query.method, query.url, query.async);
this.xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
this.xhr.send(data);
this.current++;
return true;
}
this.abort = function () {
this.xhr.abort();
//return this.queue[this.current - 1];
}
this.linearize = function (arr) {
var str = '';
for (var i in arr) {
str += arr[i]['name'] + "=" + arr[i]['value'];
}
return str;
}
}
function xhrMonitor() {
}