-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabbed-random-choice-picker.html
396 lines (343 loc) · 10.6 KB
/
tabbed-random-choice-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
<!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</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;
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-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</h1>
<nav>
<button id="choices-tab-btn" class="active" onclick="showTab('choices-tab')">Choices</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>
<!-- Picker Tab -->
<div id="picker-tab" class="tab-content hidden">
<h2>Picker</h2>
<p class="instructions">
Click "Pick a Choice" to randomly select a choice. Choices that have already been selected will be marked appropriately.
</p>
<!-- 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?</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="pickChoice()">Pick a Choice</button>
<button onclick="resetPicker()">Reset Picker</button>
<div id="picker-display" class="hidden">No choice picked yet.</div>
<h3 id="picked-heading" class="hidden">Picked Choices</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 pickedChoices = []; // List of picked choices
//-----------
// TABS
//-----------
// Show a specific tab
function showTab(tabId) {
const tabs = ["choices-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") {
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);
});
}
}
// 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.includes(choice)) {
li.classList.add("highlighted");
}
pickerChoicesList.appendChild(li);
});
}
// Pick a choice
function pickChoice() {
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.push(originalChoices[randomIndex]);
} else {
alert("No more choices to pick from!");
return;
}
} else {
const randomIndex = Math.floor(Math.random() * remainingChoices.length);
const picked = remainingChoices.splice(randomIndex, 1)[0];
pickedChoices.push(picked);
}
updatePickedList();
}
// Reset picker
function resetPicker() {
remainingChoices = [...originalChoices];
pickedChoices = [];
updatePickedList();
}
// Update picked list
function updatePickedList() {
const pickedList = document.getElementById("picked-list");
const pickedHeading = document.getElementById("picked-heading");
pickedList.innerHTML = "";
if (pickedChoices.length === 0) {
pickedHeading.classList.add("hidden");
} else {
pickedHeading.classList.remove("hidden");
pickedChoices.forEach(choice => {
const li = document.createElement("li");
li.textContent = choice;
pickedList.appendChild(li);
});
}
updatePickerChoicesList();
updateReuseQuestionVisibility();
}
// ----------
// REUSE LOGIC
// ----------
// Show or hide the reuse question based on remaining choices
function updateReuseQuestionVisibility() {
const reuseQuestion = document.getElementById("reuse-question");
if (remainingChoices.length === 0) {
reuseQuestion.classList.remove("hidden");
} else {
reuseQuestion.classList.add("hidden");
}
}
</script>
</body>
</html>