forked from Khan/khan-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.js
452 lines (379 loc) · 17.5 KB
/
history.js
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
/**
* Event handlers for when problem history is being viewed.
*/
(function() {
$(Exercises)
.bind("newProblem", renderReadOnlyProblem);
function renderReadOnlyProblem(event, args) {
var framework = Exercises.getCurrentFramework();
var userExercise = args.userExercise;
var answerData = args.answerData;
var answerType = args.answerType;
var solution = args.solution;
var hints = args.hints;
var problem = args.problem;
var solutionarea = $("#solutionarea");
if (typeof userExercise !== "undefined" && userExercise.readOnly) {
if (framework === "perseus") {
$(Exercises).trigger("warning", [$._("Problem history is " +
"not yet available for this exercise."), true]);
} else if (framework === "khan-exercises") {
if (!userExercise.current) {
$(Exercises).trigger("warning", [$._("This exercise may have " +
"changed since it was completed"), true]);
}
}
var timelineEvents, timeline;
var timelinecontainer = $("<div id='timelinecontainer'>")
.append("<div>\n" +
"<div id='previous-problem' class='simple-button'>" +
$._("Previous Problem") + "</div>\n" +
"<div id='previous-step' class='simple-button'><span>" +
$._("Previous Step") + "</span></div>\n</div>")
.insertBefore("#problem-and-answer");
$.fn.disable = function() {
this.addClass("disabled")
.css({
cursor: "default !important"
})
.data("disabled", true);
return this;
};
$.fn.enable = function() {
this.removeClass("disabled")
.css({
cursor: "pointer"
})
.data("disabled", false);
return this;
};
if (userExercise.totalDone === 0) {
$("#previous-problem").disable();
}
timeline = $("<div id='timeline'>").appendTo(timelinecontainer);
timelineEvents = $("<div id='timeline-events'>").appendTo(timeline);
// Grab both scrubbers packaged up in one jQuery object. This is
// wrapped in a function just because the variables held inside are
// not used elsewhere
var scrubber = (function() {
var scrubberCss = {
display: "block",
width: "0",
height: "0",
"border-left": "6px solid transparent",
"border-right": "6px solid transparent",
position: "absolute"
},
scrubber1 = $("<div>")
.css($.extend({}, scrubberCss, {
"border-top": "6px solid #888",
top: "0"
}))
.appendTo(timeline),
scrubber2 = $("<div>")
.css($.extend({}, scrubberCss, {
"border-bottom": "6px solid #888",
bottom: "0"
}))
.appendTo(timeline);
return scrubber1.add(scrubber2);
})();
timelinecontainer
.append("<div>\n" +
"<div id='next-problem' class='simple-button'>" +
$._("Next Problem") + "</div>\n" +
"<div id='next-step' class='simple-button'><span>" +
$._("Next Step") + "</span></div>\n</div>");
$("<div class='user-activity correct-activity'>" + $._("Started") + "</div>")
.data("hint", false)
.appendTo(timelineEvents);
var hintNumber = 0;
/* value[0]: css class
* value[1]: guess
* value[2]: time taken since last guess
*/
$.each(userExercise.userActivity, function(index, value) {
// TODO(emily): figure out where this is coming from, and if we
// can remove it. It shouldn't be i18n-ized though
var guess = value[1] === "Activity Unavailable" ? value[1] : JSON.parse(value[1]),
thissolutionarea;
timelineEvents
// I18N: This is a number of seconds, like '3s'
.append("<div class='timeline-time'>" + $._("%(time)ss", {time: value[2]}) + "</div>");
thissolutionarea = $("<div>")
.addClass("user-activity " + value[0])
.appendTo(timelineEvents);
if (value[0] === "hint-activity") {
thissolutionarea.attr("title", $._("Hint used"));
thissolutionarea
.data("hint", hintNumber)
.prepend($._("Hint #%(num)s", {num: (hintNumber + 1)}));
hintNumber += 1;
} else { // This panel is a solution (or the first panel)
thissolutionarea.data("hint", false);
// See above, this shouldn't be i18n-ized
if (guess === "Activity Unavailable") {
thissolutionarea.text(guess);
} else {
if (framework === "perseus") {
thissolutionarea
.removeClass("incorrect-activity")
.addClass("correct-activity");
thissolutionarea.attr("title", $._("Answer Attempted"));
thissolutionarea.append(
$("<p class='solution'>" + $._("Answer attempted") + "</p>")
);
} else if (framework === "khan-exercises") {
// radio and custom are the only answer types that
// can't display its own guesses in the activity bar
var validator = Khan.answerTypes[answerType].setup(null, solution).validator;
if (answerType === "radio") {
thissolutionarea.append(
// Add the guess to the activity bar
// TODO(emily): remove this
// backwards-compatible code in 7/13
$("<p class='solution'>" +
(guess.value != null ? guess.value : guess) +
"</p>").runModules()
);
if (validator(guess)) {
thissolutionarea
.removeClass("incorrect-activity")
.addClass("correct-activity");
thissolutionarea.attr("title", $._("Correct Answer"));
} else {
thissolutionarea.attr("title", $._("Incorrect Answer"));
}
} else if (answerType === "custom") {
if (validator(guess)) {
thissolutionarea
.removeClass("incorrect-activity")
.addClass("correct-activity");
thissolutionarea.attr("title", $._("Correct Answer"));
thissolutionarea.append(
$("<p class='solution'>" + $._("Answer correct") + "</p>")
);
} else {
thissolutionarea
.removeClass("correct-activity")
.addClass("incorrect-activity");
thissolutionarea.attr("title", $._("Incorrect Answer"));
thissolutionarea.append(
$("<p class='solution'>" + $._("Answer incorrect") + "</p>")
);
}
} else {
var thisAnswerData = Khan.answerTypes[answerType].setup(thissolutionarea, solution);
thisAnswerData.showGuess(guess);
if (thisAnswerData.validator(guess) === true) {
// If the user didn't get the problem right on the first try, all
// answers are labelled incorrect by default
thissolutionarea
.removeClass("incorrect-activity")
.addClass("correct-activity");
thissolutionarea.attr("title", $._("Correct Answer"));
} else {
thissolutionarea
.removeClass("correct-activity")
.addClass("incorrect-activity");
thissolutionarea.attr("title", $._("Incorrect Answer"));
}
}
}
thissolutionarea
.data("guess", guess)
.find("input")
.attr("disabled", true)
.end()
.find("select")
.attr("disabled", true);
}
}
});
if (timelinecontainer.height() > timeline.height()) {
timeline.height(timelinecontainer.height());
}
var states = timelineEvents.children(".user-activity"),
currentSlide = Math.min(states.length - 1, 1),
numSlides = states.length,
timelineMiddle = timeline.width() / 2,
realHintsArea = $("#hintsarea"),
realWorkArea = $("#workarea"),
statelist = [],
previousHintNum = 100000;
// So highlighting doesn't fade to white
$("#solutionarea").css("background-color", $("#answercontent").css("background-color"));
// scroll to the slide held in state
var scrub = function(state, fadeTime) {
var timeline = $("#timeline"),
slide = state.slide;
timeline.animate({
scrollLeft: state.scroll
}, fadeTime);
scrubber.animate({
left: (timeline.scrollLeft() + slide.position().left + slide.outerWidth() / 2 + 2) + "px"
}, fadeTime);
};
// Set the width of the timeline (starts as 10000px) after MathJax loads
MathJax.Hub.Queue(function() {
var maxHeight = 0;
timelineEvents.children().each(function() {
maxHeight = Math.max(maxHeight, $(this).outerHeight(true));
});
// This thing looks ridiculous above about 100px
if (maxHeight > 100) {
timelineEvents.children(".correct-activity, .incorrect-activity").each(function() {
$(this).text($._("Answer"));
});
} else if (maxHeight > timelinecontainer.height()) {
timelinecontainer.height(maxHeight);
timeline.height(maxHeight);
}
});
var create = function(i) {
var thisSlide = states.eq(i);
var thisHintArea, thisProblem,
hintNum = $("#timeline-events .user-activity:lt(" + (i + 1) + ")")
.filter(".hint-activity").length - 1,
// Bring the currently focused panel as close to the middle as possible
itemOffset = thisSlide.position().left,
itemMiddle = itemOffset + thisSlide.width() / 2,
offset = timelineMiddle - itemMiddle,
currentScroll = timeline.scrollLeft(),
timelineMax = states.eq(-1).position().left + states.eq(-1).width() + 5,
scroll = Math.min(currentScroll - offset, currentScroll + timelineMax - timeline.width() + 25);
if (hintNum >= 0) {
$(hints[hintNum]).appendTo(realHintsArea).runModules(problem);
}
MathJax.Hub.Queue(function() {
var recordState = function() {
$("#problemarea input").attr({disabled: "disabled"});
thisHintArea = realHintsArea.clone();
thisProblem = realWorkArea.clone();
var thisState = {
slide: thisSlide,
hintNum: hintNum,
hintArea: thisHintArea,
problem: thisProblem,
scroll: scroll
};
statelist[i] = thisState;
if (i + 1 < states.length) {
// Create the next state
MathJax.Hub.Queue(function() {
create(i + 1);
});
} else {
// Scroll to the starting state
activate(currentSlide);
}
};
if (framework === "khan-exercises") {
if (thisSlide.data("guess") !== undefined && _.isFunction(answerData.showCustomGuess)) {
KhanUtil.currentGraph = $(realWorkArea).find(".graphie").data("graphie");
answerData.showCustomGuess(thisSlide.data("guess"));
MathJax.Hub.Queue(recordState);
} else {
recordState();
}
}
});
};
var activate = function(slideNum) {
var thisState,
thisSlide = states.eq(slideNum),
fadeTime = 150;
// All content for this state has been built before
if (statelist[slideNum]) {
thisState = statelist[slideNum];
scrub(thisState, fadeTime);
$("#workarea").remove();
$("#hintsarea").remove();
$("#problemarea").append(thisState.problem).append(thisState.hintArea);
if (thisSlide.data("guess") !== undefined) {
solutionarea.effect("highlight", {}, fadeTime);
// If there is a guess we show it as if it was filled in by the user
answerData.showGuess(thisSlide.data("guess"));
} else {
answerData.showGuess();
}
// fire the "show guess" event
$(Khan).trigger("showGuess");
// TODO: still highlight even if hint modifies problem (and highlight following hints)
if (slideNum > 0 && (thisState.hintNum > statelist[slideNum - 1].hintNum)) {
$("#hintsarea").children().each(function(index, elem) {
if (index > previousHintNum) {
$(elem).effect("highlight", {}, fadeTime);
}
});
previousHintNum = thisState.hintNum;
}
$("#previous-step, #next-step").enable();
if (slideNum === 0) {
previousHintNum = -1;
$("#previous-step").disable();
} else if (slideNum === numSlides - 1) {
$("#next-step").disable();
}
}
};
MathJax.Hub.Queue(function() {create(0);});
// Allow users to use arrow keys to move left and right in the
// timeline
$(document).keydown(function(event) {
if (event.keyCode === 37) { // left
currentSlide -= 1;
} else if (event.keyCode === 39) { // right
currentSlide += 1;
} else {
return;
}
currentSlide = Math.min(currentSlide, numSlides - 1);
currentSlide = Math.max(currentSlide, 0);
activate(currentSlide);
return false;
});
// Allow users to click on points of the timeline
$(states).click(function() {
var index = $(this).index("#timeline .user-activity");
currentSlide = index;
activate(currentSlide);
return false;
});
$("#previous-step").click(function() {
if (currentSlide > 0) {
currentSlide -= 1;
activate(currentSlide);
}
return false;
});
$("#next-step").click(function() {
if (currentSlide < numSlides - 1) {
currentSlide += 1;
activate(currentSlide);
}
return false;
});
$("#next-problem").click(function() {
window.location.href = userExercise.nextProblemUrl;
});
$("#previous-problem").click(function() {
if (!$(this).data("disabled")) {
window.location.href = userExercise.previousProblemUrl;
}
});
// Some exercises use custom css
$("#timeline input[type='text']").css("width",
$("#answer_area input[type='text']").css("width")
);
$("#hint").attr("disabled", true);
$("#answercontent input").attr("disabled", true);
$("#answercontent select").attr("disabled", true);
if (framework === "perseus") {
// TODO(cbhl): Implement Problem History for Perseus, then remove
// this hack.
timelinecontainer.hide();
}
}
}
})();