2006-09-13 17:39:53 -04:00
|
|
|
var WPAjax = Class.create();
|
|
|
|
Object.extend(WPAjax.prototype, Ajax.Request.prototype);
|
|
|
|
Object.extend(WPAjax.prototype, {
|
|
|
|
WPComplete: false, // onComplete function
|
|
|
|
WPError: false, // onWPError function
|
2007-03-06 12:35:01 -05:00
|
|
|
defaultUrl: '', // We get these from WPAjaxL10n
|
|
|
|
permText: '',
|
|
|
|
strangeText: '',
|
|
|
|
whoaText: '',
|
|
|
|
|
2006-09-13 17:39:53 -04:00
|
|
|
initialize: function(url, responseEl) {
|
|
|
|
var tempObj = this;
|
|
|
|
this.transport = Ajax.getTransport();
|
|
|
|
if ( !this.transport )
|
|
|
|
return false;
|
|
|
|
this.setOptions( {
|
|
|
|
parameters: 'cookie=' + encodeURIComponent(document.cookie),
|
|
|
|
onComplete: function(transport) { // transport = XMLHttpRequest object
|
|
|
|
if ( tempObj.parseAjaxResponse() ) {
|
|
|
|
if ( 'function' == typeof tempObj.WPComplete )
|
|
|
|
tempObj.WPComplete(transport);
|
|
|
|
} else if ( 'function' == typeof tempObj.WPError ) // if response corresponds to an error (bad data, say, not 404)
|
|
|
|
tempObj.WPError(transport);
|
|
|
|
}
|
|
|
|
});
|
2007-03-06 12:35:01 -05:00
|
|
|
this.url = url ? url : this.defaultUrl;
|
2006-09-13 17:39:53 -04:00
|
|
|
this.getResponseElement(responseEl);
|
|
|
|
},
|
|
|
|
addArg: function(key, value) {
|
2007-05-21 08:56:39 -04:00
|
|
|
var a = $H();
|
2006-09-13 17:39:53 -04:00
|
|
|
a[encodeURIComponent(key)] = encodeURIComponent(value);
|
2007-05-21 08:56:39 -04:00
|
|
|
this.options.parameters = $H(this.options.parameters).merge(a);
|
2006-09-13 17:39:53 -04:00
|
|
|
},
|
|
|
|
getResponseElement: function(r) {
|
|
|
|
var p = $(r + '-p');
|
|
|
|
if ( !p ) {
|
|
|
|
new Insertion.Bottom(r, "<span id='" + r + "-p'></span>");
|
|
|
|
var p = $(r + '-p');
|
|
|
|
}
|
|
|
|
this.myResponseElement = p;
|
|
|
|
},
|
|
|
|
parseAjaxResponse: function() { // 1 = good, 0 = strange (bad data?), -1 = you lack permission
|
2007-01-18 19:49:01 -05:00
|
|
|
if ( this.transport.responseXML && typeof this.transport.responseXML == 'object' && ( this.transport.responseXML.xml || 'undefined' == typeof this.transport.responseXML.xml ) ) {
|
2006-09-13 17:39:53 -04:00
|
|
|
var err = this.transport.responseXML.getElementsByTagName('wp_error');
|
|
|
|
if ( err[0] ) {
|
|
|
|
var msg = $A(err).inject( '', function(a, b) { return a + '<p>' + b.firstChild.nodeValue + '</p>'; } );
|
2007-01-18 19:49:01 -05:00
|
|
|
Element.update(this.myResponseElement,'<div class="error">' + msg + '</div>');
|
2006-09-13 17:39:53 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var r = this.transport.responseText;
|
|
|
|
if ( isNaN(r) ) {
|
2007-01-18 19:49:01 -05:00
|
|
|
Element.update(this.myResponseElement,'<div class="error"><p>' + r + '</p></div>');
|
2006-09-13 17:39:53 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var r = parseInt(r,10);
|
|
|
|
if ( -1 == r ) {
|
2007-03-06 12:35:01 -05:00
|
|
|
Element.update(this.myResponseElement,"<div class='error'><p>" + this.permText + "</p></div>");
|
2006-09-13 17:39:53 -04:00
|
|
|
return false;
|
|
|
|
} else if ( 0 == r ) {
|
2007-03-06 12:35:01 -05:00
|
|
|
Element.update(this.myResponseElement,"<div class='error'><p>" + this.strangeText + "</p></div>");
|
2006-09-13 17:39:53 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
addOnComplete: function(f) {
|
|
|
|
if ( 'function' == typeof f ) { var of = this.WPComplete; this.WPComplete = function(t) { if ( of ) of(t); f(t); } }
|
|
|
|
},
|
|
|
|
addOnWPError: function(f) {
|
|
|
|
if ( 'function' == typeof f ) { var of = this.WPError; this.WPError = function(t) { if ( of ) of(t); f(t); } }
|
|
|
|
},
|
|
|
|
notInitialized: function() {
|
|
|
|
return this.transport ? false : true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-03-06 12:35:01 -05:00
|
|
|
Event.observe( window, 'load', function() { Object.extend(WPAjax.prototype, WPAjaxL10n); }, false )
|
|
|
|
|
2006-09-13 17:39:53 -04:00
|
|
|
Ajax.activeSendCount = 0;
|
|
|
|
Ajax.Responders.register( {
|
|
|
|
onCreate: function() {
|
|
|
|
Ajax.activeSendCount++;
|
|
|
|
if ( 1 != Ajax.activeSendCount )
|
|
|
|
return;
|
|
|
|
wpBeforeUnload = window.onbeforeunload;
|
|
|
|
window.onbeforeunload = function() {
|
2007-03-06 12:35:01 -05:00
|
|
|
return WPAjax.whoaText;
|
2006-09-13 17:39:53 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onLoading: function() { // Can switch to onLoaded if we lose data
|
|
|
|
Ajax.activeSendCount--;
|
|
|
|
if ( 0 != Ajax.activeSendCount )
|
|
|
|
return;
|
|
|
|
window.onbeforeunload = wpBeforeUnload;
|
|
|
|
}
|
|
|
|
});
|
2006-11-08 16:23:41 -05:00
|
|
|
|
|
|
|
//Pretty func adapted from ALA http://www.alistapart.com/articles/gettingstartedwithajax
|
|
|
|
function getNodeValue(tree,el){try { var r = tree.getElementsByTagName(el)[0].firstChild.nodeValue; } catch(err) { var r = null; } return r; }
|