Skip to content

Commit

Permalink
fix context menus on branches on top of commit
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed May 18, 2023
1 parent c529816 commit 656bb08
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions web/src/directives/context-menu.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import Vue, { nextTick } from 'vue'
import Vue from 'vue'

``###* @typedef {{label:string,icon?:string,action:()=>any}} ContextMenuEntry ###
``###* @typedef {{
oncontextmenu: (this: HTMLElement, ev: MouseEvent) => any
onglobalclick: (ev: MouseEvent) => any
onglobalkeyup: (ev: KeyboardEvent) => any
destroy: () => any
entries_provider: (ev: MouseEvent) => ContextMenuEntry[]
}} ContextMenuData
###

``###* @type {Map<HTMLElement,ContextMenuData>} ###
context_menu_data_by_el = new Map

remove_all_context_menus = =>
context_menu_data_by_el.forEach (menu) =>
menu.destroy()
document.addEventListener 'contextmenu', remove_all_context_menus, false
document.addEventListener 'click', remove_all_context_menus, false
document.addEventListener 'keyup', remove_all_context_menus, false

set_context_menu = (###* @type HTMLElement ### el, ###* @type {(ev: MouseEvent)=>ContextMenuEntry[]} ### entries_provider) =>
existing_context_menu_data = context_menu_data_by_el.get el
if existing_context_menu_data
Expand All @@ -24,7 +30,7 @@ set_context_menu = (###* @type HTMLElement ### el, ###* @type {(ev: MouseEvent)=
# The element(s) created by this is quite similar to the template of <git-action-button>
build_context_menu = (###* @type MouseEvent ### event) =>
entries = entries_provider(event)
return if not entries
return if not entries or wrapper_el
wrapper_el = document.createElement('ul')
wrapper_el.setAttribute('aria-label', 'Context menu')
wrapper_el.classList.add 'context-menu-wrapper'
Expand All @@ -44,35 +50,27 @@ set_context_menu = (###* @type HTMLElement ### el, ###* @type {(ev: MouseEvent)=
entry_el.onclick = entry.action
wrapper_el?.appendChild(entry_el)
document.body.appendChild(wrapper_el)
destroy_context_menu = =>
return if not wrapper_el
document.body.removeChild(wrapper_el)
wrapper_el = null

``###* @type ContextMenuData ###
context_menu_data =
oncontextmenu: (e) =>
e.preventDefault()
setTimeout (=>build_context_menu(e)), 0
onglobalclick: destroy_context_menu
onglobalkeyup: destroy_context_menu
e.stopPropagation()
remove_all_context_menus()
build_context_menu(e)
destroy: =>
return if not wrapper_el
document.body.removeChild(wrapper_el)
wrapper_el = null
entries_provider: entries_provider

el.addEventListener 'contextmenu', context_menu_data.oncontextmenu, false
document.addEventListener 'contextmenu', context_menu_data.onglobalclick, false
document.addEventListener 'click', context_menu_data.onglobalclick, false
document.addEventListener 'keyup', context_menu_data.onglobalkeyup, false

context_menu_data_by_el.set el, context_menu_data

disable_context_menu = (###* @type {HTMLElement} ### el) =>
context_menu_data = context_menu_data_by_el.get el
if not context_menu_data
return
el.removeEventListener 'contextmenu', context_menu_data.oncontextmenu
document.removeEventListener 'contextmenu', context_menu_data.onglobalclick
document.removeEventListener 'click', context_menu_data.onglobalclick
document.removeEventListener 'keyup', context_menu_data.onglobalkeyup
context_menu_data_by_el.delete el

``###* @type {Vue.Directive} ###
Expand Down

0 comments on commit 656bb08

Please sign in to comment.