UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

175 lines (168 loc) 6.73 kB
/** * DevExtreme (cjs/exporter/jspdf/common/rows_generator.js) * Version: 24.2.6 * Build date: Mon Mar 17 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; exports.generateRowsInfo = generateRowsInfo; exports.getBaseTableStyle = getBaseTableStyle; var _type = require("../../../core/utils/type"); var _date = _interopRequireDefault(require("../../../common/core/localization/date")); var _number = _interopRequireDefault(require("../../../common/core/localization/number")); var _pdf_utils = require("./pdf_utils"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } const defaultStyles = { base: { font: { size: 10 }, borderWidth: .5, borderColor: "#979797" }, header: { textColor: "#979797" }, group: {}, data: {}, groupFooter: {}, totalFooter: {} }; function generateRowsInfo(doc, dataProvider, dataGrid, headerBackgroundColor) { const result = []; const rowsCount = dataProvider.getRowsCount(); const wordWrapEnabled = !!dataGrid.option("wordWrapEnabled"); const rtlEnabled = !!dataGrid.option("rtlEnabled"); const columns = dataProvider.getColumns(); const styles = dataProvider.getStyles(); for (let rowIndex = 0; rowIndex < rowsCount; rowIndex++) { const rowType = dataProvider.getCellData(rowIndex, 0, true).cellSourceData.rowType; let indentLevel = "header" !== rowType ? dataProvider.getGroupLevel(rowIndex) : 0; const previousRow = result[rowIndex - 1]; if ("groupFooter" === rowType && "groupFooter" === (null === previousRow || void 0 === previousRow ? void 0 : previousRow.rowType)) { indentLevel = previousRow.indentLevel - 1 } result.push({ rowType: rowType, indentLevel: indentLevel, cells: generateRowCells({ doc: doc, dataProvider: dataProvider, rowIndex: rowIndex, wordWrapEnabled: wordWrapEnabled, columns: columns, styles: styles, rowType: rowType, backgroundColor: "header" === rowType ? headerBackgroundColor : void 0, rtlEnabled: rtlEnabled }), rowIndex: rowIndex }) } return result } function generateRowCells(_ref) { let { doc: doc, dataProvider: dataProvider, rowIndex: rowIndex, wordWrapEnabled: wordWrapEnabled, columns: columns, styles: styles, rowType: rowType, backgroundColor: backgroundColor, rtlEnabled: rtlEnabled } = _ref; const result = []; for (let cellIndex = 0; cellIndex < columns.length; cellIndex++) { const cellData = dataProvider.getCellData(rowIndex, cellIndex, true); const cellStyle = styles[dataProvider.getStyleId(rowIndex, cellIndex)]; const style = getPdfCellStyle(columns[cellIndex], rowType, cellStyle); const defaultAlignment = rtlEnabled ? "right" : "left"; const paddingValue = (0, _pdf_utils.toPdfUnit)(doc, 5); const pdfCell = { text: getFormattedValue(cellData.value, cellStyle.format), verticalAlign: "middle", horizontalAlign: style.alignment ?? defaultAlignment, wordWrapEnabled: wordWrapEnabled, backgroundColor: backgroundColor, padding: { top: paddingValue, right: paddingValue, bottom: paddingValue, left: paddingValue }, _rect: {}, _internalTextOptions: {} }; if (rtlEnabled) { pdfCell._internalTextOptions.isInputVisual = false; pdfCell._internalTextOptions.isOutputVisual = true; pdfCell._internalTextOptions.isInputRtl = true; pdfCell._internalTextOptions.isOutputRtl = false } const cellInfo = { gridCell: cellData.cellSourceData, pdfCell: Object.assign({}, pdfCell, style) }; if ("header" === rowType) { const cellMerging = dataProvider.getCellMerging(rowIndex, cellIndex); if (cellMerging && cellMerging.rowspan > 0) { cellInfo.rowSpan = cellMerging.rowspan } if (cellMerging && cellMerging.colspan > 0) { cellInfo.colSpan = cellMerging.colspan } } else if ("group" === rowType) { const drawLeftBorderField = rtlEnabled ? "drawRightBorder" : "drawLeftBorder"; const drawRightBorderField = rtlEnabled ? "drawLeftBorder" : "drawRightBorder"; cellInfo.pdfCell[drawLeftBorderField] = 0 === cellIndex; cellInfo.pdfCell[drawRightBorderField] = cellIndex === columns.length - 1; if (cellIndex > 0) { const isEmptyCellsExceptFirst = result.slice(1).reduce(((accumulate, cellInfo) => accumulate && !(0, _type.isDefined)(cellInfo.pdfCell.text)), true); if (!(0, _type.isDefined)(cellInfo.pdfCell.text) && isEmptyCellsExceptFirst) { result[0].pdfCell[drawRightBorderField] = true; for (let i = 0; i < result.length; i++) { result[i].colSpan = result.length } cellInfo.colSpan = result.length } } } result.push(cellInfo) } return result } function getBaseTableStyle() { return defaultStyles.base } function getPdfCellStyle(column, rowType, cellStyle) { const styles = Object.assign({}, defaultStyles.base, defaultStyles[rowType]); const alignment = "header" === rowType ? column.alignment : cellStyle.alignment; if (alignment) { styles.alignment = alignment } if (cellStyle.bold && "header" !== rowType) { styles.font = Object.assign({}, styles.font, { style: "bold" }) } return styles } function getFormattedValue(value, format) { if ((0, _type.isDefined)(format)) { if ((0, _type.isDate)(value)) { return _date.default.format(value, format) } if ((0, _type.isNumeric)(value)) { return _number.default.format(value, format) } } return null === value || void 0 === value ? void 0 : value.toString() }