-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
99 lines (82 loc) · 2.76 KB
/
script.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
var qData = null; // THIS VARIABLE HOLDS THE DATA ;)
$(document).ready( function(){
window.onpopstate = function(){
displayQuestion(null);
};
$('.outer-container').css('min-height', $(window).height()-130);
$.get('data.json', null, function(data){
qData = data;
displayQuestion(null);
}, "json");
});
function displayQuestion(routeString){
$('.stack-container,.question-container,.choice-container,.answer-container').empty();
$('.min-strength-warning').hide();
var invalidFlag = false;
var route=[];
var i;
if(routeString===null){
routeString='';
if(window.location.hash.length>1){
// Google Analytics
ga('send', 'pageview', '/pp/' + encodeURIComponent(window.location.hash.substring(1)));
route=window.location.hash.substring(1).split('/');
for(i in route){
routeString += route[i] + '/';
}
}
}else{
route = routeString.split('/');
}
if(routeString.length > 0 && routeString[routeString.length-1]!='/')
routeString+='/';
var currentNode = qData;
var stackHTML=$('<div />'),stackNode=null;
for(i in route){
if(route[i].length===0)
break;
if(typeof(currentNode.children)==='undefined'||currentNode.children.length===0){
invalidFlag=true;
break;
}
stackHTML.append('<div class="arrow-down"></div>').append($('<div />').addClass('stack-piece').html('<a href="#'+getRouteString(route,i)+'">'+currentNode.choices[route[i]]+'</a>'));
currentNode = currentNode.children[route[i]];
}
$('.stack-container').html(stackHTML);
if(currentNode.answer!==null){
$('.answer-container').html(currentNode.answer);
if(currentNode.min_strength_level == true)
$('.min-strength-warning').show();
}else{
$('.question-container').html(currentNode.question);
var outputHTML='', loopEnd = currentNode.choices.length;
for(i in currentNode.choices){
outputHTML += '<li><a href="#'+routeString+i+'">'+currentNode.choices[i]+'</a></li>';
}
$('.choice-container').html(outputHTML);
}
$('.choice-container a,.stack-container a,.min-strength-warning a').off().click(function(){
if($(this).attr('href').charAt(0) == '#'){
// Google Analytics
ga('send', 'pageview', '/pp/' + encodeURIComponent($(this).attr('href').substring(1)));
displayQuestion($(this).attr('href').substring(1));
if($(window).scrollTop() > $('.stack-container').position().top){
$('html,body').animate({scrollTop: $('.stack-container').position().top});
}
}
});
$('.answer-container a').off().click(function(e){
// Open all answer links in a new tab
e.preventDefault();
window.open($(this).attr('href'));
});
}
function getRouteString(route,depth){
var s = '';
for(var i=0;i<depth;i++){
if(i!==0)
s += '/';
s += route[i];
}
return s;
}