@visactor/vchart
Version:
charts lib based @visactor/VGrammar
76 lines (69 loc) • 3.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.registerElementHighlight = exports.ElementHighlight = void 0;
const vutils_1 = require("@visactor/vutils"), interface_1 = require("../../compile/mark/interface"), base_1 = require("./base"), factory_1 = require("../../core/factory"), defaultOptions = {
highlightState: interface_1.STATE_VALUE_ENUM.STATE_HIGHLIGHT,
blurState: interface_1.STATE_VALUE_ENUM.STATE_BLUR,
trigger: "pointerover",
triggerOff: "pointerout"
};
class ElementHighlight extends base_1.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 (0, vutils_1.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);
}
}
exports.ElementHighlight = ElementHighlight, ElementHighlight.type = "element-highlight",
ElementHighlight.defaultOptions = defaultOptions;
const registerElementHighlight = () => {
factory_1.Factory.registerInteractionTrigger(ElementHighlight.type, ElementHighlight);
};
exports.registerElementHighlight = registerElementHighlight;
//# sourceMappingURL=element-highlight.js.map