-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (135 loc) · 4.64 KB
/
index.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
var lastQ = "This is the last question. Try the submit button.";
var firstQ = "You are on the first question.";
var unansweredQ = "Please make a selection.";
var earlySubmit = "Please complete all questions.";
/*$(document).ready( function() {
$.getJSON('https://gist.github.com/catherinemaldonado/0c72e27f347dc5204214.js', function(data) {
for (var i = 0; i < data.questions.length; i++) {
var question = '<h3>'+data.questions[i].question+'</h3>';
var thisNumber = Number(i)+1;
var answers = '<ol id="answers_'+thisNumber+'"></ol>';
var answersID = $(answers).attr('id');
var build = '<div class="question"><div class="content">'+question+'</div></div>';
$("#questions").append(build);
}
});
});*/
$(".question").css({right: '-705px'});
$("#questions div:first").addClass("active").show().animate({right: '0px'});
var questions = $("#questions > div");
for (var i = 0; i < questions.length; i++) {
$("#progress").append("<li></li>");
}
var progressItems = $("#progress li").length;
var percent = (1/progressItems)*92;
$("#progress li").css("width",percent+"%");
function checkIndex (){
var current = $(".active").index();
var total = $("#questions > div").length;
var currentNum = Number(current);
$("#progress li").removeClass("status");
$("#progress li").slice(0,currentNum).addClass("status");
if (current == total){
$("#next").addClass("disabled");
}
if (current == 1){
$("#previous").addClass("disabled");
}else{
$("#previous").removeClass("disabled");
}
}
checkIndex();
function checkChecked() {
var current = $(".active ol input:checked");
if ($(current).length == 1){
$("#next").removeClass("disabled");
}else{
$("#next").addClass("disabled");
}
if ($(current).length == 1 && $(".active").index() == $("#questions > div").length){
$("#submit").removeClass("disabled");
}
if ($(".active").index() == $("#questions > div").length){
$("#next").addClass("disabled");
}
}
$("#next").on("click", function () {
$("#error").fadeOut("slow");
if ($(this).hasClass("disabled") && $(".active").index() == $("#questions > div").length){
$("#error").html(lastQ);
$("#error").fadeIn("slow");
return false;
}else if ($(this).hasClass("disabled")){
$("#error").html(unansweredQ);
$("#error").fadeIn("slow");
return false;
}
var slide = $(".active");
var next = $(".active").next(".question");
// SLIDE ANIMATION TRANSITION
$(slide).animate({left: '-705px'},450).toggleClass("active");
$(next).animate({right: '0px'},450).toggleClass("active");
function resetDiv() {
$(".active").prev(".question").css({position: "absolute",width: "100%", left: "-705px",right:"auto"});
}
setTimeout(resetDiv, 900);
checkChecked();
checkIndex();
});
$("#previous").on("click", function () {
if ($(this).hasClass("disabled")){
$("#error").html(firstQ);
$("#error").fadeIn("slow");
return false;
}
/*event.preventDefault()*/
var slide = $(".active");
var prev = $(".active").prev(".question");
// SLIDE ANIMATION TRANSITION
$(slide).css({left: "auto"}).animate({right: '-705px',left:'auto'},350).toggleClass("active");
$(prev).animate({left: '0px'},450).toggleClass("active");
$("#error").fadeOut("slow");
checkIndex();
checkChecked();
});
$("ol li").on("click", function () {
var $this = $(this).find("input:radio");
var all = $(this).parent().find("input:radio").not($this);
var val = $this.val();
$this.prop("checked", true);
if ($this.prop("checked") === true) {
all.filter(function () {
return $(this).val() === val;
}).prop("checked", false);
}
$("#error").fadeOut("slow");
checkChecked();
});
$("#reset").on("click", function () {
$("#error").fadeOut("slow");
$("#submit").addClass("disabled");
$("input:radio").each( function() {
$(this).attr('checked',false);
});
checkChecked();
$(".question").removeClass("active").css({right: '-705px',left: 'auto'});
$("#questions div:first").fadeIn("slow").css({left: "auto"}).animate({right: '0',left:'auto'},350).toggleClass("active");
checkIndex();
});
$(document).keyup(function(e) {
/*checkIndex();*/
if(e.which == 13 || e.which == 39) {
$("#next").trigger('click');
}else if(e.which == 37) {
$("#previous").trigger('click');
}
});
$("#submit").on("click", function () {
if ($(this).hasClass("disabled")){
$("#error").html(earlySubmit);
$("#error").fadeIn("slow");
return false;
}else{
alert("This will eventually do something awesome!");
}
});