@stringsync/vexml
Version:
MusicXML to Vexflow
54 lines (53 loc) • 2.06 kB
JavaScript
import * as events from '../events';
import { EventMappingFactory } from './eventmappingfactory';
export class Events {
config;
root;
vexmlEventTopic;
nativeEventTopic;
bridge;
constructor(config, root, vexmlEventTopic, nativeEventTopic, bridge) {
this.config = config;
this.root = root;
this.vexmlEventTopic = vexmlEventTopic;
this.nativeEventTopic = nativeEventTopic;
this.bridge = bridge;
}
static create(config, root, elementLocator, timestampLocator) {
const vexmlEventTopic = new events.Topic();
const nativeEventTopic = new events.Topic();
const mappings = EventMappingFactory.create(root, elementLocator, timestampLocator, vexmlEventTopic, config.INPUT_TYPE);
const bridge = new events.NativeBridge(root, mappings, nativeEventTopic, {
touchstart: { passive: true },
touchmove: { passive: true },
touchcancel: { passive: true },
touchend: { passive: true },
});
return new Events(config, root, vexmlEventTopic, nativeEventTopic, bridge);
}
addEventListener(type, listener) {
if (!this.vexmlEventTopic.hasSubscribers(type) && !this.bridge.isActivated(type)) {
this.bridge.activate(type);
}
return this.vexmlEventTopic.subscribe(type, listener);
}
removeEventListener(...ids) {
for (const id of ids) {
const subscription = this.vexmlEventTopic.unsubscribe(id);
if (!subscription) {
return;
}
if (!this.vexmlEventTopic.hasSubscribers(subscription.name) && this.bridge.isActivated(subscription.name)) {
this.bridge.deactivate(subscription.name);
}
}
}
removeAllEventListeners() {
this.bridge.deactivateAll();
this.vexmlEventTopic.unsubscribeAll();
this.nativeEventTopic.unsubscribeAll();
}
dispatchNativeEvent(event) {
this.root.getOverlay().getElement().dispatchEvent(event);
}
}