forked from seisiuneer/abctools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context-menu.js
205 lines (177 loc) · 5.14 KB
/
context-menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const gCM_instances = [];
let gCM_nextId = 0;
// Tiny polyfill for Element.matches() for IE
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector;
}
// Gets an element's next/previous sibling that matches the given selector
function getSibling(el, selector, direction = 1) {
const sibling =
direction > 0 ? el.nextElementSibling : el.previousElementSibling;
if (!sibling || sibling.matches(selector)) {
return sibling;
}
return getSibling(sibling, selector, direction);
}
// Fires custom event on given element
function emit(el, type, data = {}) {
const event = document.createEvent('Event');
Object.keys(data).forEach((key) => {
event[key] = data[key];
});
event.initEvent(type, true, true);
el.dispatchEvent(event);
}
class ContextMenu {
constructor(
selector,
items,
options = {
className: '',
minimalStyling: false,
},
) {
this.selector = selector;
this.items = items;
this.options = options;
this.id = gCM_nextId++;
this.target = null;
this.create();
gCM_instances.push(this);
}
// Creates DOM elements, sets up event listeners
create() {
// Create root <ul>
this.menu = document.createElement('ul');
this.menu.className = 'ContextMenu';
this.menu.setAttribute('data-contextmenu', this.id);
this.menu.setAttribute('tabindex', -1);
this.menu.addEventListener('keyup', (e) => {
switch (e.which) {
case 38:
this.moveFocus(-1);
break;
case 40:
this.moveFocus(1);
break;
case 27:
this.hide();
break;
default:
// do nothing
}
});
if (!this.options.minimalStyling) {
this.menu.classList.add('ContextMenu--theme-default');
}
if (this.options.className) {
this.options.className
.split(' ')
.forEach((cls) => this.menu.classList.add(cls));
}
// Create <li>'s for each menu item
this.items.forEach((item, index) => {
const li = document.createElement('li');
if (!('name' in item)) {
// Insert a divider
li.className = 'ContextMenu-divider';
} else {
li.className = 'ContextMenu-item';
li.textContent = item.name;
li.setAttribute('data-contextmenuitem', index);
li.setAttribute('tabindex', 0);
li.addEventListener('click', this.select.bind(this, li));
li.addEventListener('keyup', (e) => {
if (e.which === 13) {
this.select(li);
}
});
}
this.menu.appendChild(li);
});
// Add root element to the <body>
document.body.appendChild(this.menu);
emit(this.menu, 'created');
}
// Shows context menu
show(e) {
// Keep the context menu from going off the right of the screen
if (isMobileBrowser() && (!gIsQuickEditor)){
this.menu.style.left = `${e.pageX-200}px`;
this.menu.style.top = `${e.pageY}px`;
}
else{
this.menu.style.left = `${e.pageX}px`;
this.menu.style.top = `${e.pageY}px`;
}
this.menu.classList.add('is-open');
this.target = e.target;
// Give context menu focus
this.menu.focus();
// Disable native context menu
e.preventDefault();
emit(this.menu, 'shown');
}
// Hides context menu
hide() {
this.menu.classList.remove('is-open');
this.target = null;
emit(this.menu, 'hidden');
}
// Selects the given item and calls its handler
select(item) {
const itemId = item.getAttribute('data-contextmenuitem');
if (this.items[itemId]) {
// Call item handler with target element as parameter
this.items[itemId].fn(this.target);
}
this.hide();
emit(this.menu, 'itemselected');
}
// Moves focus to the next/previous menu item
moveFocus(direction = 1) {
const focused = this.menu.querySelector('[data-contextmenuitem]:focus');
let next;
if (focused) {
next = getSibling(focused, '[data-contextmenuitem]', direction);
}
if (!next) {
next =
direction > 0
? this.menu.querySelector('[data-contextmenuitem]:first-child')
: this.menu.querySelector('[data-contextmenuitem]:last-child');
}
if (next) next.focus();
}
// Convenience method for adding an event listener
on(type, fn) {
this.menu.addEventListener(type, fn);
}
// Convenience method for removing an event listener
off(type, fn) {
this.menu.removeEventListener(type, fn);
}
// Removes DOM elements, stop listeners
destroy() {
this.menu.parentElement.removeChild(this.menu);
this.menu = null;
gCM_instances.splice(gCM_instances.indexOf(this), 1);
}
}
var gInContextMenu = false;
// Listen for c event to show menu
document.addEventListener('click', (e) => {
gCM_instances.forEach((menu) => {
if (e.target.matches(menu.selector)) {
menu.show(e);
}
});
});
// Listen for click event to hide menu
document.addEventListener('click', (e) => {
gCM_instances.forEach((menu) => {
if ((!e.target.matches(`[data-contextmenu="${menu.id}"], [data-contextmenu="${menu.id}"] *`)) && (!e.target.matches(menu.selector))) {
menu.hide();
}
});
});