@@ -3,9 +3,21 @@ import type { Coordinate } from 'ol/coordinate';
3
3
import OlMap from 'ol/Map' ;
4
4
import Control from 'ol/control/Control' ;
5
5
import BaseEvent from 'ol/events/Event' ;
6
+ import { EventsKey } from 'ol/events' ;
7
+ import { Types as ObjectEventTypes } from 'ol/ObjectEventType' ;
8
+ import { CombinedOnSignature , EventTypes as OlEventTypes , OnSignature } from 'ol/Observable' ;
9
+ import { ObjectEvent } from 'ol/Object' ;
6
10
7
11
import { CSS_CLASSES , DEFAULT_ITEMS , DEFAULT_OPTIONS } from './constants' ;
8
- import { CallbackObject , CustomEventTypes , EventTypes , Item , MenuEntry , Options } from './types' ;
12
+ import {
13
+ CallbackObject ,
14
+ ContextMenuEvent ,
15
+ CustomEventTypes ,
16
+ EventTypes ,
17
+ Item ,
18
+ MenuEntry ,
19
+ Options ,
20
+ } from './types' ;
9
21
import emitter from './emitter' ;
10
22
import { addMenuEntries , getLineHeight } from './helpers/dom' ;
11
23
@@ -40,6 +52,54 @@ export default class ContextMenu extends Control {
40
52
41
53
protected menuEntries : Map < string , MenuEntry > = new Map ( ) ;
42
54
55
+ declare on : OnSignature < OlEventTypes | `${CustomEventTypes . CLOSE } `, BaseEvent , EventsKey > &
56
+ OnSignature <
57
+ `${CustomEventTypes . BEFOREOPEN } ` | `${CustomEventTypes . OPEN } `,
58
+ ContextMenuEvent ,
59
+ EventsKey
60
+ > &
61
+ OnSignature < ObjectEventTypes , ObjectEvent , EventsKey > &
62
+ CombinedOnSignature <
63
+ | `${CustomEventTypes . BEFOREOPEN } `
64
+ | `${CustomEventTypes . OPEN } `
65
+ | ObjectEventTypes
66
+ | `${CustomEventTypes . CLOSE } `
67
+ | OlEventTypes ,
68
+ EventsKey
69
+ > ;
70
+
71
+ declare once : OnSignature < OlEventTypes | `${CustomEventTypes . CLOSE } `, BaseEvent , EventsKey > &
72
+ OnSignature <
73
+ `${CustomEventTypes . BEFOREOPEN } ` | `${CustomEventTypes . OPEN } `,
74
+ ContextMenuEvent ,
75
+ EventsKey
76
+ > &
77
+ OnSignature < ObjectEventTypes , ObjectEvent , EventsKey > &
78
+ CombinedOnSignature <
79
+ | `${CustomEventTypes . BEFOREOPEN } `
80
+ | `${CustomEventTypes . OPEN } `
81
+ | ObjectEventTypes
82
+ | `${CustomEventTypes . CLOSE } `
83
+ | OlEventTypes ,
84
+ EventsKey
85
+ > ;
86
+
87
+ declare un : OnSignature < OlEventTypes | `${CustomEventTypes . CLOSE } `, BaseEvent , void > &
88
+ OnSignature <
89
+ `${CustomEventTypes . BEFOREOPEN } ` | `${CustomEventTypes . OPEN } `,
90
+ ContextMenuEvent ,
91
+ EventsKey
92
+ > &
93
+ OnSignature < ObjectEventTypes , ObjectEvent , void > &
94
+ CombinedOnSignature <
95
+ | `${CustomEventTypes . BEFOREOPEN } `
96
+ | `${CustomEventTypes . OPEN } `
97
+ | ObjectEventTypes
98
+ | `${CustomEventTypes . CLOSE } `
99
+ | OlEventTypes ,
100
+ void
101
+ > ;
102
+
43
103
options : Options ;
44
104
45
105
constructor ( opts : Partial < Options > = { } ) {
@@ -72,14 +132,6 @@ export default class ContextMenu extends Control {
72
132
} ;
73
133
this . disabled = false ;
74
134
this . opened = false ;
75
-
76
- emitter . on (
77
- CustomEventTypes . ADD_MENU_ENTRY ,
78
- ( item : MenuEntry , element : HTMLLIElement ) => {
79
- this . handleAddMenuEntry ( item , element ) ;
80
- } ,
81
- this
82
- ) ;
83
135
}
84
136
85
137
clear ( ) {
@@ -164,6 +216,14 @@ export default class ContextMenu extends Control {
164
216
this . handleMapMove ( ) ;
165
217
} ) ;
166
218
219
+ emitter . on (
220
+ CustomEventTypes . ADD_MENU_ENTRY ,
221
+ ( item : MenuEntry , element : HTMLLIElement ) => {
222
+ this . handleAddMenuEntry ( item , element ) ;
223
+ } ,
224
+ this
225
+ ) ;
226
+
167
227
this . items = this . options . defaultItems
168
228
? this . options . items . concat ( DEFAULT_ITEMS )
169
229
: this . options . items ;
@@ -190,6 +250,8 @@ export default class ContextMenu extends Control {
190
250
this . map
191
251
. getViewport ( )
192
252
. removeEventListener ( this . options . eventType , this . contextMenuEventListener , false ) ;
253
+
254
+ emitter . off ( CustomEventTypes . ADD_MENU_ENTRY ) ;
193
255
}
194
256
195
257
protected removeMenuEntry ( id : string ) {
@@ -203,11 +265,13 @@ export default class ContextMenu extends Control {
203
265
protected handleContextMenu ( evt : MouseEvent ) {
204
266
this . coordinate = this . map . getEventCoordinate ( evt ) ;
205
267
this . pixel = this . map . getEventPixel ( evt ) ;
206
- this . dispatchEvent ( {
207
- type : CustomEventTypes . BEFOREOPEN ,
208
- pixel : this . pixel ,
209
- coordinate : this . coordinate ,
210
- } as unknown as BaseEvent ) ;
268
+ this . dispatchEvent (
269
+ new ContextMenuEvent ( {
270
+ type : CustomEventTypes . BEFOREOPEN ,
271
+ pixel : this . pixel ,
272
+ coordinate : this . coordinate ,
273
+ } )
274
+ ) ;
211
275
212
276
if ( this . disabled ) return ;
213
277
@@ -238,6 +302,14 @@ export default class ContextMenu extends Control {
238
302
this . opened = true ;
239
303
this . positionContainer ( ) ;
240
304
this . container . classList . remove ( CSS_CLASSES . hidden ) ;
305
+
306
+ this . dispatchEvent (
307
+ new ContextMenuEvent ( {
308
+ type : CustomEventTypes . OPEN ,
309
+ pixel : this . pixel ,
310
+ coordinate : this . coordinate ,
311
+ } )
312
+ ) ;
241
313
}
242
314
243
315
protected getMenuEntriesLength ( ) : number {
0 commit comments