/
util.js
59 lines (47 loc) · 1.19 KB
/
util.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
export function buffer(fn, ms) {
let timer;
function clear() {
if (timer) {
clearTimeout(timer);
timer = null;
}
}
function bufferFn() {
clear();
timer = setTimeout(fn, ms);
}
bufferFn.clear = clear;
return bufferFn;
}
export function isSamePoint(prev, next) {
if (prev === next) return true;
if (!prev || !next) return false;
if ('pageX' in next && 'pageY' in next) {
return prev.pageX === next.pageX && prev.pageY === next.pageY;
}
if ('clientX' in next && 'clientY' in next) {
return prev.clientX === next.clientX && prev.clientY === next.clientY;
}
return false;
}
export function isWindow(obj) {
return obj && typeof obj === 'object' && obj.window === obj;
}
export function isSimilarValue(val1, val2) {
const int1 = Math.floor(val1);
const int2 = Math.floor(val2);
return Math.abs(int1 - int2) <= 1;
}
export function restoreFocus(activeElement, container) {
let currentElement = activeElement;
while (currentElement) {
if (currentElement === container) {
break;
}
currentElement = currentElement.parentNode;
}
// Focus back if is in the container
if (currentElement) {
activeElement.focus();
}
}