-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
102 lines (92 loc) · 2.52 KB
/
node.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
/**
* Generates a unique ID for a node.
*
* @returns {String} The unique ID.
*/
function nodeUniqueId(node) {
if (!node || !node.id) {
var id;
do {
id = 'ruid-' + Math.random().toString().replace('.', '');
} while (document.getElementById(id))
if (!node) {
return id;
}
node.id = id;
}
return node.id;
}
function nodeClosestByClassName(node, className) {
while (node.parentNode && node.parentNode.className != className) {
node = node.parentNode;
}
if (node.parentNode) {
return node.parentNode;
}
return null;
}
function nodeFromHtml(html, wrapper) {
var node = document.createElement(wrapper || 'div');
node.innerHTML = html;
return node.children[0];
}
function nodeClassSwitch(node, classAdd, classRemove) {
node.classList.add(classAdd);
node.classList.remove(classRemove);
}
function nodeLastChild(node) {
var lastChild = node.lastChild
while (lastChild && lastChild.nodeType !== 1) {
lastChild = lastChild.previousSibling;
}
return lastChild;
}
function nodeOffsetTop(node) {
var offsetTop = 0;
do {
if (node.tagName === 'BODY') {
break;
} else {
offsetTop += node.offsetTop;
}
node = node.offsetParent;
} while(node);
return offsetTop;
}
function nodeFreezeHeight(node) {
if (typeof node.dataset.height === 'undefined') {
node.dataset.height = node.style.height;
node.style.height = document.body.clientHeight + 'px';
}
}
function nodeUnfreezeHeight(node) {
if (typeof node.dataset.height !== 'undefined') {
node.style.height = node.dataset.height;
delete node.dataset.height;
}
}
function nodeMatches(node, selector) {
var method =
Element.prototype.matches ||
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector;
return method.call(node, selector);
}
function nodeFindUnnested(node, findSelector, nestedSelector) {
var nodes = node.querySelectorAll(findSelector),
result = [];
for (var i = 0; i < nodes.length; i++) {
var closest = nodes[i];
do {
if (nodeMatches(closest, nestedSelector)) {
break;
}
} while (closest = closest.parentNode);
if (closest == node) {
result.push(nodes[i]);
}
}
return result;
}