From 0255b5f7f31c4c6caefc736c59ba6959d671a92d Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Sat, 20 Mar 2021 10:58:25 +0200 Subject: Broke up loggedin.js into modules --- front/contextmenu.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 front/contextmenu.ts (limited to 'front/contextmenu.ts') 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 -- cgit v1.2.3