-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifier.js
More file actions
792 lines (668 loc) · 28.6 KB
/
modifier.js
File metadata and controls
792 lines (668 loc) · 28.6 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
// � by Caspar Goeke and Holger Finger
/**
* stores the modifications of properties for different trials.
*
* @param {ExpData} expData - the global expData where instances can be retrieved.
* @param objToModify - the content element of which the properties should be modified.
* @constructor
*/
var Modifier = function (expData, objToModify) {
this.expData = expData;
this.objToModify = objToModify;
this.factorGroup = null; // this is dynamically retrieved by this.getFactorGroup()...
// serialized:
this.factors = ko.observableArray([]); // list of N interacting factors
this.ndimModifierTrialTypes = []; // N-dim Array of ModifierTrialTypes (each dimension corresponds to one factor)
this.dependsOnTrialVariations = ko.observable(false);
this.type = "Modifier";
// helpers:
this.selectedTrialType = ko.observable({ type: 'allTrials' });
this.selectedTrialView = {};
var modifiableProp = objToModify.modifiableProp;
for (var i = 0; i < modifiableProp.length; i++) {
var propName = modifiableProp[i];
this.addProp(propName);
}
};
/**
* Recursive function to select an element from a multidimensional array.
* For example selectFromMultiDimArr(arr, [2,6,3]) will return the value of arr[2][6][3].
*
* @param {Array} arr - the multidimensional array
* @param {Array} indices - the indices of each dimension
* @returns {*}
*/
function selectFromMultiDimArr(arr, indices) {
var current_array = arr;
for (var i = 0; i < indices.length; i++) {
if (!(current_array instanceof Array)) {
// if there were more indices than dimensions return undefined:
return undefined;
}
current_array = current_array[indices[i]];
}
return current_array;
}
/**
* Recursive function to set an element in a multidimensional array.
* For example setMultiDimArr(arr, [2,6,3], elem) will set the value of arr[2][6][3] to elem.
*
* @param {Array} arr - the multidimensional array
* @param {Array} indices - the indices of each dimension
* @param {Array} elem - the element to set
* @returns {*}
*/
function setMultiDimArr(arr, indices, elem) {
var current_array = arr;
for (var i = 0; i < indices.length - 1; i++) {
current_array = current_array[indices[i]];
}
current_array[indices[indices.length - 1]] = elem;
}
/**
* deep clones a multi dimensional array
* @param multiDimArr - the most inner object which is not an array must implement a method deepCopy()
* @returns {*}
*/
function deepCloneMultiDimArr(multiDimArr) {
if (multiDimArr instanceof Array) {
var clonedSubArr = [];
// recursive call:
for (var t = 0; t < multiDimArr.length; t++) {
clonedSubArr.push(deepCloneMultiDimArr(multiDimArr[t]));
}
return clonedSubArr;
}
else {
return multiDimArr.deepCopy();
}
}
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
Modifier.prototype.getFlattendArray = function () {
return flatten(this.ndimModifierTrialTypes);
};
/**
* get the factor group on which this modifier depends on.
* @returns {FactorGroup}
*/
Modifier.prototype.getFactorGroup = function () {
if (this.factorGroup == null) {
var parent = this.objToModify;
while (parent != null && !parent.hasOwnProperty("factorGroup")) {
parent = parent.parent;
}
if (parent == null) {
console.error('could not locate factor group.');
}
else {
this.factorGroup = parent.factorGroup;
}
}
return this.factorGroup;
};
/**
* adds a new modification to this object in the currently selected trial type.
*
* @param {string} propName - the name of the property to be modified.
*/
Modifier.prototype.addProp = function (propName) {
// TODO: performance improvement so that the pureComputed is only executed for properties that were really modified
// in some condtion/trial. For example use the following to save if a property depends on trials at all:
// Example: this.propIsModifiedSomewhere[propName] = ko.observable(false);
this.selectedTrialView[propName] = ko.pureComputed({
/**
* returns the value of the property based on the currently selected trial.
* @returns {number | string}
*/
read: function () {
var selectedTrialType = this.selectedTrialType();
var selectionType = selectedTrialType.type;
var factors = this.factors();
if (selectionType == 'factorLevel') {
var factorSelected = selectedTrialType.factor;
var factorIdx = factors.indexOf(factorSelected);
// check if this element depends on the selected factor:
if (!(factorIdx > -1)) {
// return the property of parent:
return this.objToModify[propName]();
}
// construct indices:
var indices = [];
for (var t = 0; t < factors.length; t++) {
indices.push(0);
}
indices[factorIdx] = selectedTrialType.levelIdx;
// find first matching modifierTrialType with the given level of the selected factor:
var modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
if (!modifierTrialType) {
// number of dimensions of ndimModifierTrialTypes does not match number of indices:
this.addInteractingLevels();
// repeat:
modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
}
// if there were modifications of specific trialVariations:
if (this.dependsOnTrialVariations()) {
modifierTrialType = modifierTrialType[0];
}
// check if this property is modified:
if (modifierTrialType.propIsModified[propName]()) {
return modifierTrialType.modifiedProp[propName]();
}
else {
return this.objToModify[propName]();
}
}
else if (selectionType == 'condition') {
var factorsSelected = selectedTrialType.allFactors;
var levelsSelected = selectedTrialType.allLevelIdx;
// construct indices:
var indices = [];
for (var t = 0; t < factors.length; t++) {
var factorIdx = factorsSelected.indexOf(factors[t]);
if (factorIdx == -1) {
// this factor is not in factor group anymore, so remove dependency...
this.removeFactorDependency(factors[t])
}
else {
indices.push(levelsSelected[factorIdx]);
}
}
if (factors.length == 0) {
var val = this.objToModify[propName]();
//console.log("reading parent val="+val+ " because no dependencies yet.")
return val;
}
// find matching modifierTrialType with the given levels of the selected factors:
var modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
if (!modifierTrialType) {
// number of dimensions of ndimModifierTrialTypes does not match number of indices:
this.addInteractingLevels();
// repeat:
modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
}
// if there were modifications of specific trialVariations:
if (this.dependsOnTrialVariations()) {
modifierTrialType = modifierTrialType[0];
}
// check if this property is modified:
if (modifierTrialType.propIsModified[propName]()) {
return modifierTrialType.modifiedProp[propName]();
}
else {
return this.objToModify[propName]();
}
}
else if (selectionType == 'trialVariation') {
var factorsSelected = selectedTrialType.allFactors;
var levelsSelected = selectedTrialType.allLevelIdx;
var selectedTrialIdx = selectedTrialType.trialNr;
// construct indices:
var indices = [];
for (var t = 0; t < factors.length; t++) {
var factorIdx = factorsSelected.indexOf(factors[t]);
if (factorIdx == -1) {
// this factor is not in factor group anymore, so remove dependency...
this.removeFactorDependency(factors[t])
}
else {
indices.push(levelsSelected[factorIdx]);
}
}
if (this.dependsOnTrialVariations()) {
indices.push(selectedTrialIdx);
}
if (factors.length == 0) {
var val = this.objToModify[propName]();
return val;
}
// find matching modifierTrialType with the given levels of the selected factors:
var modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
if (!modifierTrialType) {
// number of dimensions of ndimModifierTrialTypes does not match number of indices:
this.addInteractingLevels();
// repeat:
modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
}
// check if this property is modified:
if (modifierTrialType.propIsModified[propName]()) {
return modifierTrialType.modifiedProp[propName]();
}
else {
return this.objToModify[propName]();
}
}
else {
// 'default' all trials
return this.objToModify[propName]();
}
},
/**
* writes the value of the property for the currently selected trial type.
* @returns {number | string}
*/
write: function (value) {
var selectedTrialType = this.selectedTrialType();
var selectionType = selectedTrialType.type;
var factors = this.factors();
if (selectionType == 'factorLevel') {
var factorSelected = selectedTrialType.factor;
var factorIdx = factors.indexOf(factorSelected);
// check if this element already depends on the selected factor:
if (!(factorIdx > -1)) {
// add new factor dependency:
this.addFactorDependency(factorSelected);
factorIdx = factors.indexOf(factorSelected);
}
// modify all existing modifierTrialTypes with the given level of the selected factor:
function modifyAll(modifierTrialTypes, factorIdx, levelIdx, value) {
if (modifierTrialTypes.constructor === Array) {
if (factorIdx == 0) {
// here we select the corresponding level of the specified factor:
modifyAll(modifierTrialTypes[levelIdx], null, null, value);
}
else {
// modify all variations of all other factors:
for (var l = 0; l < modifierTrialTypes.length; l++) {
modifyAll(modifierTrialTypes[l], factorIdx - 1, levelIdx, value);
}
}
}
else {
modifierTrialTypes.setModification(propName, value);
}
}
modifyAll(this.ndimModifierTrialTypes, factorIdx, selectedTrialType.levelIdx, value);
}
else if (selectionType == 'condition') {
var factorsSelected = selectedTrialType.allFactors;
var levelsSelected = selectedTrialType.allLevelIdx;
// make sure to add all dependencies of all factors:
this._addAllConditionDep();
// construct indices:
var indices = [];
for (var t = 0; t < factorsSelected.length; t++) {
var factorIdx = factorsSelected.indexOf(factors[t]);
indices.push(levelsSelected[factorIdx]);
}
// find matching modifierTrialType with the given levels of the selected factors:
var modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
if (!modifierTrialType) {
// number of dimensions of ndimModifierTrialTypes does not match number of indices:
console.log("this level was not specified previously... make sure that all levels of dependent factors are added...");
this.addInteractingLevels();
modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
}
// if there is a dependency on trialVariations:
if (this.dependsOnTrialVariations()) {
// modify all sub trials:
for (var t = 0; t < modifierTrialType.length; t++) {
modifierTrialType[t].setModification(propName, value);
}
}
else {
modifierTrialType.setModification(propName, value);
}
}
else if (selectionType == 'trialVariation') {
var factorsSelected = selectedTrialType.allFactors;
var levelsSelected = selectedTrialType.allLevelIdx;
var selectedTrialIdx = selectedTrialType.trialNr;
// make sure to add dependency on all trials:
this._addTrialVariationDep();
// construct indices:
var indices = [];
for (var t = 0; t < factors.length; t++) {
var factorIdx = factorsSelected.indexOf(factors[t]);
indices.push(levelsSelected[factorIdx]);
}
indices.push(selectedTrialIdx);
// find matching modifierTrialType with the given levels of the selected factors:
var modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
if (!modifierTrialType) {
// number of dimensions of ndimModifierTrialTypes does not match number of indices:
console.log("this level was not specified previously... make sure that all levels of dependent factors are added...");
this.addInteractingLevels();
modifierTrialType = selectFromMultiDimArr(this.ndimModifierTrialTypes, indices);
}
modifierTrialType.setModification(propName, value);
}
else {
// 'default' all trials
this.objToModify[propName](value);
// remove all modifications that were made to this property in any trial types:
this.removeModificationRecursive(this.ndimModifierTrialTypes, propName);
}
},
owner: this
});
};
/**
* make sure that this modifier depends on all factors.
* @private
*/
Modifier.prototype._addAllConditionDep = function () {
var allFactors = this.getFactorGroup().factors();
var depFactors = this.factors();
for (var f = 0; f < allFactors.length; f++) {
if (!(depFactors.indexOf(allFactors[f]) > -1)) {
// add new factor dependency:
this.addFactorDependency(allFactors[f]);
}
}
};
/**
* make sure that this modifier depends on trialVariations.
* @private
*/
Modifier.prototype._addTrialVariationDep = function () {
// check if dependency is already there:
if (this.dependsOnTrialVariations()) {
return;
}
this._addAllConditionDep();
function deepClone(multiDimArr, multiDimArrConditions) {
if (multiDimArr[0].constructor === Array) {
// recursive call:
for (var t = 0; t < multiDimArrConditions.length; t++) {
deepClone(multiDimArr[t], multiDimArrConditions[t]);
}
}
else {
// create new array of new interacting trialTypes with all combinations:
for (var t = 0; t < multiDimArrConditions.length; t++) {
var oldMod = multiDimArr[t];
var numTrials = multiDimArrConditions[t].trials().length;
// add all trials:
var newModfiers = [];
for (var l = 0; l < numTrials; l++) {
newModfiers.push(oldMod.deepCopy());
}
// overwrite object in multiDimArr[t] with array of cloned objects:
multiDimArr[t] = newModfiers;
}
}
}
// one more dimension of interacting trialTypes with all combinations:
deepClone(this.ndimModifierTrialTypes, this.getFactorGroup().conditions());
// add new interacting factor:
this.dependsOnTrialVariations(true);
};
/**
* add a dependency on a new factor variable.
* @param factorVar
*/
Modifier.prototype.addFactorDependency = function (factorVar) {
function deepCloneOuter(multiDimArr, numNewLevels, remainingDepthTillClone) {
var t;
if (remainingDepthTillClone > 0) {
// recursive call:
for (t = 0; t < multiDimArr.length; t++) {
multiDimArr[t] = deepCloneOuter(multiDimArr[t], numNewLevels, remainingDepthTillClone - 1);
}
return multiDimArr;
}
else {
// create new array of new trialTypes with all combinations:
var clonedMultiDimArr = [];
for (var i = 0; i < numNewLevels; i++) {
clonedMultiDimArr.push(deepCloneMultiDimArr(multiDimArr));
}
return clonedMultiDimArr;
}
}
var allFactors = this.getFactorGroup().factors();
var depFactors = this.factors();
var numNewLevels = factorVar.globalVar().levels().length;
if (this.factors().length == 0) {
// special case, because this is the first factor dependency. Therefore just add all levels to top array:
// add all levels of this new factor:
for (var l = 0; l < numNewLevels; l++) {
// add this level of the non-interacting factor:
this.ndimModifierTrialTypes.push(new ModifierTrialType(this.expData, this.objToModify));
}
// add new interacting factor:
this.factors.push(factorVar);
}
else {
// find the previous factor that is already a dependent Factor:
var positionInAllFactors = allFactors.indexOf(factorVar);
var insertionDepth = 0;
for (var i = positionInAllFactors; i >= 0; i--) {
insertionDepth = depFactors.indexOf(allFactors[i]);
if (insertionDepth != -1) {
break;
}
}
insertionDepth++;
this.ndimModifierTrialTypes = deepCloneOuter(this.ndimModifierTrialTypes, numNewLevels, insertionDepth);
// add new interacting factor:
this.factors.splice(insertionDepth, 0, factorVar);
}
};
/**
* remove a dependency on a factor variable.
* @param factorVar
*/
Modifier.prototype.removeFactorDependency = function (factorVar) {
function deepRemoveFactor(multiDimArr, remainingDepthTillRemove) {
var t;
if (remainingDepthTillRemove > 0) {
// recursive call:
for (t = 0; t < multiDimArr.length; t++) {
multiDimArr[t] = deepRemoveFactor(multiDimArr[t], remainingDepthTillRemove - 1);
}
return multiDimArr;
}
else {
// use first entry as new trialType:
multiDimArr = multiDimArr[0];
return multiDimArr;
}
}
// check if this modifier depends on the factor:
var facIdx = this.factors.indexOf(factorVar);
if (facIdx > -1) {
this.ndimModifierTrialTypes = deepRemoveFactor(this.ndimModifierTrialTypes, facIdx);
// if there is only one trial type left, then remove the modification completely and use default trial type instead:
if (this.ndimModifierTrialTypes instanceof ModifierTrialType) {
this.ndimModifierTrialTypes = [];
}
this.factors.splice(facIdx, 1);
}
};
/**
* removes one level from one factor
*/
Modifier.prototype.removeLevelFromFactor = function (factor, lvlIdx) {
function deepRemoveLevel(multiDimArr, remainingDepth, lvlIdx) {
if (remainingDepth > 0) {
for (var t = 0; t < multiDimArr.length; t++) {
deepRemoveLevel(multiDimArr, remainingDepth - 1, lvlIdx);
}
}
else {
multiDimArr.splice(lvlIdx, 1);
}
}
// check if this modifier depends on the factor:
var facIdx = this.factors.indexOf(factor);
if (facIdx > -1) {
console.log("removing level from Factor Dependency");
deepRemoveLevel(this.ndimModifierTrialTypes, facIdx, lvlIdx);
}
};
/**
* recursively removes the modification of a property from the multidimensional array
* @param objArr
* @param propName
*/
Modifier.prototype.removeModificationRecursive = function (objArr, propName) {
if (objArr.constructor === Array) {
for (var i = 0; i < objArr.length; i++) {
this.removeModificationRecursive(objArr[i], propName);
}
}
else {
objArr.removeModification(propName);
}
};
/**
* Select a specific or multiple trial types.
*
* @param {object} selectionSpec - the specification of the trials that are selected:
* 4 types possible:
* { type: 'allTrials', factorGroup: facGroup_obj }
* { type: 'factorLevel', factorGroup: facGroup_obj, factor: factor_obj, level: level_obj, levelIdx: numeric}
* { type: 'condition', factorGroup: facGroup_obj, condition: condition_obj }
* { type: 'trialVariation', factorGroup: facGroup_obj, trialVariation: trialVariation_obj }
*/
Modifier.prototype.selectTrialType = function (selectionSpec) {
if (selectionSpec.type == 'trialVariation') {
// convert to make sure that computed does not depend on nr, because of garbage collector
var newSelectionSpec = {
type: 'trialVariation',
factorGroup: selectionSpec.factorGroup,
allFactors: selectionSpec.allFactors,
allLevelIdx: selectionSpec.allLevelIdx,
trialNr: selectionSpec.trialVariation.nr()
};
this.selectedTrialType(newSelectionSpec);
}
else {
this.selectedTrialType(selectionSpec);
}
};
/**
* this function makes sure that trial types for all levels of all factors are present
*/
Modifier.prototype.addInteractingLevels = function () {
console.log("adding all new levels to modifier...");
var self = this;
// if this modifier depends on trialVariations, then add all conditions.
if (this.dependsOnTrialVariations()) {
this._addAllConditionDep();
}
function updateLevels(multiDimTrialTypes, multiDimConditions, depFactors, allFactors) {
while (depFactors[0] != allFactors[0]) {
// remove first entry:
allFactors = allFactors.slice(1);
multiDimConditions = multiDimConditions.slice(1);
}
// calculate the desired length of the current dimension:
if (multiDimConditions instanceof Array) {
// make sure that this factor (factorVars[0]) has all it's levels:
var desired_len = depFactors[0].globalVar().levels().length;
}
else {
// make sure that all trial variations are added as dependencies:
var desired_len = multiDimConditions.trials().length;
}
if (desired_len < multiDimTrialTypes.length) {
// remove the levels that are not required:
multiDimTrialTypes.splice(desired_len, desired_len - multiDimTrialTypes.length);
}
else if (desired_len > multiDimTrialTypes.length) {
// fill this dimension with the corresponding number of levels as dependencies:
var lastElem = multiDimTrialTypes[multiDimTrialTypes.length - 1];
for (var i = multiDimTrialTypes.length; i < desired_len; i++) {
multiDimTrialTypes[i] = deepCloneMultiDimArr(lastElem); // adding new level with all sub-levels here
}
}
// go into all existing levels of this factor (factorVars[0]) and make sure that all sub-factors (factorVars[1...]) have their levels:
if (multiDimTrialTypes[0] instanceof Array) {
for (var i = 0; i < multiDimTrialTypes.length; i++) {
updateLevels(multiDimTrialTypes[i], multiDimConditions[i], depFactors.slice(1), allFactors.slice(1)); // recursively add levels within this level
}
}
}
var factorGroup = this.getFactorGroup();
updateLevels(this.ndimModifierTrialTypes, factorGroup.conditions(), this.factors(), factorGroup.factors());
};
/**
* 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.
*/
Modifier.prototype.setPointers = function (entitiesArr) {
var self = this;
// convert ids to actual pointers:
this.factors(jQuery.map(this.factors(), 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.
*/
Modifier.prototype.reAddEntities = function (entitiesArr) {
// add the direct child nodes:
jQuery.each(this.factors(), 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:
if (elem.reAddEntities)
elem.reAddEntities(entitiesArr);
});
};
/**
* load from a json object to deserialize the states.
* @param {object} data - the json description of the states.
* @returns {Modifier}
*/
Modifier.prototype.fromJS = function (data) {
var self = this;
function modifierFactoryFromArray(dataArr) {
if (dataArr.constructor === Array) {
var objArr = [];
for (var i = 0; i < dataArr.length; i++) {
var subObjArr = modifierFactoryFromArray(dataArr[i]);
objArr.push(subObjArr);
}
return objArr;
}
else {
var modifierTrialType = new ModifierTrialType(self.expData, self.objToModify);
return modifierTrialType.fromJS(dataArr);
}
}
this.dependsOnTrialVariations(data.dependsOnTrialVariations);
if (data.factors)
this.factors(data.factors);
if (data.ndimModifierTrialTypes)
this.ndimModifierTrialTypes = modifierFactoryFromArray(data.ndimModifierTrialTypes);
return this;
};
/**
* serialize the state of this instance into a json object, which can later be restored using the method fromJS.
* @returns {object}
*/
Modifier.prototype.toJS = function () {
function modifierSerializeArray(objArr) {
if (objArr.constructor === Array) {
var dataArr = [];
for (var i = 0; i < objArr.length; i++) {
var subData = modifierSerializeArray(objArr[i]);
dataArr.push(subData);
}
return dataArr;
}
else {
return objArr.toJS();
}
}
var data = {
factors: jQuery.map(this.factors(), function (factor) { return factor.id(); }),
dependsOnTrialVariations: this.dependsOnTrialVariations(),
ndimModifierTrialTypes: modifierSerializeArray(this.ndimModifierTrialTypes),
type: this.type
};
return data;
};