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

Commit c7dda64

Browse files
committed
✨ Allow resetting the input
1 parent 9211c6e commit c7dda64

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

runestone/hparsons/js/horizontal-parsons.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,9 +3034,6 @@ var RegexEvent;
30343034
class ParsonsInput {
30353035
// The input element
30363036
el;
3037-
// TODO(refactor): make expandable blocks more easy to use
3038-
expandableBlocks;
3039-
expandableBlockTooltips;
30403037
_dropArea;
30413038
_dragArea;
30423039
_dropSortable;
@@ -3045,6 +3042,10 @@ class ParsonsInput {
30453042
_prevPosition;
30463043
reusable;
30473044
randomize;
3045+
storedSourceBlocks;
3046+
storedSourceBlockExplanations;
3047+
// if the input has been initialized once
3048+
initialized;
30483049
constructor(parentElement, reusable, randomize) {
30493050
this.el = document.createElement('div');
30503051
this.parentElement = parentElement;
@@ -3064,21 +3065,14 @@ class ParsonsInput {
30643065
this.el.appendChild(this._dropArea);
30653066
this._dropArea.classList.add('drop-area');
30663067
this._prevPosition = -1;
3067-
this.expandableBlocks = [];
3068-
this.expandableBlockTooltips = null;
3068+
this.storedSourceBlocks = [];
3069+
this.storedSourceBlockExplanations = null;
30693070
this.reusable = reusable;
30703071
this.randomize = randomize;
3071-
this._dragSortable = new Sortable(this._dragArea, {
3072-
group: 'shared',
3073-
direction: 'horizontal',
3074-
animation: 150
3075-
});
3076-
this._dropSortable = new Sortable(this._dropArea, {
3077-
group: 'shared',
3078-
direction: 'horizontal',
3079-
animation: 150
3080-
});
3072+
this._dragSortable = null;
3073+
this._dropSortable = null;
30813074
this._initSortable();
3075+
this.initialized = false;
30823076
}
30833077
getText = () => {
30843078
let ret = '';
@@ -3105,38 +3099,45 @@ class ParsonsInput {
31053099
}
31063100
}
31073101
setSourceBlocks = (data, tooltips) => {
3108-
// reset
3109-
// this._dragSortable.destroy();
3110-
// this._dropSortable.destroy();
3111-
// clearing previous settings
3112-
this._dragArea.innerHTML = '';
3113-
this._dropArea.innerHTML = '';
3114-
// adding normal blocks
3102+
// shuffle source blocks if randomize
31153103
if (this.randomize) {
31163104
let originalData = JSON.stringify(data);
31173105
this.shuffleArray(data);
31183106
while (JSON.stringify(data) == originalData) {
31193107
this.shuffleArray(data);
31203108
}
31213109
}
3122-
for (let i = 0; i < data.length; ++i) {
3110+
this.storedSourceBlocks = data;
3111+
this.storedSourceBlockExplanations = tooltips;
3112+
this._resetInput();
3113+
};
3114+
// TODO: not efficient enough. should not need to create new elements; simply sorting them should be good.
3115+
_resetInput = () => {
3116+
// clearing previous blocks
3117+
while (this._dragArea.firstChild) {
3118+
this._dragArea.removeChild(this._dragArea.firstChild);
3119+
}
3120+
while (this._dropArea.firstChild) {
3121+
this._dropArea.removeChild(this._dropArea.firstChild);
3122+
}
3123+
// add new blocks
3124+
for (let i = 0; i < this.storedSourceBlocks.length; ++i) {
31233125
const newBlock = document.createElement('div');
31243126
this._dragArea.appendChild(newBlock);
3125-
if (data[i] === ' ') {
3127+
if (this.storedSourceBlocks[i] === ' ') {
31263128
// console.log('here');
31273129
newBlock.innerHTML = '&nbsp;';
31283130
}
31293131
else {
31303132
// console.log(data[i]);
3131-
newBlock.innerText = data[i];
3133+
newBlock.innerText = this.storedSourceBlocks[i];
31323134
}
31333135
newBlock.style.display = 'inline-block';
31343136
newBlock.classList.add('parsons-block');
31353137
newBlock.onclick = () => {
31363138
this._onBlockClicked(newBlock);
31373139
};
31383140
}
3139-
this._initSortable();
31403141
};
31413142
_onBlockClicked = (block) => {
31423143
if (block.parentElement == this._dragArea) {
@@ -3177,8 +3178,6 @@ class ParsonsInput {
31773178
}
31783179
};
31793180
_initSortable = () => {
3180-
this._dragSortable.destroy();
3181-
this._dropSortable.destroy();
31823181
if (this.reusable) {
31833182
this._dragSortable = new Sortable(this._dragArea, {
31843183
group: {
@@ -3296,9 +3295,6 @@ class ParsonsInput {
32963295
}
32973296
this._dropArea.classList.add(result);
32983297
};
3299-
setExpandableBlocks = (expandableBlocks) => {
3300-
this.expandableBlocks = expandableBlocks;
3301-
};
33023298
_getBlockPosition = (block) => {
33033299
let position = -1;
33043300
const parent = this._dropArea;
@@ -3356,6 +3352,16 @@ class ParsonsInput {
33563352
}
33573353
}
33583354
};
3355+
resetInput = () => {
3356+
if (this.reusable) {
3357+
while (this._dropArea.firstChild) {
3358+
this._dropArea.removeChild(this._dropArea.firstChild);
3359+
}
3360+
}
3361+
else {
3362+
this._resetInput();
3363+
}
3364+
};
33593365
// TODO: not used for now, not sure if is working correctly
33603366
restoreAnswer(type, answer) {
33613367
if (type != 'parsons' || !Array.isArray(answer)) {
@@ -15593,11 +15599,18 @@ class HParsonsElement extends HTMLElement {
1559315599
});
1559415600
}
1559515601
}
15596-
resetTool() {
15602+
resetInput() {
1559715603
if (this.inputType != 'parsons') {
1559815604
const regexInput = this.hparsonsInput;
1559915605
regexInput.quill?.setText('', 'silent');
1560015606
}
15607+
else if (this.inputType == 'parsons') {
15608+
this.hparsonsInput.resetInput();
15609+
}
15610+
const resetEvent = {
15611+
'event-type': 'reset',
15612+
};
15613+
this.logEvent(resetEvent);
1560115614
}
1560215615
// restore student answer from outside storage
1560315616
restoreAnswer(type, answer) {

runestone/hparsons/js/hparsons-sql.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ export default class SQLHParons extends RunestoneBase {
207207
this.runButton.onclick = this.runButtonHandler.bind(this);
208208
$(butt).attr("type", "button");
209209

210+
// Reset button
211+
var resetBtn;
212+
resetBtn = document.createElement("button");
213+
$(resetBtn).text("Reset");
214+
$(resetBtn).addClass("btn btn-warning run-button");
215+
ctrlDiv.appendChild(resetBtn);
216+
this.resetButton = resetBtn;
217+
this.resetButton.onclick = () => this.hparsons.resetInput();
218+
$(resetBtn).attr("type", "button");
219+
210220
// TODO: maybe remove the question part
211221
$(this.outerDiv).prepend(ctrlDiv);
212222
this.controlDiv = ctrlDiv;

0 commit comments

Comments
 (0)