-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpSession.js
More file actions
93 lines (81 loc) · 2.97 KB
/
expSession.js
File metadata and controls
93 lines (81 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// � by Caspar Goeke and Holger Finger
/**
* Stores the specification of one experimental session.
*
* @param expData
* @constructor
*/
var ExpSession = function (expData) {
this.expData = expData;
this.id = ko.observable(guid());
this.name = ko.observable("session_1");
this.type = "ExpSession";
this.blockRandomization = ko.observable('fixed');
// the following array is extended with sortById to fix a bug with ko-sortable when adding new sub items in a newly created item:
this.blocks = ko.observableArray([]).extend({ sortById: { do_not_warn_when_double_entries: true } });
};
ExpSession.prototype.addBlock = function (block) {
this.blocks.push(block);
};
ExpSession.prototype.removeBlock = function (data, event) {
var idx = this.blocks().indexOf(data);
while (idx > -1) {
var tmpArr = this.blocks();
tmpArr.splice(idx, 1);
this.blocks(tmpArr);
idx = this.blocks().indexOf(data); // check for more in loop
}
};
/**
* This function initializes all internal state variables to point to other instances in the same experiment. Usually
* this is called after ALL experiment instances were deserialized using fromJS(). In this function use
* 'entitiesArr.byId[id]' to retrieve an instance from the global list given some unique id.
*
* @param {ko.observableArray} entitiesArr - this is the knockout array that holds all instances.
*/
ExpSession.prototype.setPointers = function (entitiesArr) {
// convert ids to actual pointers:
this.blocks(jQuery.map(this.blocks(), function (id) {
return entitiesArr.byId[id];
}));
};
/**
* Recursively adds all child objects that have a unique id to the global list of entities.
*
* @param {ko.observableArray} entitiesArr - this is the knockout array that holds all instances.
*/
ExpSession.prototype.reAddEntities = function (entitiesArr) {
// add the direct child nodes:
jQuery.each(this.blocks(), function (index, elem) {
// check if they are not already in the list:
if (!entitiesArr.byId.hasOwnProperty(elem.id()))
entitiesArr.push(elem);
// recursively make sure that all deep tree nodes are in the entities list:
elem.reAddEntities(entitiesArr);
});
};
/**
* load from a json object to deserialize the states.
* @param {object} data - the json description of the states.
* @returns {ExpSession}
*/
ExpSession.prototype.fromJS = function (data) {
this.id(data.id);
this.name(data.name);
this.blocks(data.blocks);
this.blockRandomization(data.blockRandomization);
return this;
};
/**
* serialize the state of this instance into a json object, which can later be restored using the method fromJS.
* @returns {object}
*/
ExpSession.prototype.toJS = function () {
return {
id: this.id(),
name: this.name(),
type: this.type,
blockRandomization: this.blockRandomization(),
blocks: jQuery.map(this.blocks(), function (elem) { return elem.id(); })
};
};