all files / workbook/workers/ save-worker.js

16.36% Statements 9/55
0% Branches 0/30
27.27% Functions 3/11
16.36% Lines 9/55
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   883×                                                                                                                                                                              
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SaveWorker = (function () {
        function SaveWorker(parent) {
            this.parent = parent;
        }
        SaveWorker.prototype.processSheet = function (sheet, sheetIndex) {
            var parsedSheet = JSON.parse(sheet, function (key, value) {
                if ((Array.isArray(value) || typeof value === 'string') && !value.length) {
                    return undefined;
                }
                return value;
            });
            return [sheetIndex, parsedSheet];
        };
        SaveWorker.prototype.processSave = function (saveJSON, saveSettings, customParams, pdfLayoutSettings, successCallBack) {
            var _this = this;
            var formData = new FormData();
            var i;
            var keys = Object.keys(saveSettings);
            formData.append('JSONData', JSON.stringify(saveJSON));
            for (i = 0; i < keys.length; i++) {
                formData.append(keys[i], saveSettings[keys[i]]);
            }
            keys = Object.keys(customParams);
            for (i = 0; i < keys.length; i++) {
                formData.append(keys[i], customParams[keys[i]]);
            }
            formData.append('pdfLayoutSettings', JSON.stringify(pdfLayoutSettings));
            fetch(saveSettings.url, { method: 'POST', body: formData })
                .then(function (response) {
                if (response.ok) {
                    return response.blob();
                }
                else {
                    return Promise.reject({
                        message: response.statusText
                    });
                }
            })
                .then(function (data) {
                new Promise(function (resolve) {
                    var reader = new FileReader();
                    reader.onload = function () {
                        var result = reader.result.toString();
                        var saveAsHtml = customParams['customParams'] && typeof customParams['customParams'] === 'string' &&
                            customParams['customParams'].toLowerCase() === 'saveashtml';
                        if (result.indexOf('data:text/plain;base64,') > -1 || (!saveAsHtml && result.indexOf('data:text/html;base64,') > -1) ||
                            result.indexOf('data:application/json;base64,') > -1) {
                            var str = void 0;
                            result = result.replace('data:text/plain;base64,', '');
                            result = result.replace('data:text/html;base64,', '');
                            if (result.indexOf('data:application/json;base64,') > -1) {
                                result = result.replace('data:application/json;base64,', '');
                                str = atob(result).split('.');
                            }
                            else {
                                str = atob(result).split(/(\r\n|\n|\r)/gm);
                            }
                            if (str.length) {
                                var text = str[0].length > 1 && str[0][0] === '"' ? str[0].split('"')[1] + '.' : str[0];
                                if (successCallBack) {
                                    successCallBack.apply(_this, [{ dialog: text }]);
                                }
                                else {
                                    postMessage({ dialog: text });
                                }
                            }
                        }
                        else {
                            if (successCallBack) {
                                successCallBack.apply(_this, [data]);
                            }
                            else {
                                postMessage(data);
                            }
                        }
                        resolve(reader.result);
                    };
                    reader.readAsDataURL(data);
                });
            })
                .catch(function (error) {
                if (successCallBack) {
                    successCallBack.apply(_this, [{ error: error.message }]);
                }
                else {
                    postMessage({ error: error.message });
                }
            });
        };
        return SaveWorker;
    }());
    exports.SaveWorker = SaveWorker;
});