-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
238 lines (221 loc) · 8.83 KB
/
index.html
File metadata and controls
238 lines (221 loc) · 8.83 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PoolSelectionViewer</title>
<script type="application/javascript">
function init() {
window.data = getData();
setXapiObjectGetter(getXapiObject);
registerOnSubmitListener(onSubmit);
}
function updateLayout() {
propagateLayoutChanges();
}
function sendStatement(stmt) {
sendXapiStatement(stmt);
}
</script>
<style type ="text/css">
#overlay {
width: 100%;
height: 100%;
z-index: 100;
display: block;
position: absolute;
top: 0;
left: 0;
background: rgba(0,0,0,0);
}
.item {
border: 1px solid black;
border-radius: 5px;
display: inline-block;
text-align: center;
min-width: 2em;
padding: 0.5em;
margin: 10px;
cursor: move;
}
.correct {
background-color: rgba(0,255,0,0.6);
}
.false {
background-color: rgba(255,0,0,0.6);
}
#description {
margin-bottom: 10px;
}
.black {
color: black;
}
</style>
</head>
<body>
<div id="description"></div>
<div id="pool" style="width: 90%; min-height: 4em; border: 1px dashed black; border-radius: 15px; margin: 5%;"></div>
<div id="itemArea" style="width: 100%; min-height: 4em;"></div>
<div id="overlay"></div>
<!-- for testing only
<h1>testingfunction</h1><div id="testingfunction"></div> -->
<script type="application/javascript">
function getXapiObject(){
return {};
}
function initializeItems(){
var shuffle = function (a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
};
document.getElementById("description").innerHTML = window.data.question;
shuffle(window.data.items).forEach(function(item){
var elem = document.createElement("div");
elem.setAttribute("id", item.id);
elem.innerHTML=item.text;
elem.setAttribute("class", "item");
itemArea.appendChild(elem);
items.push(elem);
});
overlay.addEventListener("mousedown", onMouseDown);
overlay.addEventListener("mouseup", onMouseUp);
overlay.addEventListener("mousemove", onMouseMove);
overlay.addEventListener("touchstart", onMouseDown);
overlay.addEventListener("touchend", onMouseUp);
overlay.addEventListener("touchmove", onMouseMove);
}
function onMouseUp(e){
if(!isShowingSolution){
var outerRect = getBoundingClientRect();
if(e.changedTouches !== undefined && e.changedTouches.length > 0){
var x = (e.changedTouches[0].clientX - outerRect.left);
var y = (e.changedTouches[0].clientY - outerRect.top);
} else {
var x = (e.clientX - outerRect.left);
var y = (e.clientY - outerRect.top);
}
isDragging = false;
if(selectedItem !== undefined){
selectedItem.style = "";
var hitbox = pool.getBoundingClientRect();
if(x < hitbox.right && x > hitbox.left
&& y < hitbox.bottom && y > hitbox.top &&
placeHolderForSelected.parentNode === itemArea){
// into the pool
selectedItem.parentNode.removeChild(selectedItem);
pool.appendChild(selectedItem);
placeHolderForSelected.parentNode.removeChild(placeHolderForSelected);
updateLayout();
} else if ((x < hitbox.right && x > hitbox.left
&& y < hitbox.bottom && y > hitbox.top)
|| placeHolderForSelected.parentNode === itemArea){
// stay where you are
placeHolderForSelected.parentNode.replaceChild(selectedItem, placeHolderForSelected);
} else {
// out of the pool
selectedItem.parentNode.removeChild(selectedItem);
itemArea.appendChild(selectedItem);
placeHolderForSelected.parentNode.removeChild(placeHolderForSelected);
updateLayout();
}
}
selectedItem = undefined;
}
}
function onMouseDown(e){
if(!isShowingSolution){
var outerRect = getBoundingClientRect();
if(e.targetTouches !== undefined && e.targetTouches.length > 0){
var x = (e.targetTouches[0].clientX - outerRect.left);
var y = (e.targetTouches[0].clientY - outerRect.top);
} else {
var x = (e.clientX - outerRect.left);
var y = (e.clientY - outerRect.top);
}
if (isDragging){
//fix for missed events (e.g. when mouse leaves frame)
onMouseUp(e);
}
items.forEach(function(elem){
var hitbox = elem.getBoundingClientRect();
if(x < hitbox.right && x > hitbox.left
&& y < hitbox.bottom && y > hitbox.top){
isDragging = true;
selectedItem = elem;
clickPositionOffset.x = x - hitbox.left;
clickPositionOffset.y = y - hitbox.top;
}
});
if(isDragging){
placeHolderForSelected = document.createElement("div");
placeHolderForSelected.setAttribute("class", "item");
placeHolderForSelected.innerHTML = selectedItem.innerHTML;
placeHolderForSelected.style = "visibility: hidden;";
selectedItem.parentNode.replaceChild(placeHolderForSelected, selectedItem);
document.appendChild(selectedItem);
selectedItem.style = "position: absolute; top:" + (y - clickPositionOffset.y -10) + "px; left:" + (x - clickPositionOffset.x -10) + "px;";
}
}
}
function onMouseMove(e){
if(isDragging && !isShowingSolution){
var outerRect = getBoundingClientRect();
if(e.targetTouches !== undefined && e.targetTouches.length > 0){
var x = (e.targetTouches[0].clientX - outerRect.left);
var y = (e.targetTouches[0].clientY - outerRect.top);
} else {
var x = (e.clientX - outerRect.left);
var y = (e.clientY - outerRect.top);
}
selectedItem.style = "position: absolute; top:" + (y - clickPositionOffset.y - 10) + "px; left:" + (x - clickPositionOffset.x - 10) + "px;";
e.preventDefault();
}
}
function onSubmit(){
if (!isShowingSolution) {
isShowingSolution = true;
for (var i = 0; i<pool.childNodes.length; i++){
var correct = window.data.items.filter(function(item){ return item.isCorrect && item.id == pool.childNodes[i].id }).length === 1;
pool.childNodes[i].classList.add(correct?"correct":"false");
}
for (var i = 0; i<itemArea.childNodes.length; i++){
var correct = window.data.items.filter(function(item){ return !item.isCorrect && item.id == itemArea.childNodes[i].id }).length === 1;
itemArea.childNodes[i].classList.add(correct?"correct":"false");
}
}
else {
isShowingSolution = false;
for (var i = 0; i<pool.childNodes.length; i++){
pool.childNodes[i].classList.remove("correct");
pool.childNodes[i].classList.remove("false");
};
for (var i = 0; i<itemArea.childNodes.length; i++){
itemArea.childNodes[i].classList.remove("correct");
itemArea.childNodes[i].classList.remove("false");
};
}
}
function onInteraction(id){
}
function showHideContext(){
}
init();
var isDragging = false;
var showHint = false;
var isShowingSolution = false;
var overlay = document.getElementById("overlay");
var itemArea = document.getElementById("itemArea");
var pool = document.getElementById("pool");
var items = [];
var selectedItem = undefined;
var placeHolderForSelected = undefined;
var clickPositionOffset = {x: 0, y: 0};
initializeItems();
</script>
</body>
</html>