UNPKG

e-virt-table

Version:

A powerful data table based on canvas. You can use it as data grid、Microsoft Excel or Google sheets. It supports virtual scroll、cell edit etc.

172 lines • 5.49 kB
import Cell from './Cell'; export default class Row { constructor(ctx, rowIndex, x = 0, y = 0, width = 0, height = 0, data, rowType = 'body') { Object.defineProperty(this, "ctx", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "x", { enumerable: true, configurable: true, writable: true, value: 0 }); Object.defineProperty(this, "y", { enumerable: true, configurable: true, writable: true, value: 0 }); Object.defineProperty(this, "width", { enumerable: true, configurable: true, writable: true, value: 0 }); Object.defineProperty(this, "height", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "cells", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(this, "fixedCells", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(this, "noFixedCells", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(this, "calculatedHeightCells", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(this, "rowIndex", { enumerable: true, configurable: true, writable: true, value: 0 }); Object.defineProperty(this, "rowKey", { enumerable: true, configurable: true, writable: true, value: '' }); Object.defineProperty(this, "rowType", { enumerable: true, configurable: true, writable: true, value: 'body' }); Object.defineProperty(this, "data", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "calculatedHeight", { enumerable: true, configurable: true, writable: true, value: -1 }); this.ctx = ctx; this.x = x; this.y = y; this.width = width; this.height = height; this.rowIndex = rowIndex; this.rowKey = ctx.database.getRowKeyForRowIndex(rowIndex) || ''; this.rowType = rowType; this.data = data; this.update(); } update() { const { header } = this.ctx; const cells = []; const fixedCells = []; const noFixedCells = []; const calculatedHeightCells = []; header.renderLeafCellHeaders.forEach((header) => { const cell = new Cell(this.ctx, this.rowIndex, header.colIndex, header.x, this.y, header.width, this.height, header.column, this.data, this.rowType); if (cell.fixed) { fixedCells.push(cell); } else { noFixedCells.push(cell); } cells.push(cell); if (cell.autoRowHeight) { calculatedHeightCells.push(cell); } }); this.cells = cells; this.calculatedHeightCells = calculatedHeightCells; this.fixedCells = fixedCells; this.noFixedCells = noFixedCells; } updateCalculatedHeight() { const heights = this.calculatedHeightCells.map((cell) => { const calculatedHeight = cell.getAutoHeight(); const { key, height: curRowMaxHeight = -1 } = this.ctx.database.getMaxRowHeightItem(this.rowKey) || {}; // 设置最大值 if (calculatedHeight > curRowMaxHeight) { this.ctx.database.setMaxRowHeightItem(this.rowKey, cell.key, calculatedHeight); } else if (cell.key === key && calculatedHeight < curRowMaxHeight) { // 如果计算高度小于当前最大值,则设置为当前最大值 this.ctx.database.setMaxRowHeightItem(this.rowKey, cell.key, calculatedHeight); } return calculatedHeight; }); // 获取最高高度 this.calculatedHeight = heights.length ? Math.max(...heights) : -1; return this.calculatedHeight; } drawCenter() { this.noFixedCells.forEach((cell) => { cell.draw(); }); } drawFixed() { this.fixedCells.forEach((cell) => { cell.draw(); }); } drawContainer() { this.noFixedCells.forEach((cell) => { cell.drawContainer(); }); } drawFixedContainer() { this.fixedCells.forEach((cell) => { cell.drawContainer(); }); } drawHorizontalBorder() { this.noFixedCells.forEach((cell) => { cell.drawHorizontalBorder(); }); } drawFixedHorizontalBorder() { this.fixedCells.forEach((cell) => { cell.drawHorizontalBorder(); }); } } //# sourceMappingURL=Row.js.map