snappymail/dev/Remote/AbstractAjax.js

312 lines
7.5 KiB
JavaScript
Raw Normal View History

2014-08-20 23:03:12 +08:00
2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-20 23:03:12 +08:00
var
2014-08-25 23:49:01 +08:00
window = require('window'),
2014-08-27 23:59:44 +08:00
_ = require('_'),
$ = require('$'),
2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
Consts = require('Common/Consts'),
Enums = require('Common/Enums'),
Globals = require('Common/Globals'),
Utils = require('Common/Utils'),
Plugins = require('Common/Plugins'),
2014-10-06 02:37:31 +08:00
Links = require('Common/Links'),
2014-08-21 23:08:34 +08:00
Settings = require('Storage/Settings')
2014-08-20 23:03:12 +08:00
;
/**
* @constructor
*/
2015-02-23 00:35:17 +08:00
function AbstractAjaxRemote()
2014-10-06 02:37:31 +08:00
{
this.oRequests = {};
}
2014-08-20 23:03:12 +08:00
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.oRequests = {};
2014-08-20 23:03:12 +08:00
2014-10-06 02:37:31 +08:00
/**
* @param {?Function} fCallback
* @param {string} sRequestAction
* @param {string} sType
* @param {?AjaxJsonDefaultResponse} oData
* @param {boolean} bCached
* @param {*=} oRequestParameters
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.defaultResponse = function (fCallback, sRequestAction, sType, oData, bCached, oRequestParameters)
2014-10-06 02:37:31 +08:00
{
var
fCall = function () {
2016-06-07 05:57:52 +08:00
if (Enums.StorageResultType.Success !== sType && Globals.data.bUnload)
2014-10-06 02:37:31 +08:00
{
sType = Enums.StorageResultType.Unload;
}
if (Enums.StorageResultType.Success === sType && oData && !oData.Result)
{
if (oData && -1 < Utils.inArray(oData.ErrorCode, [
Enums.Notification.AuthError, Enums.Notification.AccessError,
Enums.Notification.ConnectionError, Enums.Notification.DomainNotAllowed, Enums.Notification.AccountNotAllowed,
Enums.Notification.MailServerError, Enums.Notification.UnknownNotification, Enums.Notification.UnknownError
]))
{
2016-06-07 05:57:52 +08:00
Globals.data.iAjaxErrorCount++;
2014-10-06 02:37:31 +08:00
}
if (oData && Enums.Notification.InvalidToken === oData.ErrorCode)
{
2016-06-07 05:57:52 +08:00
Globals.data.iTokenErrorCount++;
2014-10-06 02:37:31 +08:00
}
2016-06-07 05:57:52 +08:00
if (Consts.TOKEN_ERROR_LIMIT < Globals.data.iTokenErrorCount)
2014-10-06 02:37:31 +08:00
{
2016-06-07 05:57:52 +08:00
if (Globals.data.__APP__ && Globals.data.__APP__.loginAndLogoutReload)
2014-10-06 02:37:31 +08:00
{
2016-06-28 04:54:38 +08:00
Globals.data.__APP__.loginAndLogoutReload(false, true);
2014-10-06 02:37:31 +08:00
}
}
2016-06-07 05:57:52 +08:00
if (oData.ClearAuth || oData.Logout || Consts.AJAX_ERROR_LIMIT < Globals.data.iAjaxErrorCount)
2014-10-06 02:37:31 +08:00
{
2016-06-07 05:57:52 +08:00
if (Globals.data.__APP__ && Globals.data.__APP__.clearClientSideToken)
2014-10-06 02:37:31 +08:00
{
2016-06-07 05:57:52 +08:00
Globals.data.__APP__.clearClientSideToken();
2014-10-06 02:37:31 +08:00
2016-06-28 04:54:38 +08:00
if (!oData.ClearAuth && Globals.data.__APP__.loginAndLogoutReload)
2014-10-06 02:37:31 +08:00
{
2016-06-28 04:54:38 +08:00
Globals.data.__APP__.loginAndLogoutReload(false, true);
2014-10-06 02:37:31 +08:00
}
}
}
}
else if (Enums.StorageResultType.Success === sType && oData && oData.Result)
{
2016-06-07 05:57:52 +08:00
Globals.data.iAjaxErrorCount = 0;
Globals.data.iTokenErrorCount = 0;
2014-10-06 02:37:31 +08:00
}
if (fCallback)
{
Plugins.runHook('ajax-default-response', [sRequestAction, Enums.StorageResultType.Success === sType ? oData : null, sType, bCached, oRequestParameters]);
fCallback(
sType,
Enums.StorageResultType.Success === sType ? oData : null,
bCached,
sRequestAction,
oRequestParameters
);
}
}
;
switch (sType)
{
case 'success':
sType = Enums.StorageResultType.Success;
break;
case 'abort':
sType = Enums.StorageResultType.Abort;
break;
default:
sType = Enums.StorageResultType.Error;
break;
}
if (Enums.StorageResultType.Error === sType)
{
_.delay(fCall, 300);
}
else
{
fCall();
}
};
2014-08-20 23:03:12 +08:00
2014-10-06 02:37:31 +08:00
/**
* @param {?Function} fResultCallback
* @param {Object} oParameters
* @param {?number=} iTimeOut = 20000
* @param {string=} sGetAdd = ''
* @param {Array=} aAbortActions = []
* @return {jQuery.jqXHR}
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.ajaxRequest = function (fResultCallback, oParameters, iTimeOut, sGetAdd, aAbortActions)
2014-10-06 02:37:31 +08:00
{
var
self = this,
bPost = '' === sGetAdd,
oHeaders = {},
iStart = (new window.Date()).getTime(),
oDefAjax = null,
sAction = ''
;
oParameters = oParameters || {};
iTimeOut = Utils.isNormal(iTimeOut) ? iTimeOut : 20000;
sGetAdd = Utils.isUnd(sGetAdd) ? '' : Utils.pString(sGetAdd);
aAbortActions = Utils.isArray(aAbortActions) ? aAbortActions : [];
sAction = oParameters.Action || '';
if (sAction && 0 < aAbortActions.length)
{
_.each(aAbortActions, function (sActionToAbort) {
if (self.oRequests[sActionToAbort])
{
self.oRequests[sActionToAbort].__aborted = true;
if (self.oRequests[sActionToAbort].abort)
{
self.oRequests[sActionToAbort].abort();
}
self.oRequests[sActionToAbort] = null;
}
});
}
if (bPost)
{
2016-06-28 04:54:38 +08:00
oParameters.XToken = Settings.appSettingsGet('token');
2014-10-06 02:37:31 +08:00
}
oDefAjax = $.ajax({
2016-06-28 04:54:38 +08:00
type: bPost ? 'POST' : 'GET',
url: Links.ajax(sGetAdd),
async: true,
dataType: 'json',
data: bPost ? oParameters : {},
headers: oHeaders,
timeout: iTimeOut,
global: true
2014-10-06 02:37:31 +08:00
});
oDefAjax.always(function (oData, sType) {
var bCached = false;
2016-06-28 04:54:38 +08:00
if (oData && oData.Time)
2014-10-06 02:37:31 +08:00
{
2016-06-28 04:54:38 +08:00
bCached = Utils.pInt(oData.Time) > (new window.Date()).getTime() - iStart;
2014-10-06 02:37:31 +08:00
}
if (sAction && self.oRequests[sAction])
{
if (self.oRequests[sAction].__aborted)
{
sType = 'abort';
}
self.oRequests[sAction] = null;
}
self.defaultResponse(fResultCallback, sAction, sType, oData, bCached, oParameters);
});
if (sAction && 0 < aAbortActions.length && -1 < Utils.inArray(sAction, aAbortActions))
{
if (this.oRequests[sAction])
{
this.oRequests[sAction].__aborted = true;
if (this.oRequests[sAction].abort)
{
this.oRequests[sAction].abort();
}
this.oRequests[sAction] = null;
}
this.oRequests[sAction] = oDefAjax;
}
return oDefAjax;
};
2014-08-20 23:03:12 +08:00
2014-10-06 02:37:31 +08:00
/**
* @param {?Function} fCallback
* @param {string} sAction
* @param {Object=} oParameters
* @param {?number=} iTimeout
* @param {string=} sGetAdd = ''
* @param {Array=} aAbortActions = []
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.defaultRequest = function (fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions)
2014-10-06 02:37:31 +08:00
{
oParameters = oParameters || {};
oParameters.Action = sAction;
sGetAdd = Utils.pString(sGetAdd);
Plugins.runHook('ajax-default-request', [sAction, oParameters, sGetAdd]);
return this.ajaxRequest(fCallback, oParameters,
2015-11-15 08:23:16 +08:00
Utils.isUnd(iTimeout) ? Consts.DEFAULT_AJAX_TIMEOUT : Utils.pInt(iTimeout), sGetAdd, aAbortActions);
2014-10-06 02:37:31 +08:00
};
/**
* @param {?Function} fCallback
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.noop = function (fCallback)
2014-10-06 02:37:31 +08:00
{
this.defaultRequest(fCallback, 'Noop');
};
/**
* @param {?Function} fCallback
* @param {string} sMessage
* @param {string} sFileName
* @param {number} iLineNo
* @param {string} sLocation
* @param {string} sHtmlCapa
* @param {number} iTime
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.jsError = function (fCallback, sMessage, sFileName, iLineNo, sLocation, sHtmlCapa, iTime)
2014-10-06 02:37:31 +08:00
{
this.defaultRequest(fCallback, 'JsError', {
'Message': sMessage,
'FileName': sFileName,
'LineNo': iLineNo,
'Location': sLocation,
'HtmlCapa': sHtmlCapa,
'TimeOnPage': iTime
});
};
/**
* @param {?Function} fCallback
* @param {string} sType
* @param {Array=} mData = null
* @param {boolean=} bIsError = false
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.jsInfo = function (fCallback, sType, mData, bIsError)
2014-10-06 02:37:31 +08:00
{
this.defaultRequest(fCallback, 'JsInfo', {
'Type': sType,
'Data': mData,
'IsError': (Utils.isUnd(bIsError) ? false : !!bIsError) ? '1' : '0'
});
};
/**
* @param {?Function} fCallback
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.getPublicKey = function (fCallback)
2014-10-06 02:37:31 +08:00
{
this.defaultRequest(fCallback, 'GetPublicKey');
};
/**
* @param {?Function} fCallback
* @param {string} sVersion
*/
2015-02-23 00:35:17 +08:00
AbstractAjaxRemote.prototype.jsVersion = function (fCallback, sVersion)
2014-10-06 02:37:31 +08:00
{
this.defaultRequest(fCallback, 'Version', {
'Version': sVersion
});
};
2014-08-20 23:03:12 +08:00
2015-02-23 00:35:17 +08:00
module.exports = AbstractAjaxRemote;
2014-08-20 23:03:12 +08:00
2016-06-28 04:54:38 +08:00
}());