-
Notifications
You must be signed in to change notification settings - Fork 60
/
gapFill.html
456 lines (387 loc) · 14.7 KB
/
gapFill.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
<script type="text/javascript">
// pageChanged & sizeChanged functions are needed in every model file
// other functions for model should also be in here to avoid conflicts
var gapFill = new function() {
var labelTxt1,
labelTxt2,
labelTxt3,
targetTxt1,
targetTxt2,
tabIndex = 1,
answerData,
$pageContents,
$targetHolder;
// function called every time the page is viewed after it has initially loaded
this.pageChanged = function() {
$pageContents = $("#pageContents");
$targetHolder = $("#targetHolder");
$pageContents.find("#hint").remove();
}
// function called every time the size of the LO is changed
this.sizeChanged = function() {
var $panel = $("#mainPanel");
if (x_browserInfo.mobile == false) {
$panel.height($x_pageHolder.height() - parseInt($x_pageDiv.css("padding-top")) * 2 - parseInt($panel.css("padding-top")) * 2 - 5);
}
$pageContents.find("#hint").remove();
}
this.init = function() {
$pageContents = $("#pageContents");
$targetHolder = $("#targetHolder");
// set panel appearance
var panelWidth = x_currentPageXML.getAttribute("panelWidth");
if (panelWidth == "Full") {
$("#mainText").remove();
$("#dragDropHolder").appendTo($pageContents);
$("#pageContents .splitScreen").remove();
} else {
$("#mainText").html(x_addLineBreaks(x_currentPageXML.getAttribute("text")));
if (panelWidth == "Small") {
$("#pageContents .splitScreen").addClass("large"); // make text area on left large so panel on right is small
} else if (panelWidth == "Large") {
$("#pageContents .splitScreen").addClass("small");
} else {
$("#pageContents .splitScreen").addClass("medium");
}
}
var origPassage = x_addLineBreaks(x_currentPageXML.getAttribute("passage")),
passageArray = origPassage.split("|"),
newPassage = "",
markedWord = false,
i;
answerData = []; // for drag drop this contains objects (with text and correct index for each label) - for fill blank this contains correct text for blanks
// add passage with spaces for marked works
for (i=0; i<passageArray.length; i++) {
tabIndex++;
if (markedWord == false) {
newPassage += '<span tabindex="' + tabIndex + '">' + passageArray[i] + '</span>';
markedWord = true;
} else {
if (x_currentPageXML.getAttribute("interactivity") == "Drag Drop") {
newPassage += '<span id="gap' + (i-1)/2 + '" class="target highlight" tabindex="' + tabIndex + '">' + passageArray[i] + '</span>';
answerData.push({ text:passageArray[i], correct:(i-1)/2 });
} else { // fill in the blank
newPassage += '<input type="text" id="gap' + (i-1)/2 + '" value="' + passageArray[i] + '" tabindex="' + tabIndex + '" />';
answerData.push(passageArray[i]);
}
markedWord = false;
}
}
$targetHolder.html(newPassage);
if (x_currentPageXML.getAttribute("interactivity") == "Drag Drop") {
this.setUpDragDrop();
} else {
this.setUpFillBlank();
}
$("#feedbackTxt")
.html(x_addLineBreaks(x_currentPageXML.getAttribute("feedback")))
.hide();
if (x_currentPage != 0) {
this.sizeChanged();
}
x_pageLoaded();
}
this.setUpFillBlank = function() {
$("#labelHolder").remove();
var maxW = 0,
i;
for (i=0; i<$targetHolder.find("input").length; i++) {
maxW = Math.max(maxW, $targetHolder.find("input:eq(" + i + ")").val().length);
}
$targetHolder.find("input")
.attr({
"size" :maxW,
"maxlength" :maxW + 2,
"value" :""
})
.on("keypress", function() {
var $this = $(this);
setTimeout(function() {
if ($this.val().toLowerCase() == answerData[$targetHolder.find("input").index($this)].toLowerCase()) { // correct
$this.attr("readonly", "readonly");
$pageContents.find("#hint").remove();
if ($targetHolder.find("input[readonly]").length == $targetHolder.find("input").length) {
$("#feedbackTxt").fadeIn();
}
} else { // wrong - start showing hint after 3 wrong characters entered
var wrong = 0;
for (i=0; i<$this.val().length; i++) {
if ($this.val()[i].toLowerCase() != answerData[$targetHolder.find("input").index($this)][i].toLowerCase()) {
wrong++;
}
}
if (wrong > 0) {
if ($this.data("attempt") == undefined) {
$this.data("attempt", 1);
} else if ($this.data("attempt") >= 2) { // show hint - add extra letter at every other wrong answer
$this.data("attempt", $this.data("attempt")+1);
if ($this.data("attempt") % 2 != 0) { // odd num
var currentHint = $this.data("hint"),
correctAnswer = answerData[$targetHolder.find("input").index($this)];
if (currentHint == undefined) { // show 1st letter
currentHint = "";
for (i=0; i<correctAnswer.length; i++) {
if (i == 0) {
currentHint += correctAnswer[i];
} else {
currentHint += "_";
}
}
} else if (currentHint[currentHint.length - 1] == "_") { // then last letter
currentHint = currentHint.substring(0, currentHint.length - 1) + correctAnswer[correctAnswer.length - 1];
} else { // then random letter in between
var blanks = currentHint.match(/_/g); // num of blanks
if (blanks != null && blanks.length >= 1) {
var letterToShow = Math.floor(Math.random() * blanks.length),
tempCount = 0;
for (i=0; i<currentHint.length; i++) {
if (currentHint[i] == "_" && tempCount == letterToShow) {
currentHint = currentHint.substring(0, i) + correctAnswer[i] + currentHint.substring(i + 1, currentHint.length);
break;
} else if (currentHint[i] == "_") {
tempCount++;
}
}
}
}
$this
.data("hint", currentHint)
.attr("title", currentHint);
var $hint = $pageContents.find("#hint");
if ($hint.length < 1) {
$pageContents.append('<div id="hint" class="x_tooltip"></div>');
$hint = $pageContents.find("#hint");
}
$hint.html(currentHint);
$hint.css({
top :$this.position().top + parseInt($("#mainPanel").css("padding-top")) + parseInt($this.css("margin-top")) + $this.height() + 10,
left :$this.position().left + parseInt($("#mainPanel").css("padding-left")) + 5
});
}
} else {
$this.data("attempt", $this.data("attempt")+1);
}
}
}
}, 0);
});
$("#feedbackTxt").attr("tabindex", tabIndex+1);
}
this.setUpDragDrop = function() {
// store strings used to give titles to labels and targets when keyboard is being used (for screen readers)
labelTxt1 = x_getLangInfo(x_languageData.find("interactions").find("draggableItem")[0], "name", "Draggable Item");
labelTxt2 = x_getLangInfo(x_languageData.find("interactions").find("draggableItem")[0], "selected", "Item Selected");
labelTxt3 = x_getLangInfo(x_languageData.find("interactions").find("draggableItem")[0], "toSelect", "Press space to select");
targetTxt1 = x_getLangInfo(x_languageData.find("interactions").find("targetArea")[0], "description", "Drop zone for");
targetTxt2 = x_getLangInfo(x_languageData.find("interactions").find("targetArea")[0], "toSelect", "Press space to drop the selected item.");
// set up targets
var maxW = 0,
i;
for (i=0; i<$targetHolder.find(".target").length; i++) {
maxW = Math.max(maxW, $targetHolder.find(".target:eq(" + i + ")").width());
}
$($targetHolder.find(".target")).each(function(i) {
$(this)
.attr("title", targetTxt1 + " " + (i + 1))
.data("index", i); // stored here as using .index() won't return result needed as there are other elements (line breaks etc.) in $targetHolder
});
$targetHolder.find(".target")
.width(maxW) // make targets fit longest word before clearing text from them
.height($targetHolder.find(".target").height())
.html("")
.droppable({
drop: function(event, ui) {
gapFill.dropLabel($(this), ui.draggable); // target, label
}
})
.focusin(function(e) {
var $this = $(this);
$this.addClass("highlightDark");
if ($pageContents.data("selectedLabel") != undefined && $pageContents.data("selectedLabel") != "") {
$this.attr("title", targetTxt1 + " " + ($this.data("index") + 1) + " - " + targetTxt2);
}
})
.focusout(function() {
var $this = $(this);
$this
.removeClass("highlightDark")
.attr("title", targetTxt1 + " " + ($this.data("index") + 1));
})
.keypress(function(e) {
var charCode = e.charCode || e.keyCode;
if (charCode == 32) {
var $selectedLabel = $pageContents.data("selectedLabel");
if ($selectedLabel != undefined && $selectedLabel != "") {
// only accept drops for correct answers
if ($selectedLabel.data("correct") == $(this).data("index")) {
gapFill.dropLabel($(this), $selectedLabel); // target, label
} else {
$(this).attr("title", targetTxt1 + " " + ($(this).data("index") + 1));
$selectedLabel
.removeClass("selected")
.attr("title", labelTxt1);
$pageContents.data("selectedLabel", "");
}
}
}
});
// set up labels
if (x_currentPageXML.getAttribute("noise") != undefined && x_currentPageXML.getAttribute("noise") != "") { // save distractor data
var distractors = x_currentPageXML.getAttribute("noise").split(" ");
for (i=0; i<distractors.length; i++) { // add distractors
answerData.push({text:distractors[i]});
}
}
var tempData = answerData.slice();
// randomise order and create labels
for (i=0; i<answerData.length; i++) {
tabIndex++;
var labelNum = Math.floor(Math.random() * tempData.length);
$('<div class="label panel" id="label' + i + '" tabindex="' + tabIndex + '" title="' + labelTxt1 + '">' + tempData[labelNum].text + '</div>')
.appendTo($("#labelHolder"))
.data("correct", tempData[labelNum].correct);
tempData.splice(labelNum, 1);
}
// set up drag events (mouse and keyboard controlled)
$("#dragDropHolder .label")
.draggable({
containment: "#dragDropHolder",
stack: "#dragDropHolder .label", // item being dragged is always on top (z-index)
revert: "invalid", // snap back to original position if not dropped on target
start: function() {
// remove any focus/selection highlights made by tabbing to labels/targets
if ($("#labelHolder .label.focus").length > 0) {
$("#labelHolder .label.focus").attr("title", labelTxt1);
}
if ($pageContents.data("selectedLabel") != undefined && $pageContents.data("selectedLabel") != "") {
$pageContents.data("selectedLabel").attr("title", labelTxt1);
$pageContents.data("selectedLabel", "");
}
var targetInFocus = $("#targetHolder .target.highlightDark");
if (targetInFocus.length > 0) {
targetInFocus.attr("title", targetTxt1 + " " + (targetInFocus.index() + 1));
}
$("#dragDropHolder .selected").removeClass("selected");
$("#dragDropHolder .focus").removeClass("focus");
$("#dragDropHolder .highlightDark").removeClass("highlightDark");
}
})
// set up events used when keyboard rather than mouse is used
// these highlight selected labels / targets and set the title attr which the screen readers will use
.focusin(function() {
var $this = $(this);
if ($this.is($pageContents.data("selectedLabel")) == false) {
$this
.addClass("focus")
.attr("title", labelTxt1 + " - " + labelTxt3);
}
})
.focusout(function() {
var $this = $(this);
$this.removeClass("focus");
if ($this.is($pageContents.data("selectedLabel")) == false) {
$this.attr("title", labelTxt1);
}
})
.keypress(function(e) {
var charCode = e.charCode || e.keyCode;
if (charCode == 32) {
if ($pageContents.data("selectedLabel") != undefined && $pageContents.data("selectedLabel") != "") {
$pageContents.data("selectedLabel")
.removeClass("selected")
.attr("title", labelTxt1);
}
var $this = $(this);
$this
.removeClass("focus")
.addClass("selected")
.attr("title", labelTxt1 + ' - ' + labelTxt2);
$pageContents.data("selectedLabel", $this);
}
})
.disableSelection();
for (i=0; i<$("#targetHolder .target").length; i++) {
$("#targetHolder .target:eq(" + i + ")").droppable({
// only correct label can be dropped on target
accept: $("#labelHolder .label").filter(function() {return $(this).data("correct") == i; })
});
}
$("#feedbackTxt").attr("tabindex", tabIndex);
}
// function called when label dropped on target - by mouse or keyboard
this.dropLabel = function($thisTarget, $thisLabel) {
$thisLabel
.removeAttr("title")
.removeClass("selected")
.draggable("disable")
.off("keypress focusin focusout")
.css("opacity", 0);
$thisTarget
.removeAttr("title")
.removeClass("highlight highlightDark")
.addClass("correct")
.html($thisLabel.html())
.off("keypress focusin focusout");
$pageContents.data("selectedLabel", "");
if ($targetHolder.find(".target.highlight").length == 0) {
$("#labelHolder").hide();
$("#feedbackTxt").fadeIn();
}
}
}
gapFill.init();
</script>
<style type="text/css">
#textHolder {
margin-bottom: 10px;
}
#targetHolder .target {
display: inline-block;
text-align: center;
vertical-align: middle;
padding: 5px;
margin: 3px 0 5px 0;
}
#targetHolder input {
margin: 5px 0;
text-align: center;
}
#targetHolder input[readonly], #targetHolder .target.correct {
color: green;
border-color: transparent;
}
#labelHolder {
margin-top: 10px;
}
#labelHolder .label {
display: inline-block;
cursor: pointer;
padding: 5px;
margin: 10px;
}
#labelHolder .label.focus {
border: 2px solid yellow;
}
#labelHolder .label.selected {
border: 2px solid green;
}
#feedbackTxt {
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid gray;
}
</style>
<div id="pageContents">
<div class="splitScreen medium">
<div id="textHolder" class="left">
<div id="mainText" tabindex="1"></div>
</div>
<div id="dragDropHolder" class="right">
<div id="mainPanel" class="panel">
<div id="targetHolder"></div>
<div id="labelHolder"></div>
<div id="feedbackTxt"></div>
</div>
</div>
</div>
</div>