define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var AjaxHandler = (function () {
function AjaxHandler(pdfviewer) {
this.type = 'POST';
this.mode = true;
this.contentType = 'application/json;charset=UTF-8';
this.retryTimeout = 0;
this.pdfViewer = pdfviewer;
this.retryCount = pdfviewer.retryCount;
this.retryStatusCodes = pdfviewer.retryStatusCodes;
this.retryTimeout = 1000 * pdfviewer.retryTimeout;
}
AjaxHandler.prototype.send = function (jsonObj) {
var _this = this;
this.httpRequest = new XMLHttpRequest();
this.httpRequest.timeout = this.retryTimeout;
Iif (!this.mode) {
setTimeout(function () {
_this.sendRequest(jsonObj);
});
}
else {
this.sendRequest(jsonObj);
}
this.httpRequest.onreadystatechange = function () {
var isSkip = false;
var viewerBase = _this.pdfViewer.viewerBase;
Iif (viewerBase && viewerBase.isPasswordAvailable && viewerBase.passwordData === '') {
isSkip = true;
_this.retryCount = 0;
}
Eif (_this.retryCount > 0) {
isSkip = _this.resendRequest(_this, jsonObj, false);
}
Eif (!isSkip) {
_this.stateChange(_this);
}
};
this.httpRequest.ontimeout = function () {
var isSkip = false;
var viewerBase = _this.pdfViewer.viewerBase;
if (viewerBase && viewerBase.isPasswordAvailable && viewerBase.passwordData === '') {
isSkip = true;
_this.retryCount = 0;
}
if (_this.retryCount > 0) {
isSkip = _this.resendRequest(_this, jsonObj, true);
}
if (!isSkip) {
_this.stateChange(_this);
}
};
this.httpRequest.onerror = function () {
_this.error(_this);
};
};
AjaxHandler.prototype.clear = function () {
if (this.httpRequest) {
this.httpRequest.abort();
}
this.onSuccess = null;
this.onFailure = null;
this.onError = null;
};
AjaxHandler.prototype.resendRequest = function (proxy, jsonObj, isTimeout) {
var isSkip = false;
var status = proxy.httpRequest.status;
var statusString = this.retryStatusCodes.indexOf(status) !== -1;
if (proxy.httpRequest.readyState === 4 && status === 200) {
var data = void 0;
Eif (this.responseType !== null) {
data = proxy.httpRequest.response;
}
else {
data = proxy.httpRequest.responseText;
}
Eif (data) {
Iif (typeof data !== 'object') {
try {
data = JSON.parse(data);
}
catch (error) {
if (data === 'Document stream does not exist in the cache' || data === 'Document Reference pointer does not exist in the cache') {
isSkip = true;
}
}
}
}
}
Iif (statusString || isSkip || isTimeout) {
isSkip = true;
this.retryCount--;
proxy.pdfViewer.fireAjaxRequestFailed(status, proxy.httpRequest.statusText, jsonObj.action, true);
proxy.send(jsonObj);
}
return isSkip;
};
AjaxHandler.prototype.sendRequest = function (jsonObj) {
this.httpRequest.open(this.type, this.url, this.mode);
this.httpRequest.withCredentials = this.pdfViewer.ajaxRequestSettings.withCredentials;
this.httpRequest.setRequestHeader('Content-Type', this.contentType);
jsonObj = this.addExtraData(jsonObj);
this.setCustomAjaxHeaders();
Eif (this.responseType !== null) {
this.httpRequest.responseType = this.responseType;
}
this.httpRequest.send(JSON.stringify(jsonObj));
};
AjaxHandler.prototype.addExtraData = function (jsonObject) {
this.pdfViewer.viewerBase.ajaxData = '';
this.pdfViewer.fireAjaxRequestInitiate(jsonObject);
Iif (this.pdfViewer.viewerBase.ajaxData && this.pdfViewer.viewerBase.ajaxData !== '') {
jsonObject = this.pdfViewer.viewerBase.ajaxData;
}
return jsonObject;
};
AjaxHandler.prototype.stateChange = function (proxy) {
var status = proxy.httpRequest.status;
var statusString = status.toString().split('')[0];
if (proxy.httpRequest.readyState === 4 && status === 200) {
var data = void 0;
Eif (this.responseType !== null) {
data = proxy.httpRequest.response;
}
else {
data = proxy.httpRequest.responseText;
}
var result = {
name: 'onSuccess',
data: data,
readyState: proxy.httpRequest.readyState,
status: proxy.httpRequest.status
};
proxy.successHandler(result);
}
else Iif (proxy.httpRequest.readyState === 4 && (statusString === '4' || statusString === '5')) {
var result = {
name: 'onFailure',
status: proxy.httpRequest.status,
statusText: proxy.httpRequest.statusText
};
proxy.failureHandler(result);
}
};
AjaxHandler.prototype.error = function (proxy) {
var result = {
name: 'onError',
status: this.httpRequest.status,
statusText: this.httpRequest.statusText
};
proxy.errorHandler(result);
};
AjaxHandler.prototype.successHandler = function (response) {
Eif (this.onSuccess) {
this.onSuccess(response);
}
return response;
};
AjaxHandler.prototype.failureHandler = function (response) {
if (this.onFailure) {
this.onFailure(response);
}
return response;
};
AjaxHandler.prototype.errorHandler = function (response) {
if (this.onError) {
this.onError(response);
}
return response;
};
AjaxHandler.prototype.setCustomAjaxHeaders = function () {
for (var i = 0; i < this.pdfViewer.ajaxRequestSettings.ajaxHeaders.length; i++) {
this.httpRequest.setRequestHeader(this.pdfViewer.ajaxRequestSettings.ajaxHeaders[parseInt(i.toString(), 10)].headerName, this.pdfViewer.ajaxRequestSettings.ajaxHeaders[parseInt(i.toString(), 10)].headerValue);
}
};
return AjaxHandler;
}());
exports.AjaxHandler = AjaxHandler;
});
|