-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_menu_handler.js
77 lines (71 loc) · 3.03 KB
/
context_menu_handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const contextMenus = (function() {
const contextPane = document.createElement('div')
contextPane.className = 'contextPane'
const contextMenus = [
createContextMenu([['./assets/lock.png', 'Toggle Security'], 'Toggle Interiors', 'Set Colour:', 'Move up', 'Move down', 'Delete']),
createContextMenu(['Delete', 'Remove Employee']),
createContextMenu(['Set Joining Date', 'Set Leaving Date', 'Clear Join & Leave', 'Toggle Interiors', 'Full-time:', 'Delete']),
createContextMenu(['Delete', 'Minimum:', 'Maximum:', 'Rename:']),
createContextMenu(['Rename', 'Duplicate', 'Delete'])
]
let active = false
function openContextMenu(index, cbs, event, beforeOpen) {
if(!active) {
active = true
const menu = contextMenus[index].cloneNode(true)
const links = Array.from(menu.querySelectorAll('.partialWidth, .fullWidth'))
links.forEach((link, i) => {
link.addEventListener('click', cbs[i])
})
contextPane.appendChild(menu)
if(event.pageX + 125 < window.innerWidth + window.pageXOffset) {
contextPane.style.left = event.pageX + 'px'
} else contextPane.style.left = event.pageX - 125 + 'px'
if(event.pageY + 150 < window.innerHeight + window.pageYOffset) {
contextPane.style.top = event.pageY + 'px'
} else contextPane.style.top = event.pageY - 150 + 'px'
if(beforeOpen) beforeOpen(contextPane)
document.body.appendChild(contextPane)
setTimeout(() => addEventListener('mousedown', removeContextPane), 0)
}
}
function removeContextPane(event) {
if(!(event.target == contextPane || contextPane.contains(event.target))) {
closeContextMenu()
}
}
function closeContextMenu() {
removeEventListener('mousedown', removeContextPane)
document.body.removeChild(contextPane)
contextPane.innerHTML = ''
active = false
}
function createContextMenu(details) {
const fragment = document.createDocumentFragment()
details.forEach((item, i) => {
let text = document.createElement('div')
if(item.constructor === Array) {
let image = document.createElement('img')
let wrapper = document.createElement('div')
wrapper.className = 'wrapper'
text.className = 'partialWidth e' + i
image.src = item[0]
text.textContent = item[1]
wrapper.appendChild(image)
fragment.appendChild(wrapper)
}
else {
text.className = 'fullWidth e' + i
text.textContent = item
}
fragment.appendChild(text)
})
return fragment
}
return {
open: openContextMenu, close: closeContextMenu, pane: contextPane
}
})()
document.addEventListener('contextmenu', event => {
event.preventDefault()
})