From bbb6abab86bcdaf09197ff2c98eb0219143cd6b6 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 25 May 2015 00:33:22 +0200 Subject: [PATCH] WCF.Action.Proxy now uses WoltLab/WCF/Ajax/{Jsonp|Request} --- wcfsetup/install/files/js/WCF.js | 206 +++++++------------------------ 1 file changed, 46 insertions(+), 160 deletions(-) diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index 5d812a219b..9929c3a69e 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -2114,26 +2114,12 @@ WCF.Action = {}; /** * Basic implementation for AJAX-based proxyies * + * @deprecated 2.2 - please use `WoltLab/WCF/Ajax.api()` instead + * * @param object options */ WCF.Action.Proxy = Class.extend({ - /** - * shows loading overlay for a single request - * @var boolean - */ - _showLoadingOverlayOnce: false, - - /** - * suppresses errors - * @var boolean - */ - _suppressErrors: false, - - /** - * last request - * @var jqXHR - */ - _lastRequest: null, + _ajaxRequest: null, /** * Initializes AJAXProxy. @@ -2141,8 +2127,9 @@ WCF.Action.Proxy = Class.extend({ * @param object options */ init: function(options) { - // initialize default values - this.options = $.extend(true, { + this._ajaxRequest = null; + + options = $.extend(true, { autoSend: false, data: { }, dataType: 'json', @@ -2160,20 +2147,34 @@ WCF.Action.Proxy = Class.extend({ autoAbortPrevious: false }, options); - this.options.url = WCF.convertLegacyURL(this.options.url); - - this.confirmationDialog = null; - this.loading = null; - this._showLoadingOverlayOnce = false; - this._suppressErrors = (this.options.suppressErrors === true); - - // send request immediately after initialization - if (this.options.autoSend) { - this.sendRequest(); + if (options.dataType === 'jsonp') { + require(['AjaxJsonp'], function(AjaxJsonp) { + AjaxJsonp.send(options.url, options.success, options.failure, { + parameterName: options.jsonp + }); + }); + } + else { + require(['AjaxRequest'], (function(AjaxRequest) { + this._ajaxRequest = new AjaxRequest({ + data: options.data, + type: options.type, + url: options.url, + + autoAbort: options.autoAbortPrevious, + ignoreError: options.suppressErrors, + silent: options.suppressErrors, + + failure: options.failure, + finalize: options.after, + success: options.success + }); + + if (options.autoSend) { + this._ajaxRequest.sendRequest(); + } + }).bind(this)); } - - var self = this; - $(window).on('beforeunload', function() { self._suppressErrors = true; }); }, /** @@ -2183,65 +2184,31 @@ WCF.Action.Proxy = Class.extend({ * @return jqXHR */ sendRequest: function(abortPrevious) { - this._init(); - - if (abortPrevious || this.options.autoAbortPrevious) { - this.abortPrevious(); + if (this._ajaxRequest !== null) { + this._ajaxRequest.sendRequest(abortPrevious); } - this._lastRequest = $.ajax({ - data: this.options.data, - dataType: this.options.dataType, - jsonp: this.options.jsonp, - async: this.options.async, - type: this.options.type, - url: this.options.url, - success: $.proxy(this._success, this), - error: $.proxy(this._failure, this) - }); - return this._lastRequest; + return null; }, /** * Aborts the previous request */ abortPrevious: function() { - if (this._lastRequest !== null) { - this._lastRequest.abort(); - this._lastRequest = null; - - if (this.options.showLoadingOverlay || this._showLoadingOverlayOnce) { - WCF.LoadingOverlayHandler.hide(); - } + if (this._ajaxRequest !== null) { + this._ajaxRequest.abortPrevious(); } }, /** * Shows loading overlay for a single request. */ - showLoadingOverlayOnce: function() { - this._showLoadingOverlayOnce = true; - }, + showLoadingOverlayOnce: function() {}, /** * Suppressed errors for this action proxy. */ - suppressErrors: function() { - this._suppressErrors = true; - }, - - /** - * Fires before request is send, displays global loading status. - */ - _init: function() { - if ($.isFunction(this.options.init)) { - this.options.init(this); - } - - if (this.options.showLoadingOverlay || this._showLoadingOverlayOnce) { - WCF.LoadingOverlayHandler.show(); - } - }, + suppressErrors: function() {}, /** * Handles AJAX errors. @@ -2250,53 +2217,7 @@ WCF.Action.Proxy = Class.extend({ * @param string textStatus * @param string errorThrown */ - _failure: function(jqXHR, textStatus, errorThrown) { - if (textStatus == 'abort') { - // call child method if applicable - if ($.isFunction(this.options.aborted)) { - this.options.aborted(jqXHR); - } - - return; - } - - try { - var $data = $.parseJSON(jqXHR.responseText); - - // call child method if applicable - var $showError = true; - if ($.isFunction(this.options.failure)) { - $showError = this.options.failure($data, jqXHR, textStatus, errorThrown); - } - - if (!this._suppressErrors && $showError !== false) { - var $details = ''; - if ($data.stacktrace) $details = '

Stacktrace:

' + $data.stacktrace + '

'; - else if ($data.exceptionID) $details = '

Exception ID: ' + $data.exceptionID + '

'; - - $('

' + $data.message + '

' + $details + '
').wcfDialog({ title: WCF.Language.get('wcf.global.error.title') }); - } - } - // failed to parse JSON - catch (e) { - // call child method if applicable - var $showError = true; - if ($.isFunction(this.options.failure)) { - $showError = this.options.failure(null, jqXHR, textStatus, errorThrown); - } - - if (!this._suppressErrors && $showError !== false) { - var $message = (textStatus === 'timeout') ? WCF.Language.get('wcf.global.error.timeout') : jqXHR.responseText; - - // validate if $message is neither empty nor 'undefined' - if ($message && $message != 'undefined') { - $('

' + $message + '

').wcfDialog({ title: WCF.Language.get('wcf.global.error.title') }); - } - } - } - - this._after(); - }, + _failure: function(jqXHR, textStatus, errorThrown) {}, /** * Handles successful AJAX requests. @@ -2305,49 +2226,12 @@ WCF.Action.Proxy = Class.extend({ * @param string textStatus * @param object jqXHR */ - _success: function(data, textStatus, jqXHR) { - // call child method if applicable - if ($.isFunction(this.options.success)) { - // trim HTML before processing, see http://jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring - if (data && data.returnValues && data.returnValues.template !== undefined) { - data.returnValues.template = $.trim(data.returnValues.template); - } - - this.options.success(data, textStatus, jqXHR); - } - - this._after(); - }, + _success: function(data, textStatus, jqXHR) {}, /** * Fires after an AJAX request, hides global loading status. */ - _after: function() { - this._lastRequest = null; - if ($.isFunction(this.options.after)) { - this.options.after(); - } - - if (this.options.showLoadingOverlay || this._showLoadingOverlayOnce) { - WCF.LoadingOverlayHandler.hide(); - - if (this._showLoadingOverlayOnce) { - this._showLoadingOverlayOnce = false; - } - } - - WCF.DOMNodeInsertedHandler.execute(); - - // fix anchor tags generated through WCF::getAnchor() - $('a[href*=#]').each(function(index, link) { - var $link = $(link); - if ($link.prop('href').indexOf('AJAXProxy') != -1) { - var $anchor = $link.prop('href').substr($link.prop('href').indexOf('#')); - var $pageLink = document.location.toString().replace(/#.*/, ''); - $link.prop('href', $pageLink + $anchor); - } - }); - }, + _after: function() {}, /** * Sets options, MUST be used to set parameters before sending request @@ -2357,7 +2241,9 @@ WCF.Action.Proxy = Class.extend({ * @param mixed optionData */ setOption: function(optionName, optionData) { - this.options[optionName] = optionData; + if (this._ajaxRequest !== null) { + this._ajaxRequest.setOption(optionName, optionData); + } } }); -- 2.20.1