@visactor/vchart
Version:
charts lib based @visactor/VGrammar
102 lines (95 loc) • 5.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.registerGlyphMark = exports.GlyphMark = void 0;
const base_mark_1 = require("./base/base-mark"), vrender_bridge_1 = require("../vrender-bridge"), factory_1 = require("../core/factory"), enum_1 = require("./interface/enum"), vutils_1 = require("@visactor/vutils"), GLYPH_INHERITED_STYLE_ATTRIBUTES = new Set([ "fill", "fillOpacity", "stroke", "strokeOpacity", "opacity", "lineWidth", "lineDash", "lineDashOffset", "lineCap", "lineJoin", "miterLimit", "shadowBlur", "shadowColor", "shadowOffsetX", "shadowOffsetY", "visible", "pickable", "cursor" ]), GLYPH_STATE_ATTRIBUTE_UPDATE_TYPE = 2;
class GlyphMark extends base_mark_1.BaseMark {
constructor() {
super(...arguments), this._setStateOfGraphic = (g, hasAnimation) => {
g.clearStates(), g.context.diffState !== enum_1.DiffState.enter && g.context.diffState !== enum_1.DiffState.update || (g.glyphStateProxy = (stateName, nexStates) => {
var _a;
const glyphAttrs = {
attributes: Object.assign(Object.assign({}, this._runEncoderOfGraphic(null === (_a = this._encoderOfState) || void 0 === _a ? void 0 : _a[stateName], g)), g.runtimeStateCache ? g.runtimeStateCache[stateName] : null)
};
return g.glyphStates ? g.glyphStates[stateName] || (g.glyphStates[stateName] = glyphAttrs) : g.glyphStates = {
[stateName]: glyphAttrs
}, glyphAttrs;
}, g.useStates(g.context.states, hasAnimation));
};
}
getSubMarks() {
return this._subMarks;
}
setGlyphConfig(cfg) {
this._glyphConfig = cfg;
}
getGlyphConfig() {
return this._glyphConfig;
}
getPositionChannels() {
return this._positionChannels;
}
_getInheritedStyleAttrs(attributes) {
let inheritedStyleAttrs = null;
return Object.keys(attributes).forEach((channel => {
GLYPH_INHERITED_STYLE_ATTRIBUTES.has(channel) && (inheritedStyleAttrs = null != inheritedStyleAttrs ? inheritedStyleAttrs : {},
inheritedStyleAttrs[channel] = attributes[channel]);
})), inheritedStyleAttrs;
}
_getInheritedStyleAttrsMap(glyph, attributes) {
const inheritedStyleAttrs = this._getInheritedStyleAttrs(attributes);
return inheritedStyleAttrs ? glyph.getSubGraphic().reduce(((map, subGraphic) => (subGraphic && (map[subGraphic.name] = Object.assign({}, inheritedStyleAttrs)),
map)), {}) : null;
}
_syncInheritedStyleAttrs(glyph, attributes) {
const inheritedStyleAttrs = this._getInheritedStyleAttrs(attributes);
inheritedStyleAttrs && glyph.getSubGraphic().forEach((subGraphic => {
null == subGraphic || subGraphic.setAttributes(Object.assign({}, inheritedStyleAttrs));
}));
}
_onGlyphAttributeUpdate(glyph) {
return newAttributes => {
var _a, _b;
const positionChannels = this.getPositionChannels();
let subAttrsMap = positionChannels && this._positionEncoder && Object.keys(newAttributes).some((k => positionChannels.includes(k))) ? this._positionEncoder(newAttributes, null === (_b = null === (_a = null == glyph ? void 0 : glyph.context) || void 0 === _a ? void 0 : _a.data) || void 0 === _b ? void 0 : _b[0], glyph) : null;
this._channelEncoder && Object.keys(this._channelEncoder).forEach((channel => {
if (channel in newAttributes) {
const channelAttrsMap = this._channelEncoder[channel](newAttributes[channel]);
subAttrsMap = subAttrsMap ? (0, vutils_1.merge)(subAttrsMap, channelAttrsMap) : channelAttrsMap;
}
}));
const inheritedStyleAttrsMap = this._getInheritedStyleAttrsMap(glyph, newAttributes);
return inheritedStyleAttrsMap && (subAttrsMap = subAttrsMap ? (0, vutils_1.merge)(inheritedStyleAttrsMap, subAttrsMap) : inheritedStyleAttrsMap),
subAttrsMap && glyph.getSubGraphic().forEach((subGraphic => {
subGraphic && subAttrsMap[subGraphic.name] && subGraphic.setAttributes(subAttrsMap[subGraphic.name]);
})), newAttributes;
};
}
_createGraphic(attrs = {}) {
const glyph = (0, vrender_bridge_1.createGlyph)(attrs);
glyph.onBeforeAttributeUpdate = this._onGlyphAttributeUpdate(glyph);
const onAttributeUpdate = glyph.onAttributeUpdate.bind(glyph);
glyph.onAttributeUpdate = context => {
onAttributeUpdate(context), 2 === (null == context ? void 0 : context.type) && this._syncInheritedStyleAttrs(glyph, glyph.attribute);
};
const subMarks = this._subMarks;
if (subMarks) {
const subGraphics = [];
Object.keys(subMarks).forEach((name => {
const subGraphic = factory_1.Factory.createGraphicComponent(subMarks[name].type, Object.assign({}, subMarks[name].defaultAttributes));
subGraphic && (subGraphics.push(subGraphic), subGraphic.name = name, subGraphic.onBeforeAttributeUpdate = attrs => attrs);
})), glyph.setSubGraphic(subGraphics);
}
return glyph.onBeforeAttributeUpdate(attrs), glyph;
}
_runProgressiveEncoder(graphics) {
this._runEncoder(graphics);
}
}
exports.GlyphMark = GlyphMark;
const registerGlyphMark = () => {
factory_1.Factory.registerGraphicComponent("glyph", vrender_bridge_1.createGlyph),
(0, vrender_bridge_1.registerShadowRoot)(), (0, vrender_bridge_1.registerGlyph)();
};
exports.registerGlyphMark = registerGlyphMark;
//# sourceMappingURL=glyph.js.map