@visactor/vtable
Version:
canvas table width high performance
110 lines (105 loc) • 6.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.CellContent = void 0;
const vrender_1 = require("./../../vrender"), group_1 = require("../graphic/group"), icon_1 = require("../graphic/icon");
class CellContent extends group_1.Group {
constructor(params) {
super(params), this._autoWidth = !1, this._autoHeight = !1, this._leftGroup = new group_1.Group({
pickable: !1,
fill: !1,
stroke: !1
}), this._leftGroup.role = "content-left", this._rightGroup = new group_1.Group({
pickable: !1,
fill: !1,
stroke: !1
}), this._rightGroup.role = "content-right", this._centerGroup = new group_1.Group({
pickable: !1,
fill: !1,
stroke: !1
}), this._centerGroup.role = "content-center", this.appendChild(this._leftGroup),
this.appendChild(this._rightGroup), this.appendChild(this._centerGroup);
}
setCellContentOption(option) {
this._autoWidth = option.autoWidth, this._autoHeight = option.autoHeight, this._cellWidth = option.cellWidth,
this._originCellWidth = option.cellWidth, this._cellHeight = option.cellHeight,
this._align = option.align, this._baseline = option.baseline;
}
addLeftOccupyingIcon(icon) {
var _a, _b, _c;
icon.setAttribute("x", this._leftGroup.width + (null !== (_a = icon.attribute.marginLeft) && void 0 !== _a ? _a : 0)),
this._leftGroup.appendChild(icon), this._leftGroup.setDeltaWidth((null !== (_b = icon.attribute.marginLeft) && void 0 !== _b ? _b : 0) + (null !== (_c = icon.attribute.marginRight) && void 0 !== _c ? _c : 0) + icon.attribute.width);
}
addRightOccupyingIcon(icon) {
var _a, _b, _c;
icon.setAttribute("x", this._rightGroup.width + (null !== (_a = icon.attribute.marginLeft) && void 0 !== _a ? _a : 0)),
this._rightGroup.appendChild(icon), this._rightGroup.setDeltaWidth((null !== (_b = icon.attribute.marginLeft) && void 0 !== _b ? _b : 0) + (null !== (_c = icon.attribute.marginRight) && void 0 !== _c ? _c : 0) + icon.attribute.width);
}
addContent(content) {
const lastChild = this._centerGroup.lastChild;
lastChild && content.setAttributes({
x: lastChild.attribute.x + lastChild.AABBBounds.width()
}), this._centerGroup.appendChild(content), content instanceof vrender_1.Text && (this._centerGroup.setAttribute("pickable", !1),
content.setAttribute("textAlign", "left"));
}
layout() {
let leftOccupyingWidth = this._leftGroup.width;
leftOccupyingWidth === 1 / 0 && (leftOccupyingWidth = 0);
let rightOccupyingWidth = this._rightGroup.width;
if (rightOccupyingWidth === -1 / 0 && (rightOccupyingWidth = 0), this._autoWidth) {
const centerWidth = this._centerGroup.width;
this._cellWidth = leftOccupyingWidth + rightOccupyingWidth + centerWidth, this.setAttribute("width", this._cellWidth);
} else {
const contentWidth = this._cellWidth - leftOccupyingWidth - rightOccupyingWidth;
this.updateCenterLayout(contentWidth);
const centerWidth = this._centerGroup.width;
this._cellWidth = leftOccupyingWidth + rightOccupyingWidth + centerWidth, this.setAttribute("width", this._cellWidth);
}
this.updateHorizontalPos();
const leftOccupyingHeight = this._leftGroup.height, rightOccupyingHeight = this._rightGroup.height, centerHeight = this._centerGroup.height;
this._cellHeight = Math.max(leftOccupyingHeight, rightOccupyingHeight, centerHeight),
this.setAttribute("height", this._cellHeight), this.updateVerticalPos();
}
updateCenterLayout(contentWidth) {
let textWidth = contentWidth;
this._centerGroup.forEachChildren((child => {
(child instanceof icon_1.Icon || child instanceof icon_1.TextIcon) && (textWidth -= child.AABBBounds.width());
}));
const text = this._centerGroup.getChildByName("text");
text instanceof vrender_1.RichText ? text.setAttribute("width", textWidth) : text instanceof vrender_1.Text && text.setAttribute("maxLineWidth", textWidth);
let x = 0;
this._centerGroup.forEachChildren((child => {
child.setAttribute("x", x), x += child.AABBBounds.width();
}));
}
updateHorizontalPos() {
this._leftGroup.setAttribute("x", 0), this._rightGroup.setAttribute("x", this._cellWidth - this._rightGroup.width),
this._centerGroup.setAttribute("x", this._leftGroup.width), "left" === this._align || "start" === this._align ? this.setAttribute("dx", 0) : "center" === this._align ? this.setAttribute("dx", -this.attribute.width / 2) : "right" !== this._align && "end" !== this._align || this.setAttribute("dx", -this.attribute.width);
}
updateVerticalPos() {
"top" === this._baseline ? (this._leftGroup.setAttribute("y", 0), this._rightGroup.setAttribute("y", 0),
this._centerGroup.setAttribute("y", 0)) : "middle" === this._baseline ? (this._leftGroup.forEachChildren((icon => {
icon.setAttribute("y", (this._leftGroup.height - icon.AABBBounds.height()) / 2);
})), this._rightGroup.forEachChildren((icon => {
icon.setAttribute("y", (this._rightGroup.height - icon.AABBBounds.height()) / 2);
})), this._leftGroup.setAttribute("y", this._cellHeight / 2 - this._leftGroup.height / 2),
this._rightGroup.setAttribute("y", this._cellHeight / 2 - this._rightGroup.height / 2),
this._centerGroup.setAttribute("y", this._cellHeight / 2 - this._centerGroup.height / 2)) : "bottom" === this._baseline && (this._leftGroup.forEachChildren((icon => {
icon.setAttribute("y", this._leftGroup.height - icon.AABBBounds.height());
})), this._rightGroup.forEachChildren((icon => {
icon.setAttribute("y", this._rightGroup.height - icon.AABBBounds.height());
})), this._leftGroup.setAttribute("y", this._cellHeight - this._leftGroup.height),
this._rightGroup.setAttribute("y", this._cellHeight - this._rightGroup.height),
this._centerGroup.setAttribute("y", this._cellHeight - this._centerGroup.height));
}
updateWidth(width) {
this._cellWidth = width, this._originCellWidth = width, this.layout();
}
updateHeight(height) {
this._cellHeight = height, this._cellWidth = this._originCellWidth;
const textMark = this.getChildByName("text", !0);
textMark && textMark.setAttribute("heightLimit", height), this.layout();
}
}
exports.CellContent = CellContent;
//# sourceMappingURL=cell-content.js.map