@visactor/vchart
Version:
charts lib based @visactor/VGrammar
148 lines (143 loc) • 8.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.Interaction = void 0;
const graphic_state_1 = require("../util/graphic-state");
class Interaction {
constructor() {
this._stateGraphicsByTrigger = new Map, this._disableTriggerEvent = !1, this._triggerMapByState = new Map;
}
setDisableActiveEffect(disable) {
this._disableTriggerEvent = disable;
}
addTrigger(trigger) {
if (trigger) {
[ trigger.getStartState(), trigger.getResetState() ].forEach((state => {
if (state) {
const stateTrigger = this._triggerMapByState.get(state);
stateTrigger ? !stateTrigger.includes(trigger) && stateTrigger.push(trigger) : this._triggerMapByState.set(state, [ trigger ]);
}
}));
}
}
setStatedGraphics(trigger, graphics) {
this._stateGraphicsByTrigger.set(trigger, graphics);
}
getStatedGraphics(trigger) {
return this._stateGraphicsByTrigger.get(trigger);
}
_getMarkById(trigger) {
const markById = new Map;
return trigger.getMarks().forEach((mark => {
mark && markById.set(mark.id, mark);
})), markById;
}
_hasAnimationByGraphicState(graphic, markById) {
var _a, _b, _c;
const mark = null !== (_b = null === (_a = graphic.parent) || void 0 === _a ? void 0 : _a.mark) && void 0 !== _b ? _b : markById.get(graphic.context.markId);
return !!(null === (_c = null == mark ? void 0 : mark.hasAnimationByState) || void 0 === _c ? void 0 : _c.call(mark, "state"));
}
updateStates(trigger, newStatedGraphics, prevStatedGraphics, state, reverseState) {
return this._disableTriggerEvent ? [] : newStatedGraphics && newStatedGraphics.length ? (state && reverseState ? prevStatedGraphics && prevStatedGraphics.length ? (this.toggleReverseStateOfGraphics(trigger, newStatedGraphics, prevStatedGraphics, reverseState),
this.toggleStateOfGraphics(trigger, newStatedGraphics, prevStatedGraphics, state)) : this.addBothStateOfGraphics(trigger, newStatedGraphics, state, reverseState) : state && (prevStatedGraphics && prevStatedGraphics.length ? this.toggleStateOfGraphics(trigger, newStatedGraphics, prevStatedGraphics, state) : this.addStateOfGraphics(trigger, newStatedGraphics, state)),
newStatedGraphics) : (prevStatedGraphics && prevStatedGraphics.length && this.clearAllStatesOfTrigger(trigger, state, reverseState),
[]);
}
toggleReverseStateOfGraphics(trigger, newStatedGraphics, prevStatedGraphics, reverseState) {
const markIdByState = trigger.getMarkIdByState(), markById = this._getMarkById(trigger);
prevStatedGraphics.forEach((g => {
if (reverseState && markIdByState[reverseState] && markIdByState[reverseState].includes(g.context.markId)) {
const hasAnimation = this._hasAnimationByGraphicState(g, markById);
(0, graphic_state_1.addGraphicState)(g, reverseState, !0, hasAnimation);
}
})), newStatedGraphics.forEach((g => {
if (reverseState && markIdByState[reverseState] && markIdByState[reverseState].includes(g.context.markId)) {
const hasAnimation = this._hasAnimationByGraphicState(g, markById);
(0, graphic_state_1.removeGraphicState)(g, reverseState, hasAnimation);
}
}));
}
toggleStateOfGraphics(trigger, newStatedGraphics, prevStatedGraphics, state) {
const markIdByState = trigger.getMarkIdByState(), markById = this._getMarkById(trigger);
prevStatedGraphics.forEach((g => {
if (state && markIdByState[state] && markIdByState[state].includes(g.context.markId)) {
const hasAnimation = this._hasAnimationByGraphicState(g, markById);
(0, graphic_state_1.removeGraphicState)(g, state, hasAnimation);
}
})), newStatedGraphics.forEach((g => {
if (state && markIdByState[state] && markIdByState[state].includes(g.context.markId)) {
const hasAnimation = this._hasAnimationByGraphicState(g, markById);
(0, graphic_state_1.addGraphicState)(g, state, !0, hasAnimation);
}
}));
}
addBothStateOfGraphics(trigger, statedGraphics, state, reverseState) {
const marks = trigger.getMarks(), markIdByState = trigger.getMarkIdByState();
marks.forEach((m => {
var _a;
const hasReverse = reverseState && markIdByState[reverseState] && markIdByState[reverseState].includes(m.id), hasState = state && markIdByState[state] && markIdByState[state].includes(m.id);
if (!hasReverse && !hasState) return;
const hasAnimation = m.hasAnimationByState && m.hasAnimationByState("state");
null === (_a = m.getGraphics()) || void 0 === _a || _a.forEach((g => {
statedGraphics && statedGraphics.includes(g) ? hasState && (0, graphic_state_1.addGraphicState)(g, state, !0, hasAnimation) : hasReverse && (0,
graphic_state_1.addGraphicState)(g, reverseState, !0, hasAnimation);
}));
}));
}
addStateOfGraphics(trigger, statedGraphics, state) {
const marks = trigger.getMarks(), markIdByState = trigger.getMarkIdByState();
marks.forEach((mark => {
var _a;
const hasState = state && markIdByState[state] && markIdByState[state].includes(mark.id);
if (!hasState) return;
const hasAnimation = mark.hasAnimationByState && mark.hasAnimationByState("state");
null === (_a = mark.getGraphics()) || void 0 === _a || _a.forEach((g => {
statedGraphics && statedGraphics.includes(g) && hasState && (0, graphic_state_1.addGraphicState)(g, state, !0, hasAnimation);
}));
}));
}
clearAllStatesOfTrigger(trigger, state, reverseState) {
if (this._disableTriggerEvent) return;
const statedGraphics = this.getStatedGraphics(trigger);
if (!statedGraphics || !statedGraphics.length) return;
const marks = trigger.getMarks(), markIdByState = trigger.getMarkIdByState();
marks.forEach((mark => {
if (mark) {
const graphics = mark.getGraphics(), hasAnimation = mark.hasAnimationByState && mark.hasAnimationByState("state");
graphics && graphics.length && (reverseState && markIdByState[reverseState] && markIdByState[reverseState].includes(mark.id) && graphics.forEach((g => {
(0, graphic_state_1.removeGraphicState)(g, reverseState, hasAnimation);
})), state && markIdByState[state] && markIdByState[state].includes(mark.id) && graphics.forEach((g => {
statedGraphics.includes(g) && (0, graphic_state_1.removeGraphicState)(g, state, hasAnimation);
})));
}
}));
}
clearAllStates() {
this._disableTriggerEvent || this._triggerMapByState.forEach(((triggers, state) => {
triggers.forEach((trigger => {
this.clearAllStatesOfTrigger(trigger, state, trigger.getResetState());
}));
}));
}
clearByState(stateValue) {
if (this._disableTriggerEvent) return;
const triggers = this._triggerMapByState.get(stateValue);
triggers && triggers.length && triggers.forEach((t => {
this.clearAllStatesOfTrigger(t, stateValue, t.getResetState()), this.setStatedGraphics(t, []);
}));
}
updateStateOfGraphics(stateValue, markGraphics) {
if (this._disableTriggerEvent) return;
const triggers = this._triggerMapByState.get(stateValue);
triggers && triggers.length && triggers.forEach((t => {
const newStatedGraphics = markGraphics.filter((mg => t.getMarks().some((m => {
const graphics = m && m.getGraphics();
return graphics && graphics.includes(mg);
}))));
this.updateStates(t, newStatedGraphics, this.getStatedGraphics(t), t.getStartState(), t.getResetState()),
this.setStatedGraphics(t, newStatedGraphics);
}));
}
}
exports.Interaction = Interaction;
//# sourceMappingURL=interaction.js.map