-
Notifications
You must be signed in to change notification settings - Fork 0
/
results-calculator.js
95 lines (72 loc) · 3.2 KB
/
results-calculator.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
class ResultsCalculator {
constructor(sectionID) {
this.vata = 0;
this.pitta = 0;
this.kapha = 0;
this.sectionID = sectionID;
let sectionInputs = Array.from(document.querySelectorAll(`${sectionID} input`));
this.vataInputs = sectionInputs.filter(input => input.name.includes('vata'));
this.pittaInputs = sectionInputs.filter(input => input.name.includes('pitta'));
this.kaphaInputs = sectionInputs.filter(input => input.name.includes('kapha'));
this.result = document.querySelector(`${sectionID} output`);
for (let input of sectionInputs) {
input.addEventListener('change', this.calcResults.bind(this));
}
}
isChecked(input) {
return input.checked;
}
calcResults() {
let vata = Math.round(this.vataInputs.filter(this.isChecked).length / this.vataInputs.length * 100);
let pitta = Math.round(this.pittaInputs.filter(this.isChecked).length / this.pittaInputs.length * 100);
let kapha = Math.round(this.kaphaInputs.filter(this.isChecked).length / this.kaphaInputs.length * 100);
this.vata = vata;
this.pitta = pitta;
this.kapha = kapha;
let resultTemplate = `<strong>Итог:</strong> Вата ${vata}%, Питта ${pitta}%, Капха ${kapha}%.`;
this.result.innerHTML = resultTemplate;
let cells = document.querySelectorAll(`${this.sectionID}-results td`);
cells[0].textContent = `${vata}%`;
cells[1].textContent = `${pitta}%`;
cells[2].textContent = `${kapha}%`;
}
}
class SummaryTotalCalculator {
constructor(sectionCalculators) {
this.sectionCalculators = sectionCalculators;
let formInputs = document.querySelectorAll('form input');
for (let input of formInputs) {
input.addEventListener('change', this.calcResults.bind(this));
}
}
calcResults() {
let vata = 0;
let pitta = 0;
let kapha = 0;
for (let calculator of this.sectionCalculators) {
vata = vata + calculator.vata;
pitta = pitta + calculator.pitta;
kapha = kapha + calculator.kapha;
}
vata = Math.round(vata / this.sectionCalculators.length);
pitta = Math.round(pitta / this.sectionCalculators.length);
kapha = Math.round(kapha / this.sectionCalculators.length);
let cells = document.querySelectorAll('#summary-totals td');
cells[0].textContent = `${vata}%`;
cells[1].textContent = `${pitta}%`;
cells[2].textContent = `${kapha}%`;
}
}
let prakritiCalc = new ResultsCalculator('#prakriti');
let vikritiCalc = new ResultsCalculator('#vikriti');
let intelligenceCalc = new ResultsCalculator('#intelligence');
let emotionsCalc = new ResultsCalculator('#emotions');
new SummaryTotalCalculator([vikritiCalc, intelligenceCalc, emotionsCalc]);
// let inputsState = {};
// for (input of document.querySelectorAll('form input')) {
// if (input.checked === true) {
// inputsState[input.name] = true;
// }
// }
// localStorage.setItem('constitutionFormState', JSON.stringify(inputsState));
// let cfs = JSON.parse(localStorage.getItem('constitutionFormState'));