@visactor/vchart
Version:
charts lib based @visactor/VGrammar
238 lines (233 loc) • 12.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.LayoutItem = void 0;
const vutils_1 = require("@visactor/vutils"), space_1 = require("../util/space"), layout_1 = require("../constant/layout");
class LayoutItem {
getSpec() {
return this._spec || {};
}
getLayoutStartPoint() {
return this._layoutStartPoint;
}
get layoutRectLevelMap() {
return this._layoutRectLevelMap;
}
get minWidth() {
return this._minWidth;
}
set minWidth(v) {
this._minWidth = v;
}
get maxWidth() {
return this._maxWidth;
}
set maxWidth(v) {
this._maxWidth = v;
}
get minHeight() {
return this._minHeight;
}
set minHeight(v) {
this._minHeight = v;
}
get maxHeight() {
return this._maxHeight;
}
set maxHeight(v) {
this._maxHeight = v;
}
get lastComputeRect() {
return this._lastComputeRect;
}
getLastComputeOutBounds() {
return this._lastComputeOutBounds;
}
get layoutOrient() {
return this._layoutOrient;
}
set layoutOrient(v) {
this._layoutOrient = v;
}
get model() {
return this._model;
}
get type() {
return this._model.type;
}
get willLayoutTag() {
return this._willLayoutTag;
}
constructor(model, option) {
var _a;
this.layoutClip = !1, this.autoIndent = !1, this._layoutStartPoint = {
x: 0,
y: 0
}, this._layoutRect = {
width: 0,
height: 0
}, this._layoutRectLevelMap = {
width: layout_1.DEFAULT_LAYOUT_RECT_LEVEL,
height: layout_1.DEFAULT_LAYOUT_RECT_LEVEL
}, this._minWidth = null, this._maxWidth = null, this._minHeight = null, this._maxHeight = null,
this._lastComputeRect = null, this._lastComputeOutBounds = {
x1: 0,
x2: 0,
y1: 0,
y2: 0
}, this.getLayoutRect = () => this._layoutRect, this.layoutType = "normal", this._layoutOrient = "left",
this.layoutPaddingLeft = 0, this.layoutPaddingTop = 0, this.layoutPaddingRight = 0,
this.layoutPaddingBottom = 0, this.layoutOffsetX = 0, this.layoutOffsetY = 0, this.layoutLevel = layout_1.LayoutLevel.Region,
this._willLayoutTag = !0, this._model = model, this._option = option, this.layoutLevel = option.layoutLevel,
this.layoutType = option.layoutType, option.layoutOrient && (this.layoutOrient = option.layoutOrient),
this._spec = null === (_a = null == model ? void 0 : model.getSpec) || void 0 === _a ? void 0 : _a.call(model);
}
_setLayoutAttributeFromSpec(spec, chartViewRect) {
var _a, _b, _c, _d;
if (this._spec && !1 !== this._spec.visible) {
const padding = (0, space_1.normalizeLayoutPaddingSpec)(spec.padding);
let paddingValue = (0, space_1.calcPadding)(padding, chartViewRect, chartViewRect);
this._option.transformLayoutPadding && (paddingValue = this._option.transformLayoutPadding(paddingValue)),
this.layoutPaddingLeft = paddingValue.left, this.layoutPaddingRight = paddingValue.right,
this.layoutPaddingTop = paddingValue.top, this.layoutPaddingBottom = paddingValue.bottom,
this._minHeight = (0, vutils_1.isNil)(spec.minHeight) ? null !== (_a = this._minHeight) && void 0 !== _a ? _a : null : (0,
space_1.calcLayoutNumber)(spec.minHeight, chartViewRect.height, chartViewRect),
this._maxHeight = (0, vutils_1.isNil)(spec.maxHeight) ? null !== (_b = this._maxHeight) && void 0 !== _b ? _b : null : (0,
space_1.calcLayoutNumber)(spec.maxHeight, chartViewRect.height, chartViewRect),
this._minWidth = (0, vutils_1.isNil)(spec.minWidth) ? null !== (_c = this._minWidth) && void 0 !== _c ? _c : null : (0,
space_1.calcLayoutNumber)(spec.minWidth, chartViewRect.width, chartViewRect), this._maxWidth = (0,
vutils_1.isNil)(spec.maxWidth) ? null !== (_d = this._maxWidth) && void 0 !== _d ? _d : null : (0,
space_1.calcLayoutNumber)(spec.maxWidth, chartViewRect.width, chartViewRect), spec.width && this.setLayoutRect({
width: (0, space_1.calcLayoutNumber)(spec.width, chartViewRect.width, chartViewRect)
}, {
width: layout_1.USER_LAYOUT_RECT_LEVEL
}), spec.height && this.setLayoutRect({
height: (0, space_1.calcLayoutNumber)(spec.height, chartViewRect.height, chartViewRect)
}, {
height: layout_1.USER_LAYOUT_RECT_LEVEL
}), (0, vutils_1.isNil)(spec.offsetX) || (this.layoutOffsetX = (0, space_1.calcLayoutNumber)(spec.offsetX, chartViewRect.width, chartViewRect)),
(0, vutils_1.isNil)(spec.offsetY) || (this.layoutOffsetY = (0, space_1.calcLayoutNumber)(spec.offsetY, chartViewRect.height, chartViewRect)),
spec.alignSelf && (this.alignSelf = spec.alignSelf);
}
}
setAttrFromSpec(spec, chartViewRect) {
var _a, _b, _c, _d;
this._spec = spec, this.layoutType = null !== (_a = spec.layoutType) && void 0 !== _a ? _a : this.layoutType,
this.layoutLevel = null !== (_b = spec.layoutLevel) && void 0 !== _b ? _b : this.layoutLevel,
this.layoutOrient = null !== (_c = spec.orient) && void 0 !== _c ? _c : this.layoutOrient,
this._setLayoutAttributeFromSpec(spec, chartViewRect), this.layoutClip = null !== (_d = spec.clip) && void 0 !== _d ? _d : this.layoutClip;
}
onLayoutStart(layoutRect, viewRect) {
this._setLayoutAttributeFromSpec(this._spec, viewRect);
}
onLayoutEnd() {}
_getAbsoluteSpecValue(layoutRect) {
const result = {
top: null,
bottom: null,
left: null,
right: null
};
return [ "top", "bottom", "left", "right" ].forEach((k => {
(0, vutils_1.isNil)(this._spec[k]) || (result[k] = (0, space_1.calcLayoutNumber)(this._spec[k], "top" === k || "bottom" === k ? layoutRect.height : layoutRect.width, layoutRect));
})), result;
}
absoluteLayoutInRect(layoutRect) {
const {top: top, bottom: bottom, left: left, right: right} = this._getAbsoluteSpecValue(layoutRect), layoutSize = {
width: layoutRect.width - this.layoutPaddingLeft - this.layoutPaddingRight,
height: layoutRect.height - this.layoutPaddingTop - this.layoutPaddingBottom
};
(0, vutils_1.isNil)(left) || (layoutSize.width -= left), (0, vutils_1.isNil)(right) || (layoutSize.width -= right),
(0, vutils_1.isNil)(top) || (layoutSize.height -= top), (0, vutils_1.isNil)(bottom) || (layoutSize.height -= bottom),
this.setLayoutRect(layoutSize);
const {width: width, height: height} = this.computeBoundsInRect(this.getLayoutRect());
this.setLayoutRect({
width: width,
height: height
});
const pos = {
x: layoutRect.x,
y: layoutRect.y
};
!0 === this._spec.center ? (pos.x = layoutRect.x + .5 * layoutRect.width - .5 * width,
pos.y = layoutRect.y + .5 * layoutRect.height - .5 * height) : ((0, vutils_1.isNil)(left) ? (0,
vutils_1.isNil)(right) || (pos.x = layoutRect.x + layoutRect.width - this.layoutPaddingRight - right - width) : pos.x = layoutRect.x + left + this.layoutPaddingLeft,
(0, vutils_1.isNil)(top) ? (0, vutils_1.isNil)(bottom) || (pos.y = layoutRect.y + layoutRect.height - this.layoutPaddingBottom - bottom - height) : pos.y = layoutRect.y + top + this.layoutPaddingTop),
this.setLayoutStartPosition(pos);
}
setLayoutStartPosition(pos) {
var _a, _b;
this._option.transformLayoutPosition && (pos = this._option.transformLayoutPosition(pos)),
(0, vutils_1.isValidNumber)(pos.x) && (this._layoutStartPoint.x = pos.x), (0, vutils_1.isValidNumber)(pos.y) && (this._layoutStartPoint.y = pos.y),
null === (_b = (_a = this._model).afterSetLayoutStartPoint) || void 0 === _b || _b.call(_a, this._layoutStartPoint);
}
setLayoutRect({width: width, height: height}, levelMap) {
var _a, _b, _c, _d;
(0, vutils_1.isValidNumber)(width) && (null !== (_a = null == levelMap ? void 0 : levelMap.width) && void 0 !== _a ? _a : layout_1.DEFAULT_LAYOUT_RECT_LEVEL) >= this._layoutRectLevelMap.width && (this._layoutRect.width = width,
this._layoutRectLevelMap.width = null !== (_b = null == levelMap ? void 0 : levelMap.width) && void 0 !== _b ? _b : layout_1.DEFAULT_LAYOUT_RECT_LEVEL),
(0, vutils_1.isValidNumber)(height) && (null !== (_c = null == levelMap ? void 0 : levelMap.height) && void 0 !== _c ? _c : layout_1.DEFAULT_LAYOUT_RECT_LEVEL) >= this._layoutRectLevelMap.height && (this._layoutRect.height = height,
this._layoutRectLevelMap.height = null !== (_d = null == levelMap ? void 0 : levelMap.height) && void 0 !== _d ? _d : layout_1.DEFAULT_LAYOUT_RECT_LEVEL),
this.setRectInSpec(this._layoutRect);
}
getLayout() {
return {
x: this._layoutStartPoint.x,
y: this._layoutStartPoint.y,
width: this._layoutRect.width,
height: this._layoutRect.height
};
}
mergeLayoutRect({width: width, height: height}) {
const rect = {
width: width,
height: height
};
return this._layoutRectLevelMap.width > layout_1.DEFAULT_LAYOUT_RECT_LEVEL && (rect.width = this._layoutRect.width),
this._layoutRectLevelMap.height > layout_1.DEFAULT_LAYOUT_RECT_LEVEL && (rect.height = this._layoutRect.height),
rect;
}
getOrientPosAttribute() {
return "bottom" === this._layoutOrient || "top" === this._layoutOrient ? "x" : "y";
}
getOrientSizeAttribute() {
return "bottom" === this._layoutOrient || "top" === this._layoutOrient ? "width" : "height";
}
changeBoundsBySetting(bounds) {
return this._layoutRectLevelMap.width > layout_1.DEFAULT_LAYOUT_RECT_LEVEL && (bounds.x2 = bounds.x1 + this._layoutRect.width),
this._layoutRectLevelMap.height > layout_1.DEFAULT_LAYOUT_RECT_LEVEL && (bounds.y2 = bounds.y1 + this._layoutRect.height),
bounds.x1 -= this._layoutStartPoint.x, bounds.x2 -= this._layoutStartPoint.x, bounds.y1 -= this._layoutStartPoint.y,
bounds.y2 -= this._layoutStartPoint.y, bounds;
}
setRectInSpec(rect) {
const result = Object.assign({}, rect);
return this._layoutRectLevelMap.width < layout_1.USER_LAYOUT_RECT_LEVEL ? ((0, vutils_1.isNil)(this._minWidth) || (result.width = Math.max(result.width, this._minWidth)),
(0, vutils_1.isNil)(this._maxWidth) || (result.width = Math.min(result.width, this._maxWidth))) : result.width = this._layoutRect.width,
this._layoutRectLevelMap.height < layout_1.USER_LAYOUT_RECT_LEVEL ? ((0, vutils_1.isNil)(this._minHeight) || (result.height = Math.max(result.height, this._minHeight)),
(0, vutils_1.isNil)(this._maxHeight) || (result.height = Math.min(result.height, this._maxHeight))) : result.height = this._layoutRect.height,
result;
}
computeBoundsInRect(rect) {
if (this._lastComputeRect = rect, !("region-relative" !== this.layoutType && "region-relative-overlap" !== this.layoutType || (this._layoutRectLevelMap.width !== layout_1.USER_LAYOUT_RECT_LEVEL || "left" !== this.layoutOrient && "right" !== this.layoutOrient) && (this._layoutRectLevelMap.height !== layout_1.USER_LAYOUT_RECT_LEVEL || "bottom" !== this.layoutOrient && "top" !== this.layoutOrient))) return this._layoutRect;
const bounds = Object.assign({}, this._model.getBoundsInRect(this.setRectInSpec(rect), rect));
this.changeBoundsBySetting(bounds), this.autoIndent && bounds.x2 - bounds.x1 > 0 && bounds.y2 - bounds.y1 > 0 && (this._lastComputeOutBounds.x1 = Math.ceil(-bounds.x1),
this._lastComputeOutBounds.x2 = Math.ceil(bounds.x2 - rect.width), this._lastComputeOutBounds.y1 = Math.ceil(-bounds.y1),
this._lastComputeOutBounds.y2 = Math.ceil(bounds.y2 - rect.height));
let result = this.setRectInSpec((0, space_1.boundsInRect)(bounds, rect));
return this._option.transformLayoutRect && (result = this._option.transformLayoutRect(result)),
result;
}
getModelId() {
return this._model.id;
}
getModelVisible() {
return this._model.getVisible();
}
setWillLayoutTag() {
this._willLayoutTag = !0;
}
clearWillLayoutTag() {
this._willLayoutTag = !1;
}
}
exports.LayoutItem = LayoutItem;
//# sourceMappingURL=layout-item.js.map