@true-directive/base
Version:
The set of base classes for the TrueDirective Grid
146 lines (145 loc) • 5.99 kB
JavaScript
import { GridLayout } from '../classes/grid-layout.class';
import { SelectionMode } from '../classes/enums';
import { Utils } from '../common/utils.class';
var GridExporter = /** @class */ (function () {
function GridExporter(layouts, selection, selectionMode, resultRows, valueFormatter) {
this.layouts = layouts;
this.selection = selection;
this.selectionMode = selectionMode;
this.resultRows = resultRows;
this.valueFormatter = valueFormatter;
this.minField = '';
this.maxField = '';
this.minColumnIndex = null;
this.maxColumnIndex = null;
}
GridExporter.prototype.min = function (a, b) {
return a > b ? b : a;
};
GridExporter.prototype.max = function (a, b) {
return a < b ? b : a;
};
GridExporter.prototype.checkField = function (s) {
var i = GridLayout.columnIndex(this.layouts, s);
if (this.minColumnIndex === null || this.minColumnIndex > i) {
this.minField = s;
this.minColumnIndex = i;
}
if (this.maxColumnIndex === null || this.maxColumnIndex < i) {
this.maxField = s;
this.maxColumnIndex = i;
}
return i;
};
GridExporter.prototype.fieldName = function (i) {
return this.cols.find(function (c) { return c.idx === i; });
};
GridExporter.prototype.getRow = function (i) {
while (i >= this.result.length) {
this.result.push({});
}
return this.result[i];
};
GridExporter.prototype.getFormattedRow = function (i) {
while (i >= this.formattedResult.length) {
this.formattedResult.push({});
}
return this.formattedResult[i];
};
// -- Get the data to export
GridExporter.prototype.getData = function () {
var _this = this;
this.result = [];
this.formattedResult = [];
if (this.selection.ranges.length === 0) {
return this.result;
}
// Reset temp data
this.cols = [];
this.ranges = [];
// Sort ranges by first row index
var ranges = this.selection.ranges.sort(function (r1, r2) {
return r1.fromRow > r2.fromRow ? 1 : -1;
});
// Persist first and last field indices
this.minField = '';
this.minColumnIndex = null;
this.maxField = '';
this.maxColumnIndex = null;
ranges.forEach(function (r) {
var fromField = r.fromField;
var toField = r.toField;
if (fromField === toField && r.fromRow === r.toRow) {
if (_this.selectionMode === SelectionMode.ROW || _this.selectionMode === SelectionMode.ROW_AND_RANGE) {
fromField = GridLayout.firstColumn(_this.layouts).fieldName;
toField = GridLayout.lastColumn(_this.layouts).fieldName;
}
}
var a = _this.checkField(fromField);
var b = _this.checkField(toField);
_this.ranges.push({
range: r,
fromFieldIndex: _this.min(a, b),
toFieldIndex: _this.max(a, b)
});
});
// Create column's list
for (var i = this.minColumnIndex; i <= this.maxColumnIndex; i++) {
this.cols.push({
idx: i,
column: GridLayout.columnByIndex(this.layouts, i)
});
}
var i0 = ranges[0].fromRow;
this.ranges.forEach(function (r) {
for (var j = r.range.fromRow; j <= r.range.toRow; j++) {
var row = _this.getRow(j - i0);
var formattedRow = _this.getFormattedRow(j - i0);
var srcRow = _this.resultRows[j];
for (var k = r.fromFieldIndex; k <= r.toFieldIndex; k++) {
// Get and format value
var column = _this.fieldName(k).column;
var value = srcRow[column.fieldName];
var formattedValue = _this.valueFormatter.displayedValue(column, value, srcRow);
var plainValue = Utils.htmlToPlaintext(formattedValue);
row[column.fieldName] = value;
formattedRow[column.fieldName] = plainValue;
}
}
});
return this.result;
};
GridExporter.dataToExport = function (layouts, selection, selectionMode, resultRows, valueFormatter) {
var e = new GridExporter(layouts, selection, selectionMode, resultRows, valueFormatter);
e.getData();
return e;
};
GridExporter.prototype.toString = function (withHeaders, columnSeparator, encloseDataIntoDoubleQuotes) {
var _this = this;
if (withHeaders === void 0) { withHeaders = false; }
if (columnSeparator === void 0) { columnSeparator = ','; }
if (encloseDataIntoDoubleQuotes === void 0) { encloseDataIntoDoubleQuotes = false; }
var txt = '';
if (withHeaders) {
txt += this.cols.map(function (c) {
var v = c.column.caption;
if (encloseDataIntoDoubleQuotes) {
v = "\"" + v.replace("\"", "\"\"") + "\"";
}
return v;
}).join(columnSeparator) + "\n";
}
txt += this.formattedResult.map(function (row) {
return _this.cols.map(function (c) {
var v = row[c.column.fieldName] !== undefined ? row[c.column.fieldName] : '';
if (encloseDataIntoDoubleQuotes) {
v = "\"" + v.replace("\"", "\"\"") + "\"";
}
return v;
}).join(columnSeparator);
}).join('\n');
return txt;
};
return GridExporter;
}());
export { GridExporter };