UNPKG

@visactor/vchart

Version:

charts lib based @visactor/VGrammar

79 lines (72 loc) 4.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.registerElementSelect = exports.ElementSelect = void 0; const vutils_1 = require("@visactor/vutils"), interface_1 = require("../../compile/mark/interface"), base_1 = require("./base"), util_1 = require("./util"), factory_1 = require("../../core/factory"), defaultOptions = { state: interface_1.STATE_VALUE_ENUM.STATE_SELECTED, trigger: "click" }; class ElementSelect extends base_1.BaseTrigger { constructor(options) { super(options), this.type = "element-select", this._resetType = [], this.resetAll = e => { const {state: state, reverseState: reverseState, interaction: interaction} = this.options, statedGraphics = interaction.getStatedGraphics(this); statedGraphics && statedGraphics.length && (interaction.clearAllStatesOfTrigger(this, state, reverseState), this.dispatchEvent("reset", Object.assign({ graphics: statedGraphics, options: this.options }, e)), 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.state, this.options.reverseState ]); } getStartState() { return this.options.state; } getResetState() { return this.options.reverseState; } getEvents() { const triggerOff = this.options.triggerOff, trigger = this.options.trigger, events = [ { type: trigger, handler: this.handleStart } ], {eventNames: eventNames, resetType: resetType} = (0, util_1.parseTriggerOffOfSelect)(triggerOff); return eventNames.forEach((evt => { evt && ((0, vutils_1.isArray)(trigger) ? !trigger.includes(evt) : evt !== trigger) && events.push({ type: evt, handler: this.handleReset }); })), this._resetType = resetType, events; } start(markGraphic, e) { const {state: state, reverseState: reverseState, isMultiple: isMultiple, interaction: interaction} = this.options, statedGraphics = interaction.getStatedGraphics(this); if (markGraphic && this._markSet.getMarkInId(markGraphic.context.markId)) if (markGraphic.hasState(state)) { if (this._resetType.includes("self")) { const newStatedGraphics = statedGraphics && statedGraphics.filter((g => g !== markGraphic)); newStatedGraphics && newStatedGraphics.length ? interaction.setStatedGraphics(this, interaction.updateStates(this, newStatedGraphics, statedGraphics, state, reverseState)) : this.resetAll(e); } } else { this._timer && clearTimeout(this._timer), markGraphic.addState(state, !0); const newStatedGraphics = this.options.interaction.updateStates(this, isMultiple && statedGraphics ? [ ...statedGraphics, markGraphic ] : [ markGraphic ], statedGraphics, state, reverseState); interaction.setStatedGraphics(this, newStatedGraphics), this.dispatchEvent("start", Object.assign({ graphics: newStatedGraphics, options: this.options }, e)), this._resetType.includes("timeout") && (this._timer = setTimeout((() => { this.resetAll(e); }), this.options.triggerOff)); } else this._resetType.includes("view") && statedGraphics && statedGraphics.length && this.resetAll(e); } reset(markGraphic, e) { markGraphic ? this._markSet.getMarkInId(markGraphic.context.markId) && markGraphic.removeState([ this.options.state, this.options.reverseState ]) : this.resetAll(e); } } exports.ElementSelect = ElementSelect, ElementSelect.type = "element-select", ElementSelect.defaultOptions = defaultOptions; const registerElementSelect = () => { factory_1.Factory.registerInteractionTrigger(ElementSelect.type, ElementSelect); }; exports.registerElementSelect = registerElementSelect; //# sourceMappingURL=element-select.js.map