Skip to content

Commit

Permalink
Improve ContextMenuEvent to extend MapBrowserEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
GastonZalba committed Oct 18, 2022
1 parent c602f66 commit 8d7d1ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ export default class ContextMenu extends Control {
this.dispatchEvent(
new ContextMenuEvent({
type: CustomEventTypes.BEFOREOPEN,
pixel: this.pixel,
coordinate: this.coordinate,
map: this.map,
originalEvent: evt
})
);

Expand All @@ -281,7 +281,7 @@ export default class ContextMenu extends Control {
}

setTimeout(() => {
this.openMenu();
this.openMenu(evt);
});

evt.target?.addEventListener(
Expand All @@ -296,7 +296,7 @@ export default class ContextMenu extends Control {
);
}

protected openMenu() {
protected openMenu(evt: MouseEvent) {
if (this.menuEntries.size === 0) return;

this.opened = true;
Expand All @@ -306,8 +306,8 @@ export default class ContextMenu extends Control {
this.dispatchEvent(
new ContextMenuEvent({
type: CustomEventTypes.OPEN,
pixel: this.pixel,
coordinate: this.coordinate,
map: this.map,
originalEvent: evt
})
);
}
Expand Down
17 changes: 5 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Coordinate } from 'ol/coordinate';
import type { Pixel } from 'ol/pixel';
import { Map as OlMap } from 'ol';
import BaseEvent from 'ol/events/Event.js';
import { Map as OlMap, MapBrowserEvent } from 'ol';

export enum EventTypes {
CONTEXTMENU = 'contextmenu',
Expand All @@ -16,18 +14,13 @@ export enum CustomEventTypes {
ADD_MENU_ENTRY = 'add-menu-entry',
}

export class ContextMenuEvent extends BaseEvent {
public coordinate: Coordinate;
public pixel: Pixel;

export class ContextMenuEvent extends MapBrowserEvent<MouseEvent> {
constructor(options: {
type: `${CustomEventTypes.BEFOREOPEN}` | `${CustomEventTypes.OPEN}`;
coordinate: Coordinate;
pixel: Pixel;
map: OlMap;
originalEvent: MouseEvent;
}) {
super(options.type);
this.pixel = options.pixel;
this.coordinate = options.coordinate;
super(options.type, options.map, options.originalEvent);
}
}

Expand Down

0 comments on commit 8d7d1ff

Please sign in to comment.