-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition.js
More file actions
221 lines (179 loc) · 6.06 KB
/
condition.js
File metadata and controls
221 lines (179 loc) · 6.06 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* This class stores a condition with the associated factors and levels
*
* @constructor
*/
var Condition = function (factorGroup) {
var self = this;
this.factorGroup = factorGroup;
// serialized
this.trials = ko.observableArray([]);
this.relativePercentage = ko.observable(100);
// not serialized
this.factorLevels = ko.observableArray([]); // level_obj1, level_ob2
this.trialStartIdx = ko.observable(0);
this.conditionIdx = ko.observable();
this.trialStartIdx.subscribe(function (newTrialStartIdx) {
var trialVariations = self.trials();
for (var i = 0; i < trialVariations.length; i++) {
trialVariations[i].trialIdx(newTrialStartIdx + i);
}
});
this.isDeactivated = ko.computed(function () {
if (self.trials().length > 0) {
return false;
}
else {
return true;
}
}, this);
this.conditionGroup = ko.observable(null);
/**
this.conditionGroup = ko.computed(function() {
return self.getCondGroup();
}, this);
**/
/** instance reference to other condition in the same condition group **/
};
Condition.prototype.getPartnerConditions = function (condGroups) {
var arr = condGroups[this.getCondGroup(condGroups) - 1];
var conds = [];
if (arr) {
for (var i = 0; i < arr.length; i++) {
if (this.factorGroup.conditionsLinear()[arr[i]]) {
if (this.factorGroup.conditionsLinear()[arr[i]] !== this && this.factorGroup.conditionsLinear()[arr[i]].isDeactivated() == false) {
conds.push(this.factorGroup.conditionsLinear()[arr[i]])
}
}
else {
console.log("Warning: condition is not there")
}
}
}
return conds;
};
Condition.prototype.setCondGroup = function (condGroups) {
this.conditionGroup(this.getCondGroup(condGroups));
};
Condition.prototype.getCondGroup = function (condGroups) {
var groups = condGroups;
var found = false;
var idx = 0;
var out = 1;
while (!found && idx < groups.length) {
var currentGroup = groups[idx];
if (currentGroup.indexOf(this.conditionIdx() - 1) >= 0) {
found = true;
out = idx + 1;
}
idx++;
}
return out;
};
Condition.prototype.getCurrentValueOfFactor = function (idOfGlobalVar) {
var levelValue = null;
var levelNames = [];
for (var i = 0; i < this.factorLevels().length; i++) {
levelNames.push(this.factorLevels()[i].name());
}
var facIds = [];
for (var i = 0; i < this.factorGroup.factors().length; i++) {
facIds.push(this.factorGroup.factors()[i].globalVar().id());
}
var idx = facIds.indexOf(idOfGlobalVar);
if (idx >= 0) {
levelValue = levelNames[idx];
}
return levelValue;
};
/**
* Initializes a new instance with just one trial variation. This function is usually called after the constructor
* created a new instance.
*/
Condition.prototype.initNewInstance = function () {
this.addTrial();
};
/**
* 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.
*/
Condition.prototype.setPointers = function (entitiesArr) {
};
/**
* This function sets the number of trials in this condition to a specified value.
* @param {number} numTrialVariations - The new total number of trials in this condition.
*/
Condition.prototype.setNumTrials = function (numTrialVariations) {
var lengthToBe = parseInt(numTrialVariations);
var trialVariations = this.trials();
var currentLength = trialVariations.length;
if (currentLength != lengthToBe) {
var diff, i;
if (currentLength > lengthToBe) {
diff = currentLength - lengthToBe;
for (i = 0; i < diff; i++) {
this.removeTrialVariation();
}
this.trials(trialVariations);
}
else if (currentLength < lengthToBe) {
diff = lengthToBe - currentLength;
for (i = 0; i < diff; i++) {
this.addTrial();
}
}
// now apply the same amount of trials to all other sub-conditions in the same condition group
/**
this.conditionGroupArray().forEach(function (condition) {
if (condition.trials().length != parseInt(numTrialVariations)){
condition.setNumTrials(numTrialVariations);
}
})
**/
}
};
/**
* adds a new trial to this condition.
*/
Condition.prototype.addTrial = function () {
var trialVariations = new TrialVariation(this);
trialVariations.nr(this.trials().length);
this.trials.push(trialVariations);
};
/**
* removes the last trial from this condition.
*/
Condition.prototype.removeTrialVariation = function () {
this.trials.pop();
};
/**
* load from a json object to deserialize the states.
* @param {object} data - the json description of the states.
* @returns {Condition}
*/
Condition.prototype.fromJS = function (data) {
var self = this;
this.trials(jQuery.map(data.trials, function (trial, index) {
var trialVariation = new TrialVariation(self);
trialVariation.fromJS(trial);
trialVariation.nr(index);
return trialVariation;
}));
if (data.hasOwnProperty('relativePercentage')) {
this.relativePercentage(data.relativePercentage);
}
return this;
};
/**
* serialize the state of this instance into a json object, which can later be restored using the method fromJS.
* @returns {{trials: *}}
*/
Condition.prototype.toJS = function () {
return {
trials: jQuery.map(this.trials(), function (trial) { return trial.toJS(); }),
relativePercentage: this.relativePercentage()
};
};