UNPKG

@visactor/vchart

Version:

charts lib based @visactor/VGrammar

101 lines (93 loc) 5.41 kB
import { BaseMark } from "./base/base-mark"; import { createGlyph, registerGlyph, registerShadowRoot } from "../vrender-bridge"; import { Factory } from "../core/factory"; import { DiffState } from "./interface/enum"; import { merge } from "@visactor/vutils"; const 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; export class GlyphMark extends BaseMark { constructor() { super(...arguments), this._setStateOfGraphic = (g, hasAnimation) => { g.clearStates(), g.context.diffState !== DiffState.enter && g.context.diffState !== 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 ? merge(subAttrsMap, channelAttrsMap) : channelAttrsMap; } })); const inheritedStyleAttrsMap = this._getInheritedStyleAttrsMap(glyph, newAttributes); return inheritedStyleAttrsMap && (subAttrsMap = subAttrsMap ? merge(inheritedStyleAttrsMap, subAttrsMap) : inheritedStyleAttrsMap), subAttrsMap && glyph.getSubGraphic().forEach((subGraphic => { subGraphic && subAttrsMap[subGraphic.name] && subGraphic.setAttributes(subAttrsMap[subGraphic.name]); })), newAttributes; }; } _createGraphic(attrs = {}) { const glyph = 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.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); } } export const registerGlyphMark = () => { Factory.registerGraphicComponent("glyph", createGlyph), registerShadowRoot(), registerGlyph(); }; //# sourceMappingURL=glyph.js.map