-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reorder.html
210 lines (188 loc) · 7.44 KB
/
reorder.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Parsons Problem Creator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.controls-container {
text-align: center;
margin-bottom: 20px;
}
.problem-container {
display: flex;
flex-direction: column;
gap: 10px;
border: 1px solid #ccc;
padding: 10px;
}
.line {
background: #f5f5f5;
border: 1px solid #ccc;
padding: 5px;
cursor: move;
user-select: none;
}
.selected {
background-color: lightblue;
}
.notice {
margin-bottom: 10px;
color: red;
}
.check-button, .hint-button {
display: block;
margin: 20px auto;
padding: 10px 20px;
background: #007bff;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
}
.correct {
color: green;
}
.incorrect {
color: red;
}
.highlight {
background-color: lightgreen;
}
</style>
</head>
<body>
<div class="controls-container">
<label for="font-size">Font Size:</label>
<input type="range" id="font-size" min="10" max="30" value="16">
</div>
<div id="problem-container" class="problem-container"></div>
<button class="check-button">Check Order</button>
<button class="hint-button">Hint</button>
<div id="result"></div>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const urlParams = new URLSearchParams(window.location.search);
const codeUrl = urlParams.get('url') || 'code.txt';
let originalOrder = [];
fetch(codeUrl)
.then(response => response.text())
.then(data => {
const lines = data.split('\n').filter(line => line.trim() !== '').slice(0, 20);
const problemContainer = document.getElementById('problem-container');
originalOrder = lines.map((line, index) => ({
index,
text: line.replace(/ /g, ' ')
}));
originalOrder.forEach(item => {
const lineElement = document.createElement('div');
lineElement.className = 'line';
lineElement.innerHTML = item.text;
lineElement.dataset.index = item.index;
lineElement.draggable = true;
problemContainer.appendChild(lineElement);
});
if (data.split('\n').length > 20) {
const notice = document.createElement('div');
notice.className = 'notice';
notice.textContent = 'Note: Only the first 20 lines are used.';
document.body.insertBefore(notice, problemContainer);
}
// Shuffle the lines
for (let i = problemContainer.children.length; i >= 0; i--) {
problemContainer.appendChild(problemContainer.children[Math.random() * i | 0]);
}
let dragSrcEl = null;
function handleDragStart(e) {
dragSrcEl = this;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.innerHTML);
this.classList.add('selected');
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = 'move';
return false;
}
function handleDragEnter(e) {
this.classList.add('over');
}
function handleDragLeave(e) {
this.classList.remove('over');
}
function handleDrop(e) {
if (e.stopPropagation) {
e.stopPropagation();
}
if (dragSrcEl !== this) {
dragSrcEl.innerHTML = this.innerHTML;
this.innerHTML = e.dataTransfer.getData('text/html');
// Swap the data-index values
const tempIndex = dragSrcEl.dataset.index;
dragSrcEl.dataset.index = this.dataset.index;
this.dataset.index = tempIndex;
}
return false;
}
function handleDragEnd(e) {
this.classList.remove('selected');
document.querySelectorAll('.line').forEach(line => {
line.classList.remove('over');
});
}
document.querySelectorAll('.line').forEach(line => {
line.addEventListener('dragstart', handleDragStart);
line.addEventListener('dragenter', handleDragEnter);
line.addEventListener('dragover', handleDragOver);
line.addEventListener('dragleave', handleDragLeave);
line.addEventListener('drop', handleDrop);
line.addEventListener('dragend', handleDragEnd);
});
});
document.getElementById('font-size').addEventListener('input', function() {
const lines = document.querySelectorAll('.line');
lines.forEach(line => {
line.style.fontSize = this.value + 'px';
});
});
document.querySelector('.check-button').addEventListener('click', () => {
const problemContainer = document.getElementById('problem-container');
const resultContainer = document.getElementById('result');
const lines = Array.from(problemContainer.children);
let correctOrder = true;
lines.forEach((line, index) => {
if (parseInt(line.dataset.index, 10) !== index) {
correctOrder = false;
}
});
if (correctOrder) {
resultContainer.textContent = 'Correct order!';
resultContainer.className = 'correct';
} else {
resultContainer.textContent = 'Incorrect order. Try again!';
resultContainer.className = 'incorrect';
}
});
document.querySelector('.hint-button').addEventListener('click', () => {
const problemContainer = document.getElementById('problem-container');
const lines = Array.from(problemContainer.children);
lines.forEach((line, index) => {
if (parseInt(line.dataset.index, 10) === index) {
line.classList.add('highlight');
} else {
line.classList.remove('highlight');
}
});
});
});
</script>
<script async defer src="https://scripts.simpleanalyticscdn.com/latest.js"></script>
<noscript><img src="https://queue.simpleanalyticscdn.com/noscript.gif" alt="" referrerpolicy="no-referrer-when-downgrade" /></noscript>
</body>
</html>