-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.js
More file actions
202 lines (141 loc) · 4.9 KB
/
Copy pathscope.js
File metadata and controls
202 lines (141 loc) · 4.9 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//GLOBALS
const scopeGlobals = {
bodyElem : document.getElementById("checklist-body"),
isBodyDisplayed : false,
isRequiredInputsSet : false,
inputElements : document.querySelectorAll(".sf-select .sf-inp"),
requiredInputElements : document.getElementsByClassName("sf-required"),
IdsOfRequiredInputs : [],
inputState : {}
}
const initPage = () => {
scopeGlobals.bodyElem.style.display = "none";
//*Set selection state and event listeners
for(let elem of scopeGlobals.inputElements ){
scopeGlobals.inputState[elem.id] = elem.value;
elem.addEventListener("change", refilter);
}
//*append required input elements
for(let elem of scopeGlobals.requiredInputElements){
scopeGlobals.IdsOfRequiredInputs.push(elem.id);
updateInputState("all");
}
//Filter by difault inputs
refilter("all");
}
const refilter = (target = "") => {
//* Update input States
if(target == "all"){
target= {id:"all"};
updateInputState("all");
} else {
target = event.target;
updateInputState(target.id);
}
console.log(`event triggered ${target.id}`);
//* Filter the items by inputState
filterItems();
//* if body not displayed check to display body
if(scopeGlobals.isBodyDisplayed == false){
//*check for required inputs
if( checkForRequiredInputs() ){
scopeGlobals.isRequiredInputsSet = true;
scopeGlobals.bodyElem.style.display = "block";
}
}
}
const updateInputState = (inputId) => {
if(inputId == "all"){
//*updated and set all inputs
for(let elem of scopeGlobals.inputElements ){
scopeGlobals.inputState[elem.id] = elem.value;
}
} else {
scopeGlobals.inputState[inputId] = document.getElementById(inputId).value;
}
}
const filterItems = () => {
let elemsToBeFiltered;
for(let inputId in scopeGlobals.inputState){
elemsToBeFiltered = document.getElementsByClassName(`${inputId}-dependant`);
for(let elem of elemsToBeFiltered){
let logicState = true;
let subLogic;
for(let id in scopeGlobals.inputState){
if(elem.classList.contains(`${id}-dependant`)){
subLogic = evalScopeString(elem, id);
logicState = logicState && subLogic;
} else {
continue;
}
}
elem.style.display = logicState ? (elem.tagName == "SPAN" ? "inline-block" : "block") : "none";
}
}
2}
const checkForRequiredInputs = () => {
let isAllSelected = true;
for(let inputId of scopeGlobals.IdsOfRequiredInputs){
let isInputSet = scopeGlobals.inputState[inputId] != '';
isAllSelected = isAllSelected && isInputSet;
}
return isAllSelected;
}
const evalScopeString = (elem, inputId) => {
if(elem?.dataset[inputId] == undefined){
console.warn("variate:" + inputId + ". A logic attribute does not have a valid string");
console.warn("element id : " + elem?.id);
console.warn("element name : " + elem?.name);
return true;
}
if(scopeGlobals.inputState[inputId] == ""){
return true;
}
[operator, val] = elem?.dataset[inputId]?.split(" ");
//console.log("" + operator + " " + val);
return regLogicOperations.evaluate(operator, val, scopeGlobals.inputState[inputId]);
}
const regLogicOperations = {
evaluate: (op, val, inp) => {
val = isNaN(val) ? val : Number(val);
inp = isNaN(inp) ? inp : Number(inp);
switch (op) {
case '<':
return regLogicOperations.lessThen(val, inp);
break;
case '<=':
return regLogicOperations.lessThenEqualTo(val, inp);
break;
case '>':
return regLogicOperations.moreThen(val, inp);
break;
case '>=':
return regLogicOperations.moreThenEqualTo(val, inp);
break;
case '=':
return regLogicOperations.equalTo(val, inp);
break;
case '!=':
return regLogicOperations.notEqualTo(val, inp);
break;
}
},
lessThen: (val, inp) => {
return inp < val;
},
lessThenEqualTo: (val, inp) => {
return inp <= val;
},
moreThen: (val, inp) => {
return inp > val;
},
moreThenEqualTo: (val, inp) => {
return inp >= val;
},
equalTo: (val, inp) => {
return inp == val;
},
notEqualTo: (val, inp) => {
return inp != val;
},
}