-
Notifications
You must be signed in to change notification settings - Fork 60
/
categories.html
363 lines (311 loc) · 11.6 KB
/
categories.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
<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 categories = new function() {
var labelTxt1,
labelTxt2,
labelTxt3,
targetTxt1,
targetTxt2;
// function called every time the page is viewed after it has initially loaded
this.pageChanged = function() {
$("#dragDropHolder .label").remove();
$("#feedback").hide();
this.createLabels();
}
// function called every time the size of the LO is changed
this.sizeChanged = function() {
var tallestLabel = 0;
$("#dragDropHolder .label").each(function() {
var $this = $(this);
if ($this.outerHeight() > tallestLabel) {
tallestLabel = $this.outerHeight();
}
});
$("#initHolder").height(tallestLabel);
var $categoryHolder = $("#categoryHolder");
var $category = $categoryHolder.find(".category");
$category.css("min-height", $x_pageHolder.height() - parseInt($x_pageDiv.css("padding-top")) * 2 - parseInt($categoryHolder.position().top) - parseInt($category.css("padding-top")) * 2 - $("#button").height() - 25);
}
this.createLabels = function() {
// randomise order and create labels
var $pageContents = $("#pageContents");
var labels = [];
var tempLabels = $pageContents.data("labels").slice(0);
for (var i=0; i<$pageContents.data("labels").length; i++) {
var labelNum = Math.floor(Math.random() * tempLabels.length);
labels.push(tempLabels[labelNum]);
tempLabels.splice(labelNum, 1);
}
for (var i=0; i<labels.length; i++) {
$("#initHolder").append('<li class="label panel" id="label' + i + '" tabindex="' + (i+2) + '" title="' + labelTxt1 + '">' + labels[i].name + '</li>');
var $thisLabel = $("#label" + i);
$thisLabel.data("category", labels[i].correct);
if (i != 0) {
$thisLabel.hide();
}
}
$("#initHolder, #categoryHolder .category").sortable({
connectWith: "#categoryHolder .category",
items: ".label",
containment: "#dragDropHolder",
stop: function(event, ui) {
if (ui.item.parent().is($("#initHolder")) == false) {
// show next label if it came from initHolder
if ($(this).is($("#initHolder"))) {
$("#initHolder .label:first").show();
}
// change tab index of label so it's now after the target it's on
var categoryIndex = ui.item.parent().attr("tabindex");
ui.item.parent().children(".label").each(function(i) {
$(this).attr("tabindex", categoryIndex + i + 1);
});
}
},
start: function() {
// remove any focus/selection highlights made by tabbing to labels/targets
var $pageContents = $("#pageContents");
if ($("#dragDropHolder .label.focus").length > 0) {
$("#dragDropHolder .label.focus").attr("title", labelTxt1);
} else if ($pageContents.data("selectedLabel") != undefined && $pageContents.data("selectedLabel") != "") {
$pageContents.data("selectedLabel").attr("title", labelTxt1);
$pageContents.data("selectedLabel", "");
}
var targetInFocus = $("#dragDropHolder .category.highlightDark");
if (targetInFocus.length > 0) {
targetInFocus.attr("title", targetTxt1 + " " + targetInFocus.find("h3").html());
}
$("#dragDropHolder .selected").removeClass("selected");
$("#dragDropHolder .focus").removeClass("focus");
$("#dragDropHolder .highlightDark").removeClass("highlightDark");
$("#feedback").hide();
$("#dragDropHolder .tick").remove();
}
}).disableSelection();
// 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
$("#dragDropHolder .label")
.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) {
var $pageContents = $("#pageContents");
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);
$("#feedback").hide();
$("#dragDropHolder .tick").remove();
}
});
}
this.init = function() {
// store strings used to give titles to labels and categories 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.");
$("#textHolder")
.html(x_addLineBreaks(x_currentPageXML.getAttribute("text")))
.addClass("transparent"); /* without the text having a bg the labels strangely aren't selectable in IE */
var $feedback = $("#feedback");
if (x_currentPageXML.getAttribute("feedback") == undefined || x_currentPageXML.getAttribute("feedback") == "") {
$feedback.remove();
} else {
$feedback
.html(x_addLineBreaks(x_currentPageXML.getAttribute("feedback")))
.hide();
}
// buttonWidth attribute not used as button will be sized automatically
var buttonLabel = x_currentPageXML.getAttribute("buttonLabel");
if (buttonLabel == undefined) {
buttonLabel = "Check Answers";
}
var $button = $("#button")
.button({
label: buttonLabel
})
.click(function() { // mark labels and show feedback
$("#dragDropHolder .tick").remove();
$("#feedback").show();
$("#categoryHolder .category .label").each(function() {
var $this = $(this);
if ($this.data("category").is($this.parent())) {
$this.html($this.html() + '<img class="tick" src="' + x_templateLocation + 'common_html5/tick.png" alt="' + x_getLangInfo(x_languageData.find("tick")[0], "label", "Tick") + '"></img>');
}
});
});
// create categories
var $categoryHolder = $("#categoryHolder");
var $firstCategory = $categoryHolder.find(".category");
var labels = [];
$(x_currentPageXML).children()
.each(function(i) {
var $thisCategory;
if (i != 0) {
$thisCategory = $firstCategory.clone().appendTo($categoryHolder);
} else {
$thisCategory = $firstCategory;
}
$thisCategory.find("h3").html(this.getAttribute("name"));
$(this).children().each(function(j) {
labels.push({name:this.getAttribute("name"), correct:$thisCategory});
});
});
var $pageContents = $("#pageContents");
$pageContents.data("labels", labels);
// style categories
var numColumns = $(x_currentPageXML).children().length;
var spacerWidth = ((numColumns - 1) * 2) + (numColumns * 2); // 2% gap between categories & 1% left & 1% right padding inside categories
var columnWidth = Math.floor((100 - spacerWidth) / numColumns);
var edgeWidth = Math.floor((100 - spacerWidth - (columnWidth * numColumns)) / 2);
$("#categoryHolder .category, #initHolder")
.css({
width :columnWidth + "%",
"margin-left" :"2%"
});
$("#categoryHolder .category:first").css("margin-left", edgeWidth + "%");
// 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
$("#categoryHolder .category")
.focusin(function(e) {
if ($(e.target).hasClass("category")) {
$(this).addClass("highlightDark");
var $pageContents = $("#pageContents");
if ($pageContents.data("selectedLabel") != undefined && $pageContents.data("selectedLabel") != "") {
$(this).attr("title", targetTxt1 + " " + $(this).find("h3").html() + " - " + targetTxt2);
}
}
})
.focusout(function() {
var $pageContents = $("#pageContents");
$(this)
.removeClass("highlightDark")
.attr("title", targetTxt1 + " " + $(this).find("h3").html());
})
.keypress(function(e) {
if ($(e.target).hasClass("category")) {
var charCode = e.charCode || e.keyCode;
if (charCode == 32) {
var $pageContents = $("#pageContents");
var $selectedLabel = $pageContents.data("selectedLabel");
// drop selected label on target, remove selection and show next label
if ($selectedLabel != undefined && $selectedLabel != "") {
if ($selectedLabel.parent().is($("#initHolder"))) {
$("#initHolder .label:eq(1)").show();
}
$selectedLabel
.removeClass("selected")
.attr("title", labelTxt1)
.appendTo($(this));
$pageContents.data("selectedLabel", "");
$(this).attr("title", targetTxt1 + " " + $(this).find("h3").html());
// change tab index of label so it's now after the target it's on
var categoryIndex = $(this).attr("tabindex");
$(this).children(".label").each(function(i) {
$(this).attr("tabindex", categoryIndex + i + 1);
});
}
}
}
});
// set tab index for targets - leaving space between them for any labels that are put on them
var tabIndex = 0;
for (i=0; i<$categoryHolder.find(".category").length; i++) {
tabIndex = (i + 1) * ($pageContents.data("labels").length + 1) + 1;
var $category = $categoryHolder.find(".category:eq(" + i + ")");
$category.attr({
"tabindex" :tabIndex,
"title" :targetTxt1 + " " + $category.find("h3").html()
});
}
tabIndex += $pageContents.data("labels").length;
$button.attr("tabindex", tabIndex + 1);
$feedback.attr("tabindex", tabIndex + 2);
this.createLabels();
if (x_currentPage != 0) {
categories.sizeChanged();
}
x_pageLoaded();
}
}
categories.init();
</script>
<style type="text/css">
#dragDropHolder {
overflow: auto;
}
#dragDropHolder .label {
cursor: pointer;
list-style-type: none;
padding: 5px;
}
#dragDropHolder .label.focus {
border: 2px solid yellow;
}
#dragDropHolder .label.selected {
border: 2px solid green;
}
#dragDropHolder .tick {
float: right;
}
#initHolder {
padding: 0px;
margin-right: 2%;
float: right;
}
#categoryHolder {
clear: right;
}
#categoryHolder .category {
padding: 1%;
float: left;
}
#categoryHolder .category .label {
margin-top: 10px;
}
#categoryHolder h3 {
text-align: center;
}
#button {
clear: left;
margin-bottom: 10px;
float: right;
}
#feedback {
clear: left;
}
</style>
<div id="pageContents">
<div id="dragDropHolder">
<ul id="initHolder"></ul>
<div id="textHolder" tabindex="1"></div>
<div id="categoryHolder">
<ul class="category highlight"><h3></h3></ul>
</div>
</div>
<button id="button"></button>
<div id="feedback"></div>
</div>