forked from jdm/asknot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foopy.js
130 lines (111 loc) · 4.03 KB
/
foopy.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
var groupNode;
var choiceIndex = [];
var choices = [];
var stack = [];
function chooseNegativeResponse() {
var responses = $('.negative');
return responses[Math.floor(Math.random() * responses.length)];
}
function updateNegativeResponse() {
var negative = chooseNegativeResponse();
$($('.negative.visible')[0]).removeClass('visible');
$(negative).addClass('visible');
}
function updateCurrentChoice(lastIndex) {
var lastChoice = $('.choices li', groupNode)[choices[choices.length - 1][lastIndex]];
var choice = $('.choices li', groupNode)[choices[choices.length - 1][choiceIndex[choiceIndex.length - 1]]];
updateNegativeResponse();
lastChoice.style.display = 'none';
choice.style.display = 'inline';
$('#ok')[0].firstChild.href = choice.hasAttribute('next-group') ?
'' : choice.getAttribute('target');
}
function nextChoice() {
var lastIndex = choiceIndex[choiceIndex.length - 1];
choiceIndex[choiceIndex.length - 1]++;
if (choiceIndex[choiceIndex.length - 1] === $('.choices li', groupNode).length) {
choiceIndex[choiceIndex.length - 1] = 0;
}
updateCurrentChoice(lastIndex);
}
function switchGroup(group) {
groupNode = document.getElementById(group);
if (!stack.length || stack[stack.length - 1] !== group) {
stack.push(group);
choiceIndex.push(0);
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [rev. #1]
function shuffle(v) {
for (var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
return v;
};
var c = [];
for (var i = 0; i < $('.choices li', groupNode).length; i++)
c.push(i);
choices.push(shuffle(c));
}
$('#back')[0].style.display = group == 'proglang' ? 'none' : 'block';
$('.question', groupNode)[0].style.display = 'block';
updateCurrentChoice(choiceIndex[choiceIndex.length - 1]);
}
function cleanUpCurrent() {
if (!groupNode) {
return;
}
$('.question', groupNode)[0].style.display = 'none';
var lastChoice = $('.choices li', groupNode)[choices[choices.length - 1][choiceIndex[choiceIndex.length - 1]]];
lastChoice.style.display = 'none';
}
function investigate(ev) {
ev.preventDefault();
var choice = $('.choices li', groupNode)[choices[choices.length - 1][choiceIndex[choiceIndex.length - 1]]];
if (choice.hasAttribute('next-group')) {
cleanUpCurrent();
switchGroup(choice.getAttribute('next-group'));
} else {
window.open(choice.getAttribute('target'));
}
}
function takeBack() {
cleanUpCurrent();
stack.splice(stack.length - 1, 1);
choiceIndex.splice(choiceIndex.length - 1, 1);
choices.splice(choices.length - 1, 1);
switchGroup(stack[stack.length - 1]);
}
function langChange() {
document.webL10n.setLanguage(this.value);
}
$(window).load(function() {
$('#ok')[0].onclick = investigate;
$('#next')[0].onclick = nextChoice;
$('#back')[0].onclick = takeBack;
$('#lang select')[0].onchange = langChange;
// Detected browser language
var browserLang = document.webL10n.getLanguage();
// Default language (value of the selected <option> element)
var defaultLang = $("#lang option:selected").val();
if (defaultLang !== browserLang) {
var option = $('#lang option[value=' + browserLang + ']');
if (option.length) {
// If the browser language is supported, select the good option
option.attr('selected', 'selected');
} else {
// Else set the default language
document.webL10n.setLanguage(defaultLang);
}
}
var query = window.location.hash.substring(1);
switchGroup('proglang');
if (query.length == 0) {
return;
}
var node = document.querySelector('li[next-group="' + query + '"]');
if (!node)
return;
node = node.parentNode.parentNode;
while (node.has) {
}
cleanUpCurrent();
switchGroup(query.substring(1));
});