Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit f97e971

Browse files
committed
Fixed parsons bugs
1 parent c0a840f commit f97e971

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

runestone/parsons/js/parsons.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Parsons.prototype.populateContentArray = function () {
5858
var temp = fulltext.split(delimiter);
5959
var content = temp[1];
6060
this.contentArray = content.split("---");
61+
if (this.contentArray.length === 1) { // If there are no ---, then every line is its own block
62+
this.contentArray = content.split("\n");
63+
}
6164

6265
// remove newline characters that precede and follow the --- delimiters
6366
for (var i = 0; i < this.contentArray.length; i++) {
@@ -68,6 +71,19 @@ Parsons.prototype.populateContentArray = function () {
6871
this.contentArray[i] = this.contentArray[i].slice(0, -1);
6972
}
7073
}
74+
for (var i = 0; i < this.contentArray.length; i++) {
75+
if ($.inArray("\n", this.contentArray[i])) {
76+
var newString = "";
77+
for (var j = 0; j < this.contentArray[i].length; j ++) {
78+
if (this.contentArray[i][j] === "\n") {
79+
newString += "</br>";
80+
} else {
81+
newString += this.contentArray[i][j];
82+
}
83+
}
84+
this.contentArray[i] = newString;
85+
}
86+
}
7187
};
7288

7389
/*====================================
@@ -186,7 +202,6 @@ Parsons.prototype.createParsonsWidget = function () {
186202
return false;
187203
});
188204

189-
this.styleNewHTML();
190205
this.pwidget = new ParsonsWidget({
191206
"sortableId": "parsons-sortableCode-" + Parsons.counter,
192207
"trashId": "parsons-sortableTrash-" + Parsons.counter,
@@ -195,28 +210,29 @@ Parsons.prototype.createParsonsWidget = function () {
195210
"feedback_cb": this.displayErrors.bind(this)
196211
});
197212

198-
this.pwidget.init($(this.origDiv).text());
213+
this.pwidget.init($(this.origDiv).html());
199214
this.pwidget.shuffleLines();
200215
this.tryLocalStorage();
216+
this.styleNewHTML();
201217
};
202218

203219
Parsons.prototype.styleNewHTML = function () {
204-
$(window).load(function () {
205-
// set min width and height
206-
var sortableul = $("#ul-parsons-sortableCode-" + Parsons.counter);
207-
var trashul = $("#ul-parsons-sortableTrash-" + Parsons.counter);
208-
var sortableHeight = sortableul.height();
209-
var sortableWidth = sortableul.width();
210-
var trashWidth = trashul.width();
211-
var trashHeight = trashul.height();
212-
var minHeight = Math.max(trashHeight, sortableHeight);
213-
var minWidth = Math.max(trashWidth, sortableWidth);
214-
trashul.css("min-height", minHeight + "px");
215-
sortableul.css("min-height", minHeight + "px");
216-
sortableul.height(minHeight);
217-
trashul.css("min-width", minWidth + "px");
218-
sortableul.css("min-width", minWidth + "px");
219-
});
220+
// set min width and height
221+
var sortableul = $("#ul-parsons-sortableCode-" + Parsons.counter);
222+
var trashul = $("#ul-parsons-sortableTrash-" + Parsons.counter);
223+
var sortableHeight = sortableul.height();
224+
var sortableWidth = sortableul.width();
225+
var trashWidth = trashul.width();
226+
var trashHeight = trashul.height();
227+
var minHeight = Math.max(trashHeight, sortableHeight);
228+
var minWidth = Math.max(trashWidth, sortableWidth);
229+
var test = document.getElementById("ul-parsons-sortableTrash-" + Parsons.counter);
230+
trashul.css("min-height", minHeight + "px");
231+
sortableul.css("min-height", minHeight + "px");
232+
sortableul.height(minHeight);
233+
trashul.css("min-width", minWidth + "px");
234+
sortableul.css("min-width", minWidth + "px");
235+
test.minWidth = minWidth + "px";
220236
};
221237

222238
Parsons.prototype.displayErrors = function (fb) { // Feedback function

runestone/parsons/js/parsonsaux.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ ParsonsWidget.prototype.getRandomPermutation = function (n) {
866866
ParsonsWidget.prototype.shuffleLines = function () {
867867
var permutation = this.getRandomPermutation(this.modified_lines.length);
868868
var idlist = [];
869-
for (var i in permutation) {
869+
for (var i = 0; i < permutation.length; i++) {
870870
idlist.push(this.modified_lines[permutation[i]].id);
871871
}
872872
if (this.options.trashId) {
@@ -895,7 +895,7 @@ ParsonsWidget.prototype.codeLineToHTML = function (codeline) {
895895

896896
ParsonsWidget.prototype.codeLinesToHTML = function (codelineIDs, destinationID) {
897897
var lineHTML = [];
898-
for (var id in codelineIDs) {
898+
for (var id = 0; id < codelineIDs.length; id++) {
899899
var line = this.getLineById(codelineIDs[id]);
900900
lineHTML.push(this.codeLineToHTML(line));
901901
}

runestone/parsons/parsons.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,15 @@ def findmax(alist):
103103
self.options['qnumber'] = self.getNumber()
104104
self.options['instructions'] = ""
105105
self.options['code'] = self.content
106-
107106
if '-----' in self.content:
108107
index = self.content.index('-----')
109108
self.options['instructions'] = "\n".join(self.content[:index])
110109
self.options['code'] = self.content[index + 1:]
111110

112111
if '=====' in self.options['code']:
113-
self.options['code'] = self.options['code'].replace('====', '---')
112+
self.options['code'] = "\n".join(self.options['code'])
113+
114+
self.options['code'] = self.options['code'].replace('=====', '---')
114115
else:
115116
self.options['code'] = "\n".join(self.options['code'])
116117

0 commit comments

Comments
 (0)