-
Notifications
You must be signed in to change notification settings - Fork 0
/
treewalker.js
150 lines (131 loc) · 3.8 KB
/
treewalker.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
var currEl = {
elRole: "",
elLbl: "",
elType: ""
}
var allNodes = [];
function walk(elem) {
var walkerInside;
if (elem !== null && typeof elem !== "undefined") {
elem = elem;
// console.log(elem);
} else {
elem = document.body;
// console.log(elem);
}
walkerInside = document.createTreeWalker(elem, NodeFilter.SHOW_TEXT, {
acceptNode: function (node) {
// console.log(node.parentElement);
if (node.parentElement !== document.body) {
return NodeFilter.FILTER_ACCEPT;
}
}
}, false);
while (walkerInside.nextNode()) {
allNodes.push(walkerInside.currentNode.parentElement);
}
return walkerInside;
}
var walker = walk();
console.log(allNodes);
var currNode;
function remOutline(curr) {
qsa(".focused").forEach(function (elem) {
elem.classList.remove("focused");
});
curr.classList.add("focused");
}
/*function goNext() {
if (walker.nextNode()) {
currNode = walker.currentNode;
}
remOutline();
currNode.parentElement.classList.add("focused");
return currNode;
}*/
var curr = -1;
function goNextArr(elType) {
if (curr < allNodes.length - 1) {
if (typeof elType === "undefined") {
curr += 1;
console.log(allNodes[curr]);
currNode = allNodes[curr];
} else {
console.log("elType is: ", elType);
if (curr > -1) {
for (var i = curr; i < allNodes.length; i++) {
console.log("this is: ", allNodes[i]);
console.log("does it match? ", allNodes[i].nodeName === elType);
if (allNodes[i].nodeName === elType) {
/*too many nested if-else loops!!*/
curr = i;
} else {
curr+=1;
}
}
} else if (curr === -1) {
for (var i = 0; i < allNodes.length; i++) {
console.log("this is: ", allNodes[i]);
console.log("does it match? ", allNodes[i].nodeName === elType);
}
}
}
currNode = allNodes[curr];
console.log("finally, currNode is: ", currNode);
remOutline(currNode);
}
}
function goPrevArr(elType) {
if (curr > 0) {
if (typeof elType === "undefined") {
curr -= 1;
console.log(allNodes[curr]);
} else {
for (i = allNodes[curr]; i > -1; i--) {
console.log(allNodes[i].nodeName);
console.log(allNodes[i].nodeName === elType);
}
}
currNode = allNodes[curr];
remOutline(currNode);
}
}
/*function goPrev() {
if (walker.previousNode()) {
currNode = walker.currentNode;
}
if (currNode !== document.body || currNode.parentElement !== document.body) {
remOutline();
currNode.parentElement.classList.add("focused");
}
return currNode;
}*/
function goFwdTo(elType) {}
/* use mutationObserver to update treewalker */
/* get the current TW node every time you use the MO*/
/*function updateTW(elem) {
var tempCurr = currNode;
console.log("currentNode is: ", currNode);
walk();
console.log("currentNode is now:", currNode);
currNode = tempCurr;
console.log("currentNode NOW is: ", currNode);
console.log("walker: ", walker);
}*/
function jumpEls() {
document.body.ev("keydown", function (evt) {
console.log(evt.which);
switch (evt.which) {
case 40:
goNextArr();
break;
case 38:
goPrevArr();
break;
case 80:
goNextArr("P");
break;
}
});
}
jumpEls();