1
1
import '../../../../web_modules/@anypoint-web-components/anypoint-listbox/anypoint-listbox.js' ;
2
2
import '../../../../web_modules/@anypoint-web-components/anypoint-item/anypoint-icon-item.js' ;
3
3
import '../../../../web_modules/@advanced-rest-client/arc-icons/arc-icon.js' ;
4
+ import { UiEventTypes } from '../../../../web_modules/@advanced-rest-client/arc-events/index.js' ;
4
5
import { ContextMenuStore } from './ContextMenuStore.js' ;
5
6
6
7
/** @typedef {import('./interfaces').RegisteredCommand } RegisteredCommand */
@@ -65,6 +66,11 @@ export class ContextMenu {
65
66
*/
66
67
currentMenu = undefined ;
67
68
69
+ /**
70
+ * @type {any }
71
+ */
72
+ customArgs = undefined ;
73
+
68
74
/**
69
75
* @type {ClickVector }
70
76
*/
@@ -85,6 +91,7 @@ export class ContextMenu {
85
91
this . menuClickHandler = this . menuClickHandler . bind ( this ) ;
86
92
this . clickHandler = this . clickHandler . bind ( this ) ;
87
93
this . keydownHandler = this . keydownHandler . bind ( this ) ;
94
+ this . customHandler = this . customHandler . bind ( this ) ;
88
95
}
89
96
90
97
/**
@@ -104,6 +111,7 @@ export class ContextMenu {
104
111
this . workspace . addEventListener ( 'contextmenu' , this . contextHandler ) ;
105
112
window . addEventListener ( 'click' , this . clickHandler ) ;
106
113
window . addEventListener ( 'keydown' , this . keydownHandler ) ;
114
+ window . addEventListener ( UiEventTypes . contextMenu , this . customHandler ) ;
107
115
this . connectedValue = true ;
108
116
}
109
117
@@ -115,6 +123,7 @@ export class ContextMenu {
115
123
this . workspace . removeEventListener ( 'contextmenu' , this . contextHandler ) ;
116
124
window . removeEventListener ( 'click' , this . clickHandler ) ;
117
125
window . removeEventListener ( 'keydown' , this . keydownHandler ) ;
126
+ window . removeEventListener ( UiEventTypes . contextMenu , this . customHandler ) ;
118
127
this . connectedValue = false ;
119
128
}
120
129
@@ -141,6 +150,34 @@ export class ContextMenu {
141
150
this . build ( e , target , clickVector , targetVector ) ;
142
151
}
143
152
153
+ /**
154
+ * A handler for the `arccontextmenu` event that triggers the menu for given target passed in the
155
+ * event's detail object.
156
+ * @param {CustomEvent } e
157
+ */
158
+ customHandler ( e ) {
159
+ const { target, mouseEvent, selector, args } = e . detail ;
160
+ const commands = this . listCustomCommands ( selector ) ;
161
+ if ( ! commands . length ) {
162
+ return ;
163
+ }
164
+ mouseEvent . preventDefault ( ) ;
165
+ mouseEvent . stopPropagation ( ) ;
166
+ const clickVector = {
167
+ x : mouseEvent . clientX ,
168
+ y : mouseEvent . clientY ,
169
+ }
170
+ const targetVector = this . readClickPosition ( mouseEvent ) ;
171
+
172
+ this . targetVector = targetVector ;
173
+ this . customArgs = args ;
174
+ const groups = this . groupCommands ( commands ) ;
175
+ this . currentCommands = commands ;
176
+ this . currentTarget = target ;
177
+ this . render ( target , clickVector , groups ) ;
178
+ target . setAttribute ( 'active' , '' ) ;
179
+ }
180
+
144
181
/**
145
182
* Reads {x,y} vector of the click from the pointer event.
146
183
* @param {MouseEvent } e
@@ -292,6 +329,23 @@ export class ContextMenu {
292
329
return result ;
293
330
}
294
331
332
+ /**
333
+ * Lists all custom commands where the command's selector matches passed `selector`.
334
+ *
335
+ * @param {string } selector The build target
336
+ * @returns {RegisteredCommand[] }
337
+ */
338
+ listCustomCommands ( selector ) {
339
+ const { commands } = this ;
340
+ const result = /** @type RegisteredCommand[] */ ( [ ] ) ;
341
+ commands . forEach ( ( cmd ) => {
342
+ if ( cmd . selector === '*' || cmd . selector === selector ) {
343
+ result . push ( cmd ) ;
344
+ }
345
+ } ) ;
346
+ return result ;
347
+ }
348
+
295
349
/**
296
350
* Creates an ordered grouped list of commands
297
351
*
@@ -422,6 +476,9 @@ export class ContextMenu {
422
476
vector : targetVector ,
423
477
command : command . command ,
424
478
} ;
479
+ if ( this . customArgs ) {
480
+ opts . customArgs = this . customArgs ;
481
+ }
425
482
if ( this . #callback && command . command ) {
426
483
this . #callback( opts ) ;
427
484
} else {
0 commit comments