UNPKG

@visactor/vgrammar-core

Version:

VGrammar is a visual grammar library

44 lines (43 loc) 2.16 kB
import { array, isArray, isNil, isString, isValid } from "@visactor/vutils"; export class BaseInteraction { constructor(view, options) { this.references = new Map, this.view = view, this.depend(null == options ? void 0 : options.dependency); } getStartState() { return null; } depend(grammar) { this.references.clear(), array(grammar).map((grammar => isString(grammar) ? this.view.getGrammarById(grammar) : grammar)).filter((ref => !isNil(ref))).forEach((ref => { var _a; this.references.set(ref, (null !== (_a = this.references.get(ref)) && void 0 !== _a ? _a : 0) + 1); })); } parameters() { const params = {}; return this.references.forEach(((count, ref) => { isValid(ref.id()) && (params[ref.id()] = ref.output()); })), params; } bind() { const events = this.getEvents(); (null != events ? events : []).forEach((evt => { evt.type && evt.handler && (isArray(evt.type) ? evt.type.forEach((evtType => { evtType && "none" !== evtType && this.view.addEventListener(evtType, evt.handler); })) : "none" !== evt.type && this.view.addEventListener(evt.type, evt.handler)); })); } unbind() { const events = this.getEvents(); (null != events ? events : []).forEach((evt => { evt.type && evt.handler && (isArray(evt.type) ? evt.type.forEach((evtType => { evtType && "none" !== evtType && this.view.removeEventListener(evtType, evt.handler); })) : "none" !== evt.type && this.view.removeEventListener(evt.type, evt.handler)); })); } start(element) {} reset(element) {} dispatchEvent(type, params) { this.view.emit(`${this.type}:${type}`, params), "start" === type && this.options.onStart ? this.options.onStart(params) : "reset" === type && this.options.onReset ? this.options.onReset(params) : "update" === type && this.options.onUpdate ? this.options.onUpdate(params) : "end" === type && this.options.onEnd && this.options.onEnd(params); } } //# sourceMappingURL=base.js.map