Merge pull request #69 from jodeleeuw/dev-chunks

Dev chunks
This commit is contained in:
Josh de Leeuw 2014-09-23 16:47:27 -04:00
commit ac5c707516
18 changed files with 1175 additions and 966 deletions

View File

@ -17,11 +17,11 @@
// options
var opts = {};
// exp structure
var exp_blocks = [];
var root_chunk;
// flow control
var curr_block = 0;
// everything loaded?
var initialized = false;
var curr_chunk = 0;
var global_trial_index = 0;
var current_trial = {};
// target DOM element
var DOM_target;
// time that the experiment began
@ -33,11 +33,10 @@
core.init = function(options) {
// reset the key variables
exp_blocks = [];
// reset variables
root_chunk = {};
opts = {};
initialized = false;
curr_block = 0;
curr_chunk = 0;
// check if there is a body element on the page
var default_display_element = $('body');
@ -63,7 +62,7 @@
'show_progress_bar': false
};
// import options
// override default options if user specifies an option
opts = $.extend({}, defaults, options);
// set target
@ -72,42 +71,19 @@
// add CSS class to DOM_target
DOM_target.addClass('jspsych-display-element');
run();
};
// create experiment structure
root_chunk = parseExpStructure(opts.experiment_structure);
core.data = function() {
var all_data = [];
for (var i = 0; i < exp_blocks.length; i++) {
all_data[i] = exp_blocks[i].data;
}
return all_data;
startExperiment();
};
core.progress = function() {
var total_trials = 0;
for (var i = 0; i < exp_blocks.length; i++) {
total_trials += exp_blocks[i].num_trials;
}
var current_trial_global = 0;
var current_trial_local = -1;
for (var i = 0; i < curr_block; i++) {
current_trial_global += exp_blocks[i].num_trials;
}
if(current_trial_global < total_trials) {
current_trial_global += exp_blocks[curr_block].trial_idx;
current_trial_local = exp_blocks[curr_block].trial_idx;
}
var obj = {
"total_blocks": exp_blocks.length,
"total_trials": total_trials,
"current_trial_global": current_trial_global,
"current_trial_local": current_trial_local,
"current_block": curr_block
"total_trials": root_chunk.length(),
"current_trial_global": global_trial_index
//"current_trial_local": exp_chunks[curr_chunk].currentTrialInChunk,
//"current_timeline_location": curr_chunk + "-" + exp_chunks[curr_chunk].currentBlock
};
return obj;
@ -149,52 +125,306 @@
return DOM_target;
}
//
// private functions //
//
function run() {
core.finishTrial = function(){
// logic to advance to next trial?
// take the experiment structure and create a set of blocks
exp_blocks = new Array(opts.experiment_structure.length);
// handle callback at plugin level
if (typeof current_trial.on_finish === 'function') {
current_trial.on_finish(); // TODO: pass in data
}
// iterate through list to create trials
for (var i = 0; i < exp_blocks.length; i++) {
// handle callback at whole-experiment level
opts.on_trial_finish();
// update progress bar if shown
if (opts.show_progress_bar === true) {
updateProgressBar();
}
global_trial_index++;
// advance chunk
root_chunk.advance();
// check if experiment is over
if(root_chunk.isComplete()){
finishExperiment();
return;
}
doTrial(root_chunk.next());
}
core.currentTrial = function(){
return current_trial;
}
core.initSettings = function(){
return opts;
}
core.currentChunkID = function(){
return root_chunk.activeChunkID();
}
function parseExpStructure(experiment_structure) {
/*var chunks = [];
for(var i=0; i<experiment_structure.length; i++){
var ct = experiment_structure[i].chunk_type;
if(typeof ct !== 'undefined') {
if($.inArray(ct, ["linear", "while"]) > -1){
chunks.push(createExperimentChunk(experiment_structure[i], i));
} else {
throw new Error('Invalid experiment structure definition. Element '+i+' of the experiment_structure array has an invalid chunk_type property');
}
} else if(typeof experiment_structure[i].type !== 'undefined') {
var temp_chunk = {
chunk_type: 'linear',
blocks: [experiment_structure[i]]
}
chunks.push(createExperimentChunk(temp_chunk));
} else {
throw new Error('Invalid experiment structure definition. Element '+i+' of the experiment_structure array is improperly defined');
}
}*/
return createExperimentChunk({
chunk_type: 'root',
blocks: experiment_structure
});
}
function createExperimentChunk(chunk_definition, parent_chunk, relative_id){
var chunk = {};
chunk.timeline = parseChunkDefinition(chunk_definition.blocks);
chunk.parentChunk = parent_chunk;
chunk.relID = relative_id;
chunk.type = chunk_definition.chunk_type; // root, linear, while
chunk.currentTimelineLocation = 0;
// this is the current trial since the last time the chunk was reset
chunk.currentTrialInTimeline = 0;
// this is the current trial since the chunk started (incl. resets)
chunk.currentTrialInChunk = 0;
chunk.iteration = 0;
chunk.length = function(){
// this will recursively get the number of trials on this chunk's timeline
var n = 0;
for(var i=0; i<this.timeline.length; i++){
n += this.timeline[i].length;
}
return n;
}
chunk.activeChunkID = function(){
if(this.timeline[this.currentTimelineLocation].type === 'block'){
return this.chunkID();
} else {
return this.timeline[this.currentTimelineLocation].activeChunkID();
}
}
chunk.chunkID = function() {
if(typeof this.parentChunk === 'undefined') {
return 0 + "-" + this.iteration;
} else {
return this.parentChunk.chunkID() + "." + this.relID + "-" + this.iteration;
}
}
chunk.next = function() {
// return the next trial in the block to be run
if(chunk.isComplete()){
throw new Error('Tried to get completed trial from chunk that is finished.');
} else {
return this.timeline[this.currentTimelineLocation].next();
}
}
chunk.advance = function(){
// increment the current trial in the chunk
this.timeline[this.currentTimelineLocation].advance();
if(this.timeline[this.currentTimelineLocation].isComplete()){
this.currentTimelineLocation++;
}
this.currentTrialInTimeline++;
this.currentTrialInChunk++;
}
chunk.isComplete = function() {
// return true if the chunk is done running trials
// return false otherwise
// linear chunks just go through the blocks in order and are
// done when each trial has been completed once
// the root chunk is a special case of the linear chunk
if(this.type == 'linear' || this.type == 'root'){
if (this.currentTimelineLocation >= this.timeline.length) { return true; }
else { return false; }
}
// while chunks play the block again as long as the continue_function
// returns true
else if(this.type == 'while'){
if (this.currentTimelineLocation >= this.timeline.length) {
if(chunk_definition.continue_function(this.generatedData())){
this.reset();
return false;
} else {
return true;
}
} else {
return false;
}
}
}
chunk.generatedData = function() {
// return an array containing all of the data generated by this chunk for this iteration
var d = jsPsych.data.getTrialsFromChunk(this.chunkID());
return d;
}
chunk.reset = function() {
this.currentTimelineLocation = 0;
this.currentTrialInTimeline = 0;
this.iteration++;
for(var i = 0; i < this.timeline.length; i++){
this.timeline[i].reset();
}
}
function parseChunkDefinition(chunk_timeline){
var timeline = [];
for (var i = 0; i < chunk_timeline.length; i++) {
var ct = chunk_timeline[i].chunk_type;
if(typeof ct !== 'undefined') {
if($.inArray(ct, ["linear", "while"]) > -1){
timeline.push(createExperimentChunk(chunk_timeline[i], chunk, i));
} else {
throw new Error('Invalid experiment structure definition. Element of the experiment_structure array has an invalid chunk_type property');
}
} else {
// create a terminal block ...
// check to make sure plugin is loaded
var plugin_name = opts.experiment_structure[i].type;
if (typeof jsPsych[plugin_name] == 'undefined') {
var plugin_name = chunk_timeline[i].type;
if (typeof jsPsych[plugin_name] === 'undefined') {
throw new Error("Failed attempt to create trials using plugin type " + plugin_name + ". Is the plugin loaded?");
}
var trials = jsPsych[plugin_name]["create"].call(null, opts["experiment_structure"][i]);
var trials = jsPsych[plugin_name].create(chunk_timeline[i]);
// add options that are generic to all plugins
trials = addGenericTrialOptions(trials, opts.experiment_structure[i]);
trials = addGenericTrialOptions(trials, chunk_timeline[i]);
exp_blocks[i] = createBlock(trials);
timeline.push(createBlock(trials));
}
}
return timeline;
}
return chunk;
}
function createBlock(trial_list) {
var block = {
trial_idx: 0,
trials: trial_list,
type: 'block',
next: function() {
var curr_trial = this.trials[this.trial_idx];
return curr_trial;
},
isComplete: function() {
if(this.trial_idx >= this.trials.length){
return true;
} else {
return false;
}
},
advance: function() {
this.trial_idx++;
},
reset: function() {
this.trial_idx = 0;
},
length: trial_list.length
};
return block;
}
function startExperiment() {
// show progress bar if requested
if(opts.show_progress_bar === true) {
if (opts.show_progress_bar === true) {
drawProgressBar();
}
// record the start time
exp_start_time = new Date();
// begin! - run the first block
exp_blocks[0].next();
// begin!
doTrial(root_chunk.next());
}
function addGenericTrialOptions(trials_arr, opts){
function addGenericTrialOptions(trials_arr, opts) {
// modify this list to add new generic parameters
var genericParameters = ['type', 'data', 'timing_post_trial', 'on_finish'];
// default values for generics above
var defaultValues = [ , , 1000, ];
var defaultValues = [, , 1000, ];
for(var i = 0; i < genericParameters.length; i++){
for (var i = 0; i < genericParameters.length; i++) {
trials_arr = addParamToTrialsArr(trials_arr, opts[genericParameters[i]], genericParameters[i], defaultValues[i]);
}
@ -204,23 +434,23 @@
function addParamToTrialsArr(trials_arr, param, param_name, default_value) {
if(typeof default_value !== 'undefined') {
if (typeof default_value !== 'undefined') {
param = (typeof param === 'undefined') ? default_value : param;
}
if(typeof param !== 'undefined'){
if(Array.isArray(param)){
if (typeof param !== 'undefined') {
if (Array.isArray(param)) {
// check if data object array is the same length as the number of trials
if(param.length != trials_arr.length) {
throw new Error('Invalid specification of parameter '+param_name+' in plugin type '+trials_arr[i].type+'. Length of parameter array does not match the number of trials in the block.');
if (param.length != trials_arr.length) {
throw new Error('Invalid specification of parameter ' + param_name + ' in plugin type ' + trials_arr[i].type + '. Length of parameter array does not match the number of trials in the block.');
} else {
for(var i=0; i<trials_arr.length; i++){
for (var i = 0; i < trials_arr.length; i++) {
trials_arr[i][param_name] = param[i];
}
}
} else {
// use the same data object for each trial
for(var i=0; i<trials_arr.length; i++){
for (var i = 0; i < trials_arr.length; i++) {
trials_arr[i][param_name] = param;
}
}
@ -228,119 +458,76 @@
return trials_arr;
}
function nextBlock() {
curr_block += 1;
if (curr_block == exp_blocks.length) {
finishExperiment();
}
else {
exp_blocks[curr_block].next();
}
function finishExperiment() {
opts.on_finish(jsPsych.data.getData());
}
function createBlock(trial_list) {
var block = {
trial_idx: -1,
function doTrial(trial) {
trials: trial_list,
current_trial = trial;
data: [],
next: function() {
// trial_idx is -1 when block is created, so to start the first trial, trial_idx will be -1.
if(this.trial_idx > -1){
// handle callback at plugin level
if(typeof this.trials[this.trial_idx].on_finish === 'function') {
this.trials[this.trial_idx].on_finish(this.data[this.trial_idx]);
}
// handle callback at whole-experiment level
opts.on_trial_finish();
// update progress bar if shown
if(opts.show_progress_bar === true) {
updateProgressBar();
}
};
this.trial_idx = this.trial_idx + 1;
var curr_trial = this.trials[this.trial_idx];
if (typeof curr_trial == "undefined") {
return this.done();
}
// call on_trial_start()
// call experiment wide callback
opts.on_trial_start();
do_trial(this, curr_trial);
},
writeData: function(data_object) {
var progress = jsPsych.progress();
var default_data = {
'trial_type': this.trials[this.trial_idx].type,
'trial_index': this.trial_idx,
'trial_index_global': progress.current_trial_global,
'time_elapsed': jsPsych.totalTime(),
'block_index': curr_block
};
var ext_data_object = $.extend({}, data_object, default_data);
this.data[this.trial_idx] = ext_data_object;
opts.on_data_update(ext_data_object);
},
done: nextBlock,
num_trials: trial_list.length
};
return block;
}
function finishExperiment() {
opts["on_finish"].apply((new Object()), [core.data()]);
}
function do_trial(block, trial) {
// execute trial method
jsPsych[trial.type]["trial"].call(this, DOM_target, block, trial);
jsPsych[trial.type].trial(DOM_target, trial);
}
function drawProgressBar(){
function drawProgressBar() {
$('body').prepend($('<div id="jspsych-progressbar-container"><span>Completion Progress</span><div id="jspsych-progressbar-outer"><div id="jspsych-progressbar-inner"></div></div></div>'));
}
function updateProgressBar(){
function updateProgressBar() {
var progress = jsPsych.progress();
var percentComplete = 100 * ( (progress.current_trial_global+1) / progress.total_trials);
var percentComplete = 100 * ((progress.current_trial_global + 1) / progress.total_trials);
$('#jspsych-progressbar-inner').css('width', percentComplete+"%");
$('#jspsych-progressbar-inner').css('width', percentComplete + "%");
}
return core;
})();
jsPsych.dataAPI = (function() {
jsPsych.data = (function() {
var module = {};
// data storage object
var allData = [];
module.getData = function() {
return $.extend(true, [], allData); // deep clone
}
module.write = function(data_object) {
var progress = jsPsych.progress();
var trial = jsPsych.currentTrial();
var default_data = {
'trial_type': trial.type,
//'trial_index': progress.current_trial_local,
'trial_index_global': progress.current_trial_global,
'time_elapsed': jsPsych.totalTime(),
//'block_index': progress.current_block,
'internal_chunk_id': jsPsych.currentChunkID()
};
var ext_data_object = $.extend({}, data_object, default_data);
allData.push(ext_data_object);
var initSettings = jsPsych.initSettings();
initSettings.on_data_update(ext_data_object); //TODO: FIX callback?
}
// core.dataAsCSV returns a CSV string that contains all of the data
// append_data is an option map object that will append values
// to every row. for example, if append_data = {"subject": 4},
// then a column called subject will be added to the data and
// it will always have the value 4.
module.dataAsCSV = function(append_data) {
var dataObj = jsPsych.data();
var dataObj = module.getData();
return JSON2CSV(flattenData(dataObj, append_data));
};
@ -348,25 +535,25 @@
var data_string;
if(format == 'JSON' || format == 'json') {
data_string = JSON.stringify(flattenData(jsPsych.data(), append_data));
} else if(format == 'CSV' || format == 'csv') {
if (format == 'JSON' || format == 'json') {
data_string = JSON.stringify(flattenData(module.getData(), append_data));
} else if (format == 'CSV' || format == 'csv') {
data_string = module.dataAsCSV(append_data);
} else {
throw new Error('invalid format specified for jsPsych.dataAPI.localSave');
throw new Error('invalid format specified for jsPsych.data.localSave');
}
saveTextToFile(data_string, filename);
};
module.getTrialsOfType = function(trial_type){
var data = jsPsych.data();
module.getTrialsOfType = function(trial_type) {
var data = module.getData();
data = flatten(data);
var trials = [];
for(var i = 0; i < data.length; i++){
if(data[i].trial_type == trial_type){
for (var i = 0; i < data.length; i++) {
if (data[i].trial_type == trial_type) {
trials.push(data[i]);
}
}
@ -374,17 +561,32 @@
return trials;
};
module.getTrialsFromChunk = function(chunk_id) {
var data = module.getData();
data = flatten(data);
var trials = [];
for (var i = 0; i < data.length; i++) {
if (data[i].internal_chunk_id.slice(0, chunk_id.length) === chunk_id) { // TODO: change this to starts with
trials.push(data[i]);
}
}
return trials;
}
module.displayData = function(format) {
format = (typeof format === 'undefined') ? "json" : format.toLowerCase();
if(format != "json" && format != "csv") {
if (format != "json" && format != "csv") {
console.log('Invalid format declared for displayData function. Using json as default.');
format = "json";
}
var data_string;
if(format == 'json') {
data_string = JSON.stringify(flattenData(jsPsych.data()), undefined, 1);
if (format == 'json') {
data_string = JSON.stringify(flattenData(module.getData()), undefined, 1);
} else {
data_string = module.dataAsCSV();
}
@ -397,6 +599,7 @@
}
// private function to save text file on local drive
function saveTextToFile(textstr, filename) {
var blobToSave = new Blob([textstr], {
type: 'text/plain'
@ -404,8 +607,7 @@
var blobURL = "";
if (typeof window.webkitURL !== 'undefined') {
blobURL = window.webkitURL.createObjectURL(blobToSave);
}
else {
} else {
blobURL = window.URL.createObjectURL(blobToSave);
}
@ -426,6 +628,7 @@
//
// A few helper functions to handle data format conversion
//
function flattenData(data_object, append_data) {
append_data = (typeof append_data === undefined) ? {} : append_data;
@ -446,6 +649,7 @@
// this function based on code suggested by StackOverflow users:
// http://stackoverflow.com/users/64741/zachary
// http://stackoverflow.com/users/317/joseph-sturtevant
function JSON2CSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var line = '';
@ -465,7 +669,7 @@
}
}
line = line.slice(0, - 1);
line = line.slice(0, -1);
result += line + '\r\n';
for (var i = 0; i < array.length; i++) {
@ -476,7 +680,7 @@
line += '"' + valueString.replace(/"/g, '""') + '",';
}
line = line.slice(0, - 1);
line = line.slice(0, -1);
result += line + '\r\n';
}
@ -576,8 +780,8 @@
var rep_isArray = Array.isArray(repetitions);
// if array is not an array, then we just repeat the item
if(!arr_isArray){
if(!rep_isArray) {
if (!arr_isArray) {
if (!rep_isArray) {
array = [array];
repetitions = [repetitions];
} else {
@ -585,14 +789,14 @@
console.log('Unclear parameters given to randomizeSimpleSample. Multiple set sizes specified, but only one item exists to sample. Proceeding using the first set size.');
}
} else {
if(!rep_isArray) {
if (!rep_isArray) {
var reps = [];
for(var i = 0; i < array.length; i++){
for (var i = 0; i < array.length; i++) {
reps.push(repetitions);
}
repetitions = reps;
} else {
if(array.length != repetitions.length) {
if (array.length != repetitions.length) {
// throw warning if repetitions is too short,
// throw warning if too long, and then use the first N
}
@ -601,42 +805,44 @@
// should be clear at this point to assume that array and repetitions are arrays with == length
var allsamples = [];
for(var i = 0; i < array.length; i++){
for(var j = 0; j < repetitions[i]; j++){
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < repetitions[i]; j++) {
allsamples.push(array[i]);
}
}
var out = shuffle(allsamples);
if(unpack) { out = unpackArray(out); }
if (unpack) {
out = unpackArray(out);
}
return shuffle(out);
}
module.factorial = function(factors, repetitions, unpack){
module.factorial = function(factors, repetitions, unpack) {
var factorNames = Object.keys(factors);
var factor_combinations = [];
for(var i = 0; i < factors[factorNames[0]].length; i++){
for (var i = 0; i < factors[factorNames[0]].length; i++) {
factor_combinations.push({});
factor_combinations[i][factorNames[0]] = factors[factorNames[0]][i];
}
for(var i = 1; i< factorNames.length; i++){
for (var i = 1; i < factorNames.length; i++) {
var toAdd = factors[factorNames[i]];
var n = factor_combinations.length;
for(var j = 0; j < n; j++){
for (var j = 0; j < n; j++) {
var base = factor_combinations[j];
for(var k = 0; k < toAdd.length; k++){
for (var k = 0; k < toAdd.length; k++) {
var newpiece = {};
newpiece[factorNames[i]] = toAdd[k];
factor_combinations.push($.extend({}, base, newpiece));
}
}
factor_combinations.splice(0,n);
factor_combinations.splice(0, n);
}
repetitions = (typeof repetitions === 'undefined') ? 1 : repetitions;
@ -649,10 +855,10 @@
var out = {};
for(var i = 0; i < array.length; i++){
for (var i = 0; i < array.length; i++) {
var keys = Object.keys(array[i]);
for(var k = 0; k < keys.length; k++){
if(typeof out[keys[k]] === 'undefined') {
for (var k = 0; k < keys.length; k++) {
if (typeof out[keys[k]] === 'undefined') {
out[keys[k]] = [];
}
out[keys[k]].push(array[i][keys[k]]);
@ -663,7 +869,8 @@
}
function shuffle(array) {
var m = array.length, t, i;
var m = array.length,
t, i;
// While there remain elements to shuffle…
while (m) {
@ -725,8 +932,8 @@
}
for (var i = 0; i < valid_responses.length; i++) {
if (typeof valid_responses[i] == 'string') {
if(typeof keylookup[valid_responses[i]] !== 'undefined'){
if(e.which == keylookup[valid_responses[i]]) {
if (typeof keylookup[valid_responses[i]] !== 'undefined') {
if (e.which == keylookup[valid_responses[i]]) {
valid_response = true;
}
} else {
@ -741,12 +948,12 @@
var after_up = function(up) {
if(up.which == e.which) {
if (up.which == e.which) {
$(document).off('keyup', after_up);
if($.inArray(listener_id, keyboard_listeners) > -1) {
if ($.inArray(listener_id, keyboard_listeners) > -1) {
if(!persist){
if (!persist) {
// remove keyboard listener
module.cancelKeyboardResponse(listener_id);
}
@ -766,7 +973,10 @@
$(document).keydown(listener_function);
// create listener id object
listener_id = {type: 'keydown', fn: listener_function};
listener_id = {
type: 'keydown',
fn: listener_function
};
// add this keyboard listener to the list of listeners
keyboard_listeners.push(listener_id);
@ -780,13 +990,13 @@
$(document).off(listener.type, listener.fn);
// remove the listener from the list of listeners
if($.inArray(listener, keyboard_listeners) > -1) {
if ($.inArray(listener, keyboard_listeners) > -1) {
keyboard_listeners.splice($.inArray(listener, keyboard_listeners), 1);
}
};
module.cancelAllKeyboardResponses = function() {
for(var i = 0; i< keyboard_listeners.length; i++){
for (var i = 0; i < keyboard_listeners.length; i++) {
$(document).off(keyboard_listeners[i].type, keyboard_listeners[i].fn);
}
keyboard_listeners = [];
@ -803,9 +1013,9 @@
'pause': 19,
'capslock': 20,
'esc': 27,
'space':32,
'spacebar':32,
' ':32,
'space': 32,
'spacebar': 32,
' ': 32,
'pageup': 33,
'pagedown': 34,
'end': 35,
@ -939,8 +1149,7 @@
if (typeof trial[keys[i]] == "function" && process) {
tmp[keys[i]] = trial[keys[i]].call();
}
else {
} else {
tmp[keys[i]] = trial[keys[i]];
}
@ -961,7 +1170,7 @@
};
for (var i = 0; i < possible_arrays.length; i++) {
if(typeof params[possible_arrays[i]] !== 'undefined'){
if (typeof params[possible_arrays[i]] !== 'undefined') {
params[possible_arrays[i]] = ckArray(params[possible_arrays[i]]) ? params[possible_arrays[i]] : [params[possible_arrays[i]]];
}
}
@ -984,13 +1193,13 @@
// methods used in multiple modules
// private function to flatten nested arrays
function flatten(arr, out) {
out = (typeof out === 'undefined') ? [] : out;
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
flatten(arr[i], out);
}
else {
} else {
out.push(arr[i]);
}
}

View File

@ -27,7 +27,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -114,17 +114,17 @@
jsPsych.pluginAPI.cancelKeyboardResponse(response_listener);
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"animation_sequence": JSON.stringify(animation_sequence),
"responses": JSON.stringify(responses)
}, trial.data));
if(trial.timing_post_trial > 0){
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
}
};

View File

@ -21,15 +21,15 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
var return_val = trial.func.apply({}, [trial.args]);
if (typeof return_val !== 'undefined') {
block.writeData($.extend({},{
jsPsych.data.write($.extend({},{
value: return_val
},trial.data));
}
block.next();
jsPsych.finishTrial();
};
return plugin;

View File

@ -32,7 +32,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -135,7 +135,7 @@
"key_press": info.key
};
block.writeData($.extend({}, trial_data, trial.data));
jsPsych.data.write($.extend({}, trial_data, trial.data));
jsPsych.pluginAPI.cancelKeyboardResponse(keyboard_listener);
@ -148,10 +148,10 @@
display_element.html(''); // clear everything
if(trial.timing_post_trial > 0){
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
}
};

View File

@ -37,7 +37,7 @@
var cat_trial_complete = false;
plugin.trial = function(display_element, block, trial, part) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -95,7 +95,7 @@
"key_press": info.key
};
block.writeData($.extend({}, trial_data, trial.data));
jsPsych.data.write($.extend({}, trial_data, trial.data));
display_element.html('');
@ -157,10 +157,10 @@
display_element.html("");
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
}

View File

@ -31,7 +31,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -116,7 +116,7 @@
});
});
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"init_locations": JSON.stringify(init_locations),
"moves": JSON.stringify(moves),
"final_locations": JSON.stringify(final_locations),
@ -127,11 +127,11 @@
display_element.html("");
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
}
else {
block.next();
jsPsych.finishTrial();
}
}
}));

View File

@ -29,7 +29,7 @@ documentation: https://github.com/jodeleeuw/jsPsych/wiki/jspsych-html
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -46,7 +46,7 @@ documentation: https://github.com/jodeleeuw/jsPsych/wiki/jspsych-html
var finish = function() {
if (trial.check_fn && !trial.check_fn(display_element)) return;
if (trial.cont_key) $(document).unbind('keydown', key_listener);
block.writeData({
jsPsych.data.write({
rt: (new Date()).getTime() - t0,
url: trial.url
});
@ -56,12 +56,12 @@ documentation: https://github.com/jodeleeuw/jsPsych/wiki/jspsych-html
setTimeout(function() {
display_element.empty();
display_element.show();
block.next();
jsPsych.finishTrial();
}, trial.timing);
}
else {
display_element.empty();
block.next();
jsPsych.finishTrial();
}
};
if (trial.cont_btn) $('#' + trial.cont_btn).click(finish);

View File

@ -43,7 +43,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -257,7 +257,7 @@
var n_diff = arrayDifferences(trial.configurations, lineIsVisible);
var correct = (n_diff === 0);
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"configuration": JSON.stringify(lineIsVisible),
"target_configuration": JSON.stringify(trial.configurations),
"rt": response_time,
@ -302,11 +302,11 @@
// next trial
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
}
else {
block.next();
jsPsych.finishTrial();
}
}

View File

@ -37,7 +37,7 @@
var sd_trial_complete = false;
plugin.trial = function(display_element, block, trial, part) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -122,16 +122,16 @@
"stimulus_2": trial.b_path,
"key_press": info.key
};
block.writeData($.extend({}, trial_data, trial.data));
jsPsych.data.write($.extend({}, trial_data, trial.data));
display_element.html('');
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
}

View File

@ -40,7 +40,7 @@
var sim_trial_complete = false;
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -63,7 +63,7 @@
}
if (trial.show_response == "FIRST_STIMULUS") {
show_response_slider(display_element, trial, block);
show_response_slider(display_element, trial);
}
setTimeout(function() {
@ -91,7 +91,7 @@
$('#jspsych_sim_stim').css('visibility', 'visible');
if (trial.show_response == "SECOND_STIMULUS") {
show_response_slider(display_element, trial, block);
show_response_slider(display_element, trial);
}
if (trial.timing_second_stim > 0) {
@ -99,7 +99,7 @@
if (!sim_trial_complete) {
$("#jspsych_sim_stim").css('visibility', 'hidden');
if (trial.show_response == "POST_STIMULUS") {
show_response_slider(display_element, trial, block);
show_response_slider(display_element, trial);
}
}
}, trial.timing_second_stim);
@ -107,7 +107,7 @@
}
function show_response_slider(display_element, trial, block) {
function show_response_slider(display_element, trial) {
var startTime = (new Date()).getTime();
@ -195,7 +195,7 @@
var response_time = endTime - startTime;
sim_trial_complete = true;
var score = $("#slider").slider("value");
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"sim_score": score,
"rt": response_time,
"stimulus": trial.a_path,
@ -205,10 +205,10 @@
display_element.html('');
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
});
}

View File

@ -38,7 +38,7 @@
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -85,7 +85,7 @@
"key_press": info.key
};
block.writeData($.extend({}, trial_data, trial.data));
jsPsych.data.write($.extend({}, trial_data, trial.data));
// clear the display
display_element.html('');
@ -93,10 +93,10 @@
// move on to the next trial
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
};

View File

@ -29,7 +29,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -143,7 +143,7 @@
});
// save data
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"rt": response_time
}, question_data, trial.data));
@ -151,9 +151,9 @@
// next trial
if(trial.timing_post_trial > 0){
setTimeout(function(){ block.next(); }, trial.timing_post_trial);
setTimeout(function(){ jsPsych.finishTrial(); }, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
});

View File

@ -26,7 +26,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -70,7 +70,7 @@
});
// save data
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"rt": response_time
}, question_data, trial.data));
@ -78,9 +78,9 @@
// next trial
if(trial.timing_post_trial > 0){
setTimeout(function(){ block.next(); }, trial.timing_post_trial);
setTimeout(function(){ jsPsych.finishTrial(); }, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
});

View File

@ -27,7 +27,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -45,12 +45,12 @@
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
}
else {
block.next();
} // call block.next() to advance the experiment after a delay.
jsPsych.finishTrial();
}
};
@ -73,7 +73,7 @@
}
function save_data(key, rt) {
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"rt": rt,
"key_press": key
}, trial.data));

View File

@ -41,7 +41,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
trial = jsPsych.pluginAPI.normalizeTrialVariables(trial);
@ -169,15 +169,15 @@
// this line merges together the trial_data object and the generic
// data object (trial.data), and then stores them.
block.writeData($.extend({}, trial_data, trial.data));
jsPsych.data.write($.extend({}, trial_data, trial.data));
// go to next trial
if(trial.timing_post_trial > 0){
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
}
};

View File

@ -35,7 +35,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -144,18 +144,18 @@
jsPsych.pluginAPI.cancelKeyboardResponse(key_listener);
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"stimuli": JSON.stringify(trial.stims),
"responses": JSON.stringify(responses)
}, trial.data));
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
}
else {
block.next();
jsPsych.finishTrial();
}
}
};

View File

@ -31,7 +31,7 @@
return trials;
};
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -48,17 +48,17 @@
display_element.html('');
block.writeData($.extend({}, {
jsPsych.data.write($.extend({}, {
"stimuli": JSON.stringify(trial.stimuli)
}, trial.data));
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
}
else {
block.next();
jsPsych.finishTrial();
}
}
};

View File

@ -48,7 +48,7 @@
var xab_trial_complete = false;
plugin.trial = function(display_element, block, trial) {
plugin.trial = function(display_element, trial) {
// if any trial variables are functions
// this evaluates the function and replaces
@ -162,7 +162,7 @@
"stimulus_b": trial.b_path,
"key_press": info.key
};
block.writeData($.extend({}, trial_data, trial.data));
jsPsych.data.write($.extend({}, trial_data, trial.data));
display_element.html(''); // remove all
@ -171,10 +171,10 @@
// move on to the next trial after timing_post_trial milliseconds
if (trial.timing_post_trial > 0) {
setTimeout(function() {
block.next();
jsPsych.finishTrial();
}, trial.timing_post_trial);
} else {
block.next();
jsPsych.finishTrial();
}
};