diff --git a/jspsych-categorize-feedback.js b/jspsych-categorize-feedback.js index 8ad7f336..5490b716 100644 --- a/jspsych-categorize-feedback.js +++ b/jspsych-categorize-feedback.js @@ -5,113 +5,121 @@ // option to keep stim on screen during feedback // way to provide corrective feedback -function cf_create(params) -{ - cf_stims = params["stimuli"]; - trials = new Array(cf_stims.length); - for(var i = 0; i < trials.length; i++) - { - trials[i] = {}; - trials[i]["type"] = "cf"; - trials[i]["a_path"] = cf_stims[i]; - trials[i]["timing"] = params["timing"]; - trials[i]["key_answer"] = params["key_answer"][i]; - trials[i]["text_answer"] = params["text_answer"][i]; - trials[i]["choices"] = params["choices"]; - trials[i]["correct_text"] = params["correct_text"]; - trials[i]["incorrect_text"] = params["incorrect_text"]; - trials[i]["show_stim_feedback"] = params["show_stim_feedback"]; - if(params["prompt"] != undefined){ - trials[i]["prompt"] = params["prompt"]; +(function( $ ) { + $.fn.jsPsych.categorize-feedback = (function(){ + + var plugin = {}; + + plugin.create = function(params) { + cf_stims = params["stimuli"]; + trials = new Array(cf_stims.length); + for(var i = 0; i < trials.length; i++) + { + trials[i] = {}; + trials[i]["type"] = "cf"; + trials[i]["a_path"] = cf_stims[i]; + trials[i]["timing"] = params["timing"]; + trials[i]["key_answer"] = params["key_answer"][i]; + trials[i]["text_answer"] = params["text_answer"][i]; + trials[i]["choices"] = params["choices"]; + trials[i]["correct_text"] = params["correct_text"]; + trials[i]["incorrect_text"] = params["incorrect_text"]; + trials[i]["show_stim_feedback"] = params["show_stim_feedback"]; + if(params["prompt"] != undefined){ + trials[i]["prompt"] = params["prompt"]; + } + if(params["data"]!=undefined){ + trials[i]["data"] = params["data"][i]; + } + } + return trials; } - if(params["data"]!=undefined){ - trials[i]["data"] = params["data"][i]; - } - } - return trials; -} -function cf_trial($this, block, trial, part) -{ - switch(part){ - case 1: - p1_time = (new Date()).getTime(); - $this.append($('', { - "src": trial.a_path, - "class": 'cf' - })); - if(trial.timing[2]!=undefined){ - setTimeout(function(){cf_trial($this, block, trial, part + 1);}, trial.timing[2]); - } else { - //show prompt here - $this.append(trial.prompt); - cf_trial($this, block, trial, part + 1); - } - break; - case 2: - p2_time = (new Date()).getTime(); - if(trial.timing[2]!=undefined){ - $('.cf').remove(); - $this.append(trial.prompt); - } - startTime = (new Date()).getTime(); - var resp_func = function(e) { - var flag = false; - var correct = false; - if(e.which==trial.key_answer) // correct category - { - flag = true; - correct = true; - } - else - { - // check if the key is any of the options, or if it is an accidental keystroke - for(var i=0;i', { + "src": trial.a_path, + "class": 'cf' + })); + if(trial.timing[2]!=undefined){ + setTimeout(function(){cf_trial($this, block, trial, part + 1);}, trial.timing[2]); + } else { + //show prompt here + $this.append(trial.prompt); + cf_trial($this, block, trial, part + 1); + } + break; + case 2: + p2_time = (new Date()).getTime(); + if(trial.timing[2]!=undefined){ + $('.cf').remove(); + $this.append(trial.prompt); + } + startTime = (new Date()).getTime(); + var resp_func = function(e) { + var flag = false; + var correct = false; + if(e.which==trial.key_answer) // correct category + { flag = true; - correct = false; + correct = true; + } + else + { + // check if the key is any of the options, or if it is an accidental keystroke + for(var i=0;i', { + "src": trial.a_path, + "class": 'cf' + })); + } + // give feedback + var atext = ""; + if(block.data[block.trial_idx]["correct"]) + { + atext = trial.correct_text.replace("&ANS&", trial.text_answer); + } else { + atext = trial.incorrect_text.replace("&ANS&", trial.text_answer); + } + $this.append(atext); + setTimeout(function(){cf_trial($this, block, trial, part + 1);}, trial.timing[0]); + break; + case 4: + $this.html(""); + setTimeout(function(){block.next();}, trial.timing[1]); + break; } - $(document).keyup(resp_func); - break; - case 3: - if(trial.show_stim_feedback) - { - $this.append($('', { - "src": trial.a_path, - "class": 'cf' - })); - } - // give feedback - var atext = ""; - if(block.data[block.trial_idx]["correct"]) - { - atext = trial.correct_text.replace("&ANS&", trial.text_answer); - } else { - atext = trial.incorrect_text.replace("&ANS&", trial.text_answer); - } - $this.append(atext); - setTimeout(function(){cf_trial($this, block, trial, part + 1);}, trial.timing[0]); - break; - case 4: - $this.html(""); - setTimeout(function(){block.next();}, trial.timing[1]); - break; - } -} + } + + return plugin; + })(); +})(jQuery); \ No newline at end of file diff --git a/jspsych-categorize-unknown.js b/jspsych-categorize-unknown.js index ed68e8f1..ad45b72a 100644 --- a/jspsych-categorize-unknown.js +++ b/jspsych-categorize-unknown.js @@ -1,75 +1,83 @@ // timing parameters: [intertrial gap, optional length to display target] // if optional length to display target is missing, then target is displayed until subject responds. -function cu_create(params) -{ - cu_stims = params["stimuli"]; - trials = new Array(cu_stims.length); - for(var i = 0; i < trials.length; i++) - { - trials[i] = {}; - trials[i]["type"] = "cu"; - trials[i]["a_path"] = cu_stims[i]; - trials[i]["timing"] = params["timing"]; - trials[i]["choices"] = params["choices"]; - if(params["prompt"] != undefined){ - trials[i]["prompt"] = params["prompt"]; +(function( $ ) { + $.fn.jsPsych.categorize-unknown = (function(){ + + var plugin = {}; + + plugin.create = function(params) { + cu_stims = params["stimuli"]; + trials = new Array(cu_stims.length); + for(var i = 0; i < trials.length; i++) + { + trials[i] = {}; + trials[i]["type"] = "cu"; + trials[i]["a_path"] = cu_stims[i]; + trials[i]["timing"] = params["timing"]; + trials[i]["choices"] = params["choices"]; + if(params["prompt"] != undefined){ + trials[i]["prompt"] = params["prompt"]; + } + if(params["data"]!=undefined){ + trials[i]["data"] = params["data"][i]; + } + } + return trials; } - if(params["data"]!=undefined){ - trials[i]["data"] = params["data"][i]; - } - } - return trials; -} -function cu_trial($this, block, trial, part) -{ - switch(part){ - case 1: - p1_time = (new Date()).getTime(); - $this.append($('', { - "src": trial.a_path, - "class": 'cu' - })); - if(trial.timing[1]!=undefined){ - setTimeout(function(){cu_trial($this, block, trial, part + 1);}, trial.timing[1]); - } else { - //show prompt here - $this.append(trial.prompt); - cu_trial($this, block, trial, part + 1); - } - break; - case 2: - p2_time = (new Date()).getTime(); - if(trial.timing[1]!=undefined){ - $('.cu').remove(); - $this.append(trial.prompt); - } - startTime = (new Date()).getTime(); - var resp_func = function(e) { - var flag = false; - // check if the key is any of the options, or if it is an accidental keystroke - for(var i=0;i', { + "src": trial.a_path, + "class": 'cu' + })); + if(trial.timing[1]!=undefined){ + setTimeout(function(){cu_trial($this, block, trial, part + 1);}, trial.timing[1]); + } else { + //show prompt here + $this.append(trial.prompt); + cu_trial($this, block, trial, part + 1); } - } - if(flag) - { - endTime = (new Date()).getTime(); - rt = (endTime-startTime); - stim1_time = (p2_time-p1_time); - var trial_data = {"rt": rt, "a_path": trial.a_path, "key_press": e.which, "stim1_time": stim1_time} - block.data[block.trial_idx] = $.extend({},trial_data,trial.data); - $(document).unbind('keyup',resp_func); - $this.html(''); - setTimeout(function(){block.next();}, trial.timing[0]); - } + break; + case 2: + p2_time = (new Date()).getTime(); + if(trial.timing[1]!=undefined){ + $('.cu').remove(); + $this.append(trial.prompt); + } + startTime = (new Date()).getTime(); + var resp_func = function(e) { + var flag = false; + // check if the key is any of the options, or if it is an accidental keystroke + for(var i=0;i', { - "src": trial.a_path, - "class": 'sd' - })); - setTimeout(function(){sd_trial($this, block, trial, part + 1);}, trial.timing[0]); - break; - case 2: - p2_time = (new Date()).getTime(); - $('.sd').remove(); - setTimeout(function(){sd_trial($this, block, trial, part + 1);}, trial.timing[1]); - break; - case 3: - p3_time = (new Date()).getTime(); - $this.append($('', { - "src": trial.b_path, - "class": 'sd' - })); - if(trial.timing[3]!=undefined){ - setTimeout(function(){sd_trial($this, block, trial, part + 1);}, trial.timing[3]); - } else { - sd_trial($this, block, trial, part + 1); - } - break; - case 4: - p4_time = (new Date()).getTime(); - if(trial.timing[3]!=undefined){ - $('.sd').remove(); - $this.html(trial.prompt); - } - startTime = (new Date()).getTime(); - var resp_func = function(e) { - var flag = false; - var correct = false; - if(e.which=='80') // 'p' key -- same - { - flag = true; - if(trial.answer == "same") { correct = true; } - } else if(e.which=='81') // 'q' key -- different - { - flag = true; - if(trial.answer == "different"){ correct = true; } + var plugin = {}; + + plugin.create = function(params) { + sd_stims = params["stimuli"]; + trials = new Array(sd_stims.length); + for(var i = 0; i < trials.length; i++) + { + trials[i] = {}; + trials[i]["type"] = "sd"; + trials[i]["a_path"] = sd_stims[i][0]; + trials[i]["b_path"] = sd_stims[i][1]; + trials[i]["timing"] = params["timing"]; + trials[i]["answer"] = params["answer"][i]; + if(params["prompt"] != undefined){ + trials[i]["prompt"] = params["prompt"]; } - if(flag) - { - endTime = (new Date()).getTime(); - rt = (endTime-startTime); - stim1_time = (p2_time-p1_time); - isi_time = (p3_time-p2_time); - stim2_time = (p4_time-p3_time); - var trial_data = {"rt": rt, "correct": correct, "a_path": trial.a_path, "b_path": trial.b_path, "key_press": e.which, "stim1_time": stim1_time, "stim2_time":stim2_time, "isi_time":isi_time} - block.data[block.trial_idx] = $.extend({},trial_data,trial.data); - $(document).unbind('keyup',resp_func); + if(params["data"]!=undefined){ + trials[i]["data"] = params["data"][i]; + } + } + return trials; + } + + plugin.trial = function($this, block, trial, part) + { + switch(part){ + case 1: + p1_time = (new Date()).getTime(); + $this.append($('', { + "src": trial.a_path, + "class": 'sd' + })); + setTimeout(function(){sd_trial($this, block, trial, part + 1);}, trial.timing[0]); + break; + case 2: + p2_time = (new Date()).getTime(); $('.sd').remove(); - $this.html(''); - setTimeout(function(){block.next();}, trial.timing[2]); - } + setTimeout(function(){sd_trial($this, block, trial, part + 1);}, trial.timing[1]); + break; + case 3: + p3_time = (new Date()).getTime(); + $this.append($('', { + "src": trial.b_path, + "class": 'sd' + })); + if(trial.timing[3]!=undefined){ + setTimeout(function(){sd_trial($this, block, trial, part + 1);}, trial.timing[3]); + } else { + sd_trial($this, block, trial, part + 1); + } + break; + case 4: + p4_time = (new Date()).getTime(); + if(trial.timing[3]!=undefined){ + $('.sd').remove(); + $this.html(trial.prompt); + } + startTime = (new Date()).getTime(); + var resp_func = function(e) { + var flag = false; + var correct = false; + if(e.which=='80') // 'p' key -- same + { + flag = true; + if(trial.answer == "same") { correct = true; } + } else if(e.which=='81') // 'q' key -- different + { + flag = true; + if(trial.answer == "different"){ correct = true; } + } + if(flag) + { + endTime = (new Date()).getTime(); + rt = (endTime-startTime); + stim1_time = (p2_time-p1_time); + isi_time = (p3_time-p2_time); + stim2_time = (p4_time-p3_time); + var trial_data = {"rt": rt, "correct": correct, "a_path": trial.a_path, "b_path": trial.b_path, "key_press": e.which, "stim1_time": stim1_time, "stim2_time":stim2_time, "isi_time":isi_time} + block.data[block.trial_idx] = $.extend({},trial_data,trial.data); + $(document).unbind('keyup',resp_func); + $('.sd').remove(); + $this.html(''); + setTimeout(function(){block.next();}, trial.timing[2]); + } + } + $(document).keyup(resp_func); + break; } - $(document).keyup(resp_func); - break; - } -} + } + + return plugin; + })(); +}) (jQuery); \ No newline at end of file diff --git a/jspsych-similarity.js b/jspsych-similarity.js index 33bbf3b0..ea533a28 100755 --- a/jspsych-similarity.js +++ b/jspsych-similarity.js @@ -1,60 +1,68 @@ -function similarity_create(params) -{ - sim_stims = params["stimuli"]; - trials = new Array(sim_stims.length); - for(var i = 0; i < trials.length; i++) - { - trials[i] = {}; - trials[i]["type"] = "sim"; - trials[i]["a_path"] = sim_stims[i][0]; - trials[i]["b_path"] = sim_stims[i][1]; - trials[i]["timing"] = params["timing"]; - } - return trials; -} - -function similarity_trial($this, block, trial, part) -{ - switch(part){ - case 1: - images = [trial.a_path, trial.b_path]; - if(Math.floor(Math.random()*2)==0){ - images = [trial.b_path, trial.a_path]; +(function( $ ) { + $.fn.jsPsych.text = (function(){ + + var plugin = {}; + + plugin.create = function(params) { + sim_stims = params["stimuli"]; + trials = new Array(sim_stims.length); + for(var i = 0; i < trials.length; i++) + { + trials[i] = {}; + trials[i]["type"] = "sim"; + trials[i]["a_path"] = sim_stims[i][0]; + trials[i]["b_path"] = sim_stims[i][1]; + trials[i]["timing"] = params["timing"]; } - // show the images - $this.append($('', { - "src": images[0], - "class": 'sim' - })); - $this.append($('', { - "src": images[1], - "class": 'sim' - })); + return trials; + } - // create slider - $this.append($('', { "id": 'slider', "class": 'sim' })); - $("#slider").slider( - { - value:50, - min:0, - max:100, - step:1, - }); - - // create button - $this.append($('', {'id':'next','class':'sim'})); - $("#next").html('Next'); - $("#next").click(function(){ - similarity_trial($this,block,trial,part+1); - }); - break; - case 2: - // get data - var score = $("#slider").slider("value"); - block.data[block.trial_idx] = {"score": score, "a_path": trial.a_path, "b_path": trial.b_path} - // goto next trial in block - $('.sim').remove(); - setTimeout(function(){block.next();}, trial.timing[0]); - break; - } -} \ No newline at end of file + plugin.trial = function($this, block, trial, part) + { + switch(part){ + case 1: + images = [trial.a_path, trial.b_path]; + if(Math.floor(Math.random()*2)==0){ + images = [trial.b_path, trial.a_path]; + } + // show the images + $this.append($('', { + "src": images[0], + "class": 'sim' + })); + $this.append($('', { + "src": images[1], + "class": 'sim' + })); + + // create slider + $this.append($('', { "id": 'slider', "class": 'sim' })); + $("#slider").slider( + { + value:50, + min:0, + max:100, + step:1, + }); + + // create button + $this.append($('', {'id':'next','class':'sim'})); + $("#next").html('Next'); + $("#next").click(function(){ + similarity_trial($this,block,trial,part+1); + }); + break; + case 2: + // get data + var score = $("#slider").slider("value"); + block.data[block.trial_idx] = {"score": score, "a_path": trial.a_path, "b_path": trial.b_path} + // goto next trial in block + $('.sim').remove(); + setTimeout(function(){block.next();}, trial.timing[0]); + break; + } + } + + return plugin; + })(); +})(jQuery); \ No newline at end of file diff --git a/jspsych-text.js b/jspsych-text.js index 1f1c35ea..272a1b10 100755 --- a/jspsych-text.js +++ b/jspsych-text.js @@ -1,28 +1,36 @@ -function text_create(params) -{ - var trials = new Array(params.text.length); - for(var i = 0; i < trials.length; i++) - { - trials[i] = {}; - trials[i]["type"] = "text"; - trials[i]["text"] = params.text[i]; - trials[i]["cont_key"] = params.cont_key; - trials[i]["timing"] = params.timing; - } - return trials; -} -function text_trial($this, block, trial, part) -{ - $this.html(trial.text); - var key_listener = function(e) { - if(e.which==trial.cont_key) - { - flag = true; - $(document).unbind('keyup',key_listener); - $this.html(''); - setTimeout(function(){block.next();}, trial.timing[0]); +(function( $ ) { + $.fn.jsPsych.text = (function(){ + + var plugin = {}; + + plugin.create = function(params) { + var trials = new Array(params.text.length); + for(var i = 0; i < trials.length; i++) + { + trials[i] = {}; + trials[i]["type"] = "text"; + trials[i]["text"] = params.text[i]; + trials[i]["cont_key"] = params.cont_key; + trials[i]["timing"] = params.timing; + } + return trials; } - } - $(document).keyup(key_listener); -} \ No newline at end of file + + plugin.trial = function($this, block, trial, part) { + $this.html(trial.text); + var key_listener = function(e) { + if(e.which==trial.cont_key) + { + flag = true; + $(document).unbind('keyup',key_listener); + $this.html(''); + setTimeout(function(){block.next();}, trial.timing[0]); + } + } + $(document).keyup(key_listener); + } + + return plugin; + })(); +}) (jQuery); \ No newline at end of file diff --git a/jspsych-xab.js b/jspsych-xab.js index efb32402..1715f3df 100755 --- a/jspsych-xab.js +++ b/jspsych-xab.js @@ -1,86 +1,96 @@ -function xab_create(params) -{ - //xab_stims = shuffle(xab_stims); - xab_stims = params["stimuli"]; - trials = new Array(xab_stims.length); - for(var i = 0; i < trials.length; i++) - { - trials[i] = {}; - trials[i]["type"] = "xab"; - trials[i]["a_path"] = xab_stims[i][0]; - trials[i]["b_path"] = xab_stims[i][1]; - trials[i]["timing"] = params["timing"]; - if(params["data"]!=undefined){ - trials[i]["data"] = params["data"][i]; - } - } - return trials; -} -function xab_trial($this, block, trial, part) -{ - switch(part){ - case 1: - p1_time = (new Date()).getTime(); - $this.append($('', { - "src": trial.a_path, - "class": 'xab' - })); - setTimeout(function(){xab_trial($this, block, trial, part + 1)}, trial.timing[0]); - break; - case 2: - p2_time = (new Date()).getTime(); - $('.xab').remove(); - setTimeout(function(){xab_trial($this, block, trial, part + 1)}, trial.timing[1]); - break; - case 3: - p3_time = (new Date()).getTime(); - startTime = (new Date()).getTime(); - var images = [trial.a_path, trial.b_path]; - var target_left = (Math.floor(Math.random()*2)==0); // binary true/false choice - if(!target_left){ - images = [trial.b_path, trial.a_path]; - } - - // show the images - $this.append($('', { - "src": images[0], - "class": 'xab' - })); - $this.append($('', { - "src": images[1], - "class": 'xab' - })); - - - var resp_func = function(e) { - var flag = false; - var correct = false; - if(e.which=='80') // 'p' key - { - flag = true; - if(!target_left) { correct = true; } - } else if(e.which=='81') // 'q' key - { - flag = true; - if(target_left){ correct = true; } +(function( $ ) { + $.fn.jsPsych.xab = (function(){ + + var plugin = {} + + plugin.create = function(params) + { + //xab_stims = shuffle(xab_stims); + xab_stims = params["stimuli"]; + trials = new Array(xab_stims.length); + for(var i = 0; i < trials.length; i++) + { + trials[i] = {}; + trials[i]["type"] = "xab"; + trials[i]["a_path"] = xab_stims[i][0]; + trials[i]["b_path"] = xab_stims[i][1]; + trials[i]["timing"] = params["timing"]; + if(params["data"]!=undefined){ + trials[i]["data"] = params["data"][i]; } - if(flag) - { - endTime = (new Date()).getTime(); - rt = (endTime-startTime); - stim1_time = (p2_time-p1_time); - isi_time = (p3_time-p2_time); - var trial_data = {"rt": rt, "correct": correct, "a_path": trial.a_path, "b_path": trial.b_path, "key_press": e.which, "key_press": e.which, "stim1_time": stim1_time, "isi_time":isi_time} - block.data[block.trial_idx] = $.extend({},trial_data,trial.data); - $(document).unbind('keyup',resp_func); + } + return trials; + } + + plugin.trial = function($this, block, trial, part) + { + switch(part){ + case 1: + p1_time = (new Date()).getTime(); + $this.append($('', { + "src": trial.a_path, + "class": 'xab' + })); + setTimeout(function(){xab_trial($this, block, trial, part + 1)}, trial.timing[0]); + break; + case 2: + p2_time = (new Date()).getTime(); $('.xab').remove(); - setTimeout(function(){block.next();}, trial.timing[2]); - } + setTimeout(function(){xab_trial($this, block, trial, part + 1)}, trial.timing[1]); + break; + case 3: + p3_time = (new Date()).getTime(); + startTime = (new Date()).getTime(); + var images = [trial.a_path, trial.b_path]; + var target_left = (Math.floor(Math.random()*2)==0); // binary true/false choice + if(!target_left){ + images = [trial.b_path, trial.a_path]; + } + + // show the images + $this.append($('', { + "src": images[0], + "class": 'xab' + })); + $this.append($('', { + "src": images[1], + "class": 'xab' + })); + + + var resp_func = function(e) { + var flag = false; + var correct = false; + if(e.which=='80') // 'p' key + { + flag = true; + if(!target_left) { correct = true; } + } else if(e.which=='81') // 'q' key + { + flag = true; + if(target_left){ correct = true; } + } + if(flag) + { + endTime = (new Date()).getTime(); + rt = (endTime-startTime); + stim1_time = (p2_time-p1_time); + isi_time = (p3_time-p2_time); + var trial_data = {"rt": rt, "correct": correct, "a_path": trial.a_path, "b_path": trial.b_path, "key_press": e.which, "key_press": e.which, "stim1_time": stim1_time, "isi_time":isi_time} + block.data[block.trial_idx] = $.extend({},trial_data,trial.data); + $(document).unbind('keyup',resp_func); + $('.xab').remove(); + setTimeout(function(){block.next();}, trial.timing[2]); + } + } + $(document).keyup(resp_func); + //TODO: CHECK IF IMAGE SHOULD DISAPPEAR + //based on timings + break; } - $(document).keyup(resp_func); - //TODO: CHECK IF IMAGE SHOULD DISAPPEAR - //based on timings - break; - } -} \ No newline at end of file + } + + return plugin; + })(); +})(jQuery); \ No newline at end of file diff --git a/jspsych.js b/jspsych.js index 6fd958cb..a7cf5e6a 100755 --- a/jspsych.js +++ b/jspsych.js @@ -25,7 +25,7 @@ { if(opts["experiment_structure"][i]["type"] == opts["plugins"][j]["type"]) { - trials = opts["plugins"][j]["createFunc"].call(null, opts["experiment_structure"][i]); + trials = opts["plugins"][j]["src"]["create"].call(null, opts["experiment_structure"][i]); } } @@ -94,7 +94,7 @@ { if(trial.type == opts["plugins"][j]["type"]) { - opts["plugins"][j]["trialFunc"].call(this, $this, block, trial, 1); + opts["plugins"][j]["src"]["trial"].call(this, $this, block, trial, 1); } } }