-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChain.js
122 lines (104 loc) · 4.08 KB
/
Chain.js
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
define([
'mystique/Base',
'mystique/Result'
],
function(
Base,
Result
){
return Base.extend({
_isValid: function(value){
if (!this.validators){
return;
}
return this._concatResultList(this._loop(value, 0, []));
},
_loop: function(value, index, resultList){
//Summary:
// loops over all the validators in turn.
// This code is slightly insane, but it works.
// Most of the instanity comes from handling the possibility
// that a validator may return a promise (dojo/Deferred). If a promise is
// returned from one or more validators, then a promise will
// be returned from the Group
//
if (this.validators.length <= index){
return resultList;
}
var validator = this.validators[index],
currentValue = this._currentValue(resultList);
if ( !(validator.skipOnPass && currentValue) && !(validator.skipOnFail && ! currentValue)){
var result = this._getResult(validator, value);
if (result.value.then){
var resultPromise = {
then: function(callBack, errBack, progressBack){
this.callBack = callBack;
this.errBack = errBack;
this.progressBack = progressBack;
},
resolve: function(result){
this.callBack(result);
}
},
self = this;
resultList[index] = new Result({value: resultPromise, messages: result.messages});
result.value.then(function(result){
resultList[index] = result;
if (result.value === true){
if (validator.haltOnPass){halt = true}
} else {
if (validator.haltOnFail){halt = true}
}
if (halt){
resultPromise.resolve(self._concatResultList(resultList));
} else {
resultPromise.resolve(self._concatResultList(self._loop(value, index + 1, resultList)));
}
});
return resultList;
} else {
resultList[index] = result;
var halt = false;
if (result.value === true){
if (validator.haltOnPass){halt = true}
} else {
if (validator.haltOnFail){halt = true}
}
if (halt){
return resultList;
} else {
return this._loop(value, index + 1, resultList);
}
}
}
return resultList;
},
_getResult: function(validator, value){
return validator.isValid(value);
},
_concatResultList: function(resultList){
var result = new Result({value: this._currentValue(resultList)});
for (var index in resultList){
if (resultList[index].messages){
if (!result.messages){
result.messages = [];
}
result.messages = result.messages.concat(resultList[index].messages);
}
}
return result;
},
_currentValue: function(resultList){
var value = true;
for (var index in resultList){
if (resultList[index].value === false){
value = false;
} else if (resultList[index].value.then){
value = resultList[index].value;
return value;
}
}
return value;
}
});
})