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

171 lines
3.2 KiB
JavaScript
Executable File

function xhr(xhrmon) {
this.monitor = xhrmon;
this.processor = new XMLHTTPRequest();
this.queries = new Array();
this.index = 0;
this.current = 0;
this.isReady = true;
var self = this;
this.processor.onreadystatechange = function () {
return self.onReadyStateChange(this);
}
/****************************/
this.newQuery = function (query) {
this.queries[this.index] = query;
this.index++;
if (this.isReady && this.current == this.index - 1) this.process();
return true;
}
this.process = function () {
this.isReady = false;
var query = this.queries[this.current];
var proc = this.processor;
proc.open(query.method, query.url, query.async);
for (var i in query.headers) {
proc.setRequestHeader(query.headers[i]['name'], query.headers[i]['value']);
}
data = '';
if (query.method = 'POST')
for (var i in query.post)
data += query.post[i]['name'] + "=" + encodeURIComponent(query.post[i]['value']);
proc.send(data);
}
this.onReadyStateChange = function (xhr) {
if (this.readyState == 4) {
var query = this.queries[this.current];
}
}
/********************************************************/
/* Préparation et initialisation du moniteur d'etat xhr */
/********************************************************/
this.monitor.toggle = function (e) {
e.stopPropagation();
if (this.hasClassName('active')) return self.monitor.removeClassName('active');
return this.addClassName('active');
}
function xhrfree() {
this.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);
*/
function updateMon(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';
this.monitor.className = rS;
}
}
function xhrQuery(method, url) {
this.method = method;
this.url = url;
this.headers = new Array();
this.data = new Array();
this.async = true;
this.callback = null;
this.setHeader = function (header, value) {
var a = new Array();
a['name'] = header;
a['value'] = value;
this.headers[this.headers.length] = a;
return a;
}
this.login = function (user, passwd) {
this.user = user;
this.passwd = passwd;
return true;
}
this.setCallback = function (fn) {
this.callback = fn;
return fn;
}
this.addData = function (data) {
for(var i in data) this.addData( data[i]['name'], data[i]['value'] );
return data;
}
this.loadData = function (name, value) {
var a = new Array();
a['name'] = name;
a['value'] = value;
this.data[this.data.length] = a;
return a;
}
this.onError = function (status, statusText) {
alert(status + " : " + statusText);
}
}
function xhrResult() {
}
/* test xhr
function cb (xhr) {
alert(xhr.status + ' le status');
}
var x = new xhrQuery('post', 'http://localhost/editor/query.php);
x.callback = function (xhr) {return cb(xhr);};
var y = new xhr();
*/