suoqiu-f2
Version:
Charts for mobile visualization.
277 lines (275 loc) • 8.05 kB
JavaScript
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _common = require("../../util/common");
var _matrix = _interopRequireDefault(require("../util/matrix"));
var _vector = _interopRequireDefault(require("../util/vector2"));
var _styleParse = require("../util/style-parse");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var ALIAS_ATTRS_MAP = {
stroke: 'strokeStyle',
fill: 'fillStyle',
opacity: 'globalAlpha'
};
var SHAPE_ATTRS = ['fillStyle', 'font', 'globalAlpha', 'lineCap', 'lineWidth', 'lineJoin', 'miterLimit', 'shadowBlur', 'shadowColor', 'shadowOffsetX', 'shadowOffsetY', 'strokeStyle', 'textAlign', 'textBaseline', 'lineDash', 'shadow' // 兼容支付宝小程序
];
var CLIP_SHAPES = ['circle', 'sector', 'polygon', 'rect', 'polyline'];
var Element = /*#__PURE__*/function () {
function Element(cfg) {
this._initProperties();
(0, _common.mix)(this._attrs, cfg);
var attrs = this._attrs.attrs;
if (attrs) {
this.initAttrs(attrs);
}
this.initTransform();
}
var _proto = Element.prototype;
_proto._initProperties = function _initProperties() {
this._attrs = {
zIndex: 0,
visible: true,
destroyed: false
};
};
_proto.get = function get(name) {
return this._attrs[name];
};
_proto.set = function set(name, value) {
this._attrs[name] = value;
};
_proto.isGroup = function isGroup() {
return this.get('isGroup');
};
_proto.isShape = function isShape() {
return this.get('isShape');
};
_proto.initAttrs = function initAttrs(attrs) {
this.attr((0, _common.mix)(this.getDefaultAttrs(), attrs));
};
_proto.getDefaultAttrs = function getDefaultAttrs() {
return {};
};
_proto._setAttr = function _setAttr(name, value) {
var attrs = this._attrs.attrs;
if (name === 'clip') {
value = this._setAttrClip(value);
} else {
var alias = ALIAS_ATTRS_MAP[name];
if (alias) {
attrs[alias] = value;
}
}
attrs[name] = value;
};
_proto._getAttr = function _getAttr(name) {
return this._attrs.attrs[name];
}
// _afterAttrsSet() {}
;
_proto._setAttrClip = function _setAttrClip(clip) {
if (clip && CLIP_SHAPES.indexOf(clip._attrs.type) > -1) {
if (clip.get('canvas') === null) {
clip = Object.assign({}, clip);
}
clip.set('parent', this.get('parent'));
clip.set('context', this.get('context'));
return clip;
}
return null;
};
_proto.attr = function attr(name, value) {
var self = this;
if (self.get('destroyed')) return null;
var argumentsLen = arguments.length;
if (argumentsLen === 0) {
return self._attrs.attrs;
}
if ((0, _common.isObject)(name)) {
this._attrs.bbox = null;
for (var k in name) {
self._setAttr(k, name[k]);
}
if (self._afterAttrsSet) {
self._afterAttrsSet();
}
return self;
}
if (argumentsLen === 2) {
this._attrs.bbox = null;
self._setAttr(name, value);
if (self._afterAttrsSet) {
self._afterAttrsSet();
}
return self;
}
return self._getAttr(name);
};
_proto.getParent = function getParent() {
return this.get('parent');
};
_proto.draw = function draw(context) {
if (this.get('destroyed')) {
return;
}
if (this.get('visible')) {
this.setContext(context);
this.drawInner(context);
this.restoreContext(context);
}
};
_proto.setContext = function setContext(context) {
var clip = this._attrs.attrs.clip;
context.save();
if (clip) {
clip.resetTransform(context);
clip.createPath(context);
context.clip();
}
this.resetContext(context);
this.resetTransform(context);
};
_proto.restoreContext = function restoreContext(context) {
context.restore();
};
_proto.resetContext = function resetContext(context) {
var elAttrs = this._attrs.attrs;
for (var k in elAttrs) {
if (SHAPE_ATTRS.indexOf(k) > -1) {
var v = elAttrs[k];
if ((k === 'fillStyle' || k === 'strokeStyle') && v) {
v = (0, _styleParse.parseStyle)(v, this, context);
}
if (k === 'lineDash' && context.setLineDash && (0, _common.isArray)(v)) {
context.setLineDash(v);
} else {
context[k] = v;
}
}
}
};
_proto.hasFill = function hasFill() {
return this.get('canFill') && this._attrs.attrs.fillStyle;
};
_proto.hasStroke = function hasStroke() {
return this.get('canStroke') && this._attrs.attrs.strokeStyle;
};
_proto.drawInner = function drawInner(/* context */
) {};
_proto.show = function show() {
this.set('visible', true);
return this;
};
_proto.hide = function hide() {
this.set('visible', false);
return this;
};
_proto.isVisible = function isVisible() {
return this.get('visible');
};
_proto.getAriaLabel = function getAriaLabel() {
var _this$_attrs = this._attrs,
destroyed = _this$_attrs.destroyed,
visible = _this$_attrs.visible,
isShape = _this$_attrs.isShape,
aria = _this$_attrs.aria;
if (destroyed || !visible || isShape && !aria) {
return;
}
return this._getAriaLabel();
};
_proto._getAriaLabel = function _getAriaLabel() {
return this._attrs.ariaLabel;
};
_proto._removeFromParent = function _removeFromParent() {
var parent = this.get('parent');
if (parent) {
var children = parent.get('children');
_common.Array.remove(children, this);
}
return this;
};
_proto.remove = function remove(destroy) {
if (destroy) {
this.destroy();
} else {
this._removeFromParent();
}
};
_proto.destroy = function destroy() {
var destroyed = this.get('destroyed');
if (destroyed) {
return null;
}
this._removeFromParent();
this._attrs = {};
this.set('destroyed', true);
};
_proto.getBBox = function getBBox() {
return {
minX: 0,
maxX: 0,
minY: 0,
maxY: 0,
width: 0,
height: 0
};
};
_proto.initTransform = function initTransform() {
var attrs = this._attrs.attrs || {};
if (!attrs.matrix) {
attrs.matrix = [1, 0, 0, 1, 0, 0];
}
this._attrs.attrs = attrs;
};
_proto.getMatrix = function getMatrix() {
return this._attrs.attrs.matrix;
};
_proto.setMatrix = function setMatrix(m) {
this._attrs.attrs.matrix = [m[0], m[1], m[2], m[3], m[4], m[5]];
};
_proto.transform = function transform(actions) {
var matrix = this._attrs.attrs.matrix;
this._attrs.attrs.matrix = _matrix["default"].transform(matrix, actions);
return this;
};
_proto.setTransform = function setTransform(actions) {
this._attrs.attrs.matrix = [1, 0, 0, 1, 0, 0];
return this.transform(actions);
};
_proto.translate = function translate(x, y) {
var matrix = this._attrs.attrs.matrix;
_matrix["default"].translate(matrix, matrix, [x, y]);
};
_proto.rotate = function rotate(rad) {
var matrix = this._attrs.attrs.matrix;
_matrix["default"].rotate(matrix, matrix, rad);
};
_proto.scale = function scale(sx, sy) {
var matrix = this._attrs.attrs.matrix;
_matrix["default"].scale(matrix, matrix, [sx, sy]);
};
_proto.moveTo = function moveTo(x, y) {
var cx = this._attrs.x || 0;
var cy = this._attrs.y || 0;
this.translate(x - cx, y - cy);
this.set('x', x);
this.set('y', y);
};
_proto.apply = function apply(v) {
var m = this._attrs.attrs.matrix;
_vector["default"].transformMat2d(v, v, m);
return this;
};
_proto.resetTransform = function resetTransform(context) {
var mo = this._attrs.attrs.matrix;
if (_matrix["default"].isChanged(mo)) {
context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]);
}
};
_proto.isDestroyed = function isDestroyed() {
return this.get('destroyed');
};
return Element;
}();
var _default = exports["default"] = Element;