-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabbed-random-user-choice-pair-picker.html
584 lines (508 loc) · 17 KB
/
tabbed-random-user-choice-pair-picker.html
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Choice Picker With User-Choice Pairing</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f8f8;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
height: 100vh;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
nav {
display: flex;
justify-content: center;
margin-bottom: 20px;
gap: 10px;
}
nav button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border: none;
border-radius: 5px;
background-color: #ddd;
}
nav button.active {
background-color: #4285f4;
color: #fff;
}
.tab-content {
border: 1px solid #ccc;
border-radius: 10px;
padding: 20px;
background-color: #fff;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
max-width: 600px;
width: 100%;
text-align: center;
}
ul {
padding: 0;
list-style: none;
margin-top: 20px;
text-align: left;
}
ul li {
margin: 5px 0;
padding: 10px;
background: #fff;
border: 1px solid #ddd;
border-radius: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
.crossed-out {
text-decoration: line-through;
color: gray;
}
.highlighted {
background-color: #fffae6;
}
.delete-icon {
color: red;
cursor: pointer;
font-weight: bold;
}
.instructions {
margin-bottom: 15px;
font-size: 14px;
color: #666;
}
.item-box {
margin: 5px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
cursor: grab;
display: inline-block;
min-width: 100px;
text-align: center;
}
#picker-display {
font-size: 24px;
font-weight: bold;
margin-top: 20px;
color: #333;
}
.hidden {
display: none;
}
.reuse-question, .show-users-question, .show-choices-question {
margin: 10px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
.radio-group {
margin-top: 10px;
display: flex;
gap: 20px;
justify-content: center;
}
footer {
margin-top: 20px;
text-align: center;
font-size: 14px;
color: #555;
padding-bottom: 20px;
}
footer a {
color: #4285f4;
text-decoration: none;
}
footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Random Choice Picker With User-Choice Pairing</h1>
<nav>
<button id="choices-tab-btn" class="active" onclick="showTab('choices-tab')">Choices</button>
<button id="users-tab-btn" onclick="showTab('users-tab')">Users</button>
<button id="picker-tab-btn" onclick="showTab('picker-tab')">Picker</button>
</nav>
<!-- Choices Tab -->
<div id="choices-tab" class="tab-content">
<h2>Choices</h2>
<p class="instructions">
Enter a list of choices below and click "Add Choices" to include them. You can delete any choice by clicking the "X" next to it.
</p>
<textarea id="choices-input" rows="5" cols="30" placeholder="Enter choices, one per line"></textarea><br>
<button onclick="addChoices()">Add Choices</button>
<button onclick="resetChoices()">Reset Choices</button>
<h3 id="choices-heading" class="hidden">Choices Entered</h3>
<ul id="choices-list"></ul>
<!-- Show Choices Question -->
<div class="show-choices-question">
<p>Should the list of available choices be displayed on the Picker tab?</p>
<div class="radio-group">
<label>
<input type="radio" name="show-choices" id="show-choices-yes" value="yes" checked>
Yes
</label>
<label>
<input type="radio" name="show-choices" id="show-choices-no" value="no">
No
</label>
</div>
</div>
</div>
<!-- Users Tab -->
<div id="users-tab" class="tab-content hidden">
<h2>Users</h2>
<p class="instructions">
Enter a list of users below and click "Add Users" to include them. You can delete any user by clicking the "X" next to their name.
</p>
<textarea id="users-input" rows="5" cols="30" placeholder="Enter user names, one per line"></textarea><br>
<button onclick="addUsers()">Add Users</button>
<button onclick="resetUsers()">Reset Users</button>
<h3 id="users-heading" class="hidden">Users Entered</h3>
<ul id="users-list"></ul>
<!-- Show Users Question -->
<div class="show-users-question">
<p>Should the list of users be displayed on the Picker tab?</p>
<div class="radio-group">
<label>
<input type="radio" name="show-users" id="show-users-yes" value="yes" checked>
Yes
</label>
<label>
<input type="radio" name="show-users" id="show-users-no" value="no">
No
</label>
</div>
</div>
</div>
<!-- Picker Tab -->
<div id="picker-tab" class="tab-content hidden">
<h2>Picker</h2>
<p class="instructions">
Select a user from the dropdown, then click "Pick a Choice for User" to assign them a random choice. Drag and drop a choice box to another user box to reassign the choice to a new user.
</p>
<div>
<label for="user-select">Select a User:</label>
<select id="user-select"></select>
</div>
<!-- List of Users -->
<div id="users-on-picker-tab" class="hidden">
<h3>Users</h3>
<ul id="picker-users-list"></ul>
</div>
<!-- List of Choices -->
<div id="choices-on-picker-tab" class="hidden">
<h3>Available Choices</h3>
<ul id="picker-choices-list"></ul>
</div>
<!-- Reuse Choices Question -->
<div id="reuse-question" class="reuse-question hidden">
<p>No more choices remain. Should choices be reused if users without choices remain?</p>
<div class="radio-group">
<label>
<input type="radio" name="reuse" id="reuse-yes" value="yes">
Yes
</label>
<label>
<input type="radio" name="reuse" id="reuse-no" value="no" checked>
No
</label>
</div>
</div>
<button onclick="pickForUser()">Pick a Choice for User</button>
<button onclick="resetPicker()">Reset Picker</button>
<div id="picker-display" class="hidden">No choice picked yet.</div>
<h3 id="picked-heading" class="hidden">Choices Associated with Users</h3>
<ul id="picked-list"></ul>
</div>
<!-- Footer -->
<footer>
Designed by Katie Ravenwood. Was this script useful and easy to use?
If so, you can <a href="https://buymeacoffee.com/katieravenwood" target="_blank">buy her a coffee</a> to say thanks!
</footer>
<script>
let originalChoices = [];
let remainingChoices = [];
let users = [];
let pickedChoices = {}; // {user: choice}
//-----------
// TABS
//-----------
// Show a specific tab
function showTab(tabId) {
const tabs = ["choices-tab", "users-tab", "picker-tab"];
tabs.forEach(tab => {
document.getElementById(tab).classList.add("hidden");
document.getElementById(`${tab}-btn`).classList.remove("active");
});
document.getElementById(tabId).classList.remove("hidden");
document.getElementById(`${tabId}-btn`).classList.add("active");
if (tabId === "picker-tab") {
updatePickerUsersList();
updatePickerChoicesList();
updateReuseQuestionVisibility();
}
}
// ----------
// CHOICES
// ----------
// Add choices from the textarea
function addChoices() {
const textarea = document.getElementById("choices-input");
const newChoices = textarea.value.split("\n").map(choice => choice.trim()).filter(choice => choice);
originalChoices = [...originalChoices, ...newChoices];
remainingChoices = [...remainingChoices, ...newChoices];
updateChoicesList();
textarea.value = "";
}
// Reset choices
function resetChoices() {
if (confirm("Are you sure you want to reset all choices? This will also reset the picker.")) {
originalChoices = [];
remainingChoices = [];
pickedChoices = {};
updateChoicesList();
updatePickedList();
}
}
// Remove a choice from the list
function removeChoice(choice) {
originalChoices = originalChoices.filter(c => c !== choice);
remainingChoices = remainingChoices.filter(c => c !== choice);
updateChoicesList();
}
// Update choices list
function updateChoicesList() {
const choicesList = document.getElementById("choices-list");
const choicesHeading = document.getElementById("choices-heading");
choicesList.innerHTML = "";
if (originalChoices.length === 0) {
choicesHeading.classList.add("hidden");
} else {
choicesHeading.classList.remove("hidden");
originalChoices.forEach(choice => {
const li = document.createElement("li");
li.innerHTML = `<span>${choice}</span><span class="delete-icon" onclick="removeChoice('${choice}')">X</span>`;
choicesList.appendChild(li);
});
}
}
// ----------
// USERS
// ----------
// Add users from the textarea
function addUsers() {
const textarea = document.getElementById("users-input");
const newUsers = textarea.value.split("\n").map(user => user.trim()).filter(user => user);
users = [...users, ...newUsers];
updateUserList();
updateUserSelect();
textarea.value = "";
}
// Reset users
function resetUsers() {
if (confirm("Are you sure you want to reset all users? This will also clear associated choices.")) {
users = [];
pickedChoices = {};
updateUserList();
updateUserSelect();
updatePickedList();
}
}
// Remove a user from the list
function removeUser(user) {
users = users.filter(u => u !== user);
delete pickedChoices[user];
updateUserList();
updateUserSelect();
updatePickedList();
}
// Update user list
function updateUserList() {
const usersList = document.getElementById("users-list");
const usersHeading = document.getElementById("users-heading");
usersList.innerHTML = "";
if (users.length === 0) {
usersHeading.classList.add("hidden");
} else {
usersHeading.classList.remove("hidden");
users.forEach(user => {
const li = document.createElement("li");
li.innerHTML = `<span>${user}</span><span class="delete-icon" onclick="removeUser('${user}')">X</span>`;
usersList.appendChild(li);
});
}
}
// Update user select dropdown
function updateUserSelect() {
const userSelect = document.getElementById("user-select");
userSelect.innerHTML = "";
users.forEach(user => {
const option = document.createElement("option");
option.value = user;
option.textContent = user;
userSelect.appendChild(option);
});
}
// ----------
// PICKER LOGIC
// ----------
// Update users list on Picker tab
function updatePickerUsersList() {
const showUsersYes = document.getElementById("show-users-yes").checked;
const usersOnPickerTab = document.getElementById("users-on-picker-tab");
const pickerUsersList = document.getElementById("picker-users-list");
if (!showUsersYes) {
usersOnPickerTab.classList.add("hidden");
return;
}
usersOnPickerTab.classList.remove("hidden");
pickerUsersList.innerHTML = "";
users.forEach(user => {
const li = document.createElement("li");
li.textContent = user;
if (pickedChoices[user]) {
li.classList.add("crossed-out");
}
pickerUsersList.appendChild(li);
});
}
// Update choices list on Picker tab
function updatePickerChoicesList() {
const showChoicesYes = document.getElementById("show-choices-yes").checked;
const choicesOnPickerTab = document.getElementById("choices-on-picker-tab");
const pickerChoicesList = document.getElementById("picker-choices-list");
const reuseYes = document.getElementById("reuse-yes").checked;
if (!showChoicesYes) {
choicesOnPickerTab.classList.add("hidden");
return;
}
choicesOnPickerTab.classList.remove("hidden");
pickerChoicesList.innerHTML = "";
originalChoices.forEach(choice => {
const li = document.createElement("li");
li.textContent = choice;
if (!reuseYes && remainingChoices.indexOf(choice) === -1) {
li.classList.add("crossed-out");
} else if (reuseYes && pickedChoices[choice]) {
li.classList.add("highlighted");
}
pickerChoicesList.appendChild(li);
});
}
// Pick a choice for the selected user
function pickForUser() {
const userSelect = document.getElementById("user-select");
const user = userSelect.value;
if (!user) {
alert("Please select a user.");
return;
}
const reuseYes = document.getElementById("reuse-yes").checked;
if (remainingChoices.length === 0) {
if (reuseYes && originalChoices.length > 0) {
const randomIndex = Math.floor(Math.random() * originalChoices.length);
pickedChoices[user] = originalChoices[randomIndex];
} else {
alert("No more choices to pick from!");
return;
}
} else {
// Return previously assigned choice (if any) to remaining choices
if (pickedChoices[user]) {
remainingChoices.push(pickedChoices[user]);
}
const randomIndex = Math.floor(Math.random() * remainingChoices.length);
pickedChoices[user] = remainingChoices.splice(randomIndex, 1)[0];
}
// Remove "No choice picked yet" display
document.getElementById("picker-display").classList.add("hidden");
updatePickedList();
}
// Reset picker
function resetPicker() {
remainingChoices = [...originalChoices];
pickedChoices = {};
document.getElementById("picker-display").classList.add("hidden");
updatePickedList();
}
// Update picked list
function updatePickedList() {
const pickedList = document.getElementById("picked-list");
const pickedHeading = document.getElementById("picked-heading");
pickedList.innerHTML = "";
if (Object.keys(pickedChoices).length === 0) {
pickedHeading.classList.add("hidden");
} else {
pickedHeading.classList.remove("hidden");
Object.keys(pickedChoices).forEach(user => {
const li = document.createElement("li");
const userBox = document.createElement("div");
const choiceBox = document.createElement("div");
userBox.textContent = user;
choiceBox.textContent = pickedChoices[user];
userBox.classList.add("item-box");
choiceBox.classList.add("item-box");
choiceBox.draggable = true;
// Drag and drop events
choiceBox.ondragstart = event => {
event.dataTransfer.setData("text/plain", user);
};
choiceBox.ondrop = event => {
const sourceUser = event.dataTransfer.getData("text/plain");
const targetUser = user;
if (sourceUser !== targetUser) {
const temp = pickedChoices[sourceUser];
pickedChoices[sourceUser] = pickedChoices[targetUser];
pickedChoices[targetUser] = temp;
updatePickedList();
}
};
choiceBox.ondragover = event => event.preventDefault();
const deleteIcon = document.createElement("span");
deleteIcon.textContent = "X";
deleteIcon.classList.add("delete-icon");
deleteIcon.onclick = () => {
remainingChoices.push(pickedChoices[user]);
delete pickedChoices[user];
updatePickedList();
};
li.appendChild(userBox);
li.appendChild(choiceBox);
li.appendChild(deleteIcon);
pickedList.appendChild(li);
});
}
updatePickerUsersList();
updatePickerChoicesList();
updateReuseQuestionVisibility();
}
// ----------
// REUSE LOGIC
// ----------
// Show or hide the reuse question based on remaining choices and unassociated users
function updateReuseQuestionVisibility() {
const reuseQuestion = document.getElementById("reuse-question");
const unassociatedUsers = users.filter(user => !pickedChoices[user]);
if (remainingChoices.length === 0 && unassociatedUsers.length > 0) {
reuseQuestion.classList.remove("hidden");
} else {
reuseQuestion.classList.add("hidden");
}
}
</script>
</body>
</html>