-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
executable file
·84 lines (78 loc) · 2.27 KB
/
Copy pathengine.js
File metadata and controls
executable file
·84 lines (78 loc) · 2.27 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
$(function() {
var $cnt = $("#cnt");
var $history = $("#history");
loadNode(null);
var history = [];
$history.on('click', ".history_link", function() {
var ids = $(this).attr('id').split('_');
var node_id = ids[0];
var choice;
while (!choice || choice[0] != node_id) {
choice = history.pop();
}
loadNode(choice[0]);
logHistory();
});
$("#back").click(function() {
var last = history.pop();
loadNode(last[0]);
logHistory();
});
$("#new_site").click(function() {
history = [];
$history.html('');
loadNode();
});
$cnt.on("click", 'button', function() {
var button_id = $(this).attr('id');
var node_id = $cnt.find('p').attr('id');
history.push([node_id, button_id]);
logHistory();
var node = nodes[node_id];
var answer = node.answers[button_id];
if (answer.link) {
loadNode(answer.link);
} else {
$cnt.text(answer.result);
}
});
function logHistory() {
$history.html('');
for (var i in history) {
var choice = history[i];
var node = nodes[choice[0]];
var answer = node.answers[choice[1]];
var $link = $('<a />', {
id: choice[0] + '_' + choice[1],
text: node.text + ' ' + answer.text,
href: '#',
class: 'history_link'
});
$li = $('<li />').append($link);
$('#history').append($li);
}
}
function loadNode(id) {
if ($.isEmptyObject(nodes)) {
setTimeout(function() {loadNode(id)}, 100);
return;
}
$cnt.html('');
var node;
if (!id) node = getTopNode();
else node = nodes[id];
if (!node) {
$cnt.text('Broken link ' + id);
return null;
}
$('<p/>', {id: node.id, text: node.text}).appendTo($cnt);
for (var i in node.answers) {
$('<button/>', {id: i, text: node.answers[i].text}).appendTo($cnt);
}
}
function getTopNode() {
for (var i in nodes) {
if (!nodes[i].parent_id) return nodes[i];
}
}
});