-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.js
More file actions
123 lines (103 loc) · 4.73 KB
/
configuration.js
File metadata and controls
123 lines (103 loc) · 4.73 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
function Configuration(model, selectedFeatures, deselectedFeatures) {
if (!(this instanceof Configuration))
return new Configuration(model, selectedFeatures, deselectedFeatures);
this.model = model;
this.selectedFeatures = selectedFeatures || [];
this.deselectedFeatures = deselectedFeatures || [];
this.getSelectedFeature = featureGetter("selectedFeatures");
this.getDeselectedFeature = featureGetter("deselectedFeatures");
global.tutteFeatures = this.model.features
}
Configuration.prototype.isComplete = function() {
var doneFeatures = this.selectedFeatures.concat(this.deselectedFeatures).
concat(this.getDeactivatedFeatures()).concat(this.getActivatedFeatures());
var everyFeatureDone = this.model.features.reduce(function(acc, feature) {
return acc && !!doneFeatures.find(featureFinder(feature.name));
}, true);
return this.isValid() && everyFeatureDone;
};
Configuration.prototype.isValid = function() {
var self = this;
if (self._isValid === undefined)
self._isValid = self.model.constraintSolver.isValid(self);
return self._isValid;
};
Configuration.prototype.getDeactivatedFeatures = function() {
var self = this;
if (self._deactivatedFeatures === undefined)
self._deactivatedFeatures = self.model.features.filter(function(feature) {
if (self._activatedFeatures && self._activatedFeatures.find(featureFinder(feature.name)))
return false; // performance optimization
return self.model.constraintSolver.isDeactivated(self, feature);
});
return self._deactivatedFeatures;
};
Configuration.prototype.getActivatedFeatures = function() {
var self = this;
if (self._activatedFeatures === undefined)
self._activatedFeatures = self.model.features.filter(function(feature) {
if (self._deactivatedFeatures && self._deactivatedFeatures.find(featureFinder(feature.name)))
return false;
return self.model.constraintSolver.isActivated(self, feature);
});
return self._activatedFeatures;
};
Configuration.prototype.isDeactivated = function(feature) {
return !!this.getDeactivatedFeatures().find(featureFinder(feature.name));
};
Configuration.prototype.isActivated = function(feature) {
return !!this.getActivatedFeatures().find(featureFinder(feature.name));
};
Configuration.prototype.isEnabled = function(feature) {
return this.getSelectedFeature(feature.name) || this.isActivated(feature);
};
Configuration.prototype.isDisabled = function(feature) {
return this.getDeselectedFeature(feature.name) || this.isDeactivated(feature);
};
Configuration.prototype.isAutomatic = function(feature) {
return this.isActivated(feature) || this.isDeactivated(feature);
};
Configuration.prototype.isManual = function(feature) {
return this.getSelectedFeature(feature.name) || this.getDeselectedFeature(feature.name);
};
Configuration.prototype.serialize = function() {
var self = this;
if (!self.isComplete())
throw "configuration is not complete";
var xml = document.implementation.createDocument(null, "configuration");
function setAttribute(node, key, value) {
var attribute = document.createAttribute(key);
attribute.value = value;
node.attributes.setNamedItem(attribute);
}
self.model.features.forEach(function(feature) {
var node = xml.createElement("feature");
var manual = self.getSelectedFeature(feature.name) ? "selected" :
self.getDeselectedFeature(feature.name) ? "unselected" :
"undefined";
setAttribute(node, "automatic",
manual === "undefined" && self.isActivated(feature) ? "selected" :
manual === "undefined" && self.isDeactivated(feature) ? "unselected" :
"undefined");
setAttribute(node, "manual", manual);
setAttribute(node, "name", feature.name);
if (feature.hasValue)
setAttribute(node, "value", feature.value);
xml.children[0].appendChild(node);
});
return new XMLSerializer().serializeToString(xml);
};
Configuration.fromXml = function(model, xml) {
var selectedFeatures = [], deselectedFeatures = [];
$(xml).find("feature").each(function() {
var feature = model.getFeature($(this).attr("name")),
value = $(this).attr("value");
if ($(this).attr("manual") === "selected")
selectedFeatures.push(feature);
else if ($(this).attr("manual") === "unselected")
deselectedFeatures.push(feature);
if (typeof value !== typeof undefined)
feature.setValue(value);
});
return new Configuration(model, selectedFeatures, deselectedFeatures);
};