diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-10-19 23:50:27 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-10-21 19:41:47 +0200 |
commit | e036cc9eee1a28d9bef6d2615c22d1d9d1b86829 (patch) | |
tree | 2de560f4b239dc6854a503aa6ca6b7ea2b4b845d /public | |
parent | 3f0414936270267a8f7b74540b0a5d1ce703cccd (diff) | |
download | server-e036cc9eee1a28d9bef6d2615c22d1d9d1b86829.tar.gz |
Add popup menu template code.
Diffstat (limited to 'public')
-rw-r--r-- | public/common/popup.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/public/common/popup.js b/public/common/popup.js new file mode 100644 index 0000000..53923d0 --- /dev/null +++ b/public/common/popup.js @@ -0,0 +1,76 @@ +/* + <menu id="popup"> + <li class="title">TITLE + <li class="separator"> + <li data-action="foo"> Foo! + <li data-action="bar"> Bar? + </menu> + + <main onclick="hide_popup_menu()"> +/* + +function is_action(action, card) { + return view.actions && view.actions[action] && view.actions[action].includes(card) +} + +function show_popup_menu(evt, menu_id, card, title) { + let menu = document.getElementById(menu_id) + + let show = false + for (let item of menu.querySelectorAll("li")) { + let action = item.dataset.action + if (action) { + if (is_action(action, card)) { + show = true + item.classList.add("action") + item.classList.remove("disabled") + item.onclick = function () { + send_action(action, card) + hide_popup_menu() + evt.stopPropagation() + } + } else { + item.classList.remove("action") + item.classList.add("disabled") + item.onclick = null + } + } + } + + if (show) { + menu.onmouseleave = hide_popup_menu + menu.style.display = "block" + if (title) { + let item = menu.querySelector("li.title") + if (item) { + item.onclick = hide_popup_menu + item.textContent = title + } + } + + let w = menu.clientWidth + let h = menu.clientHeight + let x = Math.max(5, Math.min(evt.clientX - w / 2, window.innerWidth - w - 5)) + let y = Math.max(5, Math.min(evt.clientY - 12, window.innerHeight - h - 40)) + menu.style.left = x + "px" + menu.style.top = y + "px" + + evt.stopPropagation() + } else { + menu.style.display = "none" + } +} + +function hide_popup_menu() { + document.getElementById("popup").style.display = "none" +} + +function on_click_card(evt) { + if (view.actions) { + let card = evt.target.my_id + if (is_action("card", card)) + send_action("card", card) + else + show_popup_menu(evt, "popup", card, data.cards[card].title) + } +} |