-
Notifications
You must be signed in to change notification settings - Fork 4
/
OEEMenu.js
92 lines (75 loc) · 2.89 KB
/
OEEMenu.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
var OEEexidString=document.currentScript.src.match("([a-z]{32})|([0-9a-f-]{36})")[0];
var lightIsAutomatic=true;
var portWithBackground=null;
if(typeof OEEexEscape == 'undefined'){
OEEexEscape = trustedTypes.createPolicy("OEEexEscape", {
createHTML: (string, sink) => string
});
}
function addOEEMenu(){
let button=document.createElement('ee-menu-button');
button.innerHTML=OEEexEscape.createHTML('<img slot="button" style="max-height: 25px; margin-right:-5px;" src="chrome-extension://'+OEEexidString+'/images/logo_white_OEEex_open_128.png">');
button.setAttribute('align',"right");
let userBoxElement=document.getElementsByTagName('user-box')
if(userBoxElement && userBoxElement.length>0)
{
var localRoot=userBoxElement[0].shadowRoot;
localRoot.children[0].insertBefore(button,localRoot.children[0].firstChild)
}
addContextMneu(button);
}
contextOEEMenuRegistationMenuItem=null;
function addContextMneu(button){
const contextMenu = document.createElement('div');
contextMenu.classList.add('oeeMenuContextContainer');
document.body.appendChild(contextMenu);
const menuItems = [];
function registerMenuItem(label, action) {
menuItems.push({ label, action });
}
contextOEEMenuRegistationMenuItem=registerMenuItem;
// Step 3: Function to populate and display the context menu
function showContextMenu(x, y) {
contextMenu.innerHTML = OEEexEscape.createHTML(''); // Clear existing items
menuItems.forEach(item => {
const button = document.createElement('button');
button.classList.add('oeeMenuContextButton');
button.textContent = OEEexEscape.createHTML(item.label);
button. => {
item.action();
contextMenu.style.display = 'none'; // Hide menu after action
};
contextMenu.appendChild(button);
});
contextMenu.style.left = `${x}px`;
contextMenu.style.top = `${y}px`;
contextMenu.style.display = 'block';
}
button.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent click from propagating to the document
// Use the button's position as the anchor point for the context menu
const rect = e.target.getBoundingClientRect();
const x = rect.left;
const y = rect.bottom; // Position the menu at the bottom of the button
showContextMenu(x, y);
});
// Add an event listener to the entire document
document.addEventListener('click', (event) => {
// Check if the click was outside the contextMenu
if (!contextMenu.contains(event.target)) {
contextMenu.style.display = 'none';
}
});
document.addEventListener('keydown', (event) => {
if ((event.keyCode == 27)) {
contextMenu.style.display = 'none';
}
});
// Ensure the menu doesn't close when clicking inside it
contextMenu.addEventListener('click', (event) => {
event.stopPropagation();
});
}
addOEEMenu();
// Example registration of a menu item
contextOEEMenuRegistationMenuItem('Options', () => window.open('chrome-extension://'+OEEexidString+'/options.html', '_blank'));