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;
});
|