This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 866
/
history.js
333 lines (271 loc) · 11.7 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
/* TODO(csilvers): fix these lint errors (http://eslint.org/docs/rules): */
/* eslint-disable comma-dangle, indent, max-len, no-undef, no-var, one-var */
/* To fix, remove an entry above, run ka-lint, and fix errors. */
/**
* Event handlers for when problem history is being viewed.
*
* This happens in two situations currently:
* (1) viewing another user's exercises (e.g. a student)
* (2) The 'problem-out-of-order' banner is showing
* This is not well-tested code; it may currently be broken in one or both
* or these situations!
*/
(function() {
$(Exercises).bind("newProblem", renderReadOnlyProblem);
function renderReadOnlyProblem(event, args) {
var userExercise = args.userExercise;
var answerData = args.answerData;
var hints = args.hints;
var problem = args.problem;
var solutionarea = $("#solutionarea");
if (typeof userExercise !== "undefined" && userExercise.readOnly) {
$(Exercises).trigger("warning", [i18n._("Problem history is " +
"not yet available for this exercise."), true]);
var timelineEvents, timeline;
var timelinecontainer = $("<div id='timelinecontainer'>")
.append("<div>\n" +
"<div id='previous-problem' class='simple-button'>" +
i18n._("Previous Problem") + "</div>\n" +
"<div id='previous-step' class='simple-button'><span>" +
i18n._("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'>" +
i18n._("Next Problem") + "</div>\n" +
"<div id='next-step' class='simple-button'><span>" +
i18n._("Next Step") + "</span></div>\n</div>");
$("<div class='user-activity correct-activity'>" + i18n._("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'>" + i18n._("%(time)ss", {time: value[2]}) + "</div>");
thissolutionarea = $("<div>")
.addClass("user-activity " + value[0])
.appendTo(timelineEvents);
if (value[0] === "hint-activity") {
thissolutionarea.attr("title", i18n._("Hint used"));
thissolutionarea
.data("hint", hintNumber)
.prepend(i18n._("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 {
thissolutionarea
.removeClass("incorrect-activity")
.addClass("correct-activity");
thissolutionarea.attr("title", i18n._("Answer Attempted"));
thissolutionarea.append(
$("<p class='solution'>" + i18n._("Answer attempted") + "</p>")
);
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,
realHintsArea = $("#hintsarea"),
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(i18n._("Answer"));
});
} else if (maxHeight > timelinecontainer.height()) {
timelinecontainer.height(maxHeight);
timeline.height(maxHeight);
}
});
var create = function(i) {
var hintNum = $("#timeline-events .user-activity:lt(" + (i + 1) + ")")
.filter(".hint-activity").length - 1;
if (hintNum >= 0) {
$(hints[hintNum]).appendTo(realHintsArea).runModules(problem);
}
};
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();
}
// 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);
// TODO(cbhl): Implement Problem History for Perseus, then remove
// this hack.
timelinecontainer.hide();
}
}
})();