all files / document-editor/base/ ajax-helper.js

85.07% Statements 57/67
75% Branches 24/32
71.43% Functions 10/14
85.07% Lines 57/67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114   115×   22× 22× 22× 22× 22× 22× 22× 22×   22× 67×   22×     22×           22×     22× 22×   22× 20×   22× 22×     22× 22×   67×                     65× 20×           20×                       20× 19×   20×             22×            
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var XmlHttpRequestHandler = (function () {
        function XmlHttpRequestHandler() {
            this.mode = true;
        }
        XmlHttpRequestHandler.prototype.send = function (jsonObject, httpRequestEventArgs, isAsync) {
            var _this = this;
            this.xmlHttpRequest = new XMLHttpRequest();
            var timeout = 0;
            isAsync = ej2_base_1.isNullOrUndefined(isAsync) ? true : isAsync;
            Eif (!ej2_base_1.isNullOrUndefined(httpRequestEventArgs)) {
                this.xmlHttpRequest.withCredentials = httpRequestEventArgs.withCredentials;
                timeout = (httpRequestEventArgs.timeout >= 0 ? httpRequestEventArgs.timeout : 0);
                this.customHeaders = httpRequestEventArgs.headers;
            }
            this.xmlHttpRequest.onreadystatechange = function () {
                _this.stateChange(_this);
            };
            this.xmlHttpRequest.onerror = function () {
                _this.error(_this);
            };
            Iif (!this.mode) {
                setTimeout(function () {
                    _this.sendRequest(jsonObject, timeout, isAsync);
                });
            }
            else {
                this.sendRequest(jsonObject, timeout, isAsync);
            }
        };
        XmlHttpRequestHandler.prototype.sendRequest = function (jsonObj, timeout, isAsync) {
            this.xmlHttpRequest.open('POST', this.url, isAsync);
            if (isAsync) {
                this.xmlHttpRequest.timeout = timeout;
            }
            if (this.contentType) {
                this.xmlHttpRequest.setRequestHeader('Content-Type', this.contentType);
            }
            this.setCustomAjaxHeaders();
            Iif (this.responseType) {
                this.xmlHttpRequest.responseType = this.responseType;
            }
            var data = jsonObj instanceof FormData ? jsonObj : JSON.stringify(jsonObj);
            this.xmlHttpRequest.send(data);
        };
        XmlHttpRequestHandler.prototype.stateChange = function (proxyReq) {
            if (proxyReq.xmlHttpRequest.readyState === 4 && proxyReq.xmlHttpRequest.status === 200) {
                var data = void 0;
                Iif (this.responseType) {
                    data = proxyReq.xmlHttpRequest.response;
                }
                else {
                    data = proxyReq.xmlHttpRequest.responseText;
                }
                var result = {
                    name: 'onSuccess',
                    data: data,
                    readyState: proxyReq.xmlHttpRequest.readyState,
                    status: proxyReq.xmlHttpRequest.status
                };
                proxyReq.successHandler(result);
            }
            else if (proxyReq.xmlHttpRequest.readyState === 4 && !(proxyReq.xmlHttpRequest.status === 200)) {
                var result = {
                    name: 'onFailure',
                    status: proxyReq.xmlHttpRequest.status,
                    statusText: proxyReq.xmlHttpRequest.statusText,
                    url: proxyReq.url
                };
                proxyReq.failureHandler(result);
            }
        };
        XmlHttpRequestHandler.prototype.error = function (proxyReq) {
            var result = {
                name: 'onError',
                status: this.xmlHttpRequest.status,
                statusText: this.xmlHttpRequest.statusText
            };
            proxyReq.errorHandler(result);
        };
        XmlHttpRequestHandler.prototype.successHandler = function (response) {
            Eif (this.onSuccess) {
                this.onSuccess(response);
            }
            return response;
        };
        XmlHttpRequestHandler.prototype.failureHandler = function (response) {
            if (this.onFailure) {
                this.onFailure(response);
            }
            return response;
        };
        XmlHttpRequestHandler.prototype.errorHandler = function (response) {
            if (this.onError) {
                this.onError(response);
            }
            return response;
        };
        XmlHttpRequestHandler.prototype.setCustomAjaxHeaders = function () {
            for (var i = 0; i < this.customHeaders.length; i++) {
                var header = this.customHeaders[i];
                for (var _i = 0, _a = Object.keys(header); _i < _a.length; _i++) {
                    var key = _a[_i];
                    this.xmlHttpRequest.setRequestHeader(key, header[key]);
                }
            }
        };
        return XmlHttpRequestHandler;
    }());
    exports.XmlHttpRequestHandler = XmlHttpRequestHandler;
});