/
component-helper.js
641 lines (576 loc) · 16.5 KB
/
component-helper.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/**
* Component helpers
*
*/
import { registerElement } from './custom-element'
export { registerElement }
import keycode from 'keycode'
import whatInput from 'what-input'
export const PLATFORM_MAC = 'Mac|iPad|iPhone|iPod'
export const PLATFORM_WIN = 'Win'
if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'test' &&
typeof window !== 'undefined'
) {
window.IS_TEST = true
}
// run component helper functions
whatInput.specificKeys([9])
defineIsTouch()
defineNavigator()
export const isMac = () =>
typeof navigator !== 'undefined' &&
navigator.platform.match(new RegExp(PLATFORM_MAC)) !== null
/**
* Check if device is touch device or not
*/
let IS_TOUCH_DEVICE = undefined
export function isTouchDevice() {
if (typeof IS_TOUCH_DEVICE !== 'undefined') {
if (typeof window !== 'undefined') {
window.IS_TOUCH_DEVICE = IS_TOUCH_DEVICE
}
return IS_TOUCH_DEVICE
}
return IS_TOUCH_DEVICE
}
/**
* Detects if device supports touches
*
* @param {[type]} [interactive=true}] [Makes it posible that the state changes interactive]
* @return {[type]} [void]
*/
export function defineIsTouch({ interactive = true } = {}) {
const handleDefineTouch = () => {
if (typeof document === 'undefined' || typeof window === 'undefined') {
return
}
// to give it a change to have isTouch from the very beginning
if (unsafeIsTouchDeviceCheck()) {
document.documentElement.setAttribute('data-is-touch', true)
IS_TOUCH_DEVICE = true
}
window.addEventListener(
'touchstart',
function onTouch() {
try {
if (IS_TOUCH_DEVICE !== true) {
document.documentElement.setAttribute('data-is-touch', true)
}
IS_TOUCH_DEVICE = true
} catch (e) {
console.warn('Could not apply "touch attribute"', e)
}
if (!interactive) {
window.removeEventListener('touchstart', onTouch, false)
}
},
false
)
window.addEventListener(
'mouseover',
function onHover() {
try {
if (IS_TOUCH_DEVICE === true) {
document.documentElement.removeAttribute('data-is-touch')
}
IS_TOUCH_DEVICE = false
} catch (e) {
console.warn('Could not apply "touch attribute"', e)
}
if (!interactive) {
window.removeEventListener('mouseover', onHover, false)
}
},
false
)
document.removeEventListener('DOMContentLoaded', handleDefineTouch)
}
if (
typeof document !== 'undefined' &&
document.readyState === 'loading'
) {
document.addEventListener('DOMContentLoaded', handleDefineTouch)
} else {
handleDefineTouch()
}
}
function unsafeIsTouchDeviceCheck() {
if (typeof document === 'undefined' || typeof window === 'undefined') {
return false
}
let result = false
try {
if (window.PointerEvent && 'maxTouchPoints' in navigator) {
// IE gives 1, even on no touch systems, therefore we check for 1 or more
if (navigator.maxTouchPoints > 1) {
result = true
}
} else {
if (
window.matchMedia &&
window.matchMedia('(any-pointer: coarse)').matches
) {
result = true
} else if (
'ontouchstart' in window &&
document.createEvent('TouchEvent')
) {
result = true
}
}
} catch (e) {
result = false
}
return result
}
export function defineNavigator() {
const handleNavigator = () => {
if (
typeof document === 'undefined' ||
typeof window === 'undefined' ||
typeof navigator === 'undefined'
) {
return
}
try {
if (!window.IS_TEST) {
if (navigator.platform.match(new RegExp(PLATFORM_MAC)) !== null) {
document.documentElement.setAttribute('data-os', 'mac')
} else if (
navigator.platform.match(new RegExp(PLATFORM_WIN)) !== null
) {
document.documentElement.setAttribute('data-os', 'win')
}
} else {
document.documentElement.setAttribute('data-os', 'other')
}
} catch (e) {
console.warn('Could not apply "os attribute"', e)
}
document.removeEventListener('DOMContentLoaded', handleNavigator)
}
if (
typeof document !== 'undefined' &&
document.readyState === 'loading'
) {
document.addEventListener('DOMContentLoaded', handleNavigator)
} else {
handleNavigator()
}
}
export const validateDOMAttributes = (props, params) => {
// if there is an "attributes" prop, prepare these
// mostly used for prop example usage
if (props && props.attributes) {
let attr = props.attributes
if (attr) {
if (attr[0] === '{') attr = JSON.parse(attr)
if (attr && typeof attr === 'object') {
Object.entries(attr).forEach(([key, value]) => {
Object.assign(params, { [key]: value })
})
}
}
}
// remove disabled, in case it is false (this is for web components support)
if (params.disabled === null || String(params.disabled) === 'false') {
delete params.disabled
}
if (typeof params.top !== 'undefined') {
delete params.top
}
if (typeof params.right !== 'undefined') {
delete params.right
}
if (typeof params.bottom !== 'undefined') {
delete params.bottom
}
if (typeof params.left !== 'undefined') {
delete params.left
}
// in case disabled is a string, it its enabled, send it in as a true (this is for web components support)
else if (params.disabled === 'true') {
params.disabled = true
}
if (params.disabled) {
params['aria-disabled'] = true
}
if (props && props.tabindex) {
let tabIndex = props.tabindex
if (tabIndex === 'off') {
tabIndex = '-1'
}
params['tabIndex'] = tabIndex
}
// make sure we don't return a render prop as a DOM attribute
if (params && typeof params === 'object') {
for (const i in params) {
if (
// is React
typeof params[i] === 'function' &&
// only React Style props, like onClick are allowed
!/(^[a-z]{1,}[A-Z]{1})/.test(i)
) {
delete params[i]
// filter out invalid attrinutes
} else if (
// we dont want NULL values
params[i] === null ||
// we dont want
/[^a-z-]/i.test(i)
// (typeof params[i] !== 'string' && /[^a-z-]/i.test(i))
) {
delete params[i]
}
}
}
return params
}
export const processChildren = props => {
const res =
typeof props.children === 'function'
? props.children(props)
: props.children
// if we get several react children witch representates only a text
if (Array.isArray(res)) {
const onlyTexts = res.reduce((pV, cV) => {
if (typeof cV === 'string' || typeof cV === 'number') {
pV.push(cV)
}
return pV
}, [])
// if there was one or more text elements
if (onlyTexts.length === res.length && onlyTexts.length > 0) {
return onlyTexts.join('')
}
}
return res
}
// extends given objects recursively and removes entries with null values
export const extend = (...objects) => {
const list = Array.from(objects)
const recursive = list[0] !== false
return list.reduce((acc1, object) => {
if (object) {
acc1 = Object.assign(
acc1,
Object.entries(object).reduce((acc2, [key, value]) => {
if (value !== null) {
if (recursive && typeof value === 'object') {
value = extend(acc1[key] || {}, value)
if (Object.keys(value).length > 0) {
acc2[key] = value
}
} else {
acc2[key] = value
}
}
return acc2
}, {})
)
}
return acc1
}, {})
}
// extends props from a given context
// but give the context second priority only
export const extendPropsWithContext = (
props,
defaults = {},
...contexts
) => {
const context = contexts.reduce((acc, cur) => {
if (cur) {
acc = { ...acc, ...cur }
}
return acc
}, {})
return {
...props,
...Object.entries(context).reduce((acc, [key, value]) => {
if (
// check if a prop of the same name exists
typeof props[key] !== 'undefined' &&
// and if it was NOT defined as a component prop, because its still the same as the defualts
props[key] === defaults[key]
) {
// then we use the context value
acc[key] = value
}
return acc
}, {})
}
}
// check if value is "truthy"
export const isTrue = value => {
if (
value !== null &&
typeof value !== 'undefined' &&
(String(value) === 'true' || String(value) === '1')
) {
return true
}
return false
}
export const dispatchCustomElementEvent = (
src,
eventName,
eventObject
) => {
let ret = null
// distribute dataset like "data-*" to both currentTarget and target
if (eventObject && eventObject.attributes && eventObject.event) {
const currentTarget = eventObject.event.currentTarget
if (currentTarget) {
try {
// 1. create new dataset, and copy if exists
const dataset = { ...(currentTarget.dataset || {}) }
// 2. copy in our attributes if they are of "data-" type
const attributes = { ...eventObject.attributes }
for (const i in attributes) {
if (/^data-/.test(i)) {
dataset[String(i).replace(/^data-/, '')] = attributes[i]
}
}
// 3. and distribute them to the targets. Use the for method because of immutability
for (const i in dataset) {
if (eventObject.event.currentTarget.dataset) {
eventObject.event.currentTarget.dataset[i] = dataset[i]
}
if (
eventObject.event.target &&
eventObject.event.target.dataset
) {
eventObject.event.target.dataset[i] = dataset[i]
}
}
} catch (e) {
console.warn('Error on handling dataset:', e)
}
}
}
const props = (src && src.props) || src
// call Web Component events
if (props.custom_element) {
if (typeof props.custom_element.fireEvent === 'function') {
ret = props.custom_element.fireEvent(eventName, eventObject)
}
}
// call the default snail case event
if (typeof props[eventName] === 'function') {
ret = props[eventName].apply(src, [eventObject])
}
// call Syntetic React event camelCase naming events
eventName = toPascalCase(eventName)
if (typeof props[eventName] === 'function') {
ret = props[eventName].apply(src, [eventObject])
}
return ret
}
// transform on_click to onClick
export const toPascalCase = s =>
s
.split(/_/g)
.reduce(
(acc, cur, i) =>
acc +
(i === 0
? cur
: cur.replace(
/(\w)(\w*)/g,
(g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()
)),
''
)
export const pickRenderProps = (props, renderProps) =>
Object.entries(props)
.filter(([key, value]) => {
if (
typeof renderProps[key] !== 'undefined' || // TODO: remove this because of security notation
key === 'children' ||
key === 'custom_method'
)
return false
return typeof value === 'function'
})
.reduce((obj, [key, value]) => {
obj[key] = value // TODO: remove this because of security notation
return obj
}, {})
/**
* [detectOutsideClick Detects a click outside a given DOM element]
* @param {[type]} ignoreElement [The element we want to protect from a click]
* @param {[type]} onSuccess [Will be called on outside click]
* @return {[type]} [void]
*/
export const detectOutsideClick = (ignoreElement, onSuccess) =>
new DetectOutsideClickClass(ignoreElement, onSuccess)
// Used by detectOutsideClick
export class DetectOutsideClickClass {
constructor(ignoreElement, onSuccess) {
if (
!this.handleClickOutside &&
typeof document !== 'undefined' &&
typeof window !== 'undefined'
) {
this.handleClickOutside = event => {
this.checkOutsideClick(
{
currentElement: event.target,
ignoreElement
},
() => typeof onSuccess === 'function' && onSuccess({ event })
)
}
document.addEventListener('mousedown', this.handleClickOutside)
this.keydownCallback = event => {
const keyCode = keycode(event)
if (keyCode === 'esc') {
window.removeEventListener('keydown', this.keydownCallback)
if (typeof onSuccess === 'function') {
onSuccess({ event })
}
}
}
window.addEventListener('keydown', this.keydownCallback)
// use keyup so we get the correct new target
this.keyupCallback = event => {
const keyCode = keycode(event)
if (
keyCode === 'tab' &&
typeof this.handleClickOutside === 'function'
) {
this.handleClickOutside(event, () => {
if (this.keyupCallback)
window.removeEventListener('keyup', this.keyupCallback)
})
}
}
window.addEventListener('keyup', this.keyupCallback)
}
}
remove() {
if (this.handleClickOutside && typeof document !== 'undefined') {
document.removeEventListener('mousedown', this.handleClickOutside)
this.handleClickOutside = null
}
if (this.keydownCallback && typeof window !== 'undefined') {
window.removeEventListener('keydown', this.keydownCallback)
this.keydownCallback = null
}
if (this.keyupCallback && typeof window !== 'undefined') {
window.removeEventListener('keyup', this.keyupCallback)
this.keyupCallback = null
}
}
checkOutsideClick = (
{ currentElement, ignoreElement },
onSuccess = null
) => {
try {
// scrollbars are on HTML, therefore we ignroe the click
if (
typeof currentElement.tagName === 'undefined' ||
/html/i.test(currentElement.tagName) // we may also ignore |body
) {
return // stop here
}
// check if element has like "overflow: scroll"
if (this.checkIfHasScrollbar(currentElement)) {
return // stop here
}
// check the rest
do {
if (currentElement === ignoreElement) {
return // stop here
}
currentElement = currentElement.parentNode
} while (currentElement)
if (typeof onSuccess === 'function') {
onSuccess()
}
} catch (e) {
console.warn(e)
}
}
checkIfHasScrollbar = elem => {
return (
elem &&
(elem.scrollHeight > elem.offsetHeight ||
elem.scrollWidth > elem.offsetWidth) &&
this.overflowIsScrollable(elem)
)
}
overflowIsScrollable = elem => {
const style = window.getComputedStyle(elem)
return /scroll|auto/i.test(
style.overflow + (style.overflowX || '') + (style.overflowY || '')
)
}
}
export const filterProps = (props, remove = null, allowed = null) => {
if (Array.isArray(remove)) {
remove = remove.reduce((acc, key) => {
acc[key] = true
return acc
}, {})
}
if (Array.isArray(allowed)) {
allowed = allowed.reduce((acc, key) => {
acc[key] = true
return acc
}, {})
}
return Object.entries(props).reduce((acc, [k, v]) => {
if ((remove && !remove[k]) || (allowed && allowed[k])) {
acc[k] = v
}
return acc
}, {})
}
export const makeUniqueId = (prefix = '', length = 8) =>
prefix +
String(
Math.random()
.toString(36)
.substr(2, length) + idIncrement++
).slice(-length)
let idIncrement = 0
export const slugify = s =>
String(s)
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '')
// NB: in future we can use String.matchAll() instead
export const matchAll = (string, regex) => {
if (typeof string.matchAll === 'function') {
return string.matchAll(regex)
}
const matches = []
let match
while ((match = regex.exec(string))) {
matches.push(match)
}
return matches
}
/**
* [getPreviousSibling traverses down the DOM tree until it finds the wanted element]
* @param {[string]} className [CSS class]
* @param {[HTMLElement]} elem [starting HTMLElement]
* @return {[HTMLElement]} [HTMLElement]
*/
export const getPreviousSibling = (className, elem) => {
try {
const contains = elem => elem && elem.classList.contains(className)
if (contains(elem)) {
return elem
}
while ((elem = elem && elem.parentElement) && !contains(elem));
} catch (e) {
console.warn(e)
}
return elem
}