-
-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathcontextMenu.js
More file actions
166 lines (160 loc) · 4.53 KB
/
contextMenu.js
File metadata and controls
166 lines (160 loc) · 4.53 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import i18n from '../i18n'
export default function (mind, option) {
let createTips = words => {
let div = document.createElement('div')
div.innerHTML = words
div.style.cssText= 'position:absolute;bottom:20px;left:50%;transform:translateX(-50%);'
return div
}
let createLi = (id, name, keyname) => {
let li = document.createElement('li')
li.id = id
li.innerHTML = `<span>${name}</span><span>${keyname}</span>`
return li
}
let locale = i18n[mind.locale] ? mind.locale : 'en'
let add_child = createLi('cm-add_child', i18n[locale].addChild, 'tab')
let add_sibling = createLi('cm-add_sibling', i18n[locale].addSibling, 'enter')
let remove_child = createLi(
'cm-remove_child',
i18n[locale].removeNode,
'delete'
)
let focus = createLi('cm-fucus', i18n[locale].focus, '')
let unfocus = createLi('cm-unfucus', i18n[locale].cancelFocus, '')
let up = createLi('cm-up', i18n[locale].moveUp, 'PgUp')
let down = createLi('cm-down', i18n[locale].moveDown, 'Pgdn')
let link = createLi('cm-down', i18n[locale].link, '')
let menuUl = document.createElement('ul')
menuUl.className = 'menu-list'
menuUl.appendChild(add_child)
menuUl.appendChild(add_sibling)
menuUl.appendChild(remove_child)
if (!option || option.focus) {
menuUl.appendChild(focus)
menuUl.appendChild(unfocus)
}
menuUl.appendChild(up)
menuUl.appendChild(down)
if (!option || option.link) {
menuUl.appendChild(link)
}
if (option && option.extend) {
for (let i = 0; i < option.extend.length; i++) {
let item = option.extend[i]
let dom = createLi(item.name, item.name, item.key || '')
menuUl.appendChild(dom)
dom.onclick = e => {
item.onclick(e)
}
}
}
let menuContainer = document.createElement('cmenu')
menuContainer.appendChild(menuUl)
menuContainer.hidden = true
mind.container.append(menuContainer)
let isRoot = true
mind.container.oncontextmenu = function (e) {
e.preventDefault()
// console.log(e.pageY, e.screenY, e.clientY)
let target = e.target
if (target.tagName === 'TPC') {
if (target.parentElement.tagName === 'ROOT') {
isRoot = true
} else {
isRoot = false
}
if (isRoot) {
focus.className = 'disabled'
up.className = 'disabled'
down.className = 'disabled'
add_sibling.className = 'disabled'
remove_child.className = 'disabled'
} else {
focus.className = ''
up.className = ''
down.className = ''
add_sibling.className = ''
remove_child.className = ''
}
mind.selectNode(target)
menuContainer.hidden = false
let height = menuUl.offsetHeight
let width = menuUl.offsetWidth
if (height + e.clientY > window.innerHeight) {
menuUl.style.top = ''
menuUl.style.bottom = '0px'
} else {
menuUl.style.bottom = ''
menuUl.style.top = e.clientY + 15 + 'px'
}
if (width + e.clientX > window.innerWidth) {
menuUl.style.left = ''
menuUl.style.right = '0px'
} else {
menuUl.style.right = ''
menuUl.style.left = e.clientX + 10 + 'px'
}
}
}
menuContainer.onclick = e => {
if (e.target === menuContainer) menuContainer.hidden = true
}
add_child.onclick = e => {
mind.addChild()
menuContainer.hidden = true
}
add_sibling.onclick = e => {
if (isRoot) return
mind.insertSibling()
menuContainer.hidden = true
}
remove_child.onclick = e => {
if (isRoot) return
mind.removeNode()
menuContainer.hidden = true
}
focus.onclick = e => {
if (isRoot) return
mind.focusNode(mind.currentNode)
menuContainer.hidden = true
}
unfocus.onclick = e => {
mind.cancelFocus()
menuContainer.hidden = true
}
up.onclick = e => {
if (isRoot) return
mind.moveUpNode()
menuContainer.hidden = true
}
down.onclick = e => {
if (isRoot) return
mind.moveDownNode()
menuContainer.hidden = true
}
link.onclick = e => {
menuContainer.hidden = true
let from = mind.currentNode
let tips = createTips(i18n[locale].clickTips)
m.container.appendChild(tips)
mind.map.addEventListener(
'click',
e => {
e.preventDefault()
tips.remove()
if (
e.target.parentElement.nodeName === 'T' ||
e.target.parentElement.nodeName === 'ROOT'
) {
mind.createLink(from, mind.currentNode)
} else {
console.log('取消连接')
}
},
{
once: true,
}
)
}
}