Rescoped everything so that jsPsych is not a jQuery plugin.

The top level object is jsPsych, and plugins are defined as jsPsych.PLUGIN.

The library also loads everything dynamically now, so the user only needs to load the main jsPsych script (and specify the src for any plugins).

This also changes how the library is loaded initially, since it is not based on the jQuery method anymore. See wiki.
This commit is contained in:
Josh de Leeuw 2012-05-17 09:19:04 -04:00
parent 1d1a9c7e34
commit befdcfc1cd
9 changed files with 71 additions and 37 deletions

View File

@ -2,7 +2,7 @@
// Josh de Leeuw // Josh de Leeuw
(function( $ ) { (function( $ ) {
$.fn.jsPsych.animation = (function(){ jsPsych.animation = (function(){
var plugin = {}; var plugin = {};
@ -12,7 +12,7 @@
for(var i = 0; i < trials.length; i++) for(var i = 0; i < trials.length; i++)
{ {
trials[i] = {}; trials[i] = {};
trials[i]["type"] = "animate"; trials[i]["type"] = "animation";
trials[i]["stims"] = stims[i]; trials[i]["stims"] = stims[i];
trials[i]["frame_time"] = params["frame_time"]; trials[i]["frame_time"] = params["frame_time"];
trials[i]["repetitions"] = params["repetitions"]; trials[i]["repetitions"] = params["repetitions"];

View File

@ -6,7 +6,7 @@
// way to provide corrective feedback // way to provide corrective feedback
(function( $ ) { (function( $ ) {
$.fn.jsPsych.categorize-feedback = (function(){ jsPsych.categorize-feedback = (function(){
var plugin = {}; var plugin = {};
@ -16,7 +16,7 @@
for(var i = 0; i < trials.length; i++) for(var i = 0; i < trials.length; i++)
{ {
trials[i] = {}; trials[i] = {};
trials[i]["type"] = "cf"; trials[i]["type"] = "categorize-feedback";
trials[i]["a_path"] = cf_stims[i]; trials[i]["a_path"] = cf_stims[i];
trials[i]["timing"] = params["timing"]; trials[i]["timing"] = params["timing"];
trials[i]["key_answer"] = params["key_answer"][i]; trials[i]["key_answer"] = params["key_answer"][i];

View File

@ -2,7 +2,7 @@
// if optional length to display target is missing, then target is displayed until subject responds. // if optional length to display target is missing, then target is displayed until subject responds.
(function( $ ) { (function( $ ) {
$.fn.jsPsych.categorize-unknown = (function(){ jsPsych.categorize-unknown = (function(){
var plugin = {}; var plugin = {};
@ -12,7 +12,7 @@
for(var i = 0; i < trials.length; i++) for(var i = 0; i < trials.length; i++)
{ {
trials[i] = {}; trials[i] = {};
trials[i]["type"] = "cu"; trials[i]["type"] = "categorize-unknown";
trials[i]["a_path"] = cu_stims[i]; trials[i]["a_path"] = cu_stims[i];
trials[i]["timing"] = params["timing"]; trials[i]["timing"] = params["timing"];
trials[i]["choices"] = params["choices"]; trials[i]["choices"] = params["choices"];

View File

@ -1,5 +1,5 @@
(function( $ ) { (function( $ ) {
$.fn.jsPsych.samedifferent = (function(){ jsPsych.samedifferent = (function(){
var plugin = {}; var plugin = {};
@ -9,7 +9,7 @@
for(var i = 0; i < trials.length; i++) for(var i = 0; i < trials.length; i++)
{ {
trials[i] = {}; trials[i] = {};
trials[i]["type"] = "sd"; trials[i]["type"] = "samedifferent";
trials[i]["a_path"] = sd_stims[i][0]; trials[i]["a_path"] = sd_stims[i][0];
trials[i]["b_path"] = sd_stims[i][1]; trials[i]["b_path"] = sd_stims[i][1];
trials[i]["timing"] = params["timing"]; trials[i]["timing"] = params["timing"];

View File

@ -1,5 +1,5 @@
(function( $ ) { (function( $ ) {
$.fn.jsPsych.similarity = (function(){ jsPsych.similarity = (function(){
var plugin = {}; var plugin = {};
@ -9,7 +9,7 @@
for(var i = 0; i < trials.length; i++) for(var i = 0; i < trials.length; i++)
{ {
trials[i] = {}; trials[i] = {};
trials[i]["type"] = "sim"; trials[i]["type"] = "similarity";
trials[i]["a_path"] = sim_stims[i][0]; trials[i]["a_path"] = sim_stims[i][0];
trials[i]["b_path"] = sim_stims[i][1]; trials[i]["b_path"] = sim_stims[i][1];
trials[i]["timing"] = params["timing"]; trials[i]["timing"] = params["timing"];

View File

@ -1,6 +1,6 @@
(function( $ ) { (function( $ ) {
$.fn.jsPsych.text = (function(){ jsPsych.text = (function(){
var plugin = {}; var plugin = {};

View File

@ -1,5 +1,5 @@
(function( $ ) { (function( $ ) {
$.fn.jsPsych.xab-touch = (function(){ jsPsych.xab-touch = (function(){
var plugin = {}; var plugin = {};
@ -10,7 +10,7 @@
for(var i = 0; i < trials.length; i++) for(var i = 0; i < trials.length; i++)
{ {
trials[i] = {}; trials[i] = {};
trials[i]["type"] = "xab_touch"; trials[i]["type"] = "xab-touch";
trials[i]["a_path"] = xab_stims[i][0]; trials[i]["a_path"] = xab_stims[i][0];
trials[i]["b_path"] = xab_stims[i][1]; trials[i]["b_path"] = xab_stims[i][1];
trials[i]["timing"] = params["timing"]; trials[i]["timing"] = params["timing"];

View File

@ -1,6 +1,6 @@
(function( $ ) { (function( $ ) {
$.fn.jsPsych.xab = (function(){ jsPsych.xab = (function(){
var plugin = {} var plugin = {}

View File

@ -1,22 +1,65 @@
// jspsych.js - a jQuery plugin for running psychology experiments // jspsych.js
// //
// Josh de Leeuw and Drew Hendrickson // Josh de Leeuw and Drew Hendrickson
// Percepts and Concepts Lab, Indiana University // Percepts and Concepts Lab, Indiana University
// //
(function( $ ) { (function( $ ) {
$.fn.jsPsych = function(options) { jsPsych = (function() {
// build main options list before element iteration
var opts = $.extend({}, $.fn.jsPsych.defaults, options); //
// public object
//
var core = {};
//
// private class variables
//
// options
var opts = {};
// exp structure // exp structure
var exp_blocks = []; var exp_blocks = [];
// flow control
var curr_block = 0; var curr_block = 0;
// execute experiment // everything loaded?
return this.each(function() { var initialized = false;
$this = $(this); // target DOM element
var DOM_target;
//
// public methods
//
core.init = function($this, options){
// import options
opts = $.extend({}, jsPsych.defaults, options);
// set target
DOM_target = $this;
// load plugin script files
var scripts_loaded = 0;
// load all of the plugins that are defined in the opts["plugins"]
for(var j = 0; j < opts["plugins"].length; j++)
{
$.getScript(opts["plugins"][j]["src"], function(){
scripts_loaded++;
if(scripts_loaded==opts["plugins"].length) {
intialized = true;
run();
}
});
}
}
//
// private functions //
//
function run()
{
// take the experiment structure and dynamically create a set of blocks // take the experiment structure and dynamically create a set of blocks
exp_blocks = new Array(opts["experiment_structure"].length); exp_blocks = new Array(opts["experiment_structure"].length);
// iterate through block list to create trials
for(var i = exp_blocks.length-1; i>=0; i--) for(var i = exp_blocks.length-1; i>=0; i--)
{ {
var trials = "undefined"; var trials = "undefined";
@ -25,20 +68,17 @@
{ {
if(opts["experiment_structure"][i]["type"] == opts["plugins"][j]["type"]) if(opts["experiment_structure"][i]["type"] == opts["plugins"][j]["type"])
{ {
trials = opts["plugins"][j]["src"]["create"].call(null, opts["experiment_structure"][i]); trials = jsPsych[opts["plugins"][j]["type"]]["create"].call(null, opts["experiment_structure"][i]);
} }
} }
exp_blocks[i] = createBlock(trials); exp_blocks[i] = createBlock(trials);
} }
// run the first block // run the first block
exp_blocks[0].next(); exp_blocks[0].next();
}); }
//
// private functions //
//
function nextBlock() function nextBlock()
{ {
curr_block += 1; curr_block += 1;
@ -94,19 +134,13 @@
{ {
if(trial.type == opts["plugins"][j]["type"]) if(trial.type == opts["plugins"][j]["type"])
{ {
opts["plugins"][j]["src"]["trial"].call(this, $this, block, trial, 1); jsPsych[opts["plugins"][j]["type"]]["trial"].call(this, DOM_target, block, trial, 1);
} }
} }
} }
};
// return core;
// default parameters // })();
//
$.fn.jsPsych.defaults = {
// overall experiment parameters
experiment_structure: [],
plugins: []
};
}) (jQuery); }) (jQuery);