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

Commit 736bda1

Browse files
committed
⚗️ Experimenting adding storage
1 parent 01fd166 commit 736bda1

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

runestone/hparsons/js/hparsons.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export default class HParsons extends RunestoneBase {
2828
this.divid = opts.orig.id;
2929
this.containerDiv = opts.orig;
3030
this.useRunestoneServices = opts.useRunestoneServices;
31+
32+
// Set the storageId (key for storing data)
33+
var storageId = super.localStorageKey();
34+
this.storageId = storageId;
35+
3136
this.origElem = orig;
3237
this.origText = this.origElem.textContent;
3338
this.code = $(orig).text() || "\n\n\n\n\n";
@@ -143,6 +148,46 @@ export default class HParsons extends RunestoneBase {
143148
this.controlDiv = ctrlDiv;
144149
}
145150

151+
// Return previous answers in local storage
152+
localData() {
153+
var data = localStorage.getItem(this.storageId);
154+
if (data !== null) {
155+
if (data.charAt(0) == "{") {
156+
data = JSON.parse(data);
157+
} else {
158+
data = {};
159+
}
160+
} else {
161+
data = {};
162+
}
163+
return data;
164+
}
165+
// RunestoneBase: Sent when the server has data
166+
restoreAnswers(serverData) {
167+
this.loadData(serverData);
168+
}
169+
// RunestoneBase: Load what is in local storage
170+
checkLocalStorage() {
171+
if (this.graderactive) {
172+
return;
173+
}
174+
this.loadData(this.localData());
175+
}
176+
// RunestoneBase: Set the state of the problem in local storage
177+
setLocalStorage(data) {
178+
var toStore;
179+
if (data == undefined) {
180+
toStore = {
181+
source: this.sourceHash(),
182+
answer: this.answerHash(),
183+
timestamp: new Date(),
184+
};
185+
} else {
186+
toStore = data;
187+
}
188+
localStorage.setItem(this.storageId, JSON.stringify(toStore));
189+
}
190+
146191
logHorizontalParsonsEvent(hparsonsEvent) {
147192
let ev = {
148193
event: "hparsons",

0 commit comments

Comments
 (0)