diff options
author | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2006-09-14 15:04:44 +0000 |
---|---|---|
committer | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2006-09-14 15:04:44 +0000 |
commit | b1d917d5d377c88241aa233d4c6403213afa05df (patch) | |
tree | c2cbc61fa1ca5297a5dd065d8a6ef7c5a855061b /wp-includes/js/wp-ajax-js.php | |
parent | 0594b4fefe09d84f0ed8ba46a6a0bb8f925027ea (diff) | |
download | wordpress-mu-b1d917d5d377c88241aa233d4c6403213afa05df.tar.gz wordpress-mu-b1d917d5d377c88241aa233d4c6403213afa05df.tar.xz wordpress-mu-b1d917d5d377c88241aa233d4c6403213afa05df.zip |
WP Merge to rev #4191
git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@753 7be80a69-a1ef-0310-a953-fb0f7c49ff36
Diffstat (limited to 'wp-includes/js/wp-ajax-js.php')
-rw-r--r-- | wp-includes/js/wp-ajax-js.php | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/wp-includes/js/wp-ajax-js.php b/wp-includes/js/wp-ajax-js.php new file mode 100644 index 0000000..e56cba9 --- /dev/null +++ b/wp-includes/js/wp-ajax-js.php @@ -0,0 +1,93 @@ +<?php @require_once('../../wp-config.php'); cache_javascript_headers(); ?> +var WPAjax = Class.create(); +Object.extend(WPAjax.prototype, Ajax.Request.prototype); +Object.extend(WPAjax.prototype, { + WPComplete: false, // onComplete function + WPError: false, // onWPError function + 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); + } + }); + this.url = url; + this.getResponseElement(responseEl); + }, + addArg: function(key, value) { + var a = $H(this.options.parameters.parseQuery()); + a[encodeURIComponent(key)] = encodeURIComponent(value); + this.options.parameters = a.map(function(pair) { + return pair.join('='); + }).join('&'); + }, + 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 + if ( this.transport.responseXML && typeof this.transport.responseXML == 'object' ) { + 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>'; } ); + this.myResponseElement.update('<div class="error">' + msg + '</div>'); + return false; + } + return true; + } + var r = this.transport.responseText; + if ( isNaN(r) ) { + this.myResponseElement.update('<div class="error"><p>' + r + '</p></div>'); + return false; + } + var r = parseInt(r,10); + if ( -1 == r ) { + this.myResponseElement.update("<div class='error'><p><?php _e("You don't have permission to do that."); ?></p></div>"); + return false; + } else if ( 0 == r ) { + this.myResponseElement.update("<div class='error'><p><?php _e("Something strange happened. Try refreshing the page."); ?></p></div>"); + 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; + } +}); + +Ajax.activeSendCount = 0; +Ajax.Responders.register( { + onCreate: function() { + Ajax.activeSendCount++; + if ( 1 != Ajax.activeSendCount ) + return; + wpBeforeUnload = window.onbeforeunload; + window.onbeforeunload = function() { + return "<?php _e("Slow down, I'm still sending your data!"); ?>"; + } + }, + onLoading: function() { // Can switch to onLoaded if we lose data + Ajax.activeSendCount--; + if ( 0 != Ajax.activeSendCount ) + return; + window.onbeforeunload = wpBeforeUnload; + } +}); |