This repository was archived by the owner on Apr 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbasicContext.js
More file actions
264 lines (180 loc) · 5.66 KB
/
Copy pathbasicContext.js
File metadata and controls
264 lines (180 loc) · 5.66 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
let overflow = null
const ITEM = 'item',
SEPARATOR = 'separator'
const dom = function(elem = '') {
return document.querySelector('.basicContext ' + elem)
}
const valid = function(item = {}) {
let emptyItem = (Object.keys(item).length===0 ? true : false)
if (emptyItem===true) item.type = SEPARATOR
if (item.type==null) item.type = ITEM
if (item.class==null) item.class = ''
if (item.visible!==false) item.visible = true
if (item.icon==null) item.icon = null
if (item.title==null) item.title = 'Undefined'
// Add disabled class when item disabled
if (item.disabled!==true) item.disabled = false
if (item.disabled===true) item.class += ' basicContext__item--disabled'
// Item requires a function when
// it's not a separator and not disabled
if (item.fn==null && item.type!==SEPARATOR && item.disabled===false) {
console.warn(`Missing fn for item '${ item.title }'`)
return false
}
return true
}
const buildItem = function(item, num) {
let html = '',
span = ''
// Parse and validate item
if (valid(item)===false) return ''
// Skip when invisible
if (item.visible===false) return ''
// Give item a unique number
item.num = num
// Generate span/icon-element
if (item.icon!==null) span = `<span class='basicContext__icon ${ item.icon }'></span>`
// Generate item
if (item.type===ITEM) {
html = `
<tr class='basicContext__item ${ item.class }'>
<td class='basicContext__data' data-num='${ item.num }'>${ span }${ item.title }</td>
</tr>
`
} else if (item.type===SEPARATOR) {
html = `
<tr class='basicContext__item basicContext__item--separator'></tr>
`
}
return html
}
const build = function(items) {
let html = ''
html += `
<div class='basicContextContainer'>
<div class='basicContext'>
<table>
<tbody>
`
items.forEach((item, i) => html += buildItem(item, i))
html += `
</tbody>
</table>
</div>
</div>
`
return html
}
const getNormalizedEvent = function(e = {}) {
let pos = {
x : e.clientX,
y : e.clientY
}
if (e.type==='touchend' && (pos.x==null || pos.y==null)) {
// We need to capture clientX and clientY from original event
// when the event 'touchend' does not return the touch position
let touches = e.changedTouches
if (touches!=null&&touches.length>0) {
pos.x = touches[0].clientX
pos.y = touches[0].clientY
}
}
// Position unknown
if (pos.x==null || pos.x < 0) pos.x = 0
if (pos.y==null || pos.y < 0) pos.y = 0
return pos
}
const getPosition = function(e, context) {
// Get the click position
let normalizedEvent = getNormalizedEvent(e)
// Set the initial position
let x = normalizedEvent.x,
y = normalizedEvent.y
// Get size of browser
let browserSize = {
width : window.innerWidth,
height : window.innerHeight
}
// Get size of context
let contextSize = {
width : context.offsetWidth,
height : context.offsetHeight
}
// Fix position based on context and browser size
if ((x + contextSize.width) > browserSize.width) x = x - ((x + contextSize.width) - browserSize.width)
if ((y + contextSize.height) > browserSize.height) y = y - ((y + contextSize.height) - browserSize.height)
// Make context scrollable and start at the top of the browser
// when context is higher than the browser
if (contextSize.height > browserSize.height) {
y = 0
context.classList.add('basicContext--scrollable')
}
// Calculate the relative position of the mouse to the context
let rx = normalizedEvent.x - x,
ry = normalizedEvent.y - y
return { x, y, rx, ry }
}
const bind = function(item = {}) {
if (item.fn==null) return false
if (item.visible===false) return false
if (item.disabled===true) return false
dom(`td[data-num='${ item.num }']`).onclick = item.fn
dom(`td[data-num='${ item.num }']`).oncontextmenu = item.fn
return true
}
const show = function(items, e, fnClose, fnCallback) {
// Build context
let html = build(items)
// Add context to the body
document.body.insertAdjacentHTML('beforeend', html)
// Save current overflow and block scrolling of site
if (overflow==null) {
overflow = document.body.style.overflow
document.body.style.overflow = 'hidden'
}
// Cache the context
let context = dom()
// Calculate position
let position = getPosition(e, context)
// Set position
context.style.left = `${ position.x }px`
context.style.top = `${ position.y }px`
context.style.transformOrigin = `${ position.rx }px ${ position.ry }px`
context.style.opacity = 1
// Close fn fallback
if (fnClose==null) fnClose = close
// Bind click on background
context.parentElement.onclick = fnClose
context.parentElement.oncontextmenu = fnClose
// Bind click on items
items.forEach(bind)
// Do not trigger default event or further propagation
if (typeof e.preventDefault === 'function') e.preventDefault()
if (typeof e.stopPropagation === 'function') e.stopPropagation()
// Call callback when a function
if (typeof fnCallback === 'function') fnCallback()
return true
}
const visible = function() {
let elem = dom()
if (elem==null || elem.length===0) return false
else return true
}
const close = function() {
if (visible()===false) return false
let container = document.querySelector('.basicContextContainer')
container.parentElement.removeChild(container)
// Reset overflow to its original value
if (overflow!=null) {
document.body.style.overflow = overflow
overflow = null
}
return true
}
return {
ITEM,
SEPARATOR,
show,
visible,
close
}