-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
187 lines (170 loc) · 7.11 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Choice Maker</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="text-center my-5">Choice Maker</h1>
<div class="row">
<div class="col-12 col-md-6 order-2 order-md-1">
<div class="instructions">
<p>Welcome to the Choice Maker! Follow the instructions below to sort your domain preferences:</p>
<ol>
<li>Enter your choices in the left box, one choice per line.</li>
<li>Click the 'Start' button to begin the sorting process.</li>
<li>Make your selection between the presented pairs by clicking the buttons or pressing '1' or '2' keys.
</li>
<li>Once all comparisons are done, your top 5 preferences will be displayed on the right side.</li>
</ol>
</div>
<div class="pb-3">
<button onclick="startSorting()" class="btn btn-primary">Start</button>
<button onclick="resetSorting()" class="btn btn-secondary">Reset</button>
<button onclick="shareChoices()" class="btn btn-success">Share</button>
</div>
<textarea id="choices" rows="20" class="form-control" placeholder="Enter your choices, one per line"></textarea>
</div>
<div class="col-12 col-md-6 order-1 order-md-2">
<div id="sort-container" style="display:none;">
<div id="comparison" class="row">
<!-- Comparison buttons will be inserted here -->
</div>
</div>
<div id="result-container" style="display:none;">
<table id="result-table" class="table">
<thead>
<tr>
<th>Rank</th>
<th>Choice</th>
<th>Selection Count</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const urlParams = new URLSearchParams(window.location.search);
const encodedChoices = urlParams.get('choices');
if (encodedChoices) {
try {
const decodedChoices = atob(encodedChoices);
document.getElementById('choices').value = decodedChoices;
} catch (e) {
console.error('Failed to decode choices:', e);
}
}
});
let choices = [];
let sortedChoices = [];
let currentIndex = 0;
let comparisonIndex = 0;
let selectionCount = {}; // Store the selection count for each choice
let isSortingCompleted = false; // Flag to check whether the sorting process is completed or not
function startSorting() {
const textarea = document.getElementById('choices');
choices = textarea.value.split('\n').filter(choice => choice.trim() !== '');
if (choices.length < 2) {
alert('Please enter at least two choices.');
return;
}
isSortingCompleted = false; // Reset the flag when the sorting starts
sortedChoices = [choices[0]];
currentIndex = 1;
comparisonIndex = 0;
selectionCount = {};
choices.forEach(choice => selectionCount[choice] = 0); // Initialize selection count
showComparison();
}
function resetSorting() {
choices = [];
sortedChoices = [];
currentIndex = 0;
comparisonIndex = 0;
document.getElementById('sort-container').style.display = 'none';
document.getElementById('result-container').style.display = 'none';
}
function showComparison() {
if (currentIndex >= choices.length) {
showResult();
return;
}
const comparisonDiv = document.getElementById('comparison');
comparisonDiv.innerHTML = `
<div class="col-12 col-md-6 h-50">
<button onclick="selectChoice(1)" class="btn btn-info choice-button btn-block">
1. ${sortedChoices[comparisonIndex]}
</button>
</div>
<div class="col-12 col-md-6 h-50">
<button onclick="selectChoice(2)" class="btn btn-warning choice-button btn-block">
2. ${choices[currentIndex]}
</button>
</div>
`;
document.getElementById('sort-container').style.display = 'block';
}
function selectChoice(selectedIndex) {
const selectedChoice = selectedIndex === 1 ? sortedChoices[comparisonIndex] : choices[currentIndex];
selectionCount[selectedChoice] += 1; // Increment the selection count for the selected choice
if (selectedIndex === 1) {
comparisonIndex++;
if (comparisonIndex >= sortedChoices.length) {
sortedChoices.push(choices[currentIndex]);
currentIndex++;
comparisonIndex = 0;
}
} else {
sortedChoices.splice(comparisonIndex, 0, choices[currentIndex]);
currentIndex++;
comparisonIndex = 0;
}
showComparison();
}
function showResult() {
document.getElementById('sort-container').style.display = 'none';
const resultTableBody = document.querySelector('#result-table tbody');
resultTableBody.innerHTML = '';
for (let i = 0; i < Math.min(5, sortedChoices.length); i++) {
const row = document.createElement('tr');
row.innerHTML = `<td>${i + 1}</td><td>${sortedChoices[i]}</td><td>${selectionCount[sortedChoices[i]]}</td>`;
resultTableBody.appendChild(row);
}
document.getElementById('result-container').style.display = 'block';
}
document.addEventListener('keydown', function (event) {
if (!isSortingCompleted && (event.key === '1' || event.key === '2')) {
selectChoice(parseInt(event.key));
}
});
function showResult() {
document.getElementById('sort-container').style.display = 'none';
const resultTableBody = document.querySelector('#result-table tbody');
resultTableBody.innerHTML = '';
for (let i = 0; i < Math.min(5, sortedChoices.length); i++) {
const row = document.createElement('tr');
row.innerHTML = `<td>${i + 1}</td><td>${sortedChoices[i]}</td><td>${selectionCount[sortedChoices[i]]}</td>`;
resultTableBody.appendChild(row);
}
document.getElementById('result-container').style.display = 'block';
isSortingCompleted = true; // Set the flag to true when the sorting is completed
}
function shareChoices() {
const textarea = document.getElementById('choices');
const encodedChoices = btoa(textarea.value);
const url = `${window.location.origin}${window.location.pathname}?choices=${encodedChoices}`;
prompt("Copy the URL below to share your choices:", url);
}
</script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>