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

165 lines
3.6 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;
/**************/
/* evenements */
/**************/
this.onsuccess = null;
this.onError = function (status, statusText) {
alert(status + " : " + statusText);
}
/************/
/* Methodes */
/************/
this.reset = function () {
this.headers = new Array();
this.postData = new Array();
this.cookies = new Array();
this.callback = null;
this.async = true;
return 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'] = encodeURIComponent(name);
a['value'] = encodeURIComponent(value);
this.cookies[this.cookies.length] = a;
return a;
}
this.addPost = function (name, value) {
var a = new Array();
a['name'] = encodeURIComponent(name);
a['value'] = encodeURIComponent(value);
this.posts[this.posts.length] = a;
return a;
}
this.toggleSync = function () {
this.async = !this.async;
return this.async;
}
}
function xhrResult(xhr) {
this.xml = xhr.responseXML;
this.text = xhr.responseText
this.status = xhr.status;
this.statusText = xhr.statusText;
this.responseHeaderStr = xhr.getAllResponseHeaders();
}
function xhrProcessor() {
this.queue = new Array('null');
this.current = 1;
this.busy = false;
this.xhr = null;
var self = this;
this.process = function (query) {
this.busy = this.current;
this.xhr = new XMLHttpRequest();
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);
this.xhr.onreadystatechange = function() {
xhrMonitor(this.readyState);
if ( this.readyState == 4 ) {
var result = new xhrResult(this);
if (query.callback) var res = query.callback(result);
xhrMonitor(0);
return res;
}
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 () {
return this.xhr.abort();
}
this.linearize = function (arr) {
var str = '';
for (var i in arr) {
str += arr[i]['name'] + "=" + arr[i]['value'] + "&";
}
return str;
}
}
function xhrMonitor(rS) {
if (rS == 0) rS = 'zero';
if (rS == 1) rS = 'one';
if (rS == 2) rS = 'two';
if (rS == 3) rS = 'three';
if (rS == 4) rS = 'four';
document.getElementById('xhrMonitor').className = rS;
}
function makeXhrMon(elem) {
function xhrtoggle(e) {
e.stopPropagation();
if (document.getElementById('xhrMonitor').hasClassName('active')) return document.getElementById('xhrMonitor').removeClassName('active');
return document.getElementById('xhrMonitor').addClassName('active');
}
function xhrfree() {
document.getElementById('xhrMonitor').removeClassName('active');
}
var sp = document.getElementById('xhrMonitor').getElementsByTagName('span');
for (var i = 0; i < 5; i++) {
eval("sp[i + 1].addEventListener('click', function () {if ( document.getElementById('xhrMonitor').hasClassName('active') ) xhrMonitor(" + i + ");}, false); ");
}
elem.addEventListener('click', xhrtoggle, false);
document.addEventListener('click', xhrfree, false);
}