cleaning up text plugin code and comments

This commit is contained in:
Josh de Leeuw 2013-10-21 17:46:43 -04:00
parent a102d46d11
commit 5c1654db57

View File

@ -1,123 +1,120 @@
/* jspsych-text.js /* jspsych-text.js
* Josh de Leeuw * Josh de Leeuw
* *
* This plugin displays text (including HTML formatted strings) during the experiment. * This plugin displays text (including HTML formatted strings) during the experiment.
* Use it to show instructions, provide performance feedback, etc... * Use it to show instructions, provide performance feedback, etc...
* *
* No data is currently collected with this plugin. Do not use it for situations in which * No data is currently collected with this plugin. Do not use it for situations in which
* data collection is important. * data collection is important.
* *
* Parameters: * Parameters:
* type: "text" * type: "text"
* text: an array of strings. Each element in the array will be displayed on a separate screen. * text: an array of strings. Each element in the array will be displayed on a separate screen.
* cont_key: the keycode of the key the user should press to advance to the next screen. Default is '13' which is ENTER. * cont_key: the keycode of the key the user should press to advance to the next screen. Default is '13' which is ENTER. May specify mouse click
* timing: an array with a single element representing the time in milliseconds to delay on a blank screen after the continue key is pressed. Default is no delay. * by listing the key as 'mouse'
* variables: see variables section below. * timing: an array with a single element representing the time in milliseconds to delay on a blank screen after the continue key is pressed. Default is no delay.
* * variables: see variables section below.
* Optional Variables: If you want to display dynamic information that is updated at the moment the text is rendered on the screen, *
* such calculating an accuracy score to tell a subject how many trials they got right, you can use the optional variables parameter. * Optional Variables: If you want to display dynamic information that is updated at the moment the text is rendered on the screen,
* The variables are specified as an array of arrays. The outer level array indexes for which block of text you are showing, and the * such calculating an accuracy score to tell a subject how many trials they got right, you can use the optional variables parameter.
* outer array should have the same length as the text array. Each element of the outer level array is also an array, with one element * The variables are specified as an array of arrays. The outer level array indexes for which block of text you are showing, and the
* for each variable that you are specifying. Variables are specified as functions which return the string that you want to display. * outer array should have the same length as the text array. Each element of the outer level array is also an array, with one element
* To indicate where a variable should be placed in the text, use the special string "%v" in the text. This will be replaced by the * for each variable that you are specifying. Variables are specified as functions which return the string that you want to display.
* return value of the function that is in the variables array. Each "%v" string will be replaced in the order that they appear in the * To indicate where a variable should be placed in the text, use the special string "%v" in the text. This will be replaced by the
* text, and only one replacement will be made per function call. Therefore, you should have the same number of "%v" strings as you have * return value of the function that is in the variables array. Each "%v" string will be replaced in the order that they appear in the
* elements in the inner arrays of the "variables" array. * text, and only one replacement will be made per function call. Therefore, you should have the same number of "%v" strings as you have
* * elements in the inner arrays of the "variables" array.
* Example: *
* var goodbye_func = function(){return "goodbye";} * Example:
* var hello_func = function(){return "hello";} * var goodbye_func = function(){return "goodbye";}
* "text": ["%v, %v.", "I don't know why you say %v, I say %v."] * var hello_func = function(){return "hello";}
* "variables":[[hello_func, hello_func],[goodbye_func, hello_func]] * "text": ["%v, %v.", "I don't know why you say %v, I say %v."]
* * "variables":[[hello_func, hello_func],[goodbye_func, hello_func]]
* When the above parameters are loaded into the text plugin, the first screen would show *
* "hello, hello." and the second screen would show "I don't know why you say goodbye, I say hello." * When the above parameters are loaded into the text plugin, the first screen would show
* * "hello, hello." and the second screen would show "I don't know why you say goodbye, I say hello."
*/ *
(function( $ ) { */
jsPsych.text = (function(){
(function($) {
var plugin = {}; jsPsych.text = (function() {
plugin.create = function(params) { var plugin = {};
var trials = new Array(params.text.length);
for(var i = 0; i < trials.length; i++) plugin.create = function(params) {
{ var trials = new Array(params.text.length);
trials[i] = {}; for (var i = 0; i < trials.length; i++) {
trials[i]["type"] = "text"; // must match plugin name trials[i] = {};
trials[i]["text"] = params.text[i]; // text of all trials trials[i].type = "text"; // must match plugin name
trials[i]["cont_key"] = params.cont_key || '13'; // keycode to press to advance screen, default is ENTER. trials[i].text = params.text[i]; // text of all trials
trials[i]["timing_post_trial"] = params.timing_post_trial || 0; // how long to delay between screens, default is no delay. trials[i].cont_key = params.cont_key || '13'; // keycode to press to advance screen, default is ENTER.
if(params.variables != undefined) trials[i].timing_post_trial = params.timing_post_trial || 0; // how long to delay between screens, default is no delay.
{ trials[i].variables = (typeof params.variables === 'undefined') ? undefined : params.variables[i];
trials[i]["variables"] = params.variables[i]; // optional variables, defined as functions. trials[i].data = (typeof params.data === 'undefined') ? {} : params.data[i];
} }
if(params.data != undefined) return trials;
{ };
trials[i]["data"] = params.data[i];
} else { plugin.trial = function(display_element, block, trial, part) {
trials[i]["data"] = {}; // the text for the trial is in trial.text, but we need to replace any variables that are in the text.
} var replaced_text = trial.text;
}
return trials; // check to see if there are any variables defined.
} if (typeof trial.variables != 'undefined') {
for (var i = 0; i < trial.variables.length; i++) {
plugin.trial = function(display_element, block, trial, part) { // loop through the array of variables and call each variable function
// the text for the trial is in trial.text, but we need to replace any variables that are in the text. // to get the actual text that should be substituted in.
var replaced_text = trial.text; var variable_text = trial.variables[i]();
// replace the "%v" with the return value of the function.
// check to see if there are any variables defined. replaced_text = replaced_text.replace("%v", variable_text);
if(trial.variables != undefined) }
{ }
for(var i = 0; i < trial.variables.length; i++) // set the HTML of the display target to replaced_text.
{ display_element.html(replaced_text);
// loop through the array of variables and call each variable function
// to get the actual text that should be substituted in. var startTime = (new Date()).getTime();
var variable_text = trial.variables[i]();
// replace the "%v" with the return value of the function. // define a function that will advance to the next trial when the user presses
replaced_text = replaced_text.replace("%v", variable_text); // the continue key.
} var key_listener = function(e) {
} if (e.which == trial.cont_key) {
// set the HTML of the display target to replaced_text. save_data();
display_element.html(replaced_text); $(document).unbind('keyup', key_listener); // remove the response function, so that it doesn't get triggered again.
display_element.html(''); // clear the display
startTime = (new Date()).getTime(); setTimeout(function() {
block.next();
// define a function that will advance to the next trial when the user presses }, trial.timing_post_trial); // call block.next() to advance the experiment after a delay.
// the continue key. }
var key_listener = function(e) { };
if(e.which==trial.cont_key)
{ var mouse_listener = function(e) {
save_data(); save_data();
$(document).unbind('keyup',key_listener); // remove the response function, so that it doesn't get triggered again. display_element.unbind('click', mouse_listener); // remove the response function, so that it doesn't get triggered again.
display_element.html(''); // clear the display display_element.html(''); // clear the display
setTimeout(function(){block.next();}, trial.timing_post_trial); // call block.next() to advance the experiment after a delay. setTimeout(function() {
} block.next();
} }, trial.timing_post_trial); // call block.next() to advance the experiment after a delay.
};
var mouse_listener = function(e){
save_data(); // check if key is 'mouse'
display_element.unbind('click', mouse_listener); // remove the response function, so that it doesn't get triggered again. if (trial.cont_key == 'mouse') {
display_element.html(''); // clear the display display_element.click(mouse_listener);
setTimeout(function(){block.next();}, trial.timing_post_trial); // call block.next() to advance the experiment after a delay. }
} else {
// attach the response function to the html document.
// check if key is 'mouse' $(document).keyup(key_listener);
if(trial.cont_key == 'mouse') }
{
display_element.click(mouse_listener); var save_data = function() {
} else { var rt = (new Date()).getTime() - startTime;
// attach the response function to the html document. block.data[block.trial_idx] = $.extend({}, {
$(document).keyup(key_listener); "trial_type": "text",
} "trial_index": block.trial_idx,
"rt": rt
var save_data = function() }, trial.data);
{ };
var rt = (new Date()).getTime() - startTime; };
block.data[block.trial_idx] = $.extend({},{"trial_type":"text", "trial_index": block.trial_idx, "rt": rt},trial.data);
} return plugin;
} })();
})(jQuery);
return plugin;
})();
}) (jQuery);