jspsych 6.3.1 → 7.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (401) hide show
  1. package/README.md +36 -37
  2. package/css/jspsych.css +39 -39
  3. package/dist/JsPsych.d.ts +112 -0
  4. package/dist/TimelineNode.d.ts +34 -0
  5. package/dist/index.browser.js +3171 -0
  6. package/dist/index.browser.js.map +1 -0
  7. package/dist/index.browser.min.js +2 -0
  8. package/dist/index.browser.min.js.map +1 -0
  9. package/dist/index.cjs +3165 -0
  10. package/dist/index.cjs.map +1 -0
  11. package/dist/index.d.ts +11 -0
  12. package/dist/index.js +3159 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/migration.d.ts +3 -0
  15. package/dist/modules/data/DataCollection.d.ts +45 -0
  16. package/dist/modules/data/DataColumn.d.ts +15 -0
  17. package/dist/modules/data/index.d.ts +25 -0
  18. package/dist/modules/data/utils.d.ts +3 -0
  19. package/dist/modules/extensions.d.ts +22 -0
  20. package/dist/modules/plugin-api/HardwareAPI.d.ts +15 -0
  21. package/dist/modules/plugin-api/KeyboardListenerAPI.d.ts +34 -0
  22. package/dist/modules/plugin-api/MediaAPI.d.ts +27 -0
  23. package/dist/modules/plugin-api/SimulationAPI.d.ts +41 -0
  24. package/dist/modules/plugin-api/TimeoutAPI.d.ts +5 -0
  25. package/dist/modules/plugin-api/index.d.ts +8 -0
  26. package/dist/modules/plugins.d.ts +136 -0
  27. package/dist/modules/randomization.d.ts +35 -0
  28. package/dist/modules/turk.d.ts +40 -0
  29. package/dist/modules/utils.d.ts +7 -0
  30. package/package.json +32 -15
  31. package/src/JsPsych.ts +884 -0
  32. package/src/TimelineNode.ts +536 -0
  33. package/src/index.ts +71 -0
  34. package/src/migration.ts +37 -0
  35. package/src/modules/data/DataCollection.ts +198 -0
  36. package/src/modules/data/DataColumn.ts +86 -0
  37. package/src/modules/data/index.ts +174 -0
  38. package/src/modules/data/utils.ts +75 -0
  39. package/src/modules/extensions.ts +23 -0
  40. package/src/modules/plugin-api/HardwareAPI.ts +32 -0
  41. package/src/modules/plugin-api/KeyboardListenerAPI.ts +165 -0
  42. package/src/modules/plugin-api/MediaAPI.ts +337 -0
  43. package/src/modules/plugin-api/SimulationAPI.ts +181 -0
  44. package/src/modules/plugin-api/TimeoutAPI.ts +16 -0
  45. package/src/modules/plugin-api/index.ts +28 -0
  46. package/src/modules/plugins.ts +165 -0
  47. package/src/modules/randomization.ts +327 -0
  48. package/src/modules/turk.ts +99 -0
  49. package/src/modules/utils.ts +30 -0
  50. package/.github/workflows/jest.yml +0 -20
  51. package/code-of-conduct.md +0 -56
  52. package/contributors.md +0 -61
  53. package/docs/CNAME +0 -1
  54. package/docs/about/about.md +0 -18
  55. package/docs/about/contributing.md +0 -43
  56. package/docs/about/license.md +0 -25
  57. package/docs/about/support.md +0 -7
  58. package/docs/core_library/jspsych-core.md +0 -719
  59. package/docs/core_library/jspsych-data.md +0 -587
  60. package/docs/core_library/jspsych-pluginAPI.md +0 -624
  61. package/docs/core_library/jspsych-randomization.md +0 -389
  62. package/docs/core_library/jspsych-turk.md +0 -98
  63. package/docs/extensions/extensions.md +0 -83
  64. package/docs/extensions/jspsych-ext-webgazer.md +0 -137
  65. package/docs/img/blue.png +0 -0
  66. package/docs/img/devtools-change-css.png +0 -0
  67. package/docs/img/devtools-css-errors.png +0 -0
  68. package/docs/img/devtools-inspect-element.png +0 -0
  69. package/docs/img/folder-setup.png +0 -0
  70. package/docs/img/folder-with-html.png +0 -0
  71. package/docs/img/githubreleases.jpg +0 -0
  72. package/docs/img/jspsych-favicon.png +0 -0
  73. package/docs/img/jspsych-logo-no-text-mono.svg +0 -493
  74. package/docs/img/jspsych-logo.jpg +0 -0
  75. package/docs/img/orange.png +0 -0
  76. package/docs/img/palmer_stim.png +0 -0
  77. package/docs/img/progress_bar.png +0 -0
  78. package/docs/img/prolific-study-completion.png +0 -0
  79. package/docs/img/prolific-study-link.png +0 -0
  80. package/docs/img/visual_search_example.jpg +0 -0
  81. package/docs/index.md +0 -9
  82. package/docs/overview/browser-device-support.md +0 -35
  83. package/docs/overview/callbacks.md +0 -180
  84. package/docs/overview/data.md +0 -281
  85. package/docs/overview/dynamic-parameters.md +0 -147
  86. package/docs/overview/exclude-browser.md +0 -32
  87. package/docs/overview/experiment-options.md +0 -149
  88. package/docs/overview/eye-tracking.md +0 -271
  89. package/docs/overview/fullscreen.md +0 -36
  90. package/docs/overview/media-preloading.md +0 -369
  91. package/docs/overview/mturk.md +0 -77
  92. package/docs/overview/plugins.md +0 -320
  93. package/docs/overview/progress-bar.md +0 -110
  94. package/docs/overview/prolific.md +0 -78
  95. package/docs/overview/record-browser-interactions.md +0 -23
  96. package/docs/overview/running-experiments.md +0 -95
  97. package/docs/overview/style.md +0 -293
  98. package/docs/overview/timeline.md +0 -457
  99. package/docs/plugins/jspsych-animation.md +0 -40
  100. package/docs/plugins/jspsych-audio-button-response.md +0 -60
  101. package/docs/plugins/jspsych-audio-keyboard-response.md +0 -58
  102. package/docs/plugins/jspsych-audio-slider-response.md +0 -53
  103. package/docs/plugins/jspsych-call-function.md +0 -81
  104. package/docs/plugins/jspsych-canvas-button-response.md +0 -66
  105. package/docs/plugins/jspsych-canvas-keyboard-response.md +0 -68
  106. package/docs/plugins/jspsych-canvas-slider-response.md +0 -89
  107. package/docs/plugins/jspsych-categorize-animation.md +0 -60
  108. package/docs/plugins/jspsych-categorize-html.md +0 -53
  109. package/docs/plugins/jspsych-categorize-image.md +0 -53
  110. package/docs/plugins/jspsych-cloze.md +0 -45
  111. package/docs/plugins/jspsych-external-html.md +0 -70
  112. package/docs/plugins/jspsych-free-sort.md +0 -56
  113. package/docs/plugins/jspsych-fullscreen.md +0 -57
  114. package/docs/plugins/jspsych-html-button-response.md +0 -42
  115. package/docs/plugins/jspsych-html-keyboard-response.md +0 -51
  116. package/docs/plugins/jspsych-html-slider-response.md +0 -45
  117. package/docs/plugins/jspsych-iat-html.md +0 -64
  118. package/docs/plugins/jspsych-iat-image.md +0 -64
  119. package/docs/plugins/jspsych-image-button-response.md +0 -48
  120. package/docs/plugins/jspsych-image-keyboard-response.md +0 -58
  121. package/docs/plugins/jspsych-image-slider-response.md +0 -54
  122. package/docs/plugins/jspsych-instructions.md +0 -58
  123. package/docs/plugins/jspsych-maxdiff.md +0 -41
  124. package/docs/plugins/jspsych-preload.md +0 -128
  125. package/docs/plugins/jspsych-rdk.md +0 -119
  126. package/docs/plugins/jspsych-reconstruction.md +0 -48
  127. package/docs/plugins/jspsych-resize.md +0 -39
  128. package/docs/plugins/jspsych-same-different-html.md +0 -53
  129. package/docs/plugins/jspsych-same-different-image.md +0 -66
  130. package/docs/plugins/jspsych-serial-reaction-time-mouse.md +0 -52
  131. package/docs/plugins/jspsych-serial-reaction-time.md +0 -57
  132. package/docs/plugins/jspsych-survey-html-form.md +0 -50
  133. package/docs/plugins/jspsych-survey-likert.md +0 -70
  134. package/docs/plugins/jspsych-survey-multi-choice.md +0 -48
  135. package/docs/plugins/jspsych-survey-multi-select.md +0 -53
  136. package/docs/plugins/jspsych-survey-text.md +0 -63
  137. package/docs/plugins/jspsych-video-button-response.md +0 -54
  138. package/docs/plugins/jspsych-video-keyboard-response.md +0 -50
  139. package/docs/plugins/jspsych-video-slider-response.md +0 -60
  140. package/docs/plugins/jspsych-virtual-chinrest.md +0 -105
  141. package/docs/plugins/jspsych-visual-search-circle.md +0 -52
  142. package/docs/plugins/jspsych-vsl-animate-occlusion.md +0 -55
  143. package/docs/plugins/jspsych-vsl-grid-scene.md +0 -62
  144. package/docs/plugins/jspsych-webgazer-calibrate.md +0 -61
  145. package/docs/plugins/jspsych-webgazer-init-camera.md +0 -30
  146. package/docs/plugins/jspsych-webgazer-validate.md +0 -44
  147. package/docs/plugins/list-of-plugins.md +0 -54
  148. package/docs/tutorials/hello-world.md +0 -162
  149. package/docs/tutorials/rt-task.md +0 -1334
  150. package/docs/tutorials/video-tutorials.md +0 -11
  151. package/examples/add-to-end-of-timeline.html +0 -38
  152. package/examples/case-sensitive-responses.html +0 -45
  153. package/examples/conditional-and-loop-functions.html +0 -64
  154. package/examples/css/jquery-ui.css +0 -1225
  155. package/examples/css-classes-parameter.html +0 -145
  156. package/examples/data-add-properties.html +0 -44
  157. package/examples/data-as-function.html +0 -39
  158. package/examples/data-from-timeline.html +0 -52
  159. package/examples/data-from-url.html +0 -21
  160. package/examples/demo-flanker.html +0 -117
  161. package/examples/demo-simple-rt-task.html +0 -120
  162. package/examples/demos/demo_1.html +0 -35
  163. package/examples/demos/demo_2.html +0 -50
  164. package/examples/demos/demo_3.html +0 -63
  165. package/examples/display-element-to-embed-experiment.html +0 -79
  166. package/examples/end-active-node.html +0 -52
  167. package/examples/end-experiment.html +0 -45
  168. package/examples/exclusions.html +0 -32
  169. package/examples/external_html/simple_consent.html +0 -4
  170. package/examples/img/1.gif +0 -0
  171. package/examples/img/10.gif +0 -0
  172. package/examples/img/11.gif +0 -0
  173. package/examples/img/12.gif +0 -0
  174. package/examples/img/2.gif +0 -0
  175. package/examples/img/3.gif +0 -0
  176. package/examples/img/4.gif +0 -0
  177. package/examples/img/5.gif +0 -0
  178. package/examples/img/6.gif +0 -0
  179. package/examples/img/7.gif +0 -0
  180. package/examples/img/8.gif +0 -0
  181. package/examples/img/9.gif +0 -0
  182. package/examples/img/age/of1.jpg +0 -0
  183. package/examples/img/age/of2.jpg +0 -0
  184. package/examples/img/age/of3.jpg +0 -0
  185. package/examples/img/age/om1.jpg +0 -0
  186. package/examples/img/age/om2.jpg +0 -0
  187. package/examples/img/age/om3.jpg +0 -0
  188. package/examples/img/age/yf1.jpg +0 -0
  189. package/examples/img/age/yf4.jpg +0 -0
  190. package/examples/img/age/yf5.jpg +0 -0
  191. package/examples/img/age/ym2.jpg +0 -0
  192. package/examples/img/age/ym3.jpg +0 -0
  193. package/examples/img/age/ym5.jpg +0 -0
  194. package/examples/img/backwardN.gif +0 -0
  195. package/examples/img/blue.png +0 -0
  196. package/examples/img/card.png +0 -0
  197. package/examples/img/con1.png +0 -0
  198. package/examples/img/con2.png +0 -0
  199. package/examples/img/fixation.gif +0 -0
  200. package/examples/img/happy_face_1.jpg +0 -0
  201. package/examples/img/happy_face_2.jpg +0 -0
  202. package/examples/img/happy_face_3.jpg +0 -0
  203. package/examples/img/happy_face_4.jpg +0 -0
  204. package/examples/img/inc1.png +0 -0
  205. package/examples/img/inc2.png +0 -0
  206. package/examples/img/normalN.gif +0 -0
  207. package/examples/img/orange.png +0 -0
  208. package/examples/img/redX.png +0 -0
  209. package/examples/img/ribbon.jpg +0 -0
  210. package/examples/img/sad_face_1.jpg +0 -0
  211. package/examples/img/sad_face_2.jpg +0 -0
  212. package/examples/img/sad_face_3.jpg +0 -0
  213. package/examples/img/sad_face_4.jpg +0 -0
  214. package/examples/js/snap.svg-min.js +0 -21
  215. package/examples/js/webgazer/ridgeWorker.mjs +0 -135
  216. package/examples/js/webgazer/webgazer.js +0 -88909
  217. package/examples/js/webgazer/worker_scripts/mat.js +0 -306
  218. package/examples/js/webgazer/worker_scripts/util.js +0 -398
  219. package/examples/jspsych-RDK.html +0 -58
  220. package/examples/jspsych-animation.html +0 -39
  221. package/examples/jspsych-audio-button-response.html +0 -58
  222. package/examples/jspsych-audio-keyboard-response.html +0 -68
  223. package/examples/jspsych-audio-slider-response.html +0 -61
  224. package/examples/jspsych-call-function.html +0 -32
  225. package/examples/jspsych-canvas-button-response.html +0 -95
  226. package/examples/jspsych-canvas-keyboard-response.html +0 -78
  227. package/examples/jspsych-canvas-slider-response.html +0 -67
  228. package/examples/jspsych-categorize-animation.html +0 -49
  229. package/examples/jspsych-categorize-html.html +0 -33
  230. package/examples/jspsych-categorize-image.html +0 -44
  231. package/examples/jspsych-cloze.html +0 -37
  232. package/examples/jspsych-free-sort.html +0 -109
  233. package/examples/jspsych-fullscreen.html +0 -45
  234. package/examples/jspsych-html-button-response.html +0 -43
  235. package/examples/jspsych-html-keyboard-response.html +0 -42
  236. package/examples/jspsych-html-slider-response.html +0 -53
  237. package/examples/jspsych-iat.html +0 -520
  238. package/examples/jspsych-image-button-response.html +0 -91
  239. package/examples/jspsych-image-keyboard-response.html +0 -85
  240. package/examples/jspsych-image-slider-response.html +0 -85
  241. package/examples/jspsych-instructions.html +0 -37
  242. package/examples/jspsych-maxdiff.html +0 -33
  243. package/examples/jspsych-preload.html +0 -140
  244. package/examples/jspsych-reconstruction.html +0 -43
  245. package/examples/jspsych-resize.html +0 -34
  246. package/examples/jspsych-same-different-html.html +0 -28
  247. package/examples/jspsych-same-different-image.html +0 -39
  248. package/examples/jspsych-serial-reaction-time-mouse.html +0 -98
  249. package/examples/jspsych-serial-reaction-time.html +0 -54
  250. package/examples/jspsych-survey-html-form.html +0 -33
  251. package/examples/jspsych-survey-likert.html +0 -42
  252. package/examples/jspsych-survey-multi-choice.html +0 -40
  253. package/examples/jspsych-survey-multi-select.html +0 -42
  254. package/examples/jspsych-survey-text.html +0 -34
  255. package/examples/jspsych-video-button-response.html +0 -65
  256. package/examples/jspsych-video-keyboard-response.html +0 -61
  257. package/examples/jspsych-video-slider-response.html +0 -63
  258. package/examples/jspsych-virtual-chinrest.html +0 -69
  259. package/examples/jspsych-visual-search-circle.html +0 -64
  260. package/examples/jspsych-vsl-animate-occlusion.html +0 -35
  261. package/examples/jspsych-vsl-grid-scene.html +0 -47
  262. package/examples/lexical-decision.html +0 -134
  263. package/examples/manual-preloading.html +0 -59
  264. package/examples/pause-unpause.html +0 -33
  265. package/examples/progress-bar.html +0 -68
  266. package/examples/save-trial-parameters.html +0 -98
  267. package/examples/sound/hammer.mp3 +0 -0
  268. package/examples/sound/sound.mp3 +0 -0
  269. package/examples/sound/speech_blue.mp3 +0 -0
  270. package/examples/sound/speech_green.mp3 +0 -0
  271. package/examples/sound/speech_joke.mp3 +0 -0
  272. package/examples/sound/speech_red.mp3 +0 -0
  273. package/examples/sound/tone.mp3 +0 -0
  274. package/examples/timeline-variables-sampling.html +0 -50
  275. package/examples/timeline-variables.html +0 -64
  276. package/examples/video/sample_video.mp4 +0 -0
  277. package/examples/webgazer.html +0 -174
  278. package/examples/webgazer_audio.html +0 -90
  279. package/examples/webgazer_image.html +0 -60
  280. package/extensions/jspsych-ext-webgazer.js +0 -265
  281. package/jspsych.js +0 -3023
  282. package/license.txt +0 -21
  283. package/mkdocs.yml +0 -118
  284. package/plugins/jspsych-animation.js +0 -189
  285. package/plugins/jspsych-audio-button-response.js +0 -269
  286. package/plugins/jspsych-audio-keyboard-response.js +0 -209
  287. package/plugins/jspsych-audio-slider-response.js +0 -278
  288. package/plugins/jspsych-call-function.js +0 -58
  289. package/plugins/jspsych-canvas-button-response.js +0 -199
  290. package/plugins/jspsych-canvas-keyboard-response.js +0 -155
  291. package/plugins/jspsych-canvas-slider-response.js +0 -207
  292. package/plugins/jspsych-categorize-animation.js +0 -266
  293. package/plugins/jspsych-categorize-html.js +0 -220
  294. package/plugins/jspsych-categorize-image.js +0 -222
  295. package/plugins/jspsych-cloze.js +0 -112
  296. package/plugins/jspsych-external-html.js +0 -112
  297. package/plugins/jspsych-free-sort.js +0 -478
  298. package/plugins/jspsych-fullscreen.js +0 -106
  299. package/plugins/jspsych-html-button-response.js +0 -188
  300. package/plugins/jspsych-html-keyboard-response.js +0 -149
  301. package/plugins/jspsych-html-slider-response.js +0 -202
  302. package/plugins/jspsych-iat-html.js +0 -284
  303. package/plugins/jspsych-iat-image.js +0 -286
  304. package/plugins/jspsych-image-button-response.js +0 -327
  305. package/plugins/jspsych-image-keyboard-response.js +0 -263
  306. package/plugins/jspsych-image-slider-response.js +0 -369
  307. package/plugins/jspsych-instructions.js +0 -237
  308. package/plugins/jspsych-maxdiff.js +0 -173
  309. package/plugins/jspsych-preload.js +0 -345
  310. package/plugins/jspsych-rdk.js +0 -1373
  311. package/plugins/jspsych-reconstruction.js +0 -134
  312. package/plugins/jspsych-resize.js +0 -166
  313. package/plugins/jspsych-same-different-html.js +0 -168
  314. package/plugins/jspsych-same-different-image.js +0 -169
  315. package/plugins/jspsych-serial-reaction-time-mouse.js +0 -212
  316. package/plugins/jspsych-serial-reaction-time.js +0 -247
  317. package/plugins/jspsych-survey-html-form.js +0 -171
  318. package/plugins/jspsych-survey-likert.js +0 -195
  319. package/plugins/jspsych-survey-multi-choice.js +0 -208
  320. package/plugins/jspsych-survey-multi-select.js +0 -232
  321. package/plugins/jspsych-survey-text.js +0 -185
  322. package/plugins/jspsych-video-button-response.js +0 -335
  323. package/plugins/jspsych-video-keyboard-response.js +0 -279
  324. package/plugins/jspsych-video-slider-response.js +0 -351
  325. package/plugins/jspsych-virtual-chinrest.js +0 -471
  326. package/plugins/jspsych-visual-search-circle.js +0 -259
  327. package/plugins/jspsych-vsl-animate-occlusion.js +0 -196
  328. package/plugins/jspsych-vsl-grid-scene.js +0 -103
  329. package/plugins/jspsych-webgazer-calibrate.js +0 -161
  330. package/plugins/jspsych-webgazer-init-camera.js +0 -139
  331. package/plugins/jspsych-webgazer-validate.js +0 -314
  332. package/plugins/template/jspsych-plugin-template.js +0 -35
  333. package/tests/README.md +0 -7
  334. package/tests/jsPsych/case-sensitive-responses.test.js +0 -53
  335. package/tests/jsPsych/css-classes-parameter.test.js +0 -107
  336. package/tests/jsPsych/default-iti.test.js +0 -51
  337. package/tests/jsPsych/default-parameters.test.js +0 -58
  338. package/tests/jsPsych/endexperiment.test.js +0 -49
  339. package/tests/jsPsych/events.test.js +0 -606
  340. package/tests/jsPsych/functions-as-parameters.test.js +0 -210
  341. package/tests/jsPsych/init.test.js +0 -48
  342. package/tests/jsPsych/loads.test.js +0 -7
  343. package/tests/jsPsych/min-rt.test.js +0 -58
  344. package/tests/jsPsych/progressbar.test.js +0 -202
  345. package/tests/jsPsych/timeline-variables.test.js +0 -531
  346. package/tests/jsPsych/timelines.test.js +0 -569
  347. package/tests/jsPsych.data/data-csv-conversion.test.js +0 -85
  348. package/tests/jsPsych.data/data-json-conversion.test.js +0 -120
  349. package/tests/jsPsych.data/datacollection.test.js +0 -117
  350. package/tests/jsPsych.data/datacolumn.test.js +0 -50
  351. package/tests/jsPsych.data/datamodule.test.js +0 -152
  352. package/tests/jsPsych.data/dataparameter.test.js +0 -251
  353. package/tests/jsPsych.data/interactions.test.js +0 -109
  354. package/tests/jsPsych.data/trialparameters.test.js +0 -175
  355. package/tests/jsPsych.extensions/extensions.test.js +0 -207
  356. package/tests/jsPsych.extensions/test-extension.js +0 -42
  357. package/tests/jsPsych.pluginAPI/pluginapi.test.js +0 -365
  358. package/tests/jsPsych.pluginAPI/preloads.test.js +0 -43
  359. package/tests/jsPsych.randomization/randomziation.test.js +0 -27
  360. package/tests/jsPsych.utils/utils.test.js +0 -58
  361. package/tests/plugins/plugin-animation.test.js +0 -34
  362. package/tests/plugins/plugin-audio-button-response.test.js +0 -15
  363. package/tests/plugins/plugin-audio-keyboard-response.test.js +0 -15
  364. package/tests/plugins/plugin-audio-slider-response.test.js +0 -15
  365. package/tests/plugins/plugin-call-function.test.js +0 -49
  366. package/tests/plugins/plugin-categorize-animation.test.js +0 -263
  367. package/tests/plugins/plugin-categorize-html.test.js +0 -17
  368. package/tests/plugins/plugin-categorize-image.test.js +0 -17
  369. package/tests/plugins/plugin-cloze.test.js +0 -157
  370. package/tests/plugins/plugin-free-sort.test.js +0 -106
  371. package/tests/plugins/plugin-fullscreen.test.js +0 -41
  372. package/tests/plugins/plugin-html-button-response.test.js +0 -161
  373. package/tests/plugins/plugin-html-keyboard-response.test.js +0 -139
  374. package/tests/plugins/plugin-html-slider-response.test.js +0 -155
  375. package/tests/plugins/plugin-iat-html.test.js +0 -299
  376. package/tests/plugins/plugin-iat-image.test.js +0 -298
  377. package/tests/plugins/plugin-image-button-response.test.js +0 -174
  378. package/tests/plugins/plugin-image-keyboard-response.test.js +0 -147
  379. package/tests/plugins/plugin-image-slider-response.test.js +0 -174
  380. package/tests/plugins/plugin-instructions.test.js +0 -85
  381. package/tests/plugins/plugin-maxdiff.test.js +0 -39
  382. package/tests/plugins/plugin-preload.test.js +0 -916
  383. package/tests/plugins/plugin-rdk.test.js +0 -61
  384. package/tests/plugins/plugin-reconstruction.test.js +0 -16
  385. package/tests/plugins/plugin-resize.test.js +0 -16
  386. package/tests/plugins/plugin-same-different-html.test.js +0 -17
  387. package/tests/plugins/plugin-same-different-image.test.js +0 -17
  388. package/tests/plugins/plugin-serial-reaction-time-mouse.test.js +0 -42
  389. package/tests/plugins/plugin-serial-reaction-time.test.js +0 -109
  390. package/tests/plugins/plugin-survey-html-form.test.js +0 -44
  391. package/tests/plugins/plugin-survey-likert.test.js +0 -48
  392. package/tests/plugins/plugin-survey-multi-choice.test.js +0 -47
  393. package/tests/plugins/plugin-survey-multi-select.test.js +0 -71
  394. package/tests/plugins/plugin-survey-text.test.js +0 -115
  395. package/tests/plugins/plugin-video-button-response.test.js +0 -32
  396. package/tests/plugins/plugin-video-keyboard-response.test.js +0 -32
  397. package/tests/plugins/plugin-video-slider-response.test.js +0 -31
  398. package/tests/plugins/plugin-visual-search-circle.test.js +0 -16
  399. package/tests/plugins/plugin-vsl-animate-occlusion.test.js +0 -16
  400. package/tests/plugins/plugin-vsl-grid-scene.test.js +0 -16
  401. package/tests/testing-utils.js +0 -13
package/dist/index.cjs ADDED
@@ -0,0 +1,3165 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /*! *****************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+
20
+ function __awaiter(thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ }
29
+
30
+ // Gets all non-builtin properties up the prototype chain
31
+ const getAllProperties = object => {
32
+ const properties = new Set();
33
+
34
+ do {
35
+ for (const key of Reflect.ownKeys(object)) {
36
+ properties.add([object, key]);
37
+ }
38
+ } while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);
39
+
40
+ return properties;
41
+ };
42
+
43
+ var autoBind = (self, {include, exclude} = {}) => {
44
+ const filter = key => {
45
+ const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
46
+
47
+ if (include) {
48
+ return include.some(match);
49
+ }
50
+
51
+ if (exclude) {
52
+ return !exclude.some(match);
53
+ }
54
+
55
+ return true;
56
+ };
57
+
58
+ for (const [object, key] of getAllProperties(self.constructor.prototype)) {
59
+ if (key === 'constructor' || !filter(key)) {
60
+ continue;
61
+ }
62
+
63
+ const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
64
+ if (descriptor && typeof descriptor.value === 'function') {
65
+ self[key] = self[key].bind(self);
66
+ }
67
+ }
68
+
69
+ return self;
70
+ };
71
+
72
+ var version = "7.1.2";
73
+
74
+ class MigrationError extends Error {
75
+ constructor(message = "The global `jsPsych` variable is no longer available in jsPsych v7.") {
76
+ super(`${message} Please follow the migration guide at https://www.jspsych.org/7.0/support/migration-v7/ to update your experiment.`);
77
+ this.name = "MigrationError";
78
+ }
79
+ }
80
+ // Define a global jsPsych object to handle invocations on it with migration errors
81
+ window.jsPsych = {
82
+ get init() {
83
+ throw new MigrationError("`jsPsych.init()` was replaced by `initJsPsych()` in jsPsych v7.");
84
+ },
85
+ get data() {
86
+ throw new MigrationError();
87
+ },
88
+ get randomization() {
89
+ throw new MigrationError();
90
+ },
91
+ get turk() {
92
+ throw new MigrationError();
93
+ },
94
+ get pluginAPI() {
95
+ throw new MigrationError();
96
+ },
97
+ get ALL_KEYS() {
98
+ throw new MigrationError('jsPsych.ALL_KEYS was replaced by the "ALL_KEYS" string in jsPsych v7.');
99
+ },
100
+ get NO_KEYS() {
101
+ throw new MigrationError('jsPsych.NO_KEYS was replaced by the "NO_KEYS" string in jsPsych v7.');
102
+ },
103
+ };
104
+
105
+ /**
106
+ * Finds all of the unique items in an array.
107
+ * @param arr The array to extract unique values from
108
+ * @returns An array with one copy of each unique item in `arr`
109
+ */
110
+ function unique(arr) {
111
+ return [...new Set(arr)];
112
+ }
113
+ function deepCopy(obj) {
114
+ if (!obj)
115
+ return obj;
116
+ let out;
117
+ if (Array.isArray(obj)) {
118
+ out = [];
119
+ for (const x of obj) {
120
+ out.push(deepCopy(x));
121
+ }
122
+ return out;
123
+ }
124
+ else if (typeof obj === "object" && obj !== null) {
125
+ out = {};
126
+ for (const key in obj) {
127
+ if (obj.hasOwnProperty(key)) {
128
+ out[key] = deepCopy(obj[key]);
129
+ }
130
+ }
131
+ return out;
132
+ }
133
+ else {
134
+ return obj;
135
+ }
136
+ }
137
+
138
+ var utils = /*#__PURE__*/Object.freeze({
139
+ __proto__: null,
140
+ unique: unique,
141
+ deepCopy: deepCopy
142
+ });
143
+
144
+ class DataColumn {
145
+ constructor(values = []) {
146
+ this.values = values;
147
+ }
148
+ sum() {
149
+ let s = 0;
150
+ for (const v of this.values) {
151
+ s += v;
152
+ }
153
+ return s;
154
+ }
155
+ mean() {
156
+ return this.sum() / this.count();
157
+ }
158
+ median() {
159
+ if (this.values.length === 0) {
160
+ return undefined;
161
+ }
162
+ const numbers = this.values.slice(0).sort(function (a, b) {
163
+ return a - b;
164
+ });
165
+ const middle = Math.floor(numbers.length / 2);
166
+ const isEven = numbers.length % 2 === 0;
167
+ return isEven ? (numbers[middle] + numbers[middle - 1]) / 2 : numbers[middle];
168
+ }
169
+ min() {
170
+ return Math.min.apply(null, this.values);
171
+ }
172
+ max() {
173
+ return Math.max.apply(null, this.values);
174
+ }
175
+ count() {
176
+ return this.values.length;
177
+ }
178
+ variance() {
179
+ const mean = this.mean();
180
+ let sum_square_error = 0;
181
+ for (const x of this.values) {
182
+ sum_square_error += Math.pow(x - mean, 2);
183
+ }
184
+ const mse = sum_square_error / (this.values.length - 1);
185
+ return mse;
186
+ }
187
+ sd() {
188
+ const mse = this.variance();
189
+ const rmse = Math.sqrt(mse);
190
+ return rmse;
191
+ }
192
+ frequencies() {
193
+ const unique = {};
194
+ for (const x of this.values) {
195
+ if (typeof unique[x] === "undefined") {
196
+ unique[x] = 1;
197
+ }
198
+ else {
199
+ unique[x]++;
200
+ }
201
+ }
202
+ return unique;
203
+ }
204
+ all(eval_fn) {
205
+ for (const x of this.values) {
206
+ if (!eval_fn(x)) {
207
+ return false;
208
+ }
209
+ }
210
+ return true;
211
+ }
212
+ subset(eval_fn) {
213
+ const out = [];
214
+ for (const x of this.values) {
215
+ if (eval_fn(x)) {
216
+ out.push(x);
217
+ }
218
+ }
219
+ return new DataColumn(out);
220
+ }
221
+ }
222
+
223
+ // private function to save text file on local drive
224
+ function saveTextToFile(textstr, filename) {
225
+ const blobToSave = new Blob([textstr], {
226
+ type: "text/plain",
227
+ });
228
+ let blobURL = "";
229
+ if (typeof window.webkitURL !== "undefined") {
230
+ blobURL = window.webkitURL.createObjectURL(blobToSave);
231
+ }
232
+ else {
233
+ blobURL = window.URL.createObjectURL(blobToSave);
234
+ }
235
+ const link = document.createElement("a");
236
+ link.id = "jspsych-download-as-text-link";
237
+ link.style.display = "none";
238
+ link.download = filename;
239
+ link.href = blobURL;
240
+ link.click();
241
+ }
242
+ // this function based on code suggested by StackOverflow users:
243
+ // http://stackoverflow.com/users/64741/zachary
244
+ // http://stackoverflow.com/users/317/joseph-sturtevant
245
+ function JSON2CSV(objArray) {
246
+ const array = typeof objArray != "object" ? JSON.parse(objArray) : objArray;
247
+ let line = "";
248
+ let result = "";
249
+ const columns = [];
250
+ for (const row of array) {
251
+ for (const key in row) {
252
+ let keyString = key + "";
253
+ keyString = '"' + keyString.replace(/"/g, '""') + '",';
254
+ if (!columns.includes(key)) {
255
+ columns.push(key);
256
+ line += keyString;
257
+ }
258
+ }
259
+ }
260
+ line = line.slice(0, -1); // removes last comma
261
+ result += line + "\r\n";
262
+ for (const row of array) {
263
+ line = "";
264
+ for (const col of columns) {
265
+ let value = typeof row[col] === "undefined" ? "" : row[col];
266
+ if (typeof value == "object") {
267
+ value = JSON.stringify(value);
268
+ }
269
+ const valueString = value + "";
270
+ line += '"' + valueString.replace(/"/g, '""') + '",';
271
+ }
272
+ line = line.slice(0, -1);
273
+ result += line + "\r\n";
274
+ }
275
+ return result;
276
+ }
277
+ // this function is modified from StackOverflow:
278
+ // http://stackoverflow.com/posts/3855394
279
+ function getQueryString() {
280
+ const a = window.location.search.substr(1).split("&");
281
+ const b = {};
282
+ for (let i = 0; i < a.length; ++i) {
283
+ const p = a[i].split("=", 2);
284
+ if (p.length == 1)
285
+ b[p[0]] = "";
286
+ else
287
+ b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
288
+ }
289
+ return b;
290
+ }
291
+
292
+ class DataCollection {
293
+ constructor(data = []) {
294
+ this.trials = data;
295
+ }
296
+ push(new_data) {
297
+ this.trials.push(new_data);
298
+ return this;
299
+ }
300
+ join(other_data_collection) {
301
+ this.trials = this.trials.concat(other_data_collection.values());
302
+ return this;
303
+ }
304
+ top() {
305
+ if (this.trials.length <= 1) {
306
+ return this;
307
+ }
308
+ else {
309
+ return new DataCollection([this.trials[this.trials.length - 1]]);
310
+ }
311
+ }
312
+ /**
313
+ * Queries the first n elements in a collection of trials.
314
+ *
315
+ * @param n A positive integer of elements to return. A value of
316
+ * n that is less than 1 will throw an error.
317
+ *
318
+ * @return First n objects of a collection of trials. If fewer than
319
+ * n trials are available, the trials.length elements will
320
+ * be returned.
321
+ *
322
+ */
323
+ first(n = 1) {
324
+ if (n < 1) {
325
+ throw `You must query with a positive nonzero integer. Please use a
326
+ different value for n.`;
327
+ }
328
+ if (this.trials.length === 0)
329
+ return new DataCollection();
330
+ if (n > this.trials.length)
331
+ n = this.trials.length;
332
+ return new DataCollection(this.trials.slice(0, n));
333
+ }
334
+ /**
335
+ * Queries the last n elements in a collection of trials.
336
+ *
337
+ * @param n A positive integer of elements to return. A value of
338
+ * n that is less than 1 will throw an error.
339
+ *
340
+ * @return Last n objects of a collection of trials. If fewer than
341
+ * n trials are available, the trials.length elements will
342
+ * be returned.
343
+ *
344
+ */
345
+ last(n = 1) {
346
+ if (n < 1) {
347
+ throw `You must query with a positive nonzero integer. Please use a
348
+ different value for n.`;
349
+ }
350
+ if (this.trials.length === 0)
351
+ return new DataCollection();
352
+ if (n > this.trials.length)
353
+ n = this.trials.length;
354
+ return new DataCollection(this.trials.slice(this.trials.length - n, this.trials.length));
355
+ }
356
+ values() {
357
+ return this.trials;
358
+ }
359
+ count() {
360
+ return this.trials.length;
361
+ }
362
+ readOnly() {
363
+ return new DataCollection(deepCopy(this.trials));
364
+ }
365
+ addToAll(properties) {
366
+ for (const trial of this.trials) {
367
+ Object.assign(trial, properties);
368
+ }
369
+ return this;
370
+ }
371
+ addToLast(properties) {
372
+ if (this.trials.length != 0) {
373
+ Object.assign(this.trials[this.trials.length - 1], properties);
374
+ }
375
+ return this;
376
+ }
377
+ filter(filters) {
378
+ // [{p1: v1, p2:v2}, {p1:v2}]
379
+ // {p1: v1}
380
+ let f;
381
+ if (!Array.isArray(filters)) {
382
+ f = deepCopy([filters]);
383
+ }
384
+ else {
385
+ f = deepCopy(filters);
386
+ }
387
+ const filtered_data = [];
388
+ for (const trial of this.trials) {
389
+ let keep = false;
390
+ for (const filter of f) {
391
+ let match = true;
392
+ for (const key of Object.keys(filter)) {
393
+ if (typeof trial[key] !== "undefined" && trial[key] === filter[key]) ;
394
+ else {
395
+ match = false;
396
+ }
397
+ }
398
+ if (match) {
399
+ keep = true;
400
+ break;
401
+ } // can break because each filter is OR.
402
+ }
403
+ if (keep) {
404
+ filtered_data.push(trial);
405
+ }
406
+ }
407
+ return new DataCollection(filtered_data);
408
+ }
409
+ filterCustom(fn) {
410
+ return new DataCollection(this.trials.filter(fn));
411
+ }
412
+ select(column) {
413
+ const values = [];
414
+ for (const trial of this.trials) {
415
+ if (typeof trial[column] !== "undefined") {
416
+ values.push(trial[column]);
417
+ }
418
+ }
419
+ return new DataColumn(values);
420
+ }
421
+ ignore(columns) {
422
+ if (!Array.isArray(columns)) {
423
+ columns = [columns];
424
+ }
425
+ const o = deepCopy(this.trials);
426
+ for (const trial of o) {
427
+ for (const delete_key of columns) {
428
+ delete trial[delete_key];
429
+ }
430
+ }
431
+ return new DataCollection(o);
432
+ }
433
+ uniqueNames() {
434
+ const names = [];
435
+ for (const trial of this.trials) {
436
+ for (const key of Object.keys(trial)) {
437
+ if (!names.includes(key)) {
438
+ names.push(key);
439
+ }
440
+ }
441
+ }
442
+ return names;
443
+ }
444
+ csv() {
445
+ return JSON2CSV(this.trials);
446
+ }
447
+ json(pretty = false) {
448
+ if (pretty) {
449
+ return JSON.stringify(this.trials, null, "\t");
450
+ }
451
+ return JSON.stringify(this.trials);
452
+ }
453
+ localSave(format, filename) {
454
+ format = format.toLowerCase();
455
+ let data_string;
456
+ if (format === "json") {
457
+ data_string = this.json();
458
+ }
459
+ else if (format === "csv") {
460
+ data_string = this.csv();
461
+ }
462
+ else {
463
+ throw new Error('Invalid format specified for localSave. Must be "json" or "csv".');
464
+ }
465
+ saveTextToFile(data_string, filename);
466
+ }
467
+ }
468
+
469
+ class JsPsychData {
470
+ constructor(jsPsych) {
471
+ this.jsPsych = jsPsych;
472
+ // data properties for all trials
473
+ this.dataProperties = {};
474
+ this.reset();
475
+ }
476
+ reset() {
477
+ this.allData = new DataCollection();
478
+ this.interactionData = new DataCollection();
479
+ }
480
+ get() {
481
+ return this.allData;
482
+ }
483
+ getInteractionData() {
484
+ return this.interactionData;
485
+ }
486
+ write(data_object) {
487
+ const progress = this.jsPsych.getProgress();
488
+ const trial = this.jsPsych.getCurrentTrial();
489
+ //var trial_opt_data = typeof trial.data == 'function' ? trial.data() : trial.data;
490
+ const default_data = {
491
+ trial_type: trial.type.info.name,
492
+ trial_index: progress.current_trial_global,
493
+ time_elapsed: this.jsPsych.getTotalTime(),
494
+ internal_node_id: this.jsPsych.getCurrentTimelineNodeID(),
495
+ };
496
+ this.allData.push(Object.assign(Object.assign(Object.assign(Object.assign({}, data_object), trial.data), default_data), this.dataProperties));
497
+ }
498
+ addProperties(properties) {
499
+ // first, add the properties to all data that's already stored
500
+ this.allData.addToAll(properties);
501
+ // now add to list so that it gets appended to all future data
502
+ this.dataProperties = Object.assign({}, this.dataProperties, properties);
503
+ }
504
+ addDataToLastTrial(data) {
505
+ this.allData.addToLast(data);
506
+ }
507
+ getDataByTimelineNode(node_id) {
508
+ return this.allData.filterCustom((x) => x.internal_node_id.slice(0, node_id.length) === node_id);
509
+ }
510
+ getLastTrialData() {
511
+ return this.allData.top();
512
+ }
513
+ getLastTimelineData() {
514
+ const lasttrial = this.getLastTrialData();
515
+ const node_id = lasttrial.select("internal_node_id").values[0];
516
+ if (typeof node_id === "undefined") {
517
+ return new DataCollection();
518
+ }
519
+ else {
520
+ const parent_node_id = node_id.substr(0, node_id.lastIndexOf("-"));
521
+ const lastnodedata = this.getDataByTimelineNode(parent_node_id);
522
+ return lastnodedata;
523
+ }
524
+ }
525
+ displayData(format = "json") {
526
+ format = format.toLowerCase();
527
+ if (format != "json" && format != "csv") {
528
+ console.log("Invalid format declared for displayData function. Using json as default.");
529
+ format = "json";
530
+ }
531
+ const data_string = format === "json" ? this.allData.json(true) : this.allData.csv();
532
+ const display_element = this.jsPsych.getDisplayElement();
533
+ display_element.innerHTML = '<pre id="jspsych-data-display"></pre>';
534
+ document.getElementById("jspsych-data-display").textContent = data_string;
535
+ }
536
+ urlVariables() {
537
+ if (typeof this.query_string == "undefined") {
538
+ this.query_string = getQueryString();
539
+ }
540
+ return this.query_string;
541
+ }
542
+ getURLVariable(whichvar) {
543
+ return this.urlVariables()[whichvar];
544
+ }
545
+ createInteractionListeners() {
546
+ // blur event capture
547
+ window.addEventListener("blur", () => {
548
+ const data = {
549
+ event: "blur",
550
+ trial: this.jsPsych.getProgress().current_trial_global,
551
+ time: this.jsPsych.getTotalTime(),
552
+ };
553
+ this.interactionData.push(data);
554
+ this.jsPsych.getInitSettings().on_interaction_data_update(data);
555
+ });
556
+ // focus event capture
557
+ window.addEventListener("focus", () => {
558
+ const data = {
559
+ event: "focus",
560
+ trial: this.jsPsych.getProgress().current_trial_global,
561
+ time: this.jsPsych.getTotalTime(),
562
+ };
563
+ this.interactionData.push(data);
564
+ this.jsPsych.getInitSettings().on_interaction_data_update(data);
565
+ });
566
+ // fullscreen change capture
567
+ const fullscreenchange = () => {
568
+ const data = {
569
+ event:
570
+ // @ts-expect-error
571
+ document.isFullScreen ||
572
+ // @ts-expect-error
573
+ document.webkitIsFullScreen ||
574
+ // @ts-expect-error
575
+ document.mozIsFullScreen ||
576
+ document.fullscreenElement
577
+ ? "fullscreenenter"
578
+ : "fullscreenexit",
579
+ trial: this.jsPsych.getProgress().current_trial_global,
580
+ time: this.jsPsych.getTotalTime(),
581
+ };
582
+ this.interactionData.push(data);
583
+ this.jsPsych.getInitSettings().on_interaction_data_update(data);
584
+ };
585
+ document.addEventListener("fullscreenchange", fullscreenchange);
586
+ document.addEventListener("mozfullscreenchange", fullscreenchange);
587
+ document.addEventListener("webkitfullscreenchange", fullscreenchange);
588
+ }
589
+ // public methods for testing purposes. not recommended for use.
590
+ _customInsert(data) {
591
+ this.allData = new DataCollection(data);
592
+ }
593
+ _fullreset() {
594
+ this.reset();
595
+ this.dataProperties = {};
596
+ }
597
+ }
598
+
599
+ class HardwareAPI {
600
+ constructor() {
601
+ /**
602
+ * Indicates whether this instance of jspsych has opened a hardware connection through our browser
603
+ * extension
604
+ **/
605
+ this.hardwareConnected = false;
606
+ //it might be useful to open up a line of communication from the extension back to this page
607
+ //script, again, this will have to pass through DOM events. For now speed is of no concern so I
608
+ //will use jQuery
609
+ document.addEventListener("jspsych-activate", (evt) => {
610
+ this.hardwareConnected = true;
611
+ });
612
+ }
613
+ /**
614
+ * Allows communication with user hardware through our custom Google Chrome extension + native C++ program
615
+ * @param mess The message to be passed to our extension, see its documentation for the expected members of this object.
616
+ * @author Daniel Rivas
617
+ *
618
+ */
619
+ hardware(mess) {
620
+ //since Chrome extension content-scripts do not share the javascript environment with the page
621
+ //script that loaded jspsych, we will need to use hacky methods like communicating through DOM
622
+ //events.
623
+ const jspsychEvt = new CustomEvent("jspsych", { detail: mess });
624
+ document.dispatchEvent(jspsychEvt);
625
+ //And voila! it will be the job of the content script injected by the extension to listen for
626
+ //the event and do the appropriate actions.
627
+ }
628
+ }
629
+
630
+ class KeyboardListenerAPI {
631
+ constructor(getRootElement, areResponsesCaseSensitive = false, minimumValidRt = 0) {
632
+ this.getRootElement = getRootElement;
633
+ this.areResponsesCaseSensitive = areResponsesCaseSensitive;
634
+ this.minimumValidRt = minimumValidRt;
635
+ this.listeners = new Set();
636
+ this.heldKeys = new Set();
637
+ this.areRootListenersRegistered = false;
638
+ autoBind(this);
639
+ this.registerRootListeners();
640
+ }
641
+ /**
642
+ * If not previously done and `this.getRootElement()` returns an element, adds the root key
643
+ * listeners to that element.
644
+ */
645
+ registerRootListeners() {
646
+ if (!this.areRootListenersRegistered) {
647
+ const rootElement = this.getRootElement();
648
+ if (rootElement) {
649
+ rootElement.addEventListener("keydown", this.rootKeydownListener);
650
+ rootElement.addEventListener("keyup", this.rootKeyupListener);
651
+ this.areRootListenersRegistered = true;
652
+ }
653
+ }
654
+ }
655
+ rootKeydownListener(e) {
656
+ // Iterate over a static copy of the listeners set because listeners might add other listeners
657
+ // that we do not want to be included in the loop
658
+ for (const listener of Array.from(this.listeners)) {
659
+ listener(e);
660
+ }
661
+ this.heldKeys.add(this.toLowerCaseIfInsensitive(e.key));
662
+ }
663
+ toLowerCaseIfInsensitive(string) {
664
+ return this.areResponsesCaseSensitive ? string : string.toLowerCase();
665
+ }
666
+ rootKeyupListener(e) {
667
+ this.heldKeys.delete(this.toLowerCaseIfInsensitive(e.key));
668
+ }
669
+ isResponseValid(validResponses, allowHeldKey, key) {
670
+ // check if key was already held down
671
+ if (!allowHeldKey && this.heldKeys.has(key)) {
672
+ return false;
673
+ }
674
+ if (validResponses === "ALL_KEYS") {
675
+ return true;
676
+ }
677
+ if (validResponses === "NO_KEYS") {
678
+ return false;
679
+ }
680
+ return validResponses.includes(key);
681
+ }
682
+ getKeyboardResponse({ callback_function, valid_responses = "ALL_KEYS", rt_method = "performance", persist, audio_context, audio_context_start_time, allow_held_key = false, minimum_valid_rt = this.minimumValidRt, }) {
683
+ if (rt_method !== "performance" && rt_method !== "audio") {
684
+ console.log('Invalid RT method specified in getKeyboardResponse. Defaulting to "performance" method.');
685
+ rt_method = "performance";
686
+ }
687
+ const usePerformanceRt = rt_method === "performance";
688
+ const startTime = usePerformanceRt ? performance.now() : audio_context_start_time * 1000;
689
+ this.registerRootListeners();
690
+ if (!this.areResponsesCaseSensitive && typeof valid_responses !== "string") {
691
+ valid_responses = valid_responses.map((r) => r.toLowerCase());
692
+ }
693
+ const listener = (e) => {
694
+ const rt = Math.round((rt_method == "performance" ? performance.now() : audio_context.currentTime * 1000) -
695
+ startTime);
696
+ if (rt < minimum_valid_rt) {
697
+ return;
698
+ }
699
+ const key = this.toLowerCaseIfInsensitive(e.key);
700
+ if (this.isResponseValid(valid_responses, allow_held_key, key)) {
701
+ // if this is a valid response, then we don't want the key event to trigger other actions
702
+ // like scrolling via the spacebar.
703
+ e.preventDefault();
704
+ if (!persist) {
705
+ // remove keyboard listener if it exists
706
+ this.cancelKeyboardResponse(listener);
707
+ }
708
+ callback_function({ key, rt });
709
+ }
710
+ };
711
+ this.listeners.add(listener);
712
+ return listener;
713
+ }
714
+ cancelKeyboardResponse(listener) {
715
+ // remove the listener from the set of listeners if it is contained
716
+ this.listeners.delete(listener);
717
+ }
718
+ cancelAllKeyboardResponses() {
719
+ this.listeners.clear();
720
+ }
721
+ compareKeys(key1, key2) {
722
+ if ((typeof key1 !== "string" && key1 !== null) ||
723
+ (typeof key2 !== "string" && key2 !== null)) {
724
+ console.error("Error in jsPsych.pluginAPI.compareKeys: arguments must be key strings or null.");
725
+ return undefined;
726
+ }
727
+ if (typeof key1 === "string" && typeof key2 === "string") {
728
+ // if both values are strings, then check whether or not letter case should be converted before comparing (case_sensitive_responses in initJsPsych)
729
+ return this.areResponsesCaseSensitive
730
+ ? key1 === key2
731
+ : key1.toLowerCase() === key2.toLowerCase();
732
+ }
733
+ return key1 === null && key2 === null;
734
+ }
735
+ }
736
+
737
+ /**
738
+ * Parameter types for plugins
739
+ */
740
+ exports.ParameterType = void 0;
741
+ (function (ParameterType) {
742
+ ParameterType[ParameterType["BOOL"] = 0] = "BOOL";
743
+ ParameterType[ParameterType["STRING"] = 1] = "STRING";
744
+ ParameterType[ParameterType["INT"] = 2] = "INT";
745
+ ParameterType[ParameterType["FLOAT"] = 3] = "FLOAT";
746
+ ParameterType[ParameterType["FUNCTION"] = 4] = "FUNCTION";
747
+ ParameterType[ParameterType["KEY"] = 5] = "KEY";
748
+ ParameterType[ParameterType["KEYS"] = 6] = "KEYS";
749
+ ParameterType[ParameterType["SELECT"] = 7] = "SELECT";
750
+ ParameterType[ParameterType["HTML_STRING"] = 8] = "HTML_STRING";
751
+ ParameterType[ParameterType["IMAGE"] = 9] = "IMAGE";
752
+ ParameterType[ParameterType["AUDIO"] = 10] = "AUDIO";
753
+ ParameterType[ParameterType["VIDEO"] = 11] = "VIDEO";
754
+ ParameterType[ParameterType["OBJECT"] = 12] = "OBJECT";
755
+ ParameterType[ParameterType["COMPLEX"] = 13] = "COMPLEX";
756
+ ParameterType[ParameterType["TIMELINE"] = 14] = "TIMELINE";
757
+ })(exports.ParameterType || (exports.ParameterType = {}));
758
+ const universalPluginParameters = {
759
+ /**
760
+ * Data to add to this trial (key-value pairs)
761
+ */
762
+ data: {
763
+ type: exports.ParameterType.OBJECT,
764
+ pretty_name: "Data",
765
+ default: {},
766
+ },
767
+ /**
768
+ * Function to execute when trial begins
769
+ */
770
+ on_start: {
771
+ type: exports.ParameterType.FUNCTION,
772
+ pretty_name: "On start",
773
+ default: function () {
774
+ return;
775
+ },
776
+ },
777
+ /**
778
+ * Function to execute when trial is finished
779
+ */
780
+ on_finish: {
781
+ type: exports.ParameterType.FUNCTION,
782
+ pretty_name: "On finish",
783
+ default: function () {
784
+ return;
785
+ },
786
+ },
787
+ /**
788
+ * Function to execute after the trial has loaded
789
+ */
790
+ on_load: {
791
+ type: exports.ParameterType.FUNCTION,
792
+ pretty_name: "On load",
793
+ default: function () {
794
+ return;
795
+ },
796
+ },
797
+ /**
798
+ * Length of gap between the end of this trial and the start of the next trial
799
+ */
800
+ post_trial_gap: {
801
+ type: exports.ParameterType.INT,
802
+ pretty_name: "Post trial gap",
803
+ default: null,
804
+ },
805
+ /**
806
+ * A list of CSS classes to add to the jsPsych display element for the duration of this trial
807
+ */
808
+ css_classes: {
809
+ type: exports.ParameterType.STRING,
810
+ pretty_name: "Custom CSS classes",
811
+ default: null,
812
+ },
813
+ /**
814
+ * Options to control simulation mode for the trial.
815
+ */
816
+ simulation_options: {
817
+ type: exports.ParameterType.COMPLEX,
818
+ default: null,
819
+ },
820
+ };
821
+
822
+ const preloadParameterTypes = [
823
+ exports.ParameterType.AUDIO,
824
+ exports.ParameterType.IMAGE,
825
+ exports.ParameterType.VIDEO,
826
+ ];
827
+ class MediaAPI {
828
+ constructor(useWebaudio, webaudioContext) {
829
+ this.useWebaudio = useWebaudio;
830
+ this.webaudioContext = webaudioContext;
831
+ // video //
832
+ this.video_buffers = {};
833
+ // audio //
834
+ this.context = null;
835
+ this.audio_buffers = [];
836
+ // preloading stimuli //
837
+ this.preload_requests = [];
838
+ this.img_cache = {};
839
+ this.preloadMap = new Map();
840
+ this.microphone_recorder = null;
841
+ }
842
+ getVideoBuffer(videoID) {
843
+ return this.video_buffers[videoID];
844
+ }
845
+ initAudio() {
846
+ this.context = this.useWebaudio ? this.webaudioContext : null;
847
+ }
848
+ audioContext() {
849
+ if (this.context !== null) {
850
+ if (this.context.state !== "running") {
851
+ this.context.resume();
852
+ }
853
+ }
854
+ return this.context;
855
+ }
856
+ getAudioBuffer(audioID) {
857
+ return new Promise((resolve, reject) => {
858
+ // check whether audio file already preloaded
859
+ if (typeof this.audio_buffers[audioID] == "undefined" ||
860
+ this.audio_buffers[audioID] == "tmp") {
861
+ // if audio is not already loaded, try to load it
862
+ this.preloadAudio([audioID], () => {
863
+ resolve(this.audio_buffers[audioID]);
864
+ }, () => { }, (e) => {
865
+ reject(e.error);
866
+ });
867
+ }
868
+ else {
869
+ // audio is already loaded
870
+ resolve(this.audio_buffers[audioID]);
871
+ }
872
+ });
873
+ }
874
+ preloadAudio(files, callback_complete = () => { }, callback_load = (filepath) => { }, callback_error = (error_msg) => { }) {
875
+ files = unique(files.flat());
876
+ let n_loaded = 0;
877
+ if (files.length == 0) {
878
+ callback_complete();
879
+ return;
880
+ }
881
+ const load_audio_file_webaudio = (source, count = 1) => {
882
+ const request = new XMLHttpRequest();
883
+ request.open("GET", source, true);
884
+ request.responseType = "arraybuffer";
885
+ request.onload = () => {
886
+ this.context.decodeAudioData(request.response, (buffer) => {
887
+ this.audio_buffers[source] = buffer;
888
+ n_loaded++;
889
+ callback_load(source);
890
+ if (n_loaded == files.length) {
891
+ callback_complete();
892
+ }
893
+ }, (e) => {
894
+ callback_error({ source: source, error: e });
895
+ });
896
+ };
897
+ request.onerror = function (e) {
898
+ let err = e;
899
+ if (this.status == 404) {
900
+ err = "404";
901
+ }
902
+ callback_error({ source: source, error: err });
903
+ };
904
+ request.onloadend = function (e) {
905
+ if (this.status == 404) {
906
+ callback_error({ source: source, error: "404" });
907
+ }
908
+ };
909
+ request.send();
910
+ this.preload_requests.push(request);
911
+ };
912
+ const load_audio_file_html5audio = (source, count = 1) => {
913
+ const audio = new Audio();
914
+ const handleCanPlayThrough = () => {
915
+ this.audio_buffers[source] = audio;
916
+ n_loaded++;
917
+ callback_load(source);
918
+ if (n_loaded == files.length) {
919
+ callback_complete();
920
+ }
921
+ audio.removeEventListener("canplaythrough", handleCanPlayThrough);
922
+ };
923
+ audio.addEventListener("canplaythrough", handleCanPlayThrough);
924
+ audio.addEventListener("error", function handleError(e) {
925
+ callback_error({ source: audio.src, error: e });
926
+ audio.removeEventListener("error", handleError);
927
+ });
928
+ audio.addEventListener("abort", function handleAbort(e) {
929
+ callback_error({ source: audio.src, error: e });
930
+ audio.removeEventListener("abort", handleAbort);
931
+ });
932
+ audio.src = source;
933
+ this.preload_requests.push(audio);
934
+ };
935
+ for (const file of files) {
936
+ if (typeof this.audio_buffers[file] !== "undefined") {
937
+ n_loaded++;
938
+ callback_load(file);
939
+ if (n_loaded == files.length) {
940
+ callback_complete();
941
+ }
942
+ }
943
+ else {
944
+ this.audio_buffers[file] = "tmp";
945
+ if (this.audioContext() !== null) {
946
+ load_audio_file_webaudio(file);
947
+ }
948
+ else {
949
+ load_audio_file_html5audio(file);
950
+ }
951
+ }
952
+ }
953
+ }
954
+ preloadImages(images, callback_complete = () => { }, callback_load = (filepath) => { }, callback_error = (error_msg) => { }) {
955
+ // flatten the images array
956
+ images = unique(images.flat());
957
+ var n_loaded = 0;
958
+ if (images.length === 0) {
959
+ callback_complete();
960
+ return;
961
+ }
962
+ for (var i = 0; i < images.length; i++) {
963
+ var img = new Image();
964
+ img.onload = function () {
965
+ n_loaded++;
966
+ callback_load(img.src);
967
+ if (n_loaded === images.length) {
968
+ callback_complete();
969
+ }
970
+ };
971
+ img.onerror = function (e) {
972
+ callback_error({ source: img.src, error: e });
973
+ };
974
+ img.src = images[i];
975
+ this.img_cache[images[i]] = img;
976
+ this.preload_requests.push(img);
977
+ }
978
+ }
979
+ preloadVideo(videos, callback_complete = () => { }, callback_load = (filepath) => { }, callback_error = (error_msg) => { }) {
980
+ // flatten the video array
981
+ videos = unique(videos.flat());
982
+ let n_loaded = 0;
983
+ if (videos.length === 0) {
984
+ callback_complete();
985
+ return;
986
+ }
987
+ for (const video of videos) {
988
+ const video_buffers = this.video_buffers;
989
+ //based on option 4 here: http://dinbror.dk/blog/how-to-preload-entire-html5-video-before-play-solved/
990
+ const request = new XMLHttpRequest();
991
+ request.open("GET", video, true);
992
+ request.responseType = "blob";
993
+ request.onload = function () {
994
+ if (this.status === 200 || this.status === 0) {
995
+ const videoBlob = this.response;
996
+ video_buffers[video] = URL.createObjectURL(videoBlob); // IE10+
997
+ n_loaded++;
998
+ callback_load(video);
999
+ if (n_loaded === videos.length) {
1000
+ callback_complete();
1001
+ }
1002
+ }
1003
+ };
1004
+ request.onerror = function (e) {
1005
+ let err = e;
1006
+ if (this.status == 404) {
1007
+ err = "404";
1008
+ }
1009
+ callback_error({ source: video, error: err });
1010
+ };
1011
+ request.onloadend = function (e) {
1012
+ if (this.status == 404) {
1013
+ callback_error({ source: video, error: "404" });
1014
+ }
1015
+ };
1016
+ request.send();
1017
+ this.preload_requests.push(request);
1018
+ }
1019
+ }
1020
+ getAutoPreloadList(timeline_description) {
1021
+ /** Map each preload parameter type to a set of paths to be preloaded */
1022
+ const preloadPaths = Object.fromEntries(preloadParameterTypes.map((type) => [type, new Set()]));
1023
+ const traverseTimeline = (node, inheritedTrialType) => {
1024
+ var _a, _b, _c, _d;
1025
+ const isTimeline = typeof node.timeline !== "undefined";
1026
+ if (isTimeline) {
1027
+ for (const childNode of node.timeline) {
1028
+ traverseTimeline(childNode, (_a = node.type) !== null && _a !== void 0 ? _a : inheritedTrialType);
1029
+ }
1030
+ }
1031
+ else if ((_c = ((_b = node.type) !== null && _b !== void 0 ? _b : inheritedTrialType)) === null || _c === void 0 ? void 0 : _c.info) {
1032
+ // node is a trial with type.info set
1033
+ // Get the plugin name and parameters object from the info object
1034
+ const { name: pluginName, parameters } = ((_d = node.type) !== null && _d !== void 0 ? _d : inheritedTrialType).info;
1035
+ // Extract parameters to be preloaded and their types from parameter info if this has not
1036
+ // yet been done for `pluginName`
1037
+ if (!this.preloadMap.has(pluginName)) {
1038
+ this.preloadMap.set(pluginName, Object.fromEntries(Object.entries(parameters)
1039
+ // Filter out parameter entries with media types and a non-false `preload` option
1040
+ .filter(([_name, { type, preload }]) => preloadParameterTypes.includes(type) && (preload !== null && preload !== void 0 ? preload : true))
1041
+ // Map each entry's value to its parameter type
1042
+ .map(([name, { type }]) => [name, type])));
1043
+ }
1044
+ // Add preload paths from this trial
1045
+ for (const [parameterName, parameterType] of Object.entries(this.preloadMap.get(pluginName))) {
1046
+ const parameterValue = node[parameterName];
1047
+ const elements = preloadPaths[parameterType];
1048
+ if (typeof parameterValue === "string") {
1049
+ elements.add(parameterValue);
1050
+ }
1051
+ else if (Array.isArray(parameterValue)) {
1052
+ for (const element of parameterValue.flat()) {
1053
+ if (typeof element === "string") {
1054
+ elements.add(element);
1055
+ }
1056
+ }
1057
+ }
1058
+ }
1059
+ }
1060
+ };
1061
+ traverseTimeline({ timeline: timeline_description });
1062
+ return {
1063
+ images: [...preloadPaths[exports.ParameterType.IMAGE]],
1064
+ audio: [...preloadPaths[exports.ParameterType.AUDIO]],
1065
+ video: [...preloadPaths[exports.ParameterType.VIDEO]],
1066
+ };
1067
+ }
1068
+ cancelPreloads() {
1069
+ for (const request of this.preload_requests) {
1070
+ request.onload = () => { };
1071
+ request.onerror = () => { };
1072
+ request.oncanplaythrough = () => { };
1073
+ request.onabort = () => { };
1074
+ }
1075
+ this.preload_requests = [];
1076
+ }
1077
+ initializeMicrophoneRecorder(stream) {
1078
+ const recorder = new MediaRecorder(stream);
1079
+ this.microphone_recorder = recorder;
1080
+ }
1081
+ getMicrophoneRecorder() {
1082
+ return this.microphone_recorder;
1083
+ }
1084
+ }
1085
+
1086
+ class SimulationAPI {
1087
+ dispatchEvent(event) {
1088
+ document.body.dispatchEvent(event);
1089
+ }
1090
+ /**
1091
+ * Dispatches a `keydown` event for the specified key
1092
+ * @param key Character code (`.key` property) for the key to press.
1093
+ */
1094
+ keyDown(key) {
1095
+ this.dispatchEvent(new KeyboardEvent("keydown", { key }));
1096
+ }
1097
+ /**
1098
+ * Dispatches a `keyup` event for the specified key
1099
+ * @param key Character code (`.key` property) for the key to press.
1100
+ */
1101
+ keyUp(key) {
1102
+ this.dispatchEvent(new KeyboardEvent("keyup", { key }));
1103
+ }
1104
+ /**
1105
+ * Dispatches a `keydown` and `keyup` event in sequence to simulate pressing a key.
1106
+ * @param key Character code (`.key` property) for the key to press.
1107
+ * @param delay Length of time to wait (ms) before executing action
1108
+ */
1109
+ pressKey(key, delay = 0) {
1110
+ if (delay > 0) {
1111
+ setTimeout(() => {
1112
+ this.keyDown(key);
1113
+ this.keyUp(key);
1114
+ }, delay);
1115
+ }
1116
+ else {
1117
+ this.keyDown(key);
1118
+ this.keyUp(key);
1119
+ }
1120
+ }
1121
+ /**
1122
+ * Dispatches `mousedown`, `mouseup`, and `click` events on the target element
1123
+ * @param target The element to click
1124
+ * @param delay Length of time to wait (ms) before executing action
1125
+ */
1126
+ clickTarget(target, delay = 0) {
1127
+ if (delay > 0) {
1128
+ setTimeout(() => {
1129
+ target.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
1130
+ target.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
1131
+ target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
1132
+ }, delay);
1133
+ }
1134
+ else {
1135
+ target.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
1136
+ target.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
1137
+ target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
1138
+ }
1139
+ }
1140
+ /**
1141
+ * Sets the value of a target text input
1142
+ * @param target A text input element to fill in
1143
+ * @param text Text to input
1144
+ * @param delay Length of time to wait (ms) before executing action
1145
+ */
1146
+ fillTextInput(target, text, delay = 0) {
1147
+ if (delay > 0) {
1148
+ setTimeout(() => {
1149
+ target.value = text;
1150
+ }, delay);
1151
+ }
1152
+ else {
1153
+ target.value = text;
1154
+ }
1155
+ }
1156
+ /**
1157
+ * Picks a valid key from `choices`, taking into account jsPsych-specific
1158
+ * identifiers like "NO_KEYS" and "ALL_KEYS".
1159
+ * @param choices Which keys are valid.
1160
+ * @returns A key selected at random from the valid keys.
1161
+ */
1162
+ getValidKey(choices) {
1163
+ const possible_keys = [
1164
+ "a",
1165
+ "b",
1166
+ "c",
1167
+ "d",
1168
+ "e",
1169
+ "f",
1170
+ "g",
1171
+ "h",
1172
+ "i",
1173
+ "j",
1174
+ "k",
1175
+ "l",
1176
+ "m",
1177
+ "n",
1178
+ "o",
1179
+ "p",
1180
+ "q",
1181
+ "r",
1182
+ "s",
1183
+ "t",
1184
+ "u",
1185
+ "v",
1186
+ "w",
1187
+ "x",
1188
+ "y",
1189
+ "z",
1190
+ "0",
1191
+ "1",
1192
+ "2",
1193
+ "3",
1194
+ "4",
1195
+ "5",
1196
+ "6",
1197
+ "7",
1198
+ "8",
1199
+ "9",
1200
+ " ",
1201
+ ];
1202
+ let key;
1203
+ if (choices == "NO_KEYS") {
1204
+ key = null;
1205
+ }
1206
+ else if (choices == "ALL_KEYS") {
1207
+ key = possible_keys[Math.floor(Math.random() * possible_keys.length)];
1208
+ }
1209
+ else {
1210
+ const flat_choices = choices.flat();
1211
+ key = flat_choices[Math.floor(Math.random() * flat_choices.length)];
1212
+ }
1213
+ return key;
1214
+ }
1215
+ mergeSimulationData(default_data, simulation_options) {
1216
+ // override any data with data from simulation object
1217
+ return Object.assign(Object.assign({}, default_data), simulation_options === null || simulation_options === void 0 ? void 0 : simulation_options.data);
1218
+ }
1219
+ ensureSimulationDataConsistency(trial, data) {
1220
+ // All RTs must be rounded
1221
+ if (data.rt) {
1222
+ data.rt = Math.round(data.rt);
1223
+ }
1224
+ // If a trial_duration and rt exist, make sure that the RT is not longer than the trial.
1225
+ if (trial.trial_duration && data.rt && data.rt > trial.trial_duration) {
1226
+ data.rt = null;
1227
+ if (data.response) {
1228
+ data.response = null;
1229
+ }
1230
+ if (data.correct) {
1231
+ data.correct = false;
1232
+ }
1233
+ }
1234
+ // If trial.choices is NO_KEYS make sure that response and RT are null
1235
+ if (trial.choices && trial.choices == "NO_KEYS") {
1236
+ if (data.rt) {
1237
+ data.rt = null;
1238
+ }
1239
+ if (data.response) {
1240
+ data.response = null;
1241
+ }
1242
+ }
1243
+ // If response is not allowed before stimulus display complete, ensure RT
1244
+ // is longer than display time.
1245
+ if (trial.allow_response_before_complete) {
1246
+ if (trial.sequence_reps && trial.frame_time) {
1247
+ const min_time = trial.sequence_reps * trial.frame_time * trial.stimuli.length;
1248
+ if (data.rt < min_time) {
1249
+ data.rt = null;
1250
+ data.response = null;
1251
+ }
1252
+ }
1253
+ }
1254
+ }
1255
+ }
1256
+
1257
+ class TimeoutAPI {
1258
+ constructor() {
1259
+ this.timeout_handlers = [];
1260
+ }
1261
+ setTimeout(callback, delay) {
1262
+ const handle = window.setTimeout(callback, delay);
1263
+ this.timeout_handlers.push(handle);
1264
+ return handle;
1265
+ }
1266
+ clearAllTimeouts() {
1267
+ for (const handler of this.timeout_handlers) {
1268
+ clearTimeout(handler);
1269
+ }
1270
+ this.timeout_handlers = [];
1271
+ }
1272
+ }
1273
+
1274
+ function createJointPluginAPIObject(jsPsych) {
1275
+ const settings = jsPsych.getInitSettings();
1276
+ return Object.assign({}, ...[
1277
+ new KeyboardListenerAPI(jsPsych.getDisplayContainerElement, settings.case_sensitive_responses, settings.minimum_valid_rt),
1278
+ new TimeoutAPI(),
1279
+ new MediaAPI(settings.use_webaudio, jsPsych.webaudio_context),
1280
+ new HardwareAPI(),
1281
+ new SimulationAPI(),
1282
+ ].map((object) => autoBind(object)));
1283
+ }
1284
+
1285
+ var wordList = [
1286
+ // Borrowed from xkcd password generator which borrowed it from wherever
1287
+ "ability","able","aboard","about","above","accept","accident","according",
1288
+ "account","accurate","acres","across","act","action","active","activity",
1289
+ "actual","actually","add","addition","additional","adjective","adult","adventure",
1290
+ "advice","affect","afraid","after","afternoon","again","against","age",
1291
+ "ago","agree","ahead","aid","air","airplane","alike","alive",
1292
+ "all","allow","almost","alone","along","aloud","alphabet","already",
1293
+ "also","although","am","among","amount","ancient","angle","angry",
1294
+ "animal","announced","another","answer","ants","any","anybody","anyone",
1295
+ "anything","anyway","anywhere","apart","apartment","appearance","apple","applied",
1296
+ "appropriate","are","area","arm","army","around","arrange","arrangement",
1297
+ "arrive","arrow","art","article","as","aside","ask","asleep",
1298
+ "at","ate","atmosphere","atom","atomic","attached","attack","attempt",
1299
+ "attention","audience","author","automobile","available","average","avoid","aware",
1300
+ "away","baby","back","bad","badly","bag","balance","ball",
1301
+ "balloon","band","bank","bar","bare","bark","barn","base",
1302
+ "baseball","basic","basis","basket","bat","battle","be","bean",
1303
+ "bear","beat","beautiful","beauty","became","because","become","becoming",
1304
+ "bee","been","before","began","beginning","begun","behavior","behind",
1305
+ "being","believed","bell","belong","below","belt","bend","beneath",
1306
+ "bent","beside","best","bet","better","between","beyond","bicycle",
1307
+ "bigger","biggest","bill","birds","birth","birthday","bit","bite",
1308
+ "black","blank","blanket","blew","blind","block","blood","blow",
1309
+ "blue","board","boat","body","bone","book","border","born",
1310
+ "both","bottle","bottom","bound","bow","bowl","box","boy",
1311
+ "brain","branch","brass","brave","bread","break","breakfast","breath",
1312
+ "breathe","breathing","breeze","brick","bridge","brief","bright","bring",
1313
+ "broad","broke","broken","brother","brought","brown","brush","buffalo",
1314
+ "build","building","built","buried","burn","burst","bus","bush",
1315
+ "business","busy","but","butter","buy","by","cabin","cage",
1316
+ "cake","call","calm","came","camera","camp","can","canal",
1317
+ "cannot","cap","capital","captain","captured","car","carbon","card",
1318
+ "care","careful","carefully","carried","carry","case","cast","castle",
1319
+ "cat","catch","cattle","caught","cause","cave","cell","cent",
1320
+ "center","central","century","certain","certainly","chain","chair","chamber",
1321
+ "chance","change","changing","chapter","character","characteristic","charge","chart",
1322
+ "check","cheese","chemical","chest","chicken","chief","child","children",
1323
+ "choice","choose","chose","chosen","church","circle","circus","citizen",
1324
+ "city","class","classroom","claws","clay","clean","clear","clearly",
1325
+ "climate","climb","clock","close","closely","closer","cloth","clothes",
1326
+ "clothing","cloud","club","coach","coal","coast","coat","coffee",
1327
+ "cold","collect","college","colony","color","column","combination","combine",
1328
+ "come","comfortable","coming","command","common","community","company","compare",
1329
+ "compass","complete","completely","complex","composed","composition","compound","concerned",
1330
+ "condition","congress","connected","consider","consist","consonant","constantly","construction",
1331
+ "contain","continent","continued","contrast","control","conversation","cook","cookies",
1332
+ "cool","copper","copy","corn","corner","correct","correctly","cost",
1333
+ "cotton","could","count","country","couple","courage","course","court",
1334
+ "cover","cow","cowboy","crack","cream","create","creature","crew",
1335
+ "crop","cross","crowd","cry","cup","curious","current","curve",
1336
+ "customs","cut","cutting","daily","damage","dance","danger","dangerous",
1337
+ "dark","darkness","date","daughter","dawn","day","dead","deal",
1338
+ "dear","death","decide","declared","deep","deeply","deer","definition",
1339
+ "degree","depend","depth","describe","desert","design","desk","detail",
1340
+ "determine","develop","development","diagram","diameter","did","die","differ",
1341
+ "difference","different","difficult","difficulty","dig","dinner","direct","direction",
1342
+ "directly","dirt","dirty","disappear","discover","discovery","discuss","discussion",
1343
+ "disease","dish","distance","distant","divide","division","do","doctor",
1344
+ "does","dog","doing","doll","dollar","done","donkey","door",
1345
+ "dot","double","doubt","down","dozen","draw","drawn","dream",
1346
+ "dress","drew","dried","drink","drive","driven","driver","driving",
1347
+ "drop","dropped","drove","dry","duck","due","dug","dull",
1348
+ "during","dust","duty","each","eager","ear","earlier","early",
1349
+ "earn","earth","easier","easily","east","easy","eat","eaten",
1350
+ "edge","education","effect","effort","egg","eight","either","electric",
1351
+ "electricity","element","elephant","eleven","else","empty","end","enemy",
1352
+ "energy","engine","engineer","enjoy","enough","enter","entire","entirely",
1353
+ "environment","equal","equally","equator","equipment","escape","especially","essential",
1354
+ "establish","even","evening","event","eventually","ever","every","everybody",
1355
+ "everyone","everything","everywhere","evidence","exact","exactly","examine","example",
1356
+ "excellent","except","exchange","excited","excitement","exciting","exclaimed","exercise",
1357
+ "exist","expect","experience","experiment","explain","explanation","explore","express",
1358
+ "expression","extra","eye","face","facing","fact","factor","factory",
1359
+ "failed","fair","fairly","fall","fallen","familiar","family","famous",
1360
+ "far","farm","farmer","farther","fast","fastened","faster","fat",
1361
+ "father","favorite","fear","feathers","feature","fed","feed","feel",
1362
+ "feet","fell","fellow","felt","fence","few","fewer","field",
1363
+ "fierce","fifteen","fifth","fifty","fight","fighting","figure","fill",
1364
+ "film","final","finally","find","fine","finest","finger","finish",
1365
+ "fire","fireplace","firm","first","fish","five","fix","flag",
1366
+ "flame","flat","flew","flies","flight","floating","floor","flow",
1367
+ "flower","fly","fog","folks","follow","food","foot","football",
1368
+ "for","force","foreign","forest","forget","forgot","forgotten","form",
1369
+ "former","fort","forth","forty","forward","fought","found","four",
1370
+ "fourth","fox","frame","free","freedom","frequently","fresh","friend",
1371
+ "friendly","frighten","frog","from","front","frozen","fruit","fuel",
1372
+ "full","fully","fun","function","funny","fur","furniture","further",
1373
+ "future","gain","game","garage","garden","gas","gasoline","gate",
1374
+ "gather","gave","general","generally","gentle","gently","get","getting",
1375
+ "giant","gift","girl","give","given","giving","glad","glass",
1376
+ "globe","go","goes","gold","golden","gone","good","goose",
1377
+ "got","government","grabbed","grade","gradually","grain","grandfather","grandmother",
1378
+ "graph","grass","gravity","gray","great","greater","greatest","greatly",
1379
+ "green","grew","ground","group","grow","grown","growth","guard",
1380
+ "guess","guide","gulf","gun","habit","had","hair","half",
1381
+ "halfway","hall","hand","handle","handsome","hang","happen","happened",
1382
+ "happily","happy","harbor","hard","harder","hardly","has","hat",
1383
+ "have","having","hay","he","headed","heading","health","heard",
1384
+ "hearing","heart","heat","heavy","height","held","hello","help",
1385
+ "helpful","her","herd","here","herself","hidden","hide","high",
1386
+ "higher","highest","highway","hill","him","himself","his","history",
1387
+ "hit","hold","hole","hollow","home","honor","hope","horn",
1388
+ "horse","hospital","hot","hour","house","how","however","huge",
1389
+ "human","hundred","hung","hungry","hunt","hunter","hurried","hurry",
1390
+ "hurt","husband","ice","idea","identity","if","ill","image",
1391
+ "imagine","immediately","importance","important","impossible","improve","in","inch",
1392
+ "include","including","income","increase","indeed","independent","indicate","individual",
1393
+ "industrial","industry","influence","information","inside","instance","instant","instead",
1394
+ "instrument","interest","interior","into","introduced","invented","involved","iron",
1395
+ "is","island","it","its","itself","jack","jar","jet",
1396
+ "job","join","joined","journey","joy","judge","jump","jungle",
1397
+ "just","keep","kept","key","kids","kill","kind","kitchen",
1398
+ "knew","knife","know","knowledge","known","label","labor","lack",
1399
+ "lady","laid","lake","lamp","land","language","large","larger",
1400
+ "largest","last","late","later","laugh","law","lay","layers",
1401
+ "lead","leader","leaf","learn","least","leather","leave","leaving",
1402
+ "led","left","leg","length","lesson","let","letter","level",
1403
+ "library","lie","life","lift","light","like","likely","limited",
1404
+ "line","lion","lips","liquid","list","listen","little","live",
1405
+ "living","load","local","locate","location","log","lonely","long",
1406
+ "longer","look","loose","lose","loss","lost","lot","loud",
1407
+ "love","lovely","low","lower","luck","lucky","lunch","lungs",
1408
+ "lying","machine","machinery","mad","made","magic","magnet","mail",
1409
+ "main","mainly","major","make","making","man","managed","manner",
1410
+ "manufacturing","many","map","mark","market","married","mass","massage",
1411
+ "master","material","mathematics","matter","may","maybe","me","meal",
1412
+ "mean","means","meant","measure","meat","medicine","meet","melted",
1413
+ "member","memory","men","mental","merely","met","metal","method",
1414
+ "mice","middle","might","mighty","mile","military","milk","mill",
1415
+ "mind","mine","minerals","minute","mirror","missing","mission","mistake",
1416
+ "mix","mixture","model","modern","molecular","moment","money","monkey",
1417
+ "month","mood","moon","more","morning","most","mostly","mother",
1418
+ "motion","motor","mountain","mouse","mouth","move","movement","movie",
1419
+ "moving","mud","muscle","music","musical","must","my","myself",
1420
+ "mysterious","nails","name","nation","national","native","natural","naturally",
1421
+ "nature","near","nearby","nearer","nearest","nearly","necessary","neck",
1422
+ "needed","needle","needs","negative","neighbor","neighborhood","nervous","nest",
1423
+ "never","new","news","newspaper","next","nice","night","nine",
1424
+ "no","nobody","nodded","noise","none","noon","nor","north",
1425
+ "nose","not","note","noted","nothing","notice","noun","now",
1426
+ "number","numeral","nuts","object","observe","obtain","occasionally","occur",
1427
+ "ocean","of","off","offer","office","officer","official","oil",
1428
+ "old","older","oldest","on","once","one","only","onto",
1429
+ "open","operation","opinion","opportunity","opposite","or","orange","orbit",
1430
+ "order","ordinary","organization","organized","origin","original","other","ought",
1431
+ "our","ourselves","out","outer","outline","outside","over","own",
1432
+ "owner","oxygen","pack","package","page","paid","pain","paint",
1433
+ "pair","palace","pale","pan","paper","paragraph","parallel","parent",
1434
+ "park","part","particles","particular","particularly","partly","parts","party",
1435
+ "pass","passage","past","path","pattern","pay","peace","pen",
1436
+ "pencil","people","per","percent","perfect","perfectly","perhaps","period",
1437
+ "person","personal","pet","phrase","physical","piano","pick","picture",
1438
+ "pictured","pie","piece","pig","pile","pilot","pine","pink",
1439
+ "pipe","pitch","place","plain","plan","plane","planet","planned",
1440
+ "planning","plant","plastic","plate","plates","play","pleasant","please",
1441
+ "pleasure","plenty","plural","plus","pocket","poem","poet","poetry",
1442
+ "point","pole","police","policeman","political","pond","pony","pool",
1443
+ "poor","popular","population","porch","port","position","positive","possible",
1444
+ "possibly","post","pot","potatoes","pound","pour","powder","power",
1445
+ "powerful","practical","practice","prepare","present","president","press","pressure",
1446
+ "pretty","prevent","previous","price","pride","primitive","principal","principle",
1447
+ "printed","private","prize","probably","problem","process","produce","product",
1448
+ "production","program","progress","promised","proper","properly","property","protection",
1449
+ "proud","prove","provide","public","pull","pupil","pure","purple",
1450
+ "purpose","push","put","putting","quarter","queen","question","quick",
1451
+ "quickly","quiet","quietly","quite","rabbit","race","radio","railroad",
1452
+ "rain","raise","ran","ranch","range","rapidly","rate","rather",
1453
+ "raw","rays","reach","read","reader","ready","real","realize",
1454
+ "rear","reason","recall","receive","recent","recently","recognize","record",
1455
+ "red","refer","refused","region","regular","related","relationship","religious",
1456
+ "remain","remarkable","remember","remove","repeat","replace","replied","report",
1457
+ "represent","require","research","respect","rest","result","return","review",
1458
+ "rhyme","rhythm","rice","rich","ride","riding","right","ring",
1459
+ "rise","rising","river","road","roar","rock","rocket","rocky",
1460
+ "rod","roll","roof","room","root","rope","rose","rough",
1461
+ "round","route","row","rubbed","rubber","rule","ruler","run",
1462
+ "running","rush","sad","saddle","safe","safety","said","sail",
1463
+ "sale","salmon","salt","same","sand","sang","sat","satellites",
1464
+ "satisfied","save","saved","saw","say","scale","scared","scene",
1465
+ "school","science","scientific","scientist","score","screen","sea","search",
1466
+ "season","seat","second","secret","section","see","seed","seeing",
1467
+ "seems","seen","seldom","select","selection","sell","send","sense",
1468
+ "sent","sentence","separate","series","serious","serve","service","sets",
1469
+ "setting","settle","settlers","seven","several","shade","shadow","shake",
1470
+ "shaking","shall","shallow","shape","share","sharp","she","sheep",
1471
+ "sheet","shelf","shells","shelter","shine","shinning","ship","shirt",
1472
+ "shoe","shoot","shop","shore","short","shorter","shot","should",
1473
+ "shoulder","shout","show","shown","shut","sick","sides","sight",
1474
+ "sign","signal","silence","silent","silk","silly","silver","similar",
1475
+ "simple","simplest","simply","since","sing","single","sink","sister",
1476
+ "sit","sitting","situation","six","size","skill","skin","sky",
1477
+ "slabs","slave","sleep","slept","slide","slight","slightly","slip",
1478
+ "slipped","slope","slow","slowly","small","smaller","smallest","smell",
1479
+ "smile","smoke","smooth","snake","snow","so","soap","social",
1480
+ "society","soft","softly","soil","solar","sold","soldier","solid",
1481
+ "solution","solve","some","somebody","somehow","someone","something","sometime",
1482
+ "somewhere","son","song","soon","sort","sound","source","south",
1483
+ "southern","space","speak","special","species","specific","speech","speed",
1484
+ "spell","spend","spent","spider","spin","spirit","spite","split",
1485
+ "spoken","sport","spread","spring","square","stage","stairs","stand",
1486
+ "standard","star","stared","start","state","statement","station","stay",
1487
+ "steady","steam","steel","steep","stems","step","stepped","stick",
1488
+ "stiff","still","stock","stomach","stone","stood","stop","stopped",
1489
+ "store","storm","story","stove","straight","strange","stranger","straw",
1490
+ "stream","street","strength","stretch","strike","string","strip","strong",
1491
+ "stronger","struck","structure","struggle","stuck","student","studied","studying",
1492
+ "subject","substance","success","successful","such","sudden","suddenly","sugar",
1493
+ "suggest","suit","sum","summer","sun","sunlight","supper","supply",
1494
+ "support","suppose","sure","surface","surprise","surrounded","swam","sweet",
1495
+ "swept","swim","swimming","swing","swung","syllable","symbol","system",
1496
+ "table","tail","take","taken","tales","talk","tall","tank",
1497
+ "tape","task","taste","taught","tax","tea","teach","teacher",
1498
+ "team","tears","teeth","telephone","television","tell","temperature","ten",
1499
+ "tent","term","terrible","test","than","thank","that","thee",
1500
+ "them","themselves","then","theory","there","therefore","these","they",
1501
+ "thick","thin","thing","think","third","thirty","this","those",
1502
+ "thou","though","thought","thousand","thread","three","threw","throat",
1503
+ "through","throughout","throw","thrown","thumb","thus","thy","tide",
1504
+ "tie","tight","tightly","till","time","tin","tiny","tip",
1505
+ "tired","title","to","tobacco","today","together","told","tomorrow",
1506
+ "tone","tongue","tonight","too","took","tool","top","topic",
1507
+ "torn","total","touch","toward","tower","town","toy","trace",
1508
+ "track","trade","traffic","trail","train","transportation","trap","travel",
1509
+ "treated","tree","triangle","tribe","trick","tried","trip","troops",
1510
+ "tropical","trouble","truck","trunk","truth","try","tube","tune",
1511
+ "turn","twelve","twenty","twice","two","type","typical","uncle",
1512
+ "under","underline","understanding","unhappy","union","unit","universe","unknown",
1513
+ "unless","until","unusual","up","upon","upper","upward","us",
1514
+ "use","useful","using","usual","usually","valley","valuable","value",
1515
+ "vapor","variety","various","vast","vegetable","verb","vertical","very",
1516
+ "vessels","victory","view","village","visit","visitor","voice","volume",
1517
+ "vote","vowel","voyage","wagon","wait","walk","wall","want",
1518
+ "war","warm","warn","was","wash","waste","watch","water",
1519
+ "wave","way","we","weak","wealth","wear","weather","week",
1520
+ "weigh","weight","welcome","well","went","were","west","western",
1521
+ "wet","whale","what","whatever","wheat","wheel","when","whenever",
1522
+ "where","wherever","whether","which","while","whispered","whistle","white",
1523
+ "who","whole","whom","whose","why","wide","widely","wife",
1524
+ "wild","will","willing","win","wind","window","wing","winter",
1525
+ "wire","wise","wish","with","within","without","wolf","women",
1526
+ "won","wonder","wonderful","wood","wooden","wool","word","wore",
1527
+ "work","worker","world","worried","worry","worse","worth","would",
1528
+ "wrapped","write","writer","writing","written","wrong","wrote","yard",
1529
+ "year","yellow","yes","yesterday","yet","you","young","younger",
1530
+ "your","yourself","youth","zero","zebra","zipper","zoo","zulu"
1531
+ ];
1532
+
1533
+ function words(options) {
1534
+
1535
+ function word() {
1536
+ if (options && options.maxLength > 1) {
1537
+ return generateWordWithMaxLength();
1538
+ } else {
1539
+ return generateRandomWord();
1540
+ }
1541
+ }
1542
+
1543
+ function generateWordWithMaxLength() {
1544
+ var rightSize = false;
1545
+ var wordUsed;
1546
+ while (!rightSize) {
1547
+ wordUsed = generateRandomWord();
1548
+ if(wordUsed.length <= options.maxLength) {
1549
+ rightSize = true;
1550
+ }
1551
+
1552
+ }
1553
+ return wordUsed;
1554
+ }
1555
+
1556
+ function generateRandomWord() {
1557
+ return wordList[randInt(wordList.length)];
1558
+ }
1559
+
1560
+ function randInt(lessThan) {
1561
+ return Math.floor(Math.random() * lessThan);
1562
+ }
1563
+
1564
+ // No arguments = generate one word
1565
+ if (typeof(options) === 'undefined') {
1566
+ return word();
1567
+ }
1568
+
1569
+ // Just a number = return that many words
1570
+ if (typeof(options) === 'number') {
1571
+ options = { exactly: options };
1572
+ }
1573
+
1574
+ // options supported: exactly, min, max, join
1575
+ if (options.exactly) {
1576
+ options.min = options.exactly;
1577
+ options.max = options.exactly;
1578
+ }
1579
+
1580
+ // not a number = one word par string
1581
+ if (typeof(options.wordsPerString) !== 'number') {
1582
+ options.wordsPerString = 1;
1583
+ }
1584
+
1585
+ //not a function = returns the raw word
1586
+ if (typeof(options.formatter) !== 'function') {
1587
+ options.formatter = (word) => word;
1588
+ }
1589
+
1590
+ //not a string = separator is a space
1591
+ if (typeof(options.separator) !== 'string') {
1592
+ options.separator = ' ';
1593
+ }
1594
+
1595
+ var total = options.min + randInt(options.max + 1 - options.min);
1596
+ var results = [];
1597
+ var token = '';
1598
+ var relativeIndex = 0;
1599
+
1600
+ for (var i = 0; (i < total * options.wordsPerString); i++) {
1601
+ if (relativeIndex === options.wordsPerString - 1) {
1602
+ token += options.formatter(word(), relativeIndex);
1603
+ }
1604
+ else {
1605
+ token += options.formatter(word(), relativeIndex) + options.separator;
1606
+ }
1607
+ relativeIndex++;
1608
+ if ((i + 1) % options.wordsPerString === 0) {
1609
+ results.push(token);
1610
+ token = '';
1611
+ relativeIndex = 0;
1612
+ }
1613
+
1614
+ }
1615
+ if (typeof options.join === 'string') {
1616
+ results = results.join(options.join);
1617
+ }
1618
+
1619
+ return results;
1620
+ }
1621
+
1622
+ var randomWords$1 = words;
1623
+ // Export the word list as it is often useful
1624
+ words.wordList = wordList;
1625
+
1626
+ function repeat(array, repetitions, unpack = false) {
1627
+ const arr_isArray = Array.isArray(array);
1628
+ const rep_isArray = Array.isArray(repetitions);
1629
+ // if array is not an array, then we just repeat the item
1630
+ if (!arr_isArray) {
1631
+ if (!rep_isArray) {
1632
+ array = [array];
1633
+ repetitions = [repetitions];
1634
+ }
1635
+ else {
1636
+ repetitions = [repetitions[0]];
1637
+ console.log("Unclear parameters given to randomization.repeat. Multiple set sizes specified, but only one item exists to sample. Proceeding using the first set size.");
1638
+ }
1639
+ }
1640
+ else {
1641
+ // if repetitions is not an array, but array is, then we
1642
+ // repeat repetitions for each entry in array
1643
+ if (!rep_isArray) {
1644
+ let reps = [];
1645
+ for (let i = 0; i < array.length; i++) {
1646
+ reps.push(repetitions);
1647
+ }
1648
+ repetitions = reps;
1649
+ }
1650
+ else {
1651
+ if (array.length != repetitions.length) {
1652
+ console.warn("Unclear parameters given to randomization.repeat. Items and repetitions are unequal lengths. Behavior may not be as expected.");
1653
+ // throw warning if repetitions is too short, use first rep ONLY.
1654
+ if (repetitions.length < array.length) {
1655
+ let reps = [];
1656
+ for (let i = 0; i < array.length; i++) {
1657
+ reps.push(repetitions);
1658
+ }
1659
+ repetitions = reps;
1660
+ }
1661
+ else {
1662
+ // throw warning if too long, and then use the first N
1663
+ repetitions = repetitions.slice(0, array.length);
1664
+ }
1665
+ }
1666
+ }
1667
+ }
1668
+ // should be clear at this point to assume that array and repetitions are arrays with == length
1669
+ let allsamples = [];
1670
+ for (let i = 0; i < array.length; i++) {
1671
+ for (let j = 0; j < repetitions[i]; j++) {
1672
+ if (array[i] == null || typeof array[i] != "object") {
1673
+ allsamples.push(array[i]);
1674
+ }
1675
+ else {
1676
+ allsamples.push(Object.assign({}, array[i]));
1677
+ }
1678
+ }
1679
+ }
1680
+ let out = shuffle(allsamples);
1681
+ if (unpack) {
1682
+ out = unpackArray(out);
1683
+ }
1684
+ return out;
1685
+ }
1686
+ function shuffle(array) {
1687
+ if (!Array.isArray(array)) {
1688
+ console.error("Argument to shuffle() must be an array.");
1689
+ }
1690
+ const copy_array = array.slice(0);
1691
+ let m = copy_array.length, t, i;
1692
+ // While there remain elements to shuffle…
1693
+ while (m) {
1694
+ // Pick a remaining element…
1695
+ i = Math.floor(Math.random() * m--);
1696
+ // And swap it with the current element.
1697
+ t = copy_array[m];
1698
+ copy_array[m] = copy_array[i];
1699
+ copy_array[i] = t;
1700
+ }
1701
+ return copy_array;
1702
+ }
1703
+ function shuffleNoRepeats(arr, equalityTest) {
1704
+ if (!Array.isArray(arr)) {
1705
+ console.error("First argument to shuffleNoRepeats() must be an array.");
1706
+ }
1707
+ if (typeof equalityTest !== "undefined" && typeof equalityTest !== "function") {
1708
+ console.error("Second argument to shuffleNoRepeats() must be a function.");
1709
+ }
1710
+ // define a default equalityTest
1711
+ if (typeof equalityTest == "undefined") {
1712
+ equalityTest = function (a, b) {
1713
+ if (a === b) {
1714
+ return true;
1715
+ }
1716
+ else {
1717
+ return false;
1718
+ }
1719
+ };
1720
+ }
1721
+ const random_shuffle = shuffle(arr);
1722
+ for (let i = 0; i < random_shuffle.length - 1; i++) {
1723
+ if (equalityTest(random_shuffle[i], random_shuffle[i + 1])) {
1724
+ // neighbors are equal, pick a new random neighbor to swap (not the first or last element, to avoid edge cases)
1725
+ let random_pick = Math.floor(Math.random() * (random_shuffle.length - 2)) + 1;
1726
+ // test to make sure the new neighbor isn't equal to the old one
1727
+ while (equalityTest(random_shuffle[i + 1], random_shuffle[random_pick]) ||
1728
+ equalityTest(random_shuffle[i + 1], random_shuffle[random_pick + 1]) ||
1729
+ equalityTest(random_shuffle[i + 1], random_shuffle[random_pick - 1])) {
1730
+ random_pick = Math.floor(Math.random() * (random_shuffle.length - 2)) + 1;
1731
+ }
1732
+ const new_neighbor = random_shuffle[random_pick];
1733
+ random_shuffle[random_pick] = random_shuffle[i + 1];
1734
+ random_shuffle[i + 1] = new_neighbor;
1735
+ }
1736
+ }
1737
+ return random_shuffle;
1738
+ }
1739
+ function shuffleAlternateGroups(arr_groups, random_group_order = false) {
1740
+ const n_groups = arr_groups.length;
1741
+ if (n_groups == 1) {
1742
+ console.warn("shuffleAlternateGroups() was called with only one group. Defaulting to simple shuffle.");
1743
+ return shuffle(arr_groups[0]);
1744
+ }
1745
+ let group_order = [];
1746
+ for (let i = 0; i < n_groups; i++) {
1747
+ group_order.push(i);
1748
+ }
1749
+ if (random_group_order) {
1750
+ group_order = shuffle(group_order);
1751
+ }
1752
+ const randomized_groups = [];
1753
+ let min_length = null;
1754
+ for (let i = 0; i < n_groups; i++) {
1755
+ min_length =
1756
+ min_length === null ? arr_groups[i].length : Math.min(min_length, arr_groups[i].length);
1757
+ randomized_groups.push(shuffle(arr_groups[i]));
1758
+ }
1759
+ const out = [];
1760
+ for (let i = 0; i < min_length; i++) {
1761
+ for (let j = 0; j < group_order.length; j++) {
1762
+ out.push(randomized_groups[group_order[j]][i]);
1763
+ }
1764
+ }
1765
+ return out;
1766
+ }
1767
+ function sampleWithoutReplacement(arr, size) {
1768
+ if (!Array.isArray(arr)) {
1769
+ console.error("First argument to sampleWithoutReplacement() must be an array");
1770
+ }
1771
+ if (size > arr.length) {
1772
+ console.error("Cannot take a sample larger than the size of the set of items to sample.");
1773
+ }
1774
+ return shuffle(arr).slice(0, size);
1775
+ }
1776
+ function sampleWithReplacement(arr, size, weights) {
1777
+ if (!Array.isArray(arr)) {
1778
+ console.error("First argument to sampleWithReplacement() must be an array");
1779
+ }
1780
+ const normalized_weights = [];
1781
+ if (typeof weights !== "undefined") {
1782
+ if (weights.length !== arr.length) {
1783
+ console.error("The length of the weights array must equal the length of the array " +
1784
+ "to be sampled from.");
1785
+ }
1786
+ let weight_sum = 0;
1787
+ for (const weight of weights) {
1788
+ weight_sum += weight;
1789
+ }
1790
+ for (const weight of weights) {
1791
+ normalized_weights.push(weight / weight_sum);
1792
+ }
1793
+ }
1794
+ else {
1795
+ for (let i = 0; i < arr.length; i++) {
1796
+ normalized_weights.push(1 / arr.length);
1797
+ }
1798
+ }
1799
+ const cumulative_weights = [normalized_weights[0]];
1800
+ for (let i = 1; i < normalized_weights.length; i++) {
1801
+ cumulative_weights.push(normalized_weights[i] + cumulative_weights[i - 1]);
1802
+ }
1803
+ const samp = [];
1804
+ for (let i = 0; i < size; i++) {
1805
+ const rnd = Math.random();
1806
+ let index = 0;
1807
+ while (rnd > cumulative_weights[index]) {
1808
+ index++;
1809
+ }
1810
+ samp.push(arr[index]);
1811
+ }
1812
+ return samp;
1813
+ }
1814
+ function factorial(factors, repetitions = 1, unpack = false) {
1815
+ let design = [{}];
1816
+ for (const [factorName, factor] of Object.entries(factors)) {
1817
+ const new_design = [];
1818
+ for (const level of factor) {
1819
+ for (const cell of design) {
1820
+ new_design.push(Object.assign(Object.assign({}, cell), { [factorName]: level }));
1821
+ }
1822
+ }
1823
+ design = new_design;
1824
+ }
1825
+ return repeat(design, repetitions, unpack);
1826
+ }
1827
+ function randomID(length = 32) {
1828
+ let result = "";
1829
+ const chars = "0123456789abcdefghjklmnopqrstuvwxyz";
1830
+ for (let i = 0; i < length; i++) {
1831
+ result += chars[Math.floor(Math.random() * chars.length)];
1832
+ }
1833
+ return result;
1834
+ }
1835
+ /**
1836
+ * Generate a random integer from `lower` to `upper`, inclusive of both end points.
1837
+ * @param lower The lowest value it is possible to generate
1838
+ * @param upper The highest value it is possible to generate
1839
+ * @returns A random integer
1840
+ */
1841
+ function randomInt(lower, upper) {
1842
+ if (upper < lower) {
1843
+ throw new Error("Upper boundary must be less than or equal to lower boundary");
1844
+ }
1845
+ return lower + Math.floor(Math.random() * (upper - lower + 1));
1846
+ }
1847
+ /**
1848
+ * Generates a random sample from a Bernoulli distribution.
1849
+ * @param p The probability of sampling 1.
1850
+ * @returns 0, with probability 1-p, or 1, with probability p.
1851
+ */
1852
+ function sampleBernoulli(p) {
1853
+ return Math.random() <= p ? 1 : 0;
1854
+ }
1855
+ function sampleNormal(mean, standard_deviation) {
1856
+ return randn_bm() * standard_deviation + mean;
1857
+ }
1858
+ function sampleExponential(rate) {
1859
+ return -Math.log(Math.random()) / rate;
1860
+ }
1861
+ function sampleExGaussian(mean, standard_deviation, rate, positive = false) {
1862
+ let s = sampleNormal(mean, standard_deviation) + sampleExponential(rate);
1863
+ if (positive) {
1864
+ while (s <= 0) {
1865
+ s = sampleNormal(mean, standard_deviation) + sampleExponential(rate);
1866
+ }
1867
+ }
1868
+ return s;
1869
+ }
1870
+ /**
1871
+ * Generate one or more random words.
1872
+ *
1873
+ * This is a wrapper function for the {@link https://www.npmjs.com/package/random-words `random-words` npm package}.
1874
+ *
1875
+ * @param opts An object with optional properties `min`, `max`, `exactly`,
1876
+ * `join`, `maxLength`, `wordsPerString`, `separator`, and `formatter`.
1877
+ *
1878
+ * @returns An array of words or a single string, depending on parameter choices.
1879
+ */
1880
+ function randomWords(opts) {
1881
+ return randomWords$1(opts);
1882
+ }
1883
+ // Box-Muller transformation for a random sample from normal distribution with mean = 0, std = 1
1884
+ // https://stackoverflow.com/a/36481059/3726673
1885
+ function randn_bm() {
1886
+ var u = 0, v = 0;
1887
+ while (u === 0)
1888
+ u = Math.random(); //Converting [0,1) to (0,1)
1889
+ while (v === 0)
1890
+ v = Math.random();
1891
+ return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
1892
+ }
1893
+ function unpackArray(array) {
1894
+ const out = {};
1895
+ for (const x of array) {
1896
+ for (const key of Object.keys(x)) {
1897
+ if (typeof out[key] === "undefined") {
1898
+ out[key] = [];
1899
+ }
1900
+ out[key].push(x[key]);
1901
+ }
1902
+ }
1903
+ return out;
1904
+ }
1905
+
1906
+ var randomization = /*#__PURE__*/Object.freeze({
1907
+ __proto__: null,
1908
+ repeat: repeat,
1909
+ shuffle: shuffle,
1910
+ shuffleNoRepeats: shuffleNoRepeats,
1911
+ shuffleAlternateGroups: shuffleAlternateGroups,
1912
+ sampleWithoutReplacement: sampleWithoutReplacement,
1913
+ sampleWithReplacement: sampleWithReplacement,
1914
+ factorial: factorial,
1915
+ randomID: randomID,
1916
+ randomInt: randomInt,
1917
+ sampleBernoulli: sampleBernoulli,
1918
+ sampleNormal: sampleNormal,
1919
+ sampleExponential: sampleExponential,
1920
+ sampleExGaussian: sampleExGaussian,
1921
+ randomWords: randomWords
1922
+ });
1923
+
1924
+ /**
1925
+ * Gets information about the Mechanical Turk Environment, HIT, Assignment, and Worker
1926
+ * by parsing the URL variables that Mechanical Turk generates.
1927
+ * @returns An object containing information about the Mechanical Turk Environment, HIT, Assignment, and Worker.
1928
+ */
1929
+ function turkInfo() {
1930
+ const turk = {
1931
+ previewMode: false,
1932
+ outsideTurk: false,
1933
+ hitId: "INVALID_URL_PARAMETER",
1934
+ assignmentId: "INVALID_URL_PARAMETER",
1935
+ workerId: "INVALID_URL_PARAMETER",
1936
+ turkSubmitTo: "INVALID_URL_PARAMETER",
1937
+ };
1938
+ const param = function (url, name) {
1939
+ name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
1940
+ const regexS = "[\\?&]" + name + "=([^&#]*)";
1941
+ const regex = new RegExp(regexS);
1942
+ const results = regex.exec(url);
1943
+ return results == null ? "" : results[1];
1944
+ };
1945
+ const src = param(window.location.href, "assignmentId")
1946
+ ? window.location.href
1947
+ : document.referrer;
1948
+ const keys = ["assignmentId", "hitId", "workerId", "turkSubmitTo"];
1949
+ keys.map(function (key) {
1950
+ turk[key] = unescape(param(src, key));
1951
+ });
1952
+ turk.previewMode = turk.assignmentId == "ASSIGNMENT_ID_NOT_AVAILABLE";
1953
+ turk.outsideTurk =
1954
+ !turk.previewMode && turk.hitId === "" && turk.assignmentId == "" && turk.workerId == "";
1955
+ return turk;
1956
+ }
1957
+ /**
1958
+ * Send data to Mechnical Turk for storage.
1959
+ * @param data An object containing `key:value` pairs to send to Mechanical Turk. Values
1960
+ * cannot contain nested objects, arrays, or functions.
1961
+ * @returns Nothing
1962
+ */
1963
+ function submitToTurk(data) {
1964
+ const turk = turkInfo();
1965
+ const assignmentId = turk.assignmentId;
1966
+ const turkSubmitTo = turk.turkSubmitTo;
1967
+ if (!assignmentId || !turkSubmitTo)
1968
+ return;
1969
+ const form = document.createElement("form");
1970
+ form.method = "POST";
1971
+ form.action = turkSubmitTo + "/mturk/externalSubmit?assignmentId=" + assignmentId;
1972
+ for (const key in data) {
1973
+ if (data.hasOwnProperty(key)) {
1974
+ const hiddenField = document.createElement("input");
1975
+ hiddenField.type = "hidden";
1976
+ hiddenField.name = key;
1977
+ hiddenField.id = key;
1978
+ hiddenField.value = data[key];
1979
+ form.appendChild(hiddenField);
1980
+ }
1981
+ }
1982
+ document.body.appendChild(form);
1983
+ form.submit();
1984
+ }
1985
+
1986
+ var turk = /*#__PURE__*/Object.freeze({
1987
+ __proto__: null,
1988
+ turkInfo: turkInfo,
1989
+ submitToTurk: submitToTurk
1990
+ });
1991
+
1992
+ class TimelineNode {
1993
+ // constructor
1994
+ constructor(jsPsych, parameters, parent, relativeID) {
1995
+ this.jsPsych = jsPsych;
1996
+ // track progress through the node
1997
+ this.progress = {
1998
+ current_location: -1,
1999
+ current_variable_set: 0,
2000
+ current_repetition: 0,
2001
+ current_iteration: 0,
2002
+ done: false,
2003
+ };
2004
+ // store a link to the parent of this node
2005
+ this.parent_node = parent;
2006
+ // create the ID for this node
2007
+ this.relative_id = typeof parent === "undefined" ? 0 : relativeID;
2008
+ // check if there is a timeline parameter
2009
+ // if there is, then this node has its own timeline
2010
+ if (typeof parameters.timeline !== "undefined") {
2011
+ // create timeline properties
2012
+ this.timeline_parameters = {
2013
+ timeline: [],
2014
+ loop_function: parameters.loop_function,
2015
+ conditional_function: parameters.conditional_function,
2016
+ sample: parameters.sample,
2017
+ randomize_order: typeof parameters.randomize_order == "undefined" ? false : parameters.randomize_order,
2018
+ repetitions: typeof parameters.repetitions == "undefined" ? 1 : parameters.repetitions,
2019
+ timeline_variables: typeof parameters.timeline_variables == "undefined"
2020
+ ? [{}]
2021
+ : parameters.timeline_variables,
2022
+ on_timeline_finish: parameters.on_timeline_finish,
2023
+ on_timeline_start: parameters.on_timeline_start,
2024
+ };
2025
+ this.setTimelineVariablesOrder();
2026
+ // extract all of the node level data and parameters
2027
+ // but remove all of the timeline-level specific information
2028
+ // since this will be used to copy things down hierarchically
2029
+ var node_data = Object.assign({}, parameters);
2030
+ delete node_data.timeline;
2031
+ delete node_data.conditional_function;
2032
+ delete node_data.loop_function;
2033
+ delete node_data.randomize_order;
2034
+ delete node_data.repetitions;
2035
+ delete node_data.timeline_variables;
2036
+ delete node_data.sample;
2037
+ delete node_data.on_timeline_start;
2038
+ delete node_data.on_timeline_finish;
2039
+ this.node_trial_data = node_data; // store for later...
2040
+ // create a TimelineNode for each element in the timeline
2041
+ for (var i = 0; i < parameters.timeline.length; i++) {
2042
+ // merge parameters
2043
+ var merged_parameters = Object.assign({}, node_data, parameters.timeline[i]);
2044
+ // merge any data from the parent node into child nodes
2045
+ if (typeof node_data.data == "object" && typeof parameters.timeline[i].data == "object") {
2046
+ var merged_data = Object.assign({}, node_data.data, parameters.timeline[i].data);
2047
+ merged_parameters.data = merged_data;
2048
+ }
2049
+ this.timeline_parameters.timeline.push(new TimelineNode(this.jsPsych, merged_parameters, this, i));
2050
+ }
2051
+ }
2052
+ // if there is no timeline parameter, then this node is a trial node
2053
+ else {
2054
+ // check to see if a valid trial type is defined
2055
+ if (typeof parameters.type === "undefined") {
2056
+ console.error('Trial level node is missing the "type" parameter. The parameters for the node are: ' +
2057
+ JSON.stringify(parameters));
2058
+ }
2059
+ // create a deep copy of the parameters for the trial
2060
+ this.trial_parameters = Object.assign({}, parameters);
2061
+ }
2062
+ }
2063
+ // recursively get the next trial to run.
2064
+ // if this node is a leaf (trial), then return the trial.
2065
+ // otherwise, recursively find the next trial in the child timeline.
2066
+ trial() {
2067
+ if (typeof this.timeline_parameters == "undefined") {
2068
+ // returns a clone of the trial_parameters to
2069
+ // protect functions.
2070
+ return deepCopy(this.trial_parameters);
2071
+ }
2072
+ else {
2073
+ if (this.progress.current_location >= this.timeline_parameters.timeline.length) {
2074
+ return null;
2075
+ }
2076
+ else {
2077
+ return this.timeline_parameters.timeline[this.progress.current_location].trial();
2078
+ }
2079
+ }
2080
+ }
2081
+ markCurrentTrialComplete() {
2082
+ if (typeof this.timeline_parameters === "undefined") {
2083
+ this.progress.done = true;
2084
+ }
2085
+ else {
2086
+ this.timeline_parameters.timeline[this.progress.current_location].markCurrentTrialComplete();
2087
+ }
2088
+ }
2089
+ nextRepetiton() {
2090
+ this.setTimelineVariablesOrder();
2091
+ this.progress.current_location = -1;
2092
+ this.progress.current_variable_set = 0;
2093
+ this.progress.current_repetition++;
2094
+ for (var i = 0; i < this.timeline_parameters.timeline.length; i++) {
2095
+ this.timeline_parameters.timeline[i].reset();
2096
+ }
2097
+ }
2098
+ // set the order for going through the timeline variables array
2099
+ setTimelineVariablesOrder() {
2100
+ const timeline_parameters = this.timeline_parameters;
2101
+ // check to make sure this node has variables
2102
+ if (typeof timeline_parameters === "undefined" ||
2103
+ typeof timeline_parameters.timeline_variables === "undefined") {
2104
+ return;
2105
+ }
2106
+ var order = [];
2107
+ for (var i = 0; i < timeline_parameters.timeline_variables.length; i++) {
2108
+ order.push(i);
2109
+ }
2110
+ if (typeof timeline_parameters.sample !== "undefined") {
2111
+ if (timeline_parameters.sample.type == "custom") {
2112
+ order = timeline_parameters.sample.fn(order);
2113
+ }
2114
+ else if (timeline_parameters.sample.type == "with-replacement") {
2115
+ order = sampleWithReplacement(order, timeline_parameters.sample.size, timeline_parameters.sample.weights);
2116
+ }
2117
+ else if (timeline_parameters.sample.type == "without-replacement") {
2118
+ order = sampleWithoutReplacement(order, timeline_parameters.sample.size);
2119
+ }
2120
+ else if (timeline_parameters.sample.type == "fixed-repetitions") {
2121
+ order = repeat(order, timeline_parameters.sample.size, false);
2122
+ }
2123
+ else if (timeline_parameters.sample.type == "alternate-groups") {
2124
+ order = shuffleAlternateGroups(timeline_parameters.sample.groups, timeline_parameters.sample.randomize_group_order);
2125
+ }
2126
+ else {
2127
+ console.error('Invalid type in timeline sample parameters. Valid options for type are "custom", "with-replacement", "without-replacement", "fixed-repetitions", and "alternate-groups"');
2128
+ }
2129
+ }
2130
+ if (timeline_parameters.randomize_order) {
2131
+ order = shuffle(order);
2132
+ }
2133
+ this.progress.order = order;
2134
+ }
2135
+ // next variable set
2136
+ nextSet() {
2137
+ this.progress.current_location = -1;
2138
+ this.progress.current_variable_set++;
2139
+ for (var i = 0; i < this.timeline_parameters.timeline.length; i++) {
2140
+ this.timeline_parameters.timeline[i].reset();
2141
+ }
2142
+ }
2143
+ // update the current trial node to be completed
2144
+ // returns true if the node is complete after advance (all subnodes are also complete)
2145
+ // returns false otherwise
2146
+ advance() {
2147
+ const progress = this.progress;
2148
+ const timeline_parameters = this.timeline_parameters;
2149
+ const internal = this.jsPsych.internal;
2150
+ // first check to see if done
2151
+ if (progress.done) {
2152
+ return true;
2153
+ }
2154
+ // if node has not started yet (progress.current_location == -1),
2155
+ // then try to start the node.
2156
+ if (progress.current_location == -1) {
2157
+ // check for on_timeline_start and conditonal function on nodes with timelines
2158
+ if (typeof timeline_parameters !== "undefined") {
2159
+ // only run the conditional function if this is the first repetition of the timeline when
2160
+ // repetitions > 1, and only when on the first variable set
2161
+ if (typeof timeline_parameters.conditional_function !== "undefined" &&
2162
+ progress.current_repetition == 0 &&
2163
+ progress.current_variable_set == 0) {
2164
+ internal.call_immediate = true;
2165
+ var conditional_result = timeline_parameters.conditional_function();
2166
+ internal.call_immediate = false;
2167
+ // if the conditional_function() returns false, then the timeline
2168
+ // doesn't run and is marked as complete.
2169
+ if (conditional_result == false) {
2170
+ progress.done = true;
2171
+ return true;
2172
+ }
2173
+ }
2174
+ // if we reach this point then the node has its own timeline and will start
2175
+ // so we need to check if there is an on_timeline_start function if we are on the first variable set
2176
+ if (typeof timeline_parameters.on_timeline_start !== "undefined" &&
2177
+ progress.current_variable_set == 0) {
2178
+ timeline_parameters.on_timeline_start();
2179
+ }
2180
+ }
2181
+ // if we reach this point, then either the node doesn't have a timeline of the
2182
+ // conditional function returned true and it can start
2183
+ progress.current_location = 0;
2184
+ // call advance again on this node now that it is pointing to a new location
2185
+ return this.advance();
2186
+ }
2187
+ // if this node has a timeline, propogate down to the current trial.
2188
+ if (typeof timeline_parameters !== "undefined") {
2189
+ var have_node_to_run = false;
2190
+ // keep incrementing the location in the timeline until one of the nodes reached is incomplete
2191
+ while (progress.current_location < timeline_parameters.timeline.length &&
2192
+ have_node_to_run == false) {
2193
+ // check to see if the node currently pointed at is done
2194
+ var target_complete = timeline_parameters.timeline[progress.current_location].advance();
2195
+ if (!target_complete) {
2196
+ have_node_to_run = true;
2197
+ return false;
2198
+ }
2199
+ else {
2200
+ progress.current_location++;
2201
+ }
2202
+ }
2203
+ // if we've reached the end of the timeline (which, if the code is here, we have)
2204
+ // there are a few steps to see what to do next...
2205
+ // first, check the timeline_variables to see if we need to loop through again
2206
+ // with a new set of variables
2207
+ if (progress.current_variable_set < progress.order.length - 1) {
2208
+ // reset the progress of the node to be with the new set
2209
+ this.nextSet();
2210
+ // then try to advance this node again.
2211
+ return this.advance();
2212
+ }
2213
+ // if we're all done with the timeline_variables, then check to see if there are more repetitions
2214
+ else if (progress.current_repetition < timeline_parameters.repetitions - 1) {
2215
+ this.nextRepetiton();
2216
+ // check to see if there is an on_timeline_finish function
2217
+ if (typeof timeline_parameters.on_timeline_finish !== "undefined") {
2218
+ timeline_parameters.on_timeline_finish();
2219
+ }
2220
+ return this.advance();
2221
+ }
2222
+ // if we're all done with the repetitions...
2223
+ else {
2224
+ // check to see if there is an on_timeline_finish function
2225
+ if (typeof timeline_parameters.on_timeline_finish !== "undefined") {
2226
+ timeline_parameters.on_timeline_finish();
2227
+ }
2228
+ // if we're all done with the repetitions, check if there is a loop function.
2229
+ if (typeof timeline_parameters.loop_function !== "undefined") {
2230
+ internal.call_immediate = true;
2231
+ if (timeline_parameters.loop_function(this.generatedData())) {
2232
+ this.reset();
2233
+ internal.call_immediate = false;
2234
+ return this.parent_node.advance();
2235
+ }
2236
+ else {
2237
+ progress.done = true;
2238
+ internal.call_immediate = false;
2239
+ return true;
2240
+ }
2241
+ }
2242
+ }
2243
+ // no more loops on this timeline, we're done!
2244
+ progress.done = true;
2245
+ return true;
2246
+ }
2247
+ }
2248
+ // check the status of the done flag
2249
+ isComplete() {
2250
+ return this.progress.done;
2251
+ }
2252
+ // getter method for timeline variables
2253
+ getTimelineVariableValue(variable_name) {
2254
+ if (typeof this.timeline_parameters == "undefined") {
2255
+ return undefined;
2256
+ }
2257
+ var v = this.timeline_parameters.timeline_variables[this.progress.order[this.progress.current_variable_set]][variable_name];
2258
+ return v;
2259
+ }
2260
+ // recursive upward search for timeline variables
2261
+ findTimelineVariable(variable_name) {
2262
+ var v = this.getTimelineVariableValue(variable_name);
2263
+ if (typeof v == "undefined") {
2264
+ if (typeof this.parent_node !== "undefined") {
2265
+ return this.parent_node.findTimelineVariable(variable_name);
2266
+ }
2267
+ else {
2268
+ return undefined;
2269
+ }
2270
+ }
2271
+ else {
2272
+ return v;
2273
+ }
2274
+ }
2275
+ // recursive downward search for active trial to extract timeline variable
2276
+ timelineVariable(variable_name) {
2277
+ if (typeof this.timeline_parameters == "undefined") {
2278
+ return this.findTimelineVariable(variable_name);
2279
+ }
2280
+ else {
2281
+ // if progress.current_location is -1, then the timeline variable is being evaluated
2282
+ // in a function that runs prior to the trial starting, so we should treat that trial
2283
+ // as being the active trial for purposes of finding the value of the timeline variable
2284
+ var loc = Math.max(0, this.progress.current_location);
2285
+ // if loc is greater than the number of elements on this timeline, then the timeline
2286
+ // variable is being evaluated in a function that runs after the trial on the timeline
2287
+ // are complete but before advancing to the next (like a loop_function).
2288
+ // treat the last active trial as the active trial for this purpose.
2289
+ if (loc == this.timeline_parameters.timeline.length) {
2290
+ loc = loc - 1;
2291
+ }
2292
+ // now find the variable
2293
+ return this.timeline_parameters.timeline[loc].timelineVariable(variable_name);
2294
+ }
2295
+ }
2296
+ // recursively get all the timeline variables for this trial
2297
+ allTimelineVariables() {
2298
+ var all_tvs = this.allTimelineVariablesNames();
2299
+ var all_tvs_vals = {};
2300
+ for (var i = 0; i < all_tvs.length; i++) {
2301
+ all_tvs_vals[all_tvs[i]] = this.timelineVariable(all_tvs[i]);
2302
+ }
2303
+ return all_tvs_vals;
2304
+ }
2305
+ // helper to get all the names at this stage.
2306
+ allTimelineVariablesNames(so_far = []) {
2307
+ if (typeof this.timeline_parameters !== "undefined") {
2308
+ so_far = so_far.concat(Object.keys(this.timeline_parameters.timeline_variables[this.progress.order[this.progress.current_variable_set]]));
2309
+ // if progress.current_location is -1, then the timeline variable is being evaluated
2310
+ // in a function that runs prior to the trial starting, so we should treat that trial
2311
+ // as being the active trial for purposes of finding the value of the timeline variable
2312
+ var loc = Math.max(0, this.progress.current_location);
2313
+ // if loc is greater than the number of elements on this timeline, then the timeline
2314
+ // variable is being evaluated in a function that runs after the trial on the timeline
2315
+ // are complete but before advancing to the next (like a loop_function).
2316
+ // treat the last active trial as the active trial for this purpose.
2317
+ if (loc == this.timeline_parameters.timeline.length) {
2318
+ loc = loc - 1;
2319
+ }
2320
+ // now find the variable
2321
+ return this.timeline_parameters.timeline[loc].allTimelineVariablesNames(so_far);
2322
+ }
2323
+ if (typeof this.timeline_parameters == "undefined") {
2324
+ return so_far;
2325
+ }
2326
+ }
2327
+ // recursively get the number of **trials** contained in the timeline
2328
+ // assuming that while loops execute exactly once and if conditionals
2329
+ // always run
2330
+ length() {
2331
+ var length = 0;
2332
+ if (typeof this.timeline_parameters !== "undefined") {
2333
+ for (var i = 0; i < this.timeline_parameters.timeline.length; i++) {
2334
+ length += this.timeline_parameters.timeline[i].length();
2335
+ }
2336
+ }
2337
+ else {
2338
+ return 1;
2339
+ }
2340
+ return length;
2341
+ }
2342
+ // return the percentage of trials completed, grouped at the first child level
2343
+ // counts a set of trials as complete when the child node is done
2344
+ percentComplete() {
2345
+ var total_trials = this.length();
2346
+ var completed_trials = 0;
2347
+ for (var i = 0; i < this.timeline_parameters.timeline.length; i++) {
2348
+ if (this.timeline_parameters.timeline[i].isComplete()) {
2349
+ completed_trials += this.timeline_parameters.timeline[i].length();
2350
+ }
2351
+ }
2352
+ return (completed_trials / total_trials) * 100;
2353
+ }
2354
+ // resets the node and all subnodes to original state
2355
+ // but increments the current_iteration counter
2356
+ reset() {
2357
+ this.progress.current_location = -1;
2358
+ this.progress.current_repetition = 0;
2359
+ this.progress.current_variable_set = 0;
2360
+ this.progress.current_iteration++;
2361
+ this.progress.done = false;
2362
+ this.setTimelineVariablesOrder();
2363
+ if (typeof this.timeline_parameters != "undefined") {
2364
+ for (var i = 0; i < this.timeline_parameters.timeline.length; i++) {
2365
+ this.timeline_parameters.timeline[i].reset();
2366
+ }
2367
+ }
2368
+ }
2369
+ // mark this node as finished
2370
+ end() {
2371
+ this.progress.done = true;
2372
+ }
2373
+ // recursively end whatever sub-node is running the current trial
2374
+ endActiveNode() {
2375
+ if (typeof this.timeline_parameters == "undefined") {
2376
+ this.end();
2377
+ this.parent_node.end();
2378
+ }
2379
+ else {
2380
+ this.timeline_parameters.timeline[this.progress.current_location].endActiveNode();
2381
+ }
2382
+ }
2383
+ // get a unique ID associated with this node
2384
+ // the ID reflects the current iteration through this node.
2385
+ ID() {
2386
+ var id = "";
2387
+ if (typeof this.parent_node == "undefined") {
2388
+ return "0." + this.progress.current_iteration;
2389
+ }
2390
+ else {
2391
+ id += this.parent_node.ID() + "-";
2392
+ id += this.relative_id + "." + this.progress.current_iteration;
2393
+ return id;
2394
+ }
2395
+ }
2396
+ // get the ID of the active trial
2397
+ activeID() {
2398
+ if (typeof this.timeline_parameters == "undefined") {
2399
+ return this.ID();
2400
+ }
2401
+ else {
2402
+ return this.timeline_parameters.timeline[this.progress.current_location].activeID();
2403
+ }
2404
+ }
2405
+ // get all the data generated within this node
2406
+ generatedData() {
2407
+ return this.jsPsych.data.getDataByTimelineNode(this.ID());
2408
+ }
2409
+ // get all the trials of a particular type
2410
+ trialsOfType(type) {
2411
+ if (typeof this.timeline_parameters == "undefined") {
2412
+ if (this.trial_parameters.type == type) {
2413
+ return this.trial_parameters;
2414
+ }
2415
+ else {
2416
+ return [];
2417
+ }
2418
+ }
2419
+ else {
2420
+ var trials = [];
2421
+ for (var i = 0; i < this.timeline_parameters.timeline.length; i++) {
2422
+ var t = this.timeline_parameters.timeline[i].trialsOfType(type);
2423
+ trials = trials.concat(t);
2424
+ }
2425
+ return trials;
2426
+ }
2427
+ }
2428
+ // add new trials to end of this timeline
2429
+ insert(parameters) {
2430
+ if (typeof this.timeline_parameters === "undefined") {
2431
+ console.error("Cannot add new trials to a trial-level node.");
2432
+ }
2433
+ else {
2434
+ this.timeline_parameters.timeline.push(new TimelineNode(this.jsPsych, Object.assign(Object.assign({}, this.node_trial_data), parameters), this, this.timeline_parameters.timeline.length));
2435
+ }
2436
+ }
2437
+ }
2438
+
2439
+ function delay(ms) {
2440
+ return new Promise((resolve) => setTimeout(resolve, ms));
2441
+ }
2442
+ class JsPsych {
2443
+ constructor(options) {
2444
+ this.extensions = {};
2445
+ this.turk = turk;
2446
+ this.randomization = randomization;
2447
+ this.utils = utils;
2448
+ //
2449
+ // private variables
2450
+ //
2451
+ /**
2452
+ * options
2453
+ */
2454
+ this.opts = {};
2455
+ // flow control
2456
+ this.global_trial_index = 0;
2457
+ this.current_trial = {};
2458
+ this.current_trial_finished = false;
2459
+ /**
2460
+ * is the experiment paused?
2461
+ */
2462
+ this.paused = false;
2463
+ this.waiting = false;
2464
+ /**
2465
+ * is the page retrieved directly via file:// protocol (true) or hosted on a server (false)?
2466
+ */
2467
+ this.file_protocol = false;
2468
+ /**
2469
+ * is the experiment running in `simulate()` mode
2470
+ */
2471
+ this.simulation_mode = null;
2472
+ // storing a single webaudio context to prevent problems with multiple inits
2473
+ // of jsPsych
2474
+ this.webaudio_context = null;
2475
+ this.internal = {
2476
+ /**
2477
+ * this flag is used to determine whether we are in a scope where
2478
+ * jsPsych.timelineVariable() should be executed immediately or
2479
+ * whether it should return a function to access the variable later.
2480
+ *
2481
+ **/
2482
+ call_immediate: false,
2483
+ };
2484
+ this.progress_bar_amount = 0;
2485
+ // override default options if user specifies an option
2486
+ options = Object.assign({ display_element: undefined, on_finish: () => { }, on_trial_start: () => { }, on_trial_finish: () => { }, on_data_update: () => { }, on_interaction_data_update: () => { }, on_close: () => { }, use_webaudio: true, exclusions: {}, show_progress_bar: false, message_progress_bar: "Completion Progress", auto_update_progress_bar: true, default_iti: 0, minimum_valid_rt: 0, experiment_width: null, override_safe_mode: false, case_sensitive_responses: false, extensions: [] }, options);
2487
+ this.opts = options;
2488
+ autoBind(this); // so we can pass JsPsych methods as callbacks and `this` remains the JsPsych instance
2489
+ this.webaudio_context =
2490
+ typeof window !== "undefined" && typeof window.AudioContext !== "undefined"
2491
+ ? new AudioContext()
2492
+ : null;
2493
+ // detect whether page is running in browser as a local file, and if so, disable web audio and video preloading to prevent CORS issues
2494
+ if (window.location.protocol == "file:" &&
2495
+ (options.override_safe_mode === false || typeof options.override_safe_mode === "undefined")) {
2496
+ options.use_webaudio = false;
2497
+ this.file_protocol = true;
2498
+ console.warn("jsPsych detected that it is running via the file:// protocol and not on a web server. " +
2499
+ "To prevent issues with cross-origin requests, Web Audio and video preloading have been disabled. " +
2500
+ "If you would like to override this setting, you can set 'override_safe_mode' to 'true' in initJsPsych. " +
2501
+ "For more information, see: https://www.jspsych.org/overview/running-experiments");
2502
+ }
2503
+ // initialize modules
2504
+ this.data = new JsPsychData(this);
2505
+ this.pluginAPI = createJointPluginAPIObject(this);
2506
+ // create instances of extensions
2507
+ for (const extension of options.extensions) {
2508
+ this.extensions[extension.type.info.name] = new extension.type(this);
2509
+ }
2510
+ // initialize audio context based on options and browser capabilities
2511
+ this.pluginAPI.initAudio();
2512
+ }
2513
+ version() {
2514
+ return version;
2515
+ }
2516
+ /**
2517
+ * Starts an experiment using the provided timeline and returns a promise that is resolved when
2518
+ * the experiment is finished.
2519
+ *
2520
+ * @param timeline The timeline to be run
2521
+ */
2522
+ run(timeline) {
2523
+ return __awaiter(this, void 0, void 0, function* () {
2524
+ if (typeof timeline === "undefined") {
2525
+ console.error("No timeline declared in jsPsych.run. Cannot start experiment.");
2526
+ }
2527
+ if (timeline.length === 0) {
2528
+ console.error("No trials have been added to the timeline (the timeline is an empty array). Cannot start experiment.");
2529
+ }
2530
+ // create experiment timeline
2531
+ this.timelineDescription = timeline;
2532
+ this.timeline = new TimelineNode(this, { timeline });
2533
+ yield this.prepareDom();
2534
+ yield this.checkExclusions(this.opts.exclusions);
2535
+ yield this.loadExtensions(this.opts.extensions);
2536
+ document.documentElement.setAttribute("jspsych", "present");
2537
+ this.startExperiment();
2538
+ yield this.finished;
2539
+ });
2540
+ }
2541
+ simulate(timeline, simulation_mode = "data-only", simulation_options = {}) {
2542
+ return __awaiter(this, void 0, void 0, function* () {
2543
+ this.simulation_mode = simulation_mode;
2544
+ this.simulation_options = simulation_options;
2545
+ yield this.run(timeline);
2546
+ });
2547
+ }
2548
+ getProgress() {
2549
+ return {
2550
+ total_trials: typeof this.timeline === "undefined" ? undefined : this.timeline.length(),
2551
+ current_trial_global: this.global_trial_index,
2552
+ percent_complete: typeof this.timeline === "undefined" ? 0 : this.timeline.percentComplete(),
2553
+ };
2554
+ }
2555
+ getStartTime() {
2556
+ return this.exp_start_time;
2557
+ }
2558
+ getTotalTime() {
2559
+ if (typeof this.exp_start_time === "undefined") {
2560
+ return 0;
2561
+ }
2562
+ return new Date().getTime() - this.exp_start_time.getTime();
2563
+ }
2564
+ getDisplayElement() {
2565
+ return this.DOM_target;
2566
+ }
2567
+ getDisplayContainerElement() {
2568
+ return this.DOM_container;
2569
+ }
2570
+ finishTrial(data = {}) {
2571
+ if (this.current_trial_finished) {
2572
+ return;
2573
+ }
2574
+ this.current_trial_finished = true;
2575
+ // remove any CSS classes that were added to the DOM via css_classes parameter
2576
+ if (typeof this.current_trial.css_classes !== "undefined" &&
2577
+ Array.isArray(this.current_trial.css_classes)) {
2578
+ this.DOM_target.classList.remove(...this.current_trial.css_classes);
2579
+ }
2580
+ // write the data from the trial
2581
+ this.data.write(data);
2582
+ // get back the data with all of the defaults in
2583
+ const trial_data = this.data.get().filter({ trial_index: this.global_trial_index });
2584
+ // for trial-level callbacks, we just want to pass in a reference to the values
2585
+ // of the DataCollection, for easy access and editing.
2586
+ const trial_data_values = trial_data.values()[0];
2587
+ const current_trial = this.current_trial;
2588
+ if (typeof current_trial.save_trial_parameters === "object") {
2589
+ for (const key of Object.keys(current_trial.save_trial_parameters)) {
2590
+ const key_val = current_trial.save_trial_parameters[key];
2591
+ if (key_val === true) {
2592
+ if (typeof current_trial[key] === "undefined") {
2593
+ console.warn(`Invalid parameter specified in save_trial_parameters. Trial has no property called "${key}".`);
2594
+ }
2595
+ else if (typeof current_trial[key] === "function") {
2596
+ trial_data_values[key] = current_trial[key].toString();
2597
+ }
2598
+ else {
2599
+ trial_data_values[key] = current_trial[key];
2600
+ }
2601
+ }
2602
+ if (key_val === false) {
2603
+ // we don't allow internal_node_id or trial_index to be deleted because it would break other things
2604
+ if (key !== "internal_node_id" && key !== "trial_index") {
2605
+ delete trial_data_values[key];
2606
+ }
2607
+ }
2608
+ }
2609
+ }
2610
+ // handle extension callbacks
2611
+ if (Array.isArray(current_trial.extensions)) {
2612
+ for (const extension of current_trial.extensions) {
2613
+ const ext_data_values = this.extensions[extension.type.info.name].on_finish(extension.params);
2614
+ Object.assign(trial_data_values, ext_data_values);
2615
+ }
2616
+ }
2617
+ // about to execute lots of callbacks, so switch context.
2618
+ this.internal.call_immediate = true;
2619
+ // handle callback at plugin level
2620
+ if (typeof current_trial.on_finish === "function") {
2621
+ current_trial.on_finish(trial_data_values);
2622
+ }
2623
+ // handle callback at whole-experiment level
2624
+ this.opts.on_trial_finish(trial_data_values);
2625
+ // after the above callbacks are complete, then the data should be finalized
2626
+ // for this trial. call the on_data_update handler, passing in the same
2627
+ // data object that just went through the trial's finish handlers.
2628
+ this.opts.on_data_update(trial_data_values);
2629
+ // done with callbacks
2630
+ this.internal.call_immediate = false;
2631
+ // wait for iti
2632
+ if (typeof current_trial.post_trial_gap === null ||
2633
+ typeof current_trial.post_trial_gap === "undefined") {
2634
+ if (this.opts.default_iti > 0) {
2635
+ setTimeout(this.nextTrial, this.opts.default_iti);
2636
+ }
2637
+ else {
2638
+ this.nextTrial();
2639
+ }
2640
+ }
2641
+ else {
2642
+ if (current_trial.post_trial_gap > 0) {
2643
+ setTimeout(this.nextTrial, current_trial.post_trial_gap);
2644
+ }
2645
+ else {
2646
+ this.nextTrial();
2647
+ }
2648
+ }
2649
+ }
2650
+ endExperiment(end_message = "", data = {}) {
2651
+ this.timeline.end_message = end_message;
2652
+ this.timeline.end();
2653
+ this.pluginAPI.cancelAllKeyboardResponses();
2654
+ this.pluginAPI.clearAllTimeouts();
2655
+ this.finishTrial(data);
2656
+ }
2657
+ endCurrentTimeline() {
2658
+ this.timeline.endActiveNode();
2659
+ }
2660
+ getCurrentTrial() {
2661
+ return this.current_trial;
2662
+ }
2663
+ getInitSettings() {
2664
+ return this.opts;
2665
+ }
2666
+ getCurrentTimelineNodeID() {
2667
+ return this.timeline.activeID();
2668
+ }
2669
+ timelineVariable(varname, immediate = false) {
2670
+ if (this.internal.call_immediate || immediate === true) {
2671
+ return this.timeline.timelineVariable(varname);
2672
+ }
2673
+ else {
2674
+ return {
2675
+ timelineVariablePlaceholder: true,
2676
+ timelineVariableFunction: () => this.timeline.timelineVariable(varname),
2677
+ };
2678
+ }
2679
+ }
2680
+ getAllTimelineVariables() {
2681
+ return this.timeline.allTimelineVariables();
2682
+ }
2683
+ addNodeToEndOfTimeline(new_timeline, preload_callback) {
2684
+ this.timeline.insert(new_timeline);
2685
+ }
2686
+ pauseExperiment() {
2687
+ this.paused = true;
2688
+ }
2689
+ resumeExperiment() {
2690
+ this.paused = false;
2691
+ if (this.waiting) {
2692
+ this.waiting = false;
2693
+ this.nextTrial();
2694
+ }
2695
+ }
2696
+ loadFail(message) {
2697
+ message = message || "<p>The experiment failed to load.</p>";
2698
+ this.DOM_target.innerHTML = message;
2699
+ }
2700
+ getSafeModeStatus() {
2701
+ return this.file_protocol;
2702
+ }
2703
+ getTimeline() {
2704
+ return this.timelineDescription;
2705
+ }
2706
+ prepareDom() {
2707
+ return __awaiter(this, void 0, void 0, function* () {
2708
+ // Wait until the document is ready
2709
+ if (document.readyState !== "complete") {
2710
+ yield new Promise((resolve) => {
2711
+ window.addEventListener("load", resolve);
2712
+ });
2713
+ }
2714
+ const options = this.opts;
2715
+ // set DOM element where jsPsych will render content
2716
+ // if undefined, then jsPsych will use the <body> tag and the entire page
2717
+ if (typeof options.display_element === "undefined") {
2718
+ // check if there is a body element on the page
2719
+ const body = document.querySelector("body");
2720
+ if (body === null) {
2721
+ document.documentElement.appendChild(document.createElement("body"));
2722
+ }
2723
+ // using the full page, so we need the HTML element to
2724
+ // have 100% height, and body to be full width and height with
2725
+ // no margin
2726
+ document.querySelector("html").style.height = "100%";
2727
+ document.querySelector("body").style.margin = "0px";
2728
+ document.querySelector("body").style.height = "100%";
2729
+ document.querySelector("body").style.width = "100%";
2730
+ options.display_element = document.querySelector("body");
2731
+ }
2732
+ else {
2733
+ // make sure that the display element exists on the page
2734
+ const display = options.display_element instanceof Element
2735
+ ? options.display_element
2736
+ : document.querySelector("#" + options.display_element);
2737
+ if (display === null) {
2738
+ console.error("The display_element specified in initJsPsych() does not exist in the DOM.");
2739
+ }
2740
+ else {
2741
+ options.display_element = display;
2742
+ }
2743
+ }
2744
+ options.display_element.innerHTML =
2745
+ '<div class="jspsych-content-wrapper"><div id="jspsych-content"></div></div>';
2746
+ this.DOM_container = options.display_element;
2747
+ this.DOM_target = document.querySelector("#jspsych-content");
2748
+ // set experiment_width if not null
2749
+ if (options.experiment_width !== null) {
2750
+ this.DOM_target.style.width = options.experiment_width + "px";
2751
+ }
2752
+ // add tabIndex attribute to scope event listeners
2753
+ options.display_element.tabIndex = 0;
2754
+ // add CSS class to DOM_target
2755
+ if (options.display_element.className.indexOf("jspsych-display-element") === -1) {
2756
+ options.display_element.className += " jspsych-display-element";
2757
+ }
2758
+ this.DOM_target.className += "jspsych-content";
2759
+ // create listeners for user browser interaction
2760
+ this.data.createInteractionListeners();
2761
+ // add event for closing window
2762
+ window.addEventListener("beforeunload", options.on_close);
2763
+ });
2764
+ }
2765
+ loadExtensions(extensions) {
2766
+ return __awaiter(this, void 0, void 0, function* () {
2767
+ // run the .initialize method of any extensions that are in use
2768
+ // these should return a Promise to indicate when loading is complete
2769
+ try {
2770
+ yield Promise.all(extensions.map((extension) => this.extensions[extension.type.info.name].initialize(extension.params || {})));
2771
+ }
2772
+ catch (error_message) {
2773
+ console.error(error_message);
2774
+ throw new Error(error_message);
2775
+ }
2776
+ });
2777
+ }
2778
+ startExperiment() {
2779
+ this.finished = new Promise((resolve) => {
2780
+ this.resolveFinishedPromise = resolve;
2781
+ });
2782
+ // show progress bar if requested
2783
+ if (this.opts.show_progress_bar === true) {
2784
+ this.drawProgressBar(this.opts.message_progress_bar);
2785
+ }
2786
+ // record the start time
2787
+ this.exp_start_time = new Date();
2788
+ // begin!
2789
+ this.timeline.advance();
2790
+ this.doTrial(this.timeline.trial());
2791
+ }
2792
+ finishExperiment() {
2793
+ const finish_result = this.opts.on_finish(this.data.get());
2794
+ const done_handler = () => {
2795
+ if (typeof this.timeline.end_message !== "undefined") {
2796
+ this.DOM_target.innerHTML = this.timeline.end_message;
2797
+ }
2798
+ this.resolveFinishedPromise();
2799
+ };
2800
+ if (finish_result) {
2801
+ Promise.resolve(finish_result).then(done_handler);
2802
+ }
2803
+ else {
2804
+ done_handler();
2805
+ }
2806
+ }
2807
+ nextTrial() {
2808
+ // if experiment is paused, don't do anything.
2809
+ if (this.paused) {
2810
+ this.waiting = true;
2811
+ return;
2812
+ }
2813
+ this.global_trial_index++;
2814
+ // advance timeline
2815
+ this.timeline.markCurrentTrialComplete();
2816
+ const complete = this.timeline.advance();
2817
+ // update progress bar if shown
2818
+ if (this.opts.show_progress_bar === true && this.opts.auto_update_progress_bar === true) {
2819
+ this.updateProgressBar();
2820
+ }
2821
+ // check if experiment is over
2822
+ if (complete) {
2823
+ this.finishExperiment();
2824
+ return;
2825
+ }
2826
+ this.doTrial(this.timeline.trial());
2827
+ }
2828
+ doTrial(trial) {
2829
+ this.current_trial = trial;
2830
+ this.current_trial_finished = false;
2831
+ // process all timeline variables for this trial
2832
+ this.evaluateTimelineVariables(trial);
2833
+ if (typeof trial.type === "string") {
2834
+ throw new MigrationError("A string was provided as the trial's `type` parameter. Since jsPsych v7, the `type` parameter needs to be a plugin object.");
2835
+ }
2836
+ // instantiate the plugin for this trial
2837
+ trial.type = Object.assign(Object.assign({}, autoBind(new trial.type(this))), { info: trial.type.info });
2838
+ // evaluate variables that are functions
2839
+ this.evaluateFunctionParameters(trial);
2840
+ // get default values for parameters
2841
+ this.setDefaultValues(trial);
2842
+ // about to execute callbacks
2843
+ this.internal.call_immediate = true;
2844
+ // call experiment wide callback
2845
+ this.opts.on_trial_start(trial);
2846
+ // call trial specific callback if it exists
2847
+ if (typeof trial.on_start === "function") {
2848
+ trial.on_start(trial);
2849
+ }
2850
+ // call any on_start functions for extensions
2851
+ if (Array.isArray(trial.extensions)) {
2852
+ for (const extension of trial.extensions) {
2853
+ this.extensions[extension.type.info.name].on_start(extension.params);
2854
+ }
2855
+ }
2856
+ // apply the focus to the element containing the experiment.
2857
+ this.DOM_container.focus();
2858
+ // reset the scroll on the DOM target
2859
+ this.DOM_target.scrollTop = 0;
2860
+ // add CSS classes to the DOM_target if they exist in trial.css_classes
2861
+ if (typeof trial.css_classes !== "undefined") {
2862
+ if (!Array.isArray(trial.css_classes) && typeof trial.css_classes === "string") {
2863
+ trial.css_classes = [trial.css_classes];
2864
+ }
2865
+ if (Array.isArray(trial.css_classes)) {
2866
+ this.DOM_target.classList.add(...trial.css_classes);
2867
+ }
2868
+ }
2869
+ // setup on_load event callback
2870
+ const load_callback = () => {
2871
+ if (typeof trial.on_load === "function") {
2872
+ trial.on_load();
2873
+ }
2874
+ // call any on_load functions for extensions
2875
+ if (Array.isArray(trial.extensions)) {
2876
+ for (const extension of trial.extensions) {
2877
+ this.extensions[extension.type.info.name].on_load(extension.params);
2878
+ }
2879
+ }
2880
+ };
2881
+ let trial_complete;
2882
+ if (!this.simulation_mode) {
2883
+ trial_complete = trial.type.trial(this.DOM_target, trial, load_callback);
2884
+ }
2885
+ if (this.simulation_mode) {
2886
+ // check if the trial supports simulation
2887
+ if (trial.type.simulate) {
2888
+ let trial_sim_opts;
2889
+ if (!trial.simulation_options) {
2890
+ trial_sim_opts = this.simulation_options.default;
2891
+ }
2892
+ if (trial.simulation_options) {
2893
+ if (typeof trial.simulation_options == "string") {
2894
+ if (this.simulation_options[trial.simulation_options]) {
2895
+ trial_sim_opts = this.simulation_options[trial.simulation_options];
2896
+ }
2897
+ else if (this.simulation_options.default) {
2898
+ console.log(`No matching simulation options found for "${trial.simulation_options}". Using "default" options.`);
2899
+ trial_sim_opts = this.simulation_options.default;
2900
+ }
2901
+ else {
2902
+ console.log(`No matching simulation options found for "${trial.simulation_options}" and no "default" options provided. Using the default values provided by the plugin.`);
2903
+ trial_sim_opts = {};
2904
+ }
2905
+ }
2906
+ else {
2907
+ trial_sim_opts = trial.simulation_options;
2908
+ }
2909
+ }
2910
+ trial_sim_opts = this.utils.deepCopy(trial_sim_opts);
2911
+ trial_sim_opts = this.replaceFunctionsWithValues(trial_sim_opts, null);
2912
+ if ((trial_sim_opts === null || trial_sim_opts === void 0 ? void 0 : trial_sim_opts.simulate) === false) {
2913
+ trial_complete = trial.type.trial(this.DOM_target, trial, load_callback);
2914
+ }
2915
+ else {
2916
+ trial_complete = trial.type.simulate(trial, (trial_sim_opts === null || trial_sim_opts === void 0 ? void 0 : trial_sim_opts.mode) || this.simulation_mode, trial_sim_opts, load_callback);
2917
+ }
2918
+ }
2919
+ else {
2920
+ // trial doesn't have a simulate method, so just run as usual
2921
+ trial_complete = trial.type.trial(this.DOM_target, trial, load_callback);
2922
+ }
2923
+ }
2924
+ // see if trial_complete is a Promise by looking for .then() function
2925
+ const is_promise = trial_complete && typeof trial_complete.then == "function";
2926
+ // in simulation mode we let the simulate function call the load_callback always.
2927
+ if (!is_promise && !this.simulation_mode) {
2928
+ load_callback();
2929
+ }
2930
+ // done with callbacks
2931
+ this.internal.call_immediate = false;
2932
+ }
2933
+ evaluateTimelineVariables(trial) {
2934
+ for (const key of Object.keys(trial)) {
2935
+ // timeline variables on the root level
2936
+ if (typeof trial[key] === "object" &&
2937
+ trial[key] !== null &&
2938
+ typeof trial[key].timelineVariablePlaceholder !== "undefined") {
2939
+ /*trial[key].toString().replace(/\s/g, "") ==
2940
+ "function(){returntimeline.timelineVariable(varname);}"
2941
+ )*/ trial[key] = trial[key].timelineVariableFunction();
2942
+ }
2943
+ // timeline variables that are nested in objects
2944
+ if (typeof trial[key] === "object" && trial[key] !== null) {
2945
+ this.evaluateTimelineVariables(trial[key]);
2946
+ }
2947
+ }
2948
+ }
2949
+ evaluateFunctionParameters(trial) {
2950
+ // set a flag so that jsPsych.timelineVariable() is immediately executed in this context
2951
+ this.internal.call_immediate = true;
2952
+ // iterate over each parameter
2953
+ for (const key of Object.keys(trial)) {
2954
+ // check to make sure parameter is not "type", since that was eval'd above.
2955
+ if (key !== "type") {
2956
+ // this if statement is checking to see if the parameter type is expected to be a function, in which case we should NOT evaluate it.
2957
+ // the first line checks if the parameter is defined in the universalPluginParameters set
2958
+ // the second line checks the plugin-specific parameters
2959
+ if (typeof universalPluginParameters[key] !== "undefined" &&
2960
+ universalPluginParameters[key].type !== exports.ParameterType.FUNCTION) {
2961
+ trial[key] = this.replaceFunctionsWithValues(trial[key], null);
2962
+ }
2963
+ if (typeof trial.type.info.parameters[key] !== "undefined" &&
2964
+ trial.type.info.parameters[key].type !== exports.ParameterType.FUNCTION) {
2965
+ trial[key] = this.replaceFunctionsWithValues(trial[key], trial.type.info.parameters[key]);
2966
+ }
2967
+ }
2968
+ }
2969
+ // reset so jsPsych.timelineVariable() is no longer immediately executed
2970
+ this.internal.call_immediate = false;
2971
+ }
2972
+ replaceFunctionsWithValues(obj, info) {
2973
+ // null typeof is 'object' (?!?!), so need to run this first!
2974
+ if (obj === null) {
2975
+ return obj;
2976
+ }
2977
+ // arrays
2978
+ else if (Array.isArray(obj)) {
2979
+ for (let i = 0; i < obj.length; i++) {
2980
+ obj[i] = this.replaceFunctionsWithValues(obj[i], info);
2981
+ }
2982
+ }
2983
+ // objects
2984
+ else if (typeof obj === "object") {
2985
+ if (info === null || !info.nested) {
2986
+ for (const key of Object.keys(obj)) {
2987
+ if (key === "type") {
2988
+ // Ignore the object's `type` field because it contains a plugin and we do not want to
2989
+ // call plugin functions
2990
+ continue;
2991
+ }
2992
+ obj[key] = this.replaceFunctionsWithValues(obj[key], null);
2993
+ }
2994
+ }
2995
+ else {
2996
+ for (const key of Object.keys(obj)) {
2997
+ if (typeof info.nested[key] === "object" &&
2998
+ info.nested[key].type !== exports.ParameterType.FUNCTION) {
2999
+ obj[key] = this.replaceFunctionsWithValues(obj[key], info.nested[key]);
3000
+ }
3001
+ }
3002
+ }
3003
+ }
3004
+ else if (typeof obj === "function") {
3005
+ return obj();
3006
+ }
3007
+ return obj;
3008
+ }
3009
+ setDefaultValues(trial) {
3010
+ for (const param in trial.type.info.parameters) {
3011
+ // check if parameter is complex with nested defaults
3012
+ if (trial.type.info.parameters[param].type === exports.ParameterType.COMPLEX) {
3013
+ if (trial.type.info.parameters[param].array === true) {
3014
+ // iterate over each entry in the array
3015
+ trial[param].forEach(function (ip, i) {
3016
+ // check each parameter in the plugin description
3017
+ for (const p in trial.type.info.parameters[param].nested) {
3018
+ if (typeof trial[param][i][p] === "undefined" || trial[param][i][p] === null) {
3019
+ if (typeof trial.type.info.parameters[param].nested[p].default === "undefined") {
3020
+ console.error("You must specify a value for the " +
3021
+ p +
3022
+ " parameter (nested in the " +
3023
+ param +
3024
+ " parameter) in the " +
3025
+ trial.type +
3026
+ " plugin.");
3027
+ }
3028
+ else {
3029
+ trial[param][i][p] = trial.type.info.parameters[param].nested[p].default;
3030
+ }
3031
+ }
3032
+ }
3033
+ });
3034
+ }
3035
+ }
3036
+ // if it's not nested, checking is much easier and do that here:
3037
+ else if (typeof trial[param] === "undefined" || trial[param] === null) {
3038
+ if (typeof trial.type.info.parameters[param].default === "undefined") {
3039
+ console.error("You must specify a value for the " +
3040
+ param +
3041
+ " parameter in the " +
3042
+ trial.type.info.name +
3043
+ " plugin.");
3044
+ }
3045
+ else {
3046
+ trial[param] = trial.type.info.parameters[param].default;
3047
+ }
3048
+ }
3049
+ }
3050
+ }
3051
+ checkExclusions(exclusions) {
3052
+ return __awaiter(this, void 0, void 0, function* () {
3053
+ if (exclusions.min_width || exclusions.min_height || exclusions.audio) {
3054
+ console.warn("The exclusions option in `initJsPsych()` is deprecated and will be removed in a future version. We recommend using the browser-check plugin instead. See https://www.jspsych.org/latest/plugins/browser-check/.");
3055
+ }
3056
+ // MINIMUM SIZE
3057
+ if (exclusions.min_width || exclusions.min_height) {
3058
+ const mw = exclusions.min_width || 0;
3059
+ const mh = exclusions.min_height || 0;
3060
+ if (window.innerWidth < mw || window.innerHeight < mh) {
3061
+ this.getDisplayElement().innerHTML =
3062
+ "<p>Your browser window is too small to complete this experiment. " +
3063
+ "Please maximize the size of your browser window. If your browser window is already maximized, " +
3064
+ "you will not be able to complete this experiment.</p>" +
3065
+ "<p>The minimum width is " +
3066
+ mw +
3067
+ "px. Your current width is " +
3068
+ window.innerWidth +
3069
+ "px.</p>" +
3070
+ "<p>The minimum height is " +
3071
+ mh +
3072
+ "px. Your current height is " +
3073
+ window.innerHeight +
3074
+ "px.</p>";
3075
+ // Wait for window size to increase
3076
+ while (window.innerWidth < mw || window.innerHeight < mh) {
3077
+ yield delay(100);
3078
+ }
3079
+ this.getDisplayElement().innerHTML = "";
3080
+ }
3081
+ }
3082
+ // WEB AUDIO API
3083
+ if (typeof exclusions.audio !== "undefined" && exclusions.audio) {
3084
+ if (!window.hasOwnProperty("AudioContext") && !window.hasOwnProperty("webkitAudioContext")) {
3085
+ this.getDisplayElement().innerHTML =
3086
+ "<p>Your browser does not support the WebAudio API, which means that you will not " +
3087
+ "be able to complete the experiment.</p><p>Browsers that support the WebAudio API include " +
3088
+ "Chrome, Firefox, Safari, and Edge.</p>";
3089
+ throw new Error();
3090
+ }
3091
+ }
3092
+ });
3093
+ }
3094
+ drawProgressBar(msg) {
3095
+ document
3096
+ .querySelector(".jspsych-display-element")
3097
+ .insertAdjacentHTML("afterbegin", '<div id="jspsych-progressbar-container">' +
3098
+ "<span>" +
3099
+ msg +
3100
+ "</span>" +
3101
+ '<div id="jspsych-progressbar-outer">' +
3102
+ '<div id="jspsych-progressbar-inner"></div>' +
3103
+ "</div></div>");
3104
+ }
3105
+ updateProgressBar() {
3106
+ this.setProgressBar(this.getProgress().percent_complete / 100);
3107
+ }
3108
+ setProgressBar(proportion_complete) {
3109
+ proportion_complete = Math.max(Math.min(1, proportion_complete), 0);
3110
+ document.querySelector("#jspsych-progressbar-inner").style.width =
3111
+ proportion_complete * 100 + "%";
3112
+ this.progress_bar_amount = proportion_complete;
3113
+ }
3114
+ getProgressBarCompleted() {
3115
+ return this.progress_bar_amount;
3116
+ }
3117
+ }
3118
+
3119
+ // temporary patch for Safari
3120
+ if (typeof window !== "undefined" &&
3121
+ window.hasOwnProperty("webkitAudioContext") &&
3122
+ !window.hasOwnProperty("AudioContext")) {
3123
+ // @ts-expect-error
3124
+ window.AudioContext = webkitAudioContext;
3125
+ }
3126
+ // end patch
3127
+ // The following function provides a uniform interface to initialize jsPsych, no matter whether a
3128
+ // browser supports ES6 classes or not (and whether the ES6 build or the Babel build is used).
3129
+ /**
3130
+ * Creates a new JsPsych instance using the provided options.
3131
+ *
3132
+ * @param options The options to pass to the JsPsych constructor
3133
+ * @returns A new JsPsych instance
3134
+ */
3135
+ function initJsPsych(options) {
3136
+ const jsPsych = new JsPsych(options);
3137
+ // Handle invocations of non-existent v6 methods with migration errors
3138
+ const migrationMessages = {
3139
+ init: "`jsPsych.init()` was replaced by `initJsPsych()` in jsPsych v7.",
3140
+ ALL_KEYS: 'jsPsych.ALL_KEYS was replaced by the "ALL_KEYS" string in jsPsych v7.',
3141
+ NO_KEYS: 'jsPsych.NO_KEYS was replaced by the "NO_KEYS" string in jsPsych v7.',
3142
+ // Getter functions that were renamed
3143
+ currentTimelineNodeID: "`currentTimelineNodeID()` was renamed to `getCurrentTimelineNodeID()` in jsPsych v7.",
3144
+ progress: "`progress()` was renamed to `getProgress()` in jsPsych v7.",
3145
+ startTime: "`startTime()` was renamed to `getStartTime()` in jsPsych v7.",
3146
+ totalTime: "`totalTime()` was renamed to `getTotalTime()` in jsPsych v7.",
3147
+ currentTrial: "`currentTrial()` was renamed to `getCurrentTrial()` in jsPsych v7.",
3148
+ initSettings: "`initSettings()` was renamed to `getInitSettings()` in jsPsych v7.",
3149
+ allTimelineVariables: "`allTimelineVariables()` was renamed to `getAllTimelineVariables()` in jsPsych v7.",
3150
+ };
3151
+ Object.defineProperties(jsPsych, Object.fromEntries(Object.entries(migrationMessages).map(([key, message]) => [
3152
+ key,
3153
+ {
3154
+ get() {
3155
+ throw new MigrationError(message);
3156
+ },
3157
+ },
3158
+ ])));
3159
+ return jsPsych;
3160
+ }
3161
+
3162
+ exports.JsPsych = JsPsych;
3163
+ exports.initJsPsych = initJsPsych;
3164
+ exports.universalPluginParameters = universalPluginParameters;
3165
+ //# sourceMappingURL=index.cjs.map