aboutsummaryrefslogtreecommitdiffstats
path: root/front/contextmenu.ts
diff options
context:
space:
mode:
Diffstat (limited to 'front/contextmenu.ts')
-rw-r--r--front/contextmenu.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/front/contextmenu.ts b/front/contextmenu.ts
new file mode 100644
index 0000000..b5a6c3c
--- /dev/null
+++ b/front/contextmenu.ts
@@ -0,0 +1,51 @@
+import { mk } from './util';
+
+// Some elements have custom right click context menus
+// If there's a custom context menu active, this will be it
+var context_menu = null;
+
+// Create a right click context menu
+export function context(e, entries) {
+ if (context_menu)
+ context_menu.remove();
+
+ context_menu = mk(document.body, 'ul', 'context');
+
+ context_menu.onmousedown = (e) => {
+ e.stopPropagation();
+ }
+
+ context_menu.onclick = (_e) => {
+ context_menu.remove();
+ context_menu = null;
+ }
+
+
+ context_menu.style.left = e.clientX + "px";
+ context_menu.style.top = e.clientY + "px";
+
+ for (const e of entries) {
+ const li = document.createElement('li');
+ li.innerText = e[0];
+ li.onclick = e[1];
+ context_menu.appendChild(li);
+ }
+}
+
+// When we click anywhere, remove the context menu
+// The context menu itself has a onmousedown that prevents propagation so we can click its elements
+document.body.onmousedown = (_e) => {
+ if (context_menu) {
+ context_menu.remove();
+ context_menu = null;
+ }
+}
+
+
+
+export function oncontextmenu_hook(_e) {
+ if (context_menu) {
+ context_menu.remove();
+ context_menu = null;
+ }
+} \ No newline at end of file