Skip to content

Commit 6ff3054

Browse files
committed
Simplified custom-events.ts
1 parent 14def5a commit 6ff3054

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

src/app/custom-events.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1-
import { PluginContext } from 'Molstar/mol-plugin/context';
2-
import { lociDetails, EventDetail } from './loci-details';
31
import { InteractivityManager } from 'Molstar/mol-plugin-state/manager/interactivity';
2+
import { PluginContext } from 'Molstar/mol-plugin/context';
43
import { debounceTime } from 'rxjs/operators';
4+
import { EventDetail, lociDetails } from './loci-details';
55

6-
export namespace CustomEvents {
76

8-
function create(eventTypeArr: string[]) {
9-
const eventObj = {} as any;
10-
for (let ei = 0, el = eventTypeArr.length; ei < el; ei++) {
11-
const eventType = eventTypeArr[ei];
12-
let event;
13-
if (typeof MouseEvent == 'function') {
14-
// current standard
15-
event = new MouseEvent(eventType, { 'view': window, 'bubbles': true, 'cancelable': true });
16-
} else if (typeof document.createEvent == 'function') {
17-
// older standard
18-
event = document.createEvent('MouseEvents');
19-
event.initEvent(eventType, true /* bubbles */, true /* cancelable */);
20-
21-
}
22-
eventObj[eventType] = event;
23-
};
24-
return eventObj;
7+
export namespace CustomEvents {
8+
function createEvent(eventType: string): MouseEvent {
9+
if (typeof MouseEvent == 'function') {
10+
// current standard
11+
return new MouseEvent(eventType, { 'view': window, 'bubbles': true, 'cancelable': true });
12+
} else if (typeof document.createEvent == 'function') {
13+
// older standard
14+
const event = document.createEvent('MouseEvents');
15+
event.initEvent(eventType, true /* bubbles */, true /* cancelable */);
16+
return event;
17+
} else {
18+
throw new Error('Cannot create event');
19+
}
2520
}
2621

27-
function dispatchCustomEvent(event: any, eventData: EventDetail, targetElement: HTMLElement) {
28-
if (typeof eventData !== 'undefined') {
29-
(eventData as any)['residueNumber'] = eventData.seq_id;
30-
event['eventData'] = eventData;
31-
event.eventData.residueNumber = eventData.seq_id;
22+
function dispatchCustomEvent(event: UIEvent, eventData: EventDetail, targetElement: HTMLElement) {
23+
if (eventData !== undefined) {
24+
if (eventData.seq_id !== undefined) {
25+
(eventData as any).residueNumber = eventData.seq_id;
26+
}
27+
(event as any).eventData = eventData;
3228
targetElement.dispatchEvent(event);
3329
}
3430
}
3531

3632
export function add(plugin: PluginContext, targetElement: HTMLElement) {
37-
const pdbevents = create(['PDB.molstar.click', 'PDB.molstar.mouseover', 'PDB.molstar.mouseout']);
33+
const PDB_molstar_click = createEvent('PDB.molstar.click');
34+
const PDB_molstar_mouseover = createEvent('PDB.molstar.mouseover');
35+
const PDB_molstar_mouseout = createEvent('PDB.molstar.mouseout');
36+
3837
plugin.behaviors.interaction.click.subscribe((e: InteractivityManager.ClickEvent) => {
3938
if (e.button === 1 && e.current && e.current.loci.kind !== 'empty-loci') {
4039
const evData = lociDetails(e.current.loci);
41-
if (evData) dispatchCustomEvent(pdbevents['PDB.molstar.click'], evData, targetElement);
40+
if (evData) dispatchCustomEvent(PDB_molstar_click, evData, targetElement);
4241
}
4342
});
4443
plugin.behaviors.interaction.hover.pipe(debounceTime(100)).subscribe((e: InteractivityManager.HoverEvent) => {
4544
if (e.current && e.current.loci && e.current.loci.kind !== 'empty-loci') {
4645
const evData = lociDetails(e.current.loci);
47-
if (evData) dispatchCustomEvent(pdbevents['PDB.molstar.mouseover'], evData, targetElement);
46+
if (evData) dispatchCustomEvent(PDB_molstar_mouseover, evData, targetElement);
4847
}
4948

5049
if (e.current && e.current.loci && e.current.loci.kind === 'empty-loci') {
51-
dispatchCustomEvent(pdbevents['PDB.molstar.mouseout'], {}, targetElement);
50+
dispatchCustomEvent(PDB_molstar_mouseout, {}, targetElement);
5251
}
5352
});
5453
}
55-
56-
}
54+
}

src/app/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class PDBeMolstarPlugin {
189189
if (this.initParams.hideCanvasControls.includes('animation')) pdbePluginSpec.config.push([PluginConfig.Viewport.ShowAnimation, false]);
190190
if (this.initParams.hideCanvasControls.includes('controlToggle')) pdbePluginSpec.config.push([PluginConfig.Viewport.ShowControls, false]);
191191
if (this.initParams.hideCanvasControls.includes('controlInfo')) pdbePluginSpec.config.push([PluginConfig.Viewport.ShowSettings, false]);
192-
if (this.initParams.superposition){
192+
if (this.initParams.superposition) {
193193
pdbePluginSpec.config.push([PluginConfig.Viewport.ShowAnimation, false]);
194194
pdbePluginSpec.config.push([PluginConfig.Viewport.ShowTrajectoryControls, false]);
195195
}
@@ -272,12 +272,12 @@ export class PDBeMolstarPlugin {
272272
this.load({ url: dataSource.url, format: dataSource.format as BuiltInTrajectoryFormat, assemblyId: this.initParams.assemblyId, isBinary: dataSource.isBinary, progressMessage: `Loading ${this.initParams.moleculeId ?? ''} ...` });
273273
}
274274

275-
// Binding to other PDB Component events
275+
// Subscribe to events from other PDB Component
276276
if (this.initParams.subscribeEvents) {
277277
subscribeToComponentEvents(this);
278278
}
279279

280-
// Event handling
280+
// Emit events for other PDB Components
281281
CustomEvents.add(this.plugin, this.targetElement);
282282

283283
}

src/app/ui/split-ui/components.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ export const UIComponents = {
99
/** Component containing 1D view of the sequences (top panel in default layout) */
1010
SequenceView,
1111
// TODO add all meaningful components
12-
// TODO fix overlay
1312
// TODO test events
1413
} as const satisfies Record<string, JSXElementConstructor<any>>;

0 commit comments

Comments
 (0)