-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.js
332 lines (299 loc) · 9.92 KB
/
metric.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// After creating a new metric, don't forget to add it to `metricTypes`, the example template, and the README.
class ToggleMetricGrid {
constructor(metric = { name: "togglegrid", category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.element = document.createElement("div");
this.value = [];
this.element.className = "grid-container"; // Add a class name for styling
const header = document.createElement("div");
header.className = "grid-header grid-item";
header.innerHTML = `Left Side of Field`;
const footer = document.createElement("div");
footer.className = "grid-footer grid-item";
footer.innerHTML = `Ride Side of Field`;
this.element.appendChild(header);
for (let i = 0; i < 3; i++) {
const row = document.createElement("div");
row.className = "grid-item" // Add a class name for styling
row.style = "grid-area: column-" + i.toString()
for (let j = 0; j < 9; j++) {
const toggle = new ToggleMetricInt({ name: `${[1, 4, 7].includes(j) ? "Cube" : "Cone"}` });
toggle.element.className = `toggle ${[2, 5].includes(j) ? "three" : ""}`;
row.appendChild(toggle.element);
this.value.push(toggle)
}
this.element.appendChild(row);
}
}
update(newValue = !this.value) {
this.value = newValue;
this.toggle.innerHTML = `<i class="square-${newValue ? "checked" : "empty"} text-icon"></i>${this.name}`;
refreshIcons(this.toggle);
}
reset() {
// this.update(false);
for (let i = 0; i < this.value.length; i++) {
this.value[i].update(0);
}
}
}
class ToggleMetric {
constructor(metric = { name: "toggle", category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.value = false;
this.element = document.createElement("div");
this.toggle = document.createElement("button");
this.toggle.innerHTML = `<i class="square-empty text-icon"></i>${this.name}`;
this.toggle.onclick = () => {
this.update();
backupSurvey();
};
this.element.appendChild(this.toggle);
}
update(newValue = !this.value) {
this.value = newValue;
this.toggle.innerHTML = `<i class="square-${newValue ? "checked" : "empty"} text-icon"></i>${this.name}`;
refreshIcons(this.toggle);
}
reset() {
this.update(false);
}
}
class ToggleMetricInt {
constructor(metric = { name: "toggleint", category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.value = 0;
this.element = document.createElement("div");
this.toggle = document.createElement("button");
this.toggle.innerHTML = `<i class="square-empty text-icon"></i>${this.name}`;
var incrementor;
this.toggle.onclick = () => {
if (this.value >= 2) {incrementor = 0} else {incrementor = this.value += 1};
this.update(incrementor);
backupSurvey();
};
this.element.appendChild(this.toggle);
}
update(newValue) {
this.value = newValue;
var square = "empty";
if (this.value == 0) {
square = "empty";
} else if (this.value == 1) {
square = "checked"
} else {
square = "x"
}
this.toggle.innerHTML = `<i class="square-${square} text-icon"></i>${this.name}`;
refreshIcons(this.toggle);
}
reset() {
this.update(0);
}
}
/** A number input with increment/decrement buttons. Value is an integer. */
class NumberMetric {
constructor(metric = { name: "Number", category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.value = 0;
this.element = document.createElement("div");
this.element.innerHTML = this.name + "<br>";
this.number = document.createElement("input");
this.number.classList.add("number");
this.number.type = "number";
this.number.value = 0;
this.number.pattern = "[0-9]*";
this.number.oninput = () => {
this.update();
backupSurvey();
};
this.decrementor = this.createCrementor("minus", -1);
this.incrementor = this.createCrementor("plus", 1);
{this.element.append(this.decrementor, this.number, this.incrementor)};
}
createCrementor(text = "", dir = 0) {
let crementor = document.createElement("button");
crementor.innerHTML = `<i class="${text}"></i>`;
crementor.onclick = () => {
if (!this.number.value) this.number.value = 0;
this.update(parseInt(this.number.value) + dir);
if (this.number.value <0) {this.number.value = 0}; //remove to get negative
backupSurvey();
};
return crementor;
}
update(newValue = this.number.value) {
this.value = newValue;
this.number.value = newValue;
}
reset() {
this.update(0);
}
}
/**
* A dropdown selector. Value is a string (selected option).
* There must be an array of string `values` to create options for the selector.
*/
class SelectMetric {
constructor(metric = { name: "Select", values: [], category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.values = metric.values;
this.value = this.values[0];
this.element = document.createElement("div");
this.element.innerHTML = this.name + "<br>";
this.select = document.createElement("select");
this.select.onchange = () => {
this.update();
backupSurvey();
};
this.values.forEach(value => {
this.select.innerHTML += `<option value="${value}">${value}</opion>`;
});
this.element.append(this.select);
}
update(newValue = this.select.value) {
this.value = this.values.indexOf(newValue);
this.select.value = newValue;
}
reset() {
this.update(this.values[0]);
}
}
/**
* A text input. Value is a string.
* Setting a `tip` value will add a placeholder within the input field.
*/
class TextMetric {
constructor(metric = { name: "Text", tip: "", category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.value = "";
this.tip = metric.tip;
this.element = document.createElement("div");
this.element.innerHTML = this.name + "<br>";
this.element.style.width = "100%";
this.input = document.createElement("input");
this.input.placeholder = this.tip;
this.input.oninput = () => {
this.update();
backupSurvey();
};
this.element.append(this.input);
}
update(newValue = this.input.value.replace('"', "'")) {
this.value = newValue;
this.input.value = newValue;
}
reset() {
this.update("");
}
}
/** A star rating bar. Value is a number (0-4). */
class RatingMetric {
constructor(metric = { name: "Rating", category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.value = 0;
this.element = document.createElement("div");
this.element.innerHTML = this.name + "<br>";
this.ratingBar = document.createElement("div");
this.ratingBar.classList.add("flex");
for (let i = 0; i < 5; i++) {
const star = document.createElement("button");
star.classList.add("star");
star.innerHTML = `<i class="star-${i == 0 ? "filled" : "empty"}"></i>`;
star.onclick = () => {
this.update(i);
backupSurvey();
};
this.ratingBar.append(star);
}
this.element.append(this.ratingBar);
}
update(newValue = 0) {
this.value = newValue;
this.ratingBar.querySelectorAll(".star").forEach((star, i) => {
star.querySelector("i").className = `star-${newValue < i ? "empty" : "filled"}`;
});
refreshIcons(this.ratingBar);
}
reset() {
this.update();
}
}
/** A number input with timing controls. Value is a decimal. */
class TimerMetric {
constructor(metric = { name: "Timer", category: "category", identifier: "identifier" }) {
this.name = metric.name;
this.category = metric.category;
this.identifier = metric.identifier;
this.value = 0.0001;
this.running = false;
this.interval = null;
this.element = document.createElement("div");
this.element.innerHTML = this.name + "<br>";
this.number = document.createElement("input");
this.number.classList.add("number");
this.number.type = "number";
this.number.value = 0;
this.number.oninput = () => {
this.update();
backupSurvey();
}
this.toggleBtn = this.createButton(`<i class="play"></i>`);
this.toggleBtn.onclick = () => this.toggle();
this.stopBtn = this.createButton(`<i class="stop"></i>`);
this.stopBtn.onclick = () => this.stop();
this.element.append(this.toggleBtn, this.number, this.stopBtn);
}
createButton(text) {
let button = document.createElement("button");
button.innerHTML = text;
return button;
}
toggle() {
if (this.running) {
this.running = false;
clearInterval(this.interval);
this.toggleBtn.innerHTML = `<i class="play"></i>`;
} else {
this.running = true;
this.interval = setInterval(() => {
if (this.running) {
this.update(parseFloat(this.value) + 0.1);
this.number.value = parseFloat(this.number.value).toFixed(1);
backupSurvey();
}
}, 100);
this.toggleBtn.innerHTML = `<i class="pause"></i>`;
}
refreshIcons(this.toggleBtn);
}
stop() {
if (this.running) {
this.toggle();
}
this.update(0);
backupSurvey();
}
update(newValue = this.number.value) {
this.value = parseFloat(newValue);
this.number.value = newValue;
}
reset() {
this.update(0);
this.stop();
}
}