This repository was archived by the owner on Jun 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathhparsons.js
More file actions
280 lines (257 loc) · 9.81 KB
/
hparsons.js
File metadata and controls
280 lines (257 loc) · 9.81 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
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
import RunestoneBase from "../../common/js/runestonebase.js";
import "../css/hparsons.css";
import "../css/hljs-xcode.css";
import BlockFeedback from "./BlockFeedback.js";
import SQLFeedback from "./SQLFeedback.js";
import {InitMicroParsons} from 'micro-parsons/micro-parsons/micro-parsons.js';
import 'micro-parsons/micro-parsons/micro-parsons.css';
// If you need to debug something in the micro-parsons library then
// gh repo clone amy21206/micro-parsons-element
// run npm install and npm build
// copy everything from bin into the hparsons/js folder and build the components.
/*import {InitMicroParsons} from './micro-parsons.js';
import './micro-parsons.css';*/
export var hpList;
// Dictionary that contains all instances of horizontal Parsons problem objects
if (hpList === undefined) hpList = {};
export default class HParsons extends RunestoneBase {
constructor(opts) {
super(opts);
// getting settings
var orig = $(opts.orig).find("textarea")[0];
this.reuse = $(orig).data("reuse") ? true : false;
this.randomize = $(orig).data("randomize") ? true : false;
this.isBlockGrading = $(orig).data("blockanswer") ? true : false;
this.language = $(orig).data("language");
this.renderRaw = false;
if (this.isBlockGrading) {
this.blockAnswer = $(orig).data("blockanswer").split(" ");
}
this.divid = opts.orig.id;
this.containerDiv = opts.orig;
this.useRunestoneServices = opts.useRunestoneServices;
// Set the storageId (key for storing data)
var storageId = super.localStorageKey();
this.storageId = storageId;
this.origElem = orig;
this.origText = this.origElem.textContent;
this.code = $(orig).text() || "\n\n\n\n\n";
this.dburl = $(orig).data("dburl");
this.runButton = null;
this.saveButton = null;
this.loadButton = null;
this.outerDiv = null;
this.controlDiv = null;
this.processContent(this.code)
// Change to factory when more execution based feedback is included
if (this.isBlockGrading) {
this.feedbackController = new BlockFeedback(this);
} else {
this.feedbackController = new SQLFeedback(this);
}
// creating UI components
this.createEditor();
this.createOutput();
this.createControls();
this.feedbackController.customizeUI();
if ($(orig).data("caption")) {
this.caption = $(orig).data("caption");
} else {
this.caption = "MicroParsons";
}
this.addCaption("runestone");
this.indicate_component_ready();
// initializing functionalities for different feedback
this.feedbackController.init();
this.checkServer('hparsonsAnswer', true);
}
processContent(code) {
// todo: add errors when blocks are nonexistent (maybe in python)?
this.hiddenPrefix = this.processSingleContent(code, '--hiddenprefix--');
this.originalBlocks = this.processSingleContent(code, '--blocks--').split('\n').slice(1,-1);
this.hiddenSuffix = this.processSingleContent(code, '--hiddensuffix--');
this.unittest = this.processSingleContent(code, '--unittest--');
// (for pretext) if all blocks can be parsed as html but language is not html,
// ask micro parsons to render raw
if (this.language != 'html') {
this.renderRaw = true;
for (let i = 0; i < this.originalBlocks.length; ++i) {
if (!this.isHTML(this.originalBlocks[i])) {
this.renderRaw = false;
break;
}
}
}
}
isHTML(block) {
let doc = new DOMParser().parseFromString(block, "text/html");
return Array.from(doc.body.childNodes).some(node => node.nodeType === 1);
}
processSingleContent(code, delimitier) {
let index = code.indexOf(delimitier);
if (index > -1) {
let content = code.substring(index + delimitier.length);
let endIndex = content.indexOf("\n--");
content =
endIndex > -1
? content.substring(0, endIndex + 1)
: content;
return content;
}
return undefined;
}
// copied from activecode, already modified to add parsons
createEditor() {
this.outerDiv = document.createElement("div");
$(this.origElem).replaceWith(this.outerDiv);
this.outerDiv.id = `${this.divid}-container`;
this.outerDiv.addEventListener("micro-parsons", (ev) => {
const eventListRunestone = ['input', 'reset'];
if (eventListRunestone.includes(ev.detail.type)) {
// only log the events in the event list
this.logHorizontalParsonsEvent(ev.detail);
// when event is input or reset: clear previous feedback
this.feedbackController.clearFeedback();
}
});
const props = {
selector: `#${this.divid}-container`,
id: `${this.divid}-hparsons`,
reuse: this.reuse,
randomize: this.randomize,
parsonsBlocks: [...this.originalBlocks],
language: this.renderRaw ? 'raw' : this.language
}
InitMicroParsons(props);
this.hparsonsInput = $(this.outerDiv).find("micro-parsons")[0];
}
createOutput() {
this.feedbackController.createOutput();
}
createControls() {
var ctrlDiv = document.createElement("div");
$(ctrlDiv).addClass("hp_actions");
// Run Button
this.runButton = document.createElement("button");
$(this.runButton).addClass("btn btn-success run-button");
ctrlDiv.appendChild(this.runButton);
$(this.runButton).attr("type", "button");
$(this.runButton).text("Run");
var that = this;
this.runButton.onclick = () => {
that.feedbackController.runButtonHandler();
that.setLocalStorage();
};
// Reset button
var resetBtn;
resetBtn = document.createElement("button");
$(resetBtn).text("Reset");
$(resetBtn).addClass("btn btn-warning run-button");
ctrlDiv.appendChild(resetBtn);
this.resetButton = resetBtn;
this.resetButton.onclick = () => {
that.hparsonsInput.resetInput();
that.setLocalStorage();
that.feedbackController.reset();
};
$(resetBtn).attr("type", "button");
$(this.outerDiv).prepend(ctrlDiv);
this.controlDiv = ctrlDiv;
}
// Return previous answers in local storage
//
localData() {
var data = localStorage.getItem(this.storageId);
if (data !== null) {
if (data.charAt(0) == "{") {
data = JSON.parse(data);
} else {
data = {};
}
} else {
data = {};
}
return data;
}
// RunestoneBase: Sent when the server has data
restoreAnswers(serverData) {
// TODO: not tested with server data yet.
// Server side data should be:
/*
{
answer: Array<string>, // list of answer block content
count: ?number // number of previous attempts if block-based feedback
}
*/
if (serverData.answer){
this.hparsonsInput.restoreAnswer(serverData.answer.blocks);
}
if (serverData.count) {
this.feedbackController.checkCount = serverData.count;
}
}
// RunestoneBase: Load what is in local storage
checkLocalStorage() {
if (this.graderactive) {
// Zihan: I think this means the component is still loading?
return;
}
let localData = this.localData();
if (localData.answer) {
this.hparsonsInput.restoreAnswer(localData.answer);
}
if (localData.count) {
this.feedbackController.checkCount = localData.count;
}
}
// RunestoneBase: Set the state of the problem in local storage
setLocalStorage(data) {
let currentState = {};
if (data == undefined) {
currentState = {
answer: this.hparsonsInput.getParsonsTextArray()
}
if (this.isBlockGrading) {
// if this is block grading, add number of previous attempts too
currentState.count = this.feedbackController.checkCount;
}
} else {
currentState = data;
}
localStorage.setItem(this.storageId, JSON.stringify(currentState));
}
logHorizontalParsonsEvent(hparsonsEvent) {
let ev = {
event: "hparsons",
div_id: this.divid,
act: JSON.stringify(hparsonsEvent),
};
this.logBookEvent(ev);
}
}
/*=================================
== Find the custom HTML tags and ==
== execute our code on them ==
=================================*/
$(document).on("runestone:login-complete", function () {
$("[data-component=hparsons]").each(function () {
if ($(this).closest("[data-component=timedAssessment]").length == 0) {
// If this element exists within a timed component, don't render it here
// try {
hpList[this.id] = new HParsons({
orig: this,
useRunestoneServices: eBookConfig.useRunestoneServices,
});
// } catch (err) {
// console.log(`Error rendering ShortAnswer Problem ${this.id}
// Details: ${err}`);
// }
}
});
});
if (typeof window.component_factory === "undefined") {
window.component_factory = {};
}
window.component_factory["hparsons"] = function (opts) {
return new HParsons(opts);
};