-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
83 lines (69 loc) · 1.9 KB
/
utils.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
export const addEvent = (object, type, callback) => {
if (object == null || typeof object === 'undefined') return;
if (object.addEventListener) {
object.addEventListener(type, callback, false);
} else if (object.attachEvent) {
object.attachEvent(`on${type}`, callback);
} else {
object[`on${type}`] = callback;
}
};
export const removeEvent = (object, type, callback) => {
if (object == null || typeof object === 'undefined') return;
if (object.removeEventListener) {
object.removeEventListener(type, callback, false);
} else if (object.detachEvent) {
object.detachEvent(`on${type}`, callback);
} else {
object[`on${type}`] = null;
}
};
export const elemOffset = elem => {
let box = { top: 0, left: 0 };
const doc = elem && elem.ownerDocument;
if (!doc) {
return box;
}
const docElem = elem.ownerDocument.documentElement;
box = elem.getBoundingClientRect();
const win = window;
return {
top:
box.top +
(win.pageYOffset || docElem.scrollTop) -
(docElem.clientTop || 0),
left:
box.left +
(win.pageXOffset || docElem.scrollLeft) -
(docElem.clientLeft || 0)
};
};
export const findByValue = (obj, val, key) => {
const result = [];
for (const outerKey in obj) {
if (obj[outerKey][key] * 1 === val * 1) {
result.push(outerKey);
}
}
return result;
};
export const getParentNodes = (className, parent) => {
const arr = [];
let par = parent;
while (document.body !== par && par.classList.contains(className)) {
arr.push(parent);
par = parent.parentElement;
}
return arr;
};
export const getCookie = name => {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts
.pop()
.split(';')
.shift();
}
return undefined;
};