-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontextMenu.js
More file actions
152 lines (119 loc) · 4.39 KB
/
Copy pathcontextMenu.js
File metadata and controls
152 lines (119 loc) · 4.39 KB
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
// Sourced @ https://www.cssscript.com/cool-custom-context-menu/
class ContextMenu {
constructor({ target = null, menuItems = [], mode = "dark" }) {
this.target = target;
this.menuItems = menuItems;
this.mode = mode;
this.targetNode = this.getTargetNode();
this.menuItemsNode = this.getMenuItemsNode();
this.isOpened = false;
}
getTargetNode() {
const nodes = document.querySelectorAll(this.target);
if (nodes && nodes.length !== 0) {
return nodes;
} else {
// console.error(`quiCKIE: contextMenu.js | getTargetNode :: "${this.target}" target not found`);
return [];
}
}
getMenuItemsNode() {
const nodes = [];
if (!this.menuItems) {
console.error("getMenuItemsNode :: Please enter menu items");
return [];
}
this.menuItems.forEach((data, index) => {
const item = this.createItemMarkup(data);
item.firstChild.setAttribute(
"style",
`animation-delay: ${index * 0.020}s`
);
nodes.push(item);
});
return nodes;
}
createItemMarkup(data) {
const button = document.createElement("BUTTON");
const item = document.createElement("LI");
if ( data.content ) {
button.innerHTML = data.content
}
button.classList.add("contextMenu-button");
item.classList.add("contextMenu-item");
if (data.divider) {
// This is a divider, so mark the LI and BUTTON
item.setAttribute("data-divider", data.divider);
button.setAttribute("data-divider", data.divider);
}
item.appendChild(button);
if (data.events && data.events.length !== 0) {
Object.entries(data.events).forEach((event) => {
const [key, value] = event;
button.addEventListener(key, value);
});
}
return item;
}
renderMenu() {
const menuContainer = document.createElement("UL");
menuContainer.classList.add("contextMenu");
menuContainer.setAttribute("data-theme", this.mode);
this.menuItemsNode.forEach((item) => menuContainer.appendChild(item));
return menuContainer;
}
closeMenu(menu) {
if (this.isOpened) {
this.isOpened = false;
menu.remove();
}
// No menu item was selected, so remove the ID from the clicked-on element
try{
document.getElementById('__CONTEXTCLICKED__').removeAttribute('id')
} catch(error) {}
}
init() {
const contextMenu = this.renderMenu();
document.addEventListener("click", () => this.closeMenu(contextMenu));
window.addEventListener("blur", () => this.closeMenu(contextMenu));
// document.addEventListener("contextmenu", (e) => {
// this.targetNode.forEach((target) => {
// if (!e.target.contains(target)) {
// contextMenu.remove();
// }
// });
// });
this.targetNode.forEach((target) => {
// This target element (bunnyButton) has not yet had a contextmenu event attached to it
target.addEventListener("contextmenu", (e) => {
// For later reference, mark this target element (BunnyButton) as having been the element that was right-clicked on to open the context menu
let targetElementId = `quiCKIE_bb_${Date.now()}`
target.id = targetElementId
e.preventDefault();
this.isOpened = true;
const { clientX, clientY } = e;
document.body.appendChild(contextMenu);
contextMenu.querySelectorAll('button.contextMenu-button').forEach( button => {
button.setAttribute('data-targetid', targetElementId)
} )
const positionY =
clientY + contextMenu.scrollHeight >= window.innerHeight
? window.innerHeight - contextMenu.scrollHeight - 20
: clientY;
const positionX =
clientX + contextMenu.scrollWidth >= window.innerWidth
? window.innerWidth - contextMenu.scrollWidth - 20
: clientX;
contextMenu.setAttribute(
"style",
`--width: ${contextMenu.scrollWidth}px;
--height: ${contextMenu.scrollHeight}px;
--top: ${positionY}px;
--left: ${positionX}px;`
);
});
// Remove the class that identifies this as a new BunnyButton without a presetsMenu
target.classList.remove('quickie_newBunnyButton')
});
}
}