-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifierTrialType.js
More file actions
112 lines (101 loc) · 3.46 KB
/
modifierTrialType.js
File metadata and controls
112 lines (101 loc) · 3.46 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
// � by Caspar Goeke and Holger Finger
/**
* This class manages the modifications of properties of content elements for one specific trial specificiation.
*
* @param {ExpData} expData - The global ExpData, where all instances can be retrieved by id.
* @param objToModify
* @constructor
*/
var ModifierTrialType = function (expData, objToModify) {
this.expData = expData;
this.objToModify = objToModify;
this.modifiedProp = {};
this.propIsModified = {};
var modifiableProp = objToModify.modifiableProp;
for (var i = 0; i < modifiableProp.length; i++) {
var propName = modifiableProp[i];
this.propIsModified[propName] = ko.observable(false);
}
this.type = "ModifierTrialType";
};
/**
* define a specific modification of a property.
* @param {string} propName - the name of the property
* @param {number | string} val - the value of the property
*/
ModifierTrialType.prototype.setModification = function (propName, val) {
if (!this.propIsModified[propName]()) {
this.modifiedProp[propName] = ko.observable(val);
this.propIsModified[propName](true);
}
else {
this.modifiedProp[propName](val);
}
};
/**
* removes a specific modification of a property (i.e. sets it back to the default trial type).
*
* @param {string} propName - the name of the property
*/
ModifierTrialType.prototype.removeModification = function (propName) {
this.propIsModified[propName](false);
delete this.modifiedProp[propName];
};
/**
* creates a deep copy of this instance.
*
* @returns {ModifierTrialType}
*/
ModifierTrialType.prototype.deepCopy = function () {
var newObj = new ModifierTrialType(this.expData, this.objToModify);
// deep copy of observables:
var modifiedPropOld = this.modifiedProp;
var modifiedPropNew = newObj.modifiedProp;
for (var property in modifiedPropOld) {
if (modifiedPropOld.hasOwnProperty(property)) {
if (typeof this.objToModify.cloneModifiableProp == 'function') {
var newPropVal = this.objToModify.cloneModifiableProp(property, modifiedPropOld[property]());
modifiedPropNew[property] = ko.observable(newPropVal);
}
else {
modifiedPropNew[property] = ko.observable(modifiedPropOld[property]());
}
newObj.propIsModified[property](true);
}
}
return newObj;
};
/**
* load from a json object to deserialize the states.
* @param {object} data - the json description of the states.
* @returns {ModifierTrialType}
*/
ModifierTrialType.prototype.fromJS = function (data) {
var modifiedProp = {};
for (var prop in data.modifiedProp) {
if (this.propIsModified.hasOwnProperty(prop)) {
modifiedProp[prop] = ko.observable(data.modifiedProp[prop]);
this.propIsModified[prop](true);
}
else {
console.log('WARNING: property ' + prop + ' that was modified does not exist anymore.');
}
}
this.modifiedProp = modifiedProp;
return this;
};
/**
* serialize the state of this instance into a json object, which can later be restored using the method fromJS.
* @returns {object}
*/
ModifierTrialType.prototype.toJS = function () {
var modifiedPropData = {};
var modifiedPropKo = this.modifiedProp;
for (var prop in modifiedPropKo) {
modifiedPropData[prop] = modifiedPropKo[prop]();
}
return {
modifiedProp: modifiedPropData,
type: this.type
};
};