UNPKG

@visactor/vchart

Version:

charts lib based @visactor/VGrammar

75 lines (67 loc) 3.63 kB
import { isString } from "@visactor/vutils"; import { STATE_VALUE_ENUM } from "../../compile/mark/interface"; import { BaseTrigger } from "./base"; import { Factory } from "../../core/factory"; const defaultOptions = { highlightState: STATE_VALUE_ENUM.STATE_HIGHLIGHT, blurState: STATE_VALUE_ENUM.STATE_BLUR, trigger: "pointerover", triggerOff: "pointerout" }; export class ElementHighlight extends BaseTrigger { constructor(options) { super(options), this.type = "element-highlight", this.resetAll = e => { const {highlightState: highlightState, blurState: blurState, interaction: interaction} = this.options; this._lastGraphic && (interaction.clearAllStatesOfTrigger(this, highlightState, blurState), this.dispatchEvent("reset", Object.assign({ graphics: [ this._lastGraphic ], options: this.options }, e)), this._lastGraphic = null, interaction.setStatedGraphics(this, [])); }, this.handleStart = e => { this.start(e.item, e); }, this.handleReset = e => { const {interaction: interaction} = this.options, statedGraphics = interaction.getStatedGraphics(this); if (!statedGraphics || !statedGraphics.length) return; const markGraphic = e.item, hasActiveElement = markGraphic && this._markSet.getMarkInId(markGraphic.context.markId); (this._resetType.includes("view") && !hasActiveElement || this._resetType.includes("self") && hasActiveElement) && this.resetAll(e); }, this.options = Object.assign({}, defaultOptions, options), this.updateMarkIdByState([ this.options.highlightState, this.options.blurState ]); } getStartState() { return this.options.highlightState; } getResetState() { return this.options.blurState; } getEvents() { const triggerOff = this.options.triggerOff, events = [ { type: this.options.trigger, handler: this.handleStart } ]; let eventName = triggerOff; return isString(triggerOff) && triggerOff.includes("view:") ? (eventName = triggerOff.replace("view:", ""), this._resetType = "view") : this._resetType = "self", events.push({ type: eventName, handler: this.handleReset }), events; } start(markGraphic, e) { if (markGraphic && this._markSet.getMarkInId(markGraphic.context.markId)) { const {highlightState: highlightState, blurState: blurState, interaction: interaction} = this.options; if (this._lastGraphic === markGraphic) return; const newStatedGraphics = interaction.updateStates(this, [ markGraphic ], interaction.getStatedGraphics(this), highlightState, blurState); interaction.setStatedGraphics(this, newStatedGraphics), this._lastGraphic = markGraphic, this.dispatchEvent("start", Object.assign({ graphics: newStatedGraphics, options: this.options }, e)); } else this._lastGraphic && "view" === this._resetType && this.resetAll(e); } reset(markGraphic, e) { markGraphic ? this._markSet.getMarkInId(markGraphic.context.markId) && markGraphic.removeState([ this.options.highlightState, this.options.blurState ]) : this.resetAll(e); } } ElementHighlight.type = "element-highlight", ElementHighlight.defaultOptions = defaultOptions; export const registerElementHighlight = () => { Factory.registerInteractionTrigger(ElementHighlight.type, ElementHighlight); }; //# sourceMappingURL=element-highlight.js.map