From e036cc9eee1a28d9bef6d2615c22d1d9d1b86829 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 19 Oct 2023 23:50:27 +0200 Subject: Add popup menu template code. --- public/common/popup.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 public/common/popup.js (limited to 'public') 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 @@ +/* + +
  • TITLE +
  • +
  • Foo! +
  • Bar? +
  • + +
    +/* + +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) + } +} -- cgit v1.2.3