Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
121 lines (100 loc) · 3.34 KB
/
Copy pathexample.js
File metadata and controls
121 lines (100 loc) · 3.34 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
angular.module('Constraints').factory('Constraints.Connector', function connectorFactory() {
return Connector;
function Connector() {
this.value = 0;
this.informant = null;
this.constraints = [];
}
Connector.prototype.RESULT_OK = 0;
Connector.prototype.RESULT_CONFLICT = 1;
Connector.prototype.RESULT_IGNORED = 2;
Connector.prototype.hasValue = function () {
return (this.informant !== null);
};
Connector.prototype.setValue = function (newval, setter) {
if (!this.hasValue()) {
this.value = newval;
this.informant = setter;
this.forEachExcept(setter, 'processNewValue', this.constraints);
return this.RESULT_OK;
} else if (newval !== this.value) {
return this.RESULT_CONFLICT;
} else {
return this.RESULT_IGNORED;
}
};
Connector.prototype.getValue = function () {
return this.value;
};
Connector.prototype.forgetValue = function (retractor) {
if (retractor === this.informant) {
this.informant = null;
this.forEachExcept(retractor, 'processNoValue', this.constraints);
return this.RESULT_OK;
} else {
return this.RESULT_IGNORED;
}
};
Connector.prototype.connect = function (newConstraint) {
var exists = false;
for (var i = 0; i < this.constraints.length; i++) {
if (this.constraints[i] === newConstraint) {
exists = true;
break;
}
}
if (!exists) {
this.constraints.push(newConstraint);
}
if (this.hasValue()) {
newConstraint.processNewValue();
}
};
Connector.prototype.forEachExcept = function (except, procedure, list) {
for (var i = 0; i < list.length; i++) {
if (list[i] !== except && angular.isFunction(list[i][procedure])) {
list[i][procedure]();
}
}
};
});
angular.module('Constraints').factory('Constraints.Probe', ['$log', function probeFactory($log) {
return Probe;
function Probe(name, connector) {
this.name = name;
this.connector = connector;
connector.connect(this);
}
Probe.prototype.processNewValue = function () {
$log.log('Probe: ' + this.name + ' = ' + this.connector.getValue());
};
Probe.prototype.processNoValue = function () {
$log.log('Probe: ' + this.name + ' = ' + '?');
};
}]);
angular.module('Constraints').factory('Constraints.Adder', function adderFactory() {
return Adder;
function Adder(firstAddend, secondAddend, sum) {
this.firstAddend = firstAddend;
this.secondAddend = secondAddend;
this.sum = sum;
this.firstAddend.connect(this);
this.secondAddend.connect(this);
this.sum.connect(this);
}
Adder.prototype.processNewValue = function () {
if (this.firstAddend.hasValue() && this.secondAddend.hasValue()) {
this.sum.setValue(this.firstAddend.getValue() + this.secondAddend.getValue(), this);
} else if (this.firstAddend.hasValue() && this.sum.hasValue()) {
this.secondAddend.setValue(this.sum.getValue() - this.firstAddend.getValue(), this);
} else if (this.secondAddend.hasValue() && this.sum.hasValue()) {
this.firstAddend.setValue(this.sum.getValue() - this.secondAddend.getValue(), this);
}
};
Adder.prototype.processNoValue = function () {
this.firstAddend.forgetValue(this);
this.secondAddend.forgetValue(this);
this.sum.forgetValue(this);
this.processNewValue();
};
});