UNPKG

@visactor/vchart

Version:

charts lib based @visactor/VGrammar

73 lines (65 loc) 2.98 kB
import { Factory } from "../../core/factory"; import { BaseTrigger } from "./base"; import { isNil } from "@visactor/vutils"; import { highlightDefaultOptions } from "./util"; const type = "element-highlight-by-group"; export class ElementHighlightByGroup extends BaseTrigger { constructor(options) { super(options), this.type = type, this.handleStart = e => { this.start(e.item); }, this.handleReset = e => { const g = e.item; g && this.isGraphicInMark(g) && this.resetAll(); }, this.options = Object.assign({}, highlightDefaultOptions, options), this.updateMarkIdByState([ this.options.highlightState, this.options.blurState ]); } getStartState() { return this.options.highlightState; } getResetState() { return this.options.blurState; } getEvents() { return [ { type: this.options.trigger, handler: this.handleStart }, { type: this.options.triggerOff, handler: this.handleReset } ]; } resetAll() { const {interaction: interaction, highlightState: highlightState, blurState: blurState} = this.options; interaction.clearAllStatesOfTrigger(this, highlightState, blurState), interaction.setStatedGraphics(this, []); } _getHightlightKey(g) { var _a; return null === (_a = g.context) || void 0 === _a ? void 0 : _a.groupKey; } start(g) { if (g && this.isGraphicInMark(g)) { const highlightKey = this._getHightlightKey(g); if (isNil(highlightKey)) return; const {interaction: interaction, highlightState: highlightState, blurState: blurState} = this.options, statedGraphics = interaction.getStatedGraphics(this), newStatedGraphics = []; this.getMarks().forEach((m => { var _a; null === (_a = m.getGraphics()) || void 0 === _a || _a.forEach((g => { this._getHightlightKey(g) === highlightKey && newStatedGraphics.push(g); })); })), interaction.updateStates(this, newStatedGraphics, statedGraphics, highlightState, blurState), interaction.setStatedGraphics(this, newStatedGraphics); } } reset(g) { if (g) { if (this.isGraphicInMark(g)) { const {interaction: interaction} = this.options, statedGraphics = interaction.getStatedGraphics(this); g.removeState([ this.options.highlightState, this.options.blurState ]), interaction.setStatedGraphics(this, statedGraphics.filter((sg => sg !== g))); } } else this.resetAll(); } } ElementHighlightByGroup.type = type, ElementHighlightByGroup.defaultOptions = highlightDefaultOptions; export const registerElementHighlightByGroup = () => { Factory.registerInteractionTrigger(type, ElementHighlightByGroup); }; //# sourceMappingURL=element-highlight-by-group.js.map