Skip to content

Commit

Permalink
Merge pull request #3368 from Max-Lovell/main
Browse files Browse the repository at this point in the history
Replace uses of Array.from() for Qualtrics compatibility
  • Loading branch information
jodeleeuw authored Aug 8, 2024
2 parents 2b1f93d + 4b080ca commit 1776f1f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-singers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jspsych/plugin-sketchpad": patch
---

Fixed broken event handlers in the sketchpad plugin.
7 changes: 7 additions & 0 deletions .changeset/sixty-bobcats-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"jspsych": patch
"@jspsych/plugin-free-sort": patch
"@jspsych/plugin-maxdiff": patch
---

Remove uses of Array.from() to improve Qualtrics compatibility
1 change: 1 addition & 0 deletions contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following people have contributed to the development of jsPsych by writing c
* kupiqu - https://github.com/kupiqu
* Daiichiro Kuroki - https://github.com/kurokida
* Jonas Lambers
* Max Lovell - https://github.com/max-lovell
* madebyafox - https://github.com/madebyafox
* Shane Martin - https://github.com/shamrt
* Vijay Marupudi - https://github.com/vijaymarupudi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class KeyboardListenerAPI {
private rootKeydownListener(e: KeyboardEvent) {
// Iterate over a static copy of the listeners set because listeners might add other listeners
// that we do not want to be included in the loop
for (const listener of Array.from(this.listeners)) {
for (const listener of [...this.listeners]) {
listener(e);
}
this.heldKeys.add(this.toLowerCaseIfInsensitive(e.key));
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-free-sort/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class FreeSortPlugin implements JsPsychPlugin<Info> {
let cur_in = false;

// draggable items
const draggables = Array.from(
const draggables = Array.prototype.slice.call(
display_element.querySelectorAll<HTMLImageElement>(".jspsych-free-sort-draggable")
);

Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-maxdiff/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ class MaxdiffPlugin implements JsPsychPlugin<Info> {
// check response
if (trial.required) {
// Now check if one of both left and right have been enabled to allow submission
var left_checked = Array.from(document.getElementsByName("left")).some(
(c: HTMLInputElement) => c.checked
);
var right_checked = Array.from(document.getElementsByName("right")).some(
(c: HTMLInputElement) => c.checked
);
var left_checked = Array.prototype.slice
.call(document.getElementsByName("left"))
.some((c: HTMLInputElement) => c.checked);
var right_checked = Array.prototype.slice
.call(document.getElementsByName("right"))
.some((c: HTMLInputElement) => c.checked);
if (left_checked && right_checked) {
(document.getElementById("jspsych-maxdiff-next") as HTMLInputElement).disabled =
false;
Expand Down
24 changes: 15 additions & 9 deletions packages/plugin-sketchpad/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ class SketchpadPlugin implements JsPsychPlugin<Info> {
});
}

this.sketchpad.addEventListener("pointerdown", this.start_draw);
this.sketchpad.addEventListener("pointermove", this.move_draw);
this.sketchpad.addEventListener("pointerup", this.end_draw);
this.sketchpad.addEventListener("pointerleave", this.end_draw);
this.sketchpad.addEventListener("pointercancel", this.end_draw);
this.sketchpad.addEventListener("pointerdown", this.start_draw.bind(this));
this.sketchpad.addEventListener("pointermove", this.move_draw.bind(this));
this.sketchpad.addEventListener("pointerup", this.end_draw.bind(this));
this.sketchpad.addEventListener("pointerleave", this.end_draw.bind(this));
this.sketchpad.addEventListener("pointercancel", this.end_draw.bind(this));

if (this.params.key_to_draw !== null) {
document.addEventListener("keydown", (e) => {
Expand Down Expand Up @@ -452,16 +452,22 @@ class SketchpadPlugin implements JsPsychPlugin<Info> {
}

if (this.params.show_undo_button) {
this.display.querySelector("#sketchpad-undo").addEventListener("click", this.undo);
this.display.querySelector("#sketchpad-undo").addEventListener("click", this.undo.bind(this));
if (this.params.show_redo_button) {
this.display.querySelector("#sketchpad-redo").addEventListener("click", this.redo);
this.display
.querySelector("#sketchpad-redo")
.addEventListener("click", this.redo.bind(this));
}
}
if (this.params.show_clear_button) {
this.display.querySelector("#sketchpad-clear").addEventListener("click", this.clear);
this.display
.querySelector("#sketchpad-clear")
.addEventListener("click", this.clear.bind(this));
}

const color_btns = Array.from(this.display.querySelectorAll(".sketchpad-color-select"));
const color_btns = Array.prototype.slice.call(
this.display.querySelectorAll(".sketchpad-color-select")
);
for (const btn of color_btns) {
btn.addEventListener("click", (e) => {
const target = e.target as HTMLButtonElement;
Expand Down

0 comments on commit 1776f1f

Please sign in to comment.