-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathvariableSelection.js
139 lines (127 loc) · 3.87 KB
/
variableSelection.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
function contains(arr, o) {
if (usages === null || usages === undefined) {
return false;
}
const l = arr.length;
for (let i = 0; i < l; i++) {
if (arr[i] === o) {
return true;
}
}
return false;
}
function findEquation(el) {
if (el.classList.contains('indep'))
return null;
while (el !== document) {
if (el.classList.contains('equation')) {
return el;
}
el = el.parentNode;
}
return null;
}
function isBranch(eq) {
let el = eq.parentNode;
while (el !== document && el.id !== 'algorithm') {
if (el.classList.contains('condBody')) {
return true;
}
el = el.parentNode;
}
return false;
}
function showEquations(eqId, level) {
const el = document.getElementById('v' + eqId);
if (el !== null) {
const eq = findEquation(el);
if (eq !== null && eq !== undefined) {
eq.classList.remove('faded');
if (!eq.classList.contains('depEq')) {
eq.classList.add('depEq');
if (level > 1)
eq.classList.add('faded2');
} else {
return; // been here already
}
}
}
const deps = var2dep[eqId];
if (deps === undefined || deps === null) {
return;
}
for (let i = 0; i < deps.length; i++) {
const id = deps[i];
showEquations(id, level + 1);
}
}
function hideEquationForIds(ids, visibleId) {
for (const co in ids) {
if (visibleId === ids[co])
continue;
const el = document.getElementById(ids[co]);
if (el !== null && el !== undefined) {
const eq = findEquation(el);
if (eq !== null && eq !== undefined)
eq.classList.add('faded');
}
}
}
function clearAllClass(className) {
const list = document.getElementsByClassName(className);
if (list !== undefined) {
while (list.length > 0) {
list[0].classList.remove(className);
}
}
}
function clickHandler(e) {
let t = e.target;
clearAllClass('selectedProp');
clearAllClass('faded');
clearAllClass('faded2');
clearAllClass('depEq');
while (t !== document) {
if (t.id !== null && t.id !== '' && t.id.charAt(0) === 'v') {
const baseId = t.id.split('_')[0];
const idval = baseId.substring(1);
let el = document.getElementById(baseId);
let n = 0;
while (el !== null) {
el.classList.add('selectedProp');
n++;
el = document.getElementById(baseId + '_' + n);
}
// fade other equations which do not use this variable
usages = dep2var[idval];
for (const i in var2dep) {
if (i === idval)
continue;
const vi = parseInt(i);
if (!contains(usages, vi)) {
let el2 = document.getElementById('v' + i);
n = 0;
while (el2 !== null) {
const eq = findEquation(el2);
if (eq === null)
break;
eq.classList.add('faded');
if (!isBranch(eq))
break;
n++;
el2 = document.getElementById('v' + i + '_' + n);
}
}
}
// unfade equations used to create that variable
showEquations(idval, 0);
hideEquationForIds(depConst, t.id);
hideEquationForIds(depIsVar, t.id);
break;
}
t = t.parentNode;
}
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('algorithm').onclick = clickHandler;
}, false);