epn-ui
Version:
Дизайн система кабинета ВМ
117 lines (114 loc) • 5.46 kB
JavaScript
import { __spreadArray } from '../../../../_virtual/_tslib.js';
import get from 'lodash/get';
import ReactDOMServer from 'react-dom/server';
import { DEFAULT_EMPTY_SYMBOL } from '../../config.js';
var ExportFormatter = (function () {
function ExportFormatter(columns, data) {
this.columns = this.dataIsValid(columns) ? __spreadArray([], columns, true) : [];
this.data = this.dataIsValid(data) ? __spreadArray([], data, true) : [];
}
ExportFormatter.prototype.format = function (format, name) {
if (!this.formatIsSupported(format)) {
console.error("".concat(format, " format not supported"));
return null;
}
if (!this.dataIsValid(this.data)) {
console.error("Table data is invalid.");
return null;
}
if (!this.dataIsValid(this.columns)) {
console.error("Table columns is invalid.");
return null;
}
var method = "format".concat(format.toUpperCase());
return this[method](name);
};
ExportFormatter.prototype.formatIsSupported = function (fileType) {
var method = "format".concat(fileType.toUpperCase());
return !!this[method];
};
ExportFormatter.prototype.dataIsValid = function (data) {
if (!Array.isArray(data))
return false;
if (!data.length)
return false;
return true;
};
ExportFormatter.prototype.formatCSV = function (name) {
var _this = this;
var filename = this.getFileName(name);
var headers = this.columns.map(function (item) { return _this.getColumnTitle(item); });
var dataSource = this.prepareData();
var options = {
filename: filename,
title: filename,
headers: headers,
};
return {
options: options,
dataSource: dataSource,
};
};
ExportFormatter.prototype.prepareData = function () {
var _this = this;
var formatedData = [];
var headers = this.columns.map(function (item) { return item.dataIndex; });
this.data.forEach(function (dataItem, index) {
var tempItem = {};
headers.forEach(function (dataIndex) {
var columnItem = _this.columns.find(function (column) { return column.dataIndex === dataIndex; });
if (columnItem !== undefined &&
typeof dataIndex === 'string' &&
dataIndex.length !== 0) {
if (tempItem[dataIndex] === undefined)
tempItem[dataIndex] = null;
var value = dataItem[dataIndex];
if (columnItem.render) {
var node = columnItem.render(dataItem[dataIndex], dataItem, index);
var htmlString = ReactDOMServer.renderToString(node);
value = htmlString.replace(/<\/?[^>]+(>|$)/g, ' ');
}
tempItem[dataIndex] = value || DEFAULT_EMPTY_SYMBOL;
}
if (columnItem !== undefined &&
Array.isArray(dataIndex) &&
dataIndex.length !== 0) {
var fieldName = dataIndex[dataIndex.length - 1];
if (tempItem[fieldName] === undefined)
tempItem[fieldName] = null;
var value = get(dataItem, dataIndex);
if (columnItem.render) {
var node = columnItem.render(value, dataItem, index);
var htmlString = ReactDOMServer.renderToString(node);
value = htmlString.replace(/<\/?[^>]+(>|$)/g, ' ');
}
tempItem[fieldName] = value || DEFAULT_EMPTY_SYMBOL;
}
});
formatedData.push(tempItem);
});
return formatedData;
};
ExportFormatter.prototype.getColumnTitle = function (column) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
if (typeof column.title === 'undefined')
return '';
if (typeof column.title === 'string')
return column.title;
if ((_c = (_b = (_a = column) === null || _a === void 0 ? void 0 : _a.title) === null || _b === void 0 ? void 0 : _b.props) === null || _c === void 0 ? void 0 : _c.title)
return (_f = (_e = (_d = column) === null || _d === void 0 ? void 0 : _d.title) === null || _e === void 0 ? void 0 : _e.props) === null || _f === void 0 ? void 0 : _f.title;
return (_l = (_k = (_j = (_h = (_g = column) === null || _g === void 0 ? void 0 : _g.title) === null || _h === void 0 ? void 0 : _h.props) === null || _j === void 0 ? void 0 : _j.children) === null || _k === void 0 ? void 0 : _k.props) === null || _l === void 0 ? void 0 : _l.children;
};
ExportFormatter.prototype.getFileName = function (name) {
var date = new Date().getMilliseconds();
var defaultName = "export-".concat(date);
if (!name)
return defaultName;
var fileName = "".concat(name, "-").concat(date);
if (fileName.length >= 30)
return defaultName;
return fileName;
};
return ExportFormatter;
}());
export { ExportFormatter as default };