-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
296 lines (247 loc) · 8.9 KB
/
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* CtxMenu
* Author: Nils Soderman <[email protected]>
* Repo: https://github.com/nils-soderman/Javascript-contextMenu
*/
const ECtxMenuNames = {
menu: "ctx-menu-wrapper",
item: "ctx-menu-item",
separator: "ctx-menu-separator",
hasIcon: "ctx-menu-hasIcon",
darkInvert: "ctx-menu-darkInvert",
};
class CtxMenuManagerClass {
constructor(){
this._currentMenuVisible = null;
this._ctxMenus = new Map();
document.addEventListener('contextmenu', this._eventOpenMenu.bind(this));
const scripts = document.getElementsByTagName('script');
const path = scripts[scripts.length - 1].src.split('?')[0];
const CtxMenuDirectory = path.split('/').slice(0, -1).join('/') + '/';
// Load the stylesheet
var link = document.createElement( "link" );
link.href = CtxMenuDirectory + "ctxmenu.css";
link.type = "text/css";
link.rel = "stylesheet";
document.getElementsByTagName("head")[0].appendChild(link);
};
_eventOpenMenu(e){
if (e.path != undefined) {
var menuAndElement = this._traceCtxMenu(e.path);
} else {
// Microsoft Edge
var menuAndElement = this._msEdgeTraceCtxMenu(e.target);
}
// Close any menu open right now
this.closeCurrentlyOpenedMenu();
if (menuAndElement == null) {
// Open default context menu if no custom menus where found.
return;
}
const menu = menuAndElement[0];
const elementClicked = menuAndElement[1];
if (menu == false){
// All context menus from appearing if the user has blocked the menu using CtxMenuBlock()
e.preventDefault();
return;
}
else if (menu == true){
// Open the defaulty menu if user has set it to default using CtxMenuDefault()
return;
}
// Open the menu
menu._elementClicked = elementClicked;
menu.openMenu(e.pageX, e.pageY);
this._currentMenuVisible = menu;
// Add event listeners to close the window
document.addEventListener("click", CtxCloseCurrentlyOpenedMenus);
window.addEventListener("resize", CtxCloseCurrentlyOpenedMenus);
e.preventDefault();
if(menu._openEventListener != undefined) {
menu._openEventListener();
}
};
closeMenu(menu){
//this.ctxMenuBackground.style.display = "none";
menu.closeMenu();
this._currentMenuVisible = null;
document.removeEventListener("click", CtxCloseCurrentlyOpenedMenus);
window.removeEventListener("resize", CtxCloseCurrentlyOpenedMenus);
};
closeCurrentlyOpenedMenu(){
if (this._currentMenuVisible != null){
this.closeMenu(this._currentMenuVisible);
}
};
_traceCtxMenu(path){
for (var i = 0; i < path.length; ++i) {
const menu = this._ctxMenusHas(path[i]);
if (menu != null){
return [menu, path[i]];
}
}
return null;
};
_msEdgeTraceCtxMenu(element){
while (element != null) {
const menu = this._ctxMenusHas(element);
if (menu != null){
return [menu, element];
}
element = element.parentNode;
}
return null;
};
_ctxMenusHas(element){
if (this._ctxMenus.has(element)) {
return this._ctxMenus.get(element);
}
if(this._ctxMenus.has("#"+element.id)){
return this._ctxMenus.get("#"+element.id);
}
if (element.className != undefined){
const classNames = element.className.split(" ");
for(var i = 0; i < classNames.length; i++) {
if(this._ctxMenus.has("."+classNames[i])){
return this._ctxMenus.get("."+classNames[i]);
}
}
}
if (this._ctxMenus.has(element.nodeName)) {
return this._ctxMenus.get(element.nodeName);
}
return null;
};
getMenuFromElement(element){
return this._ctxMenus.get(element);
};
createNewMenu(element){
var menu = new CtxMenuClass();
this._ctxMenus.set(element, menu);
return menu;
};
setCustomContexMenuValue(element, value){
this._ctxMenus.set(element, value);
};
};
class CtxMenuClass {
constructor(){
// Add the html to the body and hide it
this.menuContainer = document.createElement("div");
this.menuContainer.className = ECtxMenuNames.menu;
document.body.appendChild(this.menuContainer);
this.closeMenu();
this._elementClicked = undefined;
// Event listeners
this._openEventListener = undefined;
this._closeEventListener = undefined;
this._clickEventListener = undefined;
}
addItem(text, customFunction, icon = undefined, index = undefined, bInvertIconDarkMode = false) {
// Create the element
var element = document.createElement("div");
element.className = ECtxMenuNames.item;
// Icon
var iconElement = document.createElement("img");
if (icon != undefined && icon != null) {
iconElement.src = icon;
var bHasIcon = true;
console.log(bInvertIconDarkMode)
if (bInvertIconDarkMode)
iconElement.className = ECtxMenuNames.darkInvert;
} else {
var bHasIcon = false;
}
element.appendChild(iconElement);
element.innerHTML += text;
element.addEventListener("click", function(){
this._callItem(customFunction);
}.bind(this));
if (bHasIcon){
this.menuContainer.classList.add(ECtxMenuNames.hasIcon);
}
this.menuContainer.insertBefore(element, this.menuContainer.childNodes[index]);
return element;
}
addSeparator(index = undefined){
// Add a separator
var separator = document.createElement("div");
separator.className = ECtxMenuNames.separator;
this.menuContainer.insertBefore(separator, this.menuContainer.childNodes[index]);
return separator
}
getItems() {
return this.menuContainer.childNodes
}
getItemAtIndex(index) {
return this.menuContainer.childNodes[index]
}
addEventListener(type, listener){
switch (type) {
case "open":
this._openEventListener = listener;
case "close":
this._closeEventListener = listener;
case "click":
this._clickEventListener = listener;
}
}
openMenu(x, y){
this.menuContainer.style.display = "block";
// Set the screen position of the menu
// Ensure the menu doesn't go outside of the widnow
const PageWidth = (document.documentElement.clientWidth + document.documentElement.scrollLeft);
const PageHeight = (document.documentElement.clientHeight + document.documentElement.scrollTop);
if (x + this.menuContainer.offsetWidth > PageWidth) {
x = PageWidth - this.menuContainer.offsetWidth - 1;
}
if (y + this.menuContainer.offsetHeight > PageHeight) {
y = PageHeight - this.menuContainer.offsetHeight - 1;
}
this.menuContainer.style.left = x + "px";
this.menuContainer.style.top = y + "px";
}
closeMenu(){
// Hide the menu
this.menuContainer.style.left = "0px";
this.menuContainer.style.top = "0px";
this.menuContainer.style.display = "none";
if (this._closeEventListener != undefined){
this._closeEventListener();
}
}
_callItem(customFunction){
// Called when an item has been clicked
this.closeMenu();
// Delay function one tick so the page has time to redraw the page and hide the context menu
setTimeout(function(){
customFunction(this._elementClicked);
if (this._clickEventListener != undefined) {
this._clickEventListener(item);
}
}.bind(this), 1);
}
}
function CtxMenu(element){
// Initialize a context menu
if (element == undefined){
element = document;
}
if (ctxMenuManager.getMenuFromElement(element) != undefined) {
return ctxMenuManager.getMenuFromElement(element);
}
return ctxMenuManager.createNewMenu(element);
}
function CtxMenuBlock(element){
// Block the context menu from appearing on an element
ctxMenuManager.setCustomContexMenuValue(element, false);
}
function CtxMenuDefault(element){
// Set an element to use the browsers default context menu
ctxMenuManager.setCustomContexMenuValue(element, true);
}
function CtxCloseCurrentlyOpenedMenus(){
ctxMenuManager.closeCurrentlyOpenedMenu();
}
// Initialize the ctxMenuManager
var ctxMenuManager = new CtxMenuManagerClass();