-
Notifications
You must be signed in to change notification settings - Fork 0
/
longsurvey.js
116 lines (100 loc) · 3.41 KB
/
longsurvey.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
$(document).ready(function(){
window.PCCC = window.PCCC || {};
PCCC.survey = PCCC.survey || {};
var guid = (function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return function() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
})();
function getSurveyIdentifiers() {
return {"survey_token": $("form input[name=action_survey_token]").val(),
"akid": $("form input[name=akid]").val(),
"action_id": $("form input[name=action_id]").val()
};
}
function getSurveyToken() {
return $("form input[name=action_survey_token]").val();
}
function makeSurveyToken() {
$("form input[name=action_survey_token]").val(guid());
}
PCCC.survey.getSurveyData = function() {
var data = getSurveyIdentifiers();
if( !data.survey_token && !data.akid && !data.action_id ) {
return;
}
var url = window.location.origin + window.location.pathname
+ window.location.search + "&template_set=longsurvey";
$.getJSON(url, function(resp) {
if( resp.status == 400 ) {
window.location = window.location.origin + window.location.pathname;
return;
}
var lastFilledInput;
$("form :input[name]").each(function() {
var name = $(this).attr("name"), value;
if( !name ) { return; }
if( name.substr(0, 7) === "action_" ) {
name = name.substr(7);
value = resp.action[name];
} else {
value = resp[name];
}
if( value && value.trim() ) {
var $el = $(this), type = $el.attr("type");
switch(type) {
case 'checkbox':
$el.attr('checked', 'checked');
break;
case 'radio':
$el.filter('[value="'+value+'"]').attr('checked', 'checked');
break;
default:
$el.val(value);
}
lastFilledInput = $el;
}
});
if( lastFilledInput ) {
$(".item, .ak-progress").removeClass("active");
var activeSlide = lastFilledInput.closest(".item"),
idx = activeSlide.index();
activeSlide.addClass("active");
$($(".ak-progress").get(idx)).addClass("active").addClass("prog-complete")
.prevAll().addClass("prog-complete");
}
});
}
$('.next-btn').click(function() {
if( getSurveyToken() ) {
$.post("/update_action/", $("form").serialize());
} else {
makeSurveyToken();
$.post("/rest/v1/action/", $("form").serialize(), function(resp) {
var qs = "akid=" + resp.akid + "&action_id="
+ resp.id + "&survey_token=" + resp.fields.survey_token;
window.location = window.location.origin + window.location.pathname + "?" + qs;
});
}
var current_slide = $('#survey-steps').data("bs.carousel").getActiveIndex();
$("#survey-steps .carousel-indicators li").each(function(i) {
if(i == current_slide){
$(this).addClass("prog-complete");
}
});
$("#survey-steps").carousel("next");
});
$(".prev-btn").click(function(){
$('#survey-steps').carousel('prev');
});
$("#survey-steps .carousel-indicators li").each(function(i) {
$(this).attr("data-slide-to", i);
});
PCCC.survey.getSurveyData();
});