-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.js
166 lines (146 loc) · 4.21 KB
/
values.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { oddEvenSort, estimateOddEvenSortComparisons } from "./sort.js";
const VALUES = document.querySelector("dl");
const HIDE_UNSELECED = document.querySelector("#hide-unselected");
const SELECT_ALL = document.querySelector("#select-all");
const PICK = document.querySelector("#pick");
const COMPARATOR = document.querySelector("#comparator");
const ESTIMATE = document.querySelector("#estimate");
const PROGRESS = document.querySelector("#progress");
const RESULTS = document.querySelector("#results");
SELECT_ALL.addEventListener(
"change",
(e) => {
if (e.target.checked) {
VALUES.querySelectorAll(".value-definiton").forEach((el) => {
el.classList.add("selected");
});
} else {
VALUES.querySelectorAll(".value-definiton").forEach((el) => {
el.classList.remove("selected");
});
}
renderEstimatedComparisons();
},
false
);
VALUES.addEventListener(
"click",
(e) => {
let valueDiv;
if (e.target.tagName == "DIV") {
valueDiv = e.target;
} else if (e.target.parentElement.tagName == "DIV") {
valueDiv = e.target.parentElement;
} else {
return;
}
valueDiv.classList.toggle("selected");
renderEstimatedComparisons();
},
false
);
const getSelected = () => {
return VALUES.querySelectorAll(".selected");
};
const renderEstimatedComparisons = () => {
const selectedCount = getSelected().length;
const estimate = Math.abs(estimateOddEvenSortComparisons(selectedCount));
const niceEstimate = new Intl.NumberFormat("en", {
maximumFractionDigits: 0,
}).format(estimate);
const text = `For selected ${selectedCount} values you may need to compare ${niceEstimate} times`;
ESTIMATE.innerText = text;
};
const renderSorted = (elements, text = "") => {
RESULTS.innerHTML = "";
const list = document.createElement("ol");
elements.forEach((el) => {
const li = document.createElement("li");
const value = el.cloneNode(true);
value.className = "";
li.appendChild(value);
list.appendChild(li);
});
const info = document.createElement("h2");
info.innerText = text;
RESULTS.append(info);
RESULTS.append(list);
};
HIDE_UNSELECED.addEventListener(
"change",
(e) => {
const selected = getSelected();
if (selected.length) {
VALUES.classList.toggle("hide-selected");
} else {
e.target.checked = false;
VALUES.classList.remove("hide-selected");
}
},
false
);
async function compareValues(aOrig, bOrig) {
const cleanUp = () => {
COMPARATOR.innerHTML = "";
};
/*
compareFn(a, b) return value sort order
> 0 sort a after b, e.g. [b, a]
< 0 sort a before b, e.g. [a, b]
=== 0 keep original order of a and b
*/
return new Promise((resolve, reject) => {
const a = aOrig.cloneNode(true);
const b = bOrig.cloneNode(true);
a.addEventListener("click", () => {
resolve(-1);
cleanUp();
});
b.addEventListener("click", () => {
resolve(1);
cleanUp();
});
const instructions = document.createElement("h2");
instructions.innerText = "Pick the more imortant one:";
const options = document.createElement("div");
options.classList.add("comparator__options");
options.appendChild(a);
options.appendChild(b);
COMPARATOR.appendChild(instructions);
COMPARATOR.appendChild(options);
});
}
const sort = () => {
renderSorted([]);
const selectedEl = getSelected();
const list = [];
selectedEl.forEach((el) => {
list.push(el.cloneNode(true));
});
PROGRESS.max = estimateOddEvenSortComparisons(list.length);
let progress = 0;
PROGRESS.value = progress;
const sorted = oddEvenSort(list, async (a, b) => {
progress++;
PROGRESS.value = progress;
setTimeout(() => {
// This should give me live preview
renderSorted(list, "Pending order of your values:");
}, 0);
return compareValues(a, b);
});
PROGRESS.value = 0;
return sorted.then((result) => {
renderSorted(result, "Final list of your values:");
});
};
PICK.addEventListener(
"click",
() => {
PICK.disabled = true;
sort().then(() => {
PICK.disabled = false;
});
},
false
);