-
Notifications
You must be signed in to change notification settings - Fork 10
/
link.js
56 lines (51 loc) · 1.63 KB
/
link.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
'use strict';
module.exports = link;
function link(story) {
var labels = Object.keys(story.states);
for (var i = 0; i < labels.length; i++) {
var label = labels[i];
var state = story.states[label];
var link = linker(story, label, state);
if (state.label != null) {
state.label = link('label')(state.label);
}
if (state.next != null) {
state.next = link('next')(state.next);
}
if (state.branch != null) {
state.branch = link('branch')(state.branch);
}
if (state.question != null) {
state.question = state.question.map(link('question'));
}
if (state.answer != null) {
state.answer = state.answer.map(link('answer'));
}
}
}
function linker(story, context, state) {
var parts = context.split('.');
var ancestry = [];
while (parts.length > 0) {
ancestry.push(parts.slice());
parts.pop();
}
ancestry.push([]);
return function (role) {
return function (label) {
if (label === 'RET' || label === 'ESC') {
return label;
}
for (var i = 0; i < ancestry.length; i++) {
var candidate = ancestry[i].slice();
candidate.push(label);
candidate = candidate.join('.');
if (story.states[candidate] != null) {
return candidate;
}
}
story.error('Could not link ' + role + ' label ' + JSON.stringify(label) + ' at position ' + state.position);
return label;
};
};
}