themes/base: xhr.js: clean code style, implement XHR.get() and XHR.poll() convenience functions

This commit is contained in:
Jo-Philipp Wich 2011-09-26 00:05:17 +00:00
parent 2829139f35
commit b2b3b181d8

View file

@ -7,10 +7,10 @@ XHR = function()
{ {
this.reinit = function() this.reinit = function()
{ {
if( window.XMLHttpRequest ) { if (window.XMLHttpRequest) {
this._xmlHttp = new XMLHttpRequest(); this._xmlHttp = new XMLHttpRequest();
} }
else if( window.ActiveXObject ) { else if (window.ActiveXObject) {
this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} }
else { else {
@ -19,7 +19,10 @@ XHR = function()
} }
this.busy = function() { this.busy = function() {
switch( this._xmlHttp.readyState ) if (!this._xmlHttp)
return false;
switch (this._xmlHttp.readyState)
{ {
case 1: case 1:
case 2: case 2:
@ -32,7 +35,7 @@ XHR = function()
} }
this.abort = function() { this.abort = function() {
if( this.busy() ) if (this.busy())
this._xmlHttp.abort(); this._xmlHttp.abort();
} }
@ -41,23 +44,23 @@ XHR = function()
this.reinit(); this.reinit();
var xhr = this._xmlHttp; var xhr = this._xmlHttp;
var code = this._encode( data ); var code = this._encode(data);
url = location.protocol + '//' + location.host + url; url = location.protocol + '//' + location.host + url;
if( code ) if (code)
if( url.substr(url.length-1,1) == '&' ) if (url.substr(url.length-1,1) == '&')
url += code; url += code;
else else
url += '?' + code; url += '?' + code;
xhr.open( 'GET', url, true ); xhr.open('GET', url, true);
xhr.onreadystatechange = function() xhr.onreadystatechange = function()
{ {
if( xhr.readyState == 4 ) { if (xhr.readyState == 4) {
var json = null; var json = null;
if( xhr.getResponseHeader("Content-Type") == "application/json" ) { if (xhr.getResponseHeader("Content-Type") == "application/json") {
try { try {
json = eval('(' + xhr.responseText + ')'); json = eval('(' + xhr.responseText + ')');
} }
@ -66,11 +69,11 @@ XHR = function()
} }
} }
callback( xhr, json ); callback(xhr, json);
} }
} }
xhr.send( null ); xhr.send(null);
} }
this.post = function(url,data,callback) this.post = function(url,data,callback)
@ -78,19 +81,19 @@ XHR = function()
this.reinit(); this.reinit();
var xhr = this._xmlHttp; var xhr = this._xmlHttp;
var code = this._encode( data ); var code = this._encode(data);
xhr.onreadystatechange = function() xhr.onreadystatechange = function()
{ {
if( xhr.readyState == 4 ) if (xhr.readyState == 4)
callback( xhr ); callback(xhr);
} }
xhr.open( 'POST', url, true ); xhr.open('POST', url, true);
xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' ); xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader( 'Content-length', code.length ); xhr.setRequestHeader('Content-length', code.length);
xhr.setRequestHeader( 'Connection', 'close' ); xhr.setRequestHeader('Connection', 'close');
xhr.send( code ); xhr.send(code);
} }
this.cancel = function() this.cancel = function()
@ -103,41 +106,41 @@ XHR = function()
{ {
var code = ''; var code = '';
for( var i = 0; i < form.elements.length; i++ ) for (var i = 0; i < form.elements.length; i++)
{ {
var e = form.elements[i]; var e = form.elements[i];
if( e.options ) if (e.options)
{ {
code += ( code ? '&' : '' ) + code += (code ? '&' : '') +
form.elements[i].name + '=' + encodeURIComponent( form.elements[i].name + '=' + encodeURIComponent(
e.options[e.selectedIndex].value e.options[e.selectedIndex].value
); );
} }
else if( e.length ) else if (e.length)
{ {
for( var j = 0; j < e.length; j++ ) for (var j = 0; j < e.length; j++)
if( e[j].name ) { if (e[j].name) {
code += ( code ? '&' : '' ) + code += (code ? '&' : '') +
e[j].name + '=' + encodeURIComponent( e[j].value ); e[j].name + '=' + encodeURIComponent(e[j].value);
} }
} }
else else
{ {
code += ( code ? '&' : '' ) + code += (code ? '&' : '') +
e.name + '=' + encodeURIComponent( e.value ); e.name + '=' + encodeURIComponent(e.value);
} }
} }
if( typeof extra_values == 'object' ) if (typeof extra_values == 'object')
for( var key in extra_values ) for (var key in extra_values)
code += ( code ? '&' : '' ) + code += (code ? '&' : '') +
key + '=' + encodeURIComponent( extra_values[key] ); key + '=' + encodeURIComponent(extra_values[key]);
return( return(
( form.method == 'get' ) (form.method == 'get')
? this.get( form.getAttribute('action'), code, callback ) ? this.get(form.getAttribute('action'), code, callback)
: this.post( form.getAttribute('action'), code, callback ) : this.post(form.getAttribute('action'), code, callback)
); );
} }
@ -146,14 +149,14 @@ XHR = function()
obj = obj ? obj : { }; obj = obj ? obj : { };
obj['_'] = Math.random(); obj['_'] = Math.random();
if( typeof obj == 'object' ) if (typeof obj == 'object')
{ {
var code = ''; var code = '';
var self = this; var self = this;
for( var k in obj ) for (var k in obj)
code += ( code ? '&' : '' ) + code += (code ? '&' : '') +
k + '=' + encodeURIComponent( obj[k] ); k + '=' + encodeURIComponent(obj[k]);
return code; return code;
} }
@ -161,3 +164,41 @@ XHR = function()
return obj; return obj;
} }
} }
XHR.get = function(url, data, callback)
{
(new XHR()).get(url, data, callback);
}
XHR.poll = function(interval, url, data, callback)
{
if (isNaN(interval) || interval <= 1)
interval = 5;
if (!XHR._q)
{
XHR._t = 0;
XHR._q = [ ];
}
XHR._q.push({
interval: interval,
callback: callback,
url: url,
data: data,
xhr: new XHR()
});
if (!XHR._i)
{
XHR._i = window.setInterval(function() {
for (var i = 0, e = XHR._q[0]; i < XHR._q.length; e = XHR._q[++i])
{
if (!(XHR._t % e.interval) && !e.xhr.busy())
e.xhr.get(e.url, e.data, e.callback);
}
XHR._t++;
}, 1000);
}
}