/
step.js
258 lines (187 loc) · 4.72 KB
/
step.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
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
"use strict";
var uuid = require('node-uuid');
var ArgCollection = require('./arg-collection');
var _ = require('lodash');
class Step{
constructor(data, cells, grammar){
this.id = data.id || uuid.v4();
this.args = new ArgCollection(cells || [], data, this.id);
this.key = data.key;
if (!this.key && grammar){
this.key = grammar.key;
}
this.type = 'step';
this.grammar = grammar; // set by the StepHolder.buildStep() method
this.collections = {};
this.results = {};
}
setStatus(status){
this.results.root = {status: status};
}
getStatus(){
if (this.results.root){
return this.results.root.status;
}
return 'none';
}
allErrors(){
return _.values(this.results).filter(x => {
return x.status == 'error'
}).map(x => x.error);
}
logResult(result){
if (result.position){
this.results[result.position] = result;
}
else {
this.results.root = result;
}
var self = this;
if (result.cells){
result.cells.forEach(cellResult => {
var arg = self.args.find(cellResult.cell);
if (arg){
arg.result = cellResult;
}
else {
console.log('Unable to find an Arg for cell ' + cellResult.cell + ' of step ' + self.id);
}
});
}
}
getResult(position){
if (position){
return this.results[position];
}
else if (this.results.root){
return this.results.root;
}
else {
return {status: 'none'};
}
}
clearResults(){
this.results = {};
this.args.clearResults();
}
static merge(id, parts){
var step = new Step({}, []);
step.id = id;
for (var i = 0; i < parts.length; i++){
var part = parts[i];
part.args.mergeInto(step.args);
for (var key in part.collections){
step.collections[key] = part.collections[key];
}
}
_.values(step.collections).forEach(x => x.parent = step);
step.args.all().forEach(x => x.id = step.id);
return step;
}
children(){
return _.values(this.collections);
}
write(){
var key = this.key || this.grammar.key;
var data = {key: key, cells: {}, id: this.id};
this.args.store(data);
if (_.keys(this.collections).length > 0){
data.collections = [];
for (var key in this.collections){
var section = this.collections[key];
var sectionData = section.write();
data.collections.push(sectionData);
}
}
return data;
}
findValue(key){
var arg = this.args.find(key);
if (arg == null) return null;
if (arg.value === false) return false;
return arg.value || arg.default;
}
preview(loader){
try {
return this.grammar.preview(this, loader);
}
catch (e){
for (key in this.grammar){
console.log('grammar has ' + key);
}
throw 'Failed to write preview with step ' + this.id + ' and grammar ' + this.grammar.key + ': ' + e;
}
}
editor(loader){
return this.grammar.editor(this, loader);
}
buildResults(loader){
return this.grammar.buildResults(this, loader);
}
findByPath(path){
if ( !(path instanceof Array)){
path = path.split('.');
}
if (path.length == 0) return null;
var next = this.args.find(path[0]) || this.collections[path[0]];
if (path.length == 1){
return next;
}
else {
path.shift();
return next.findByPath(path);
}
}
isHolder(){
// TODO: Will need to get fancier later when we
// introduce embedded sections and tables
return false;
}
clearActiveState(){
this.args.clearActiveState();
}
hasArgs(){
return this.args.length() > 0;
}
hasCollections(){
return _.values(this.collections).length > 0;
}
isSimple(){
if (this.hasArgs()) return false;
if (this.hasCollections()) return false;
return true;
}
selectFirst(){
if (this.isSimple()) return {holder: this.parent, step: this, cell: null};
if (!this.hasCollections()){
return {holder: this.parent, step: this, cell: this.grammar.firstCell(this)};
}
return this.grammar.selectFirst(this);
}
selectNext(location){
if (this.isSimple()) return null;
if (this.hasCollections()){
return this.grammar.selectNext(location);
}
var nextCell = this.grammar.nextCell(location.cell, location.step);
if (nextCell == null) return null;
return {holder: this.parent, step: this, cell: nextCell};
}
selectPrevious(location){
if (this.isSimple()) return null;
if (this.hasCollections()){
return this.grammar.selectPrevious(location);
}
var previousCell = this.grammar.previousCell(location.cell, location.step);
if (previousCell == null) return null;
return {holder: this.parent, step: this, cell: previousCell};
}
selectLast(location){
if (this.isSimple()) return {holder: this.parent, step: this, cell: null};
if (!this.hasCollections()){
return {holder: this.parent, step: this, cell: this.grammar.lastCell(this)};
}
return this.grammar.selectLast(this);
}
}
module.exports = Step;