2020-09-06 20:41:54 +02:00
|
|
|
<div>
|
2020-08-28 22:36:50 +02:00
|
|
|
<% @items.find_all("/quiz/*").each_with_index do |quest, i| %>
|
|
|
|
<div id="question_<%=i+1%>" class="question hidden">
|
2020-09-06 17:03:07 +02:00
|
|
|
<%= render '/partials/question.*', question: quest, number: i.to_s %>
|
2020-03-19 22:24:54 +01:00
|
|
|
</div>
|
2020-08-28 22:36:50 +02:00
|
|
|
<% end %>
|
|
|
|
</div>
|
2020-09-06 20:41:54 +02:00
|
|
|
<div id="resultWrapper" class="hidden">
|
2020-08-28 22:36:50 +02:00
|
|
|
<% all_groups().each do |group| %>
|
|
|
|
<div id="d_<%= group[:id] %>" style="display: none;">
|
2020-09-10 00:28:10 +02:00
|
|
|
<%= render '/partials/pretty_link.*', :item => group %>
|
2020-03-19 22:24:54 +01:00
|
|
|
</div>
|
2020-08-28 22:36:50 +02:00
|
|
|
<% end %>
|
|
|
|
</div>
|
2020-09-06 17:47:16 +02:00
|
|
|
<div class="buttons">
|
2020-08-28 23:48:27 +02:00
|
|
|
<button id="previousButton" onclick="window.history.back()">
|
2020-09-10 22:02:18 +02:00
|
|
|
Vorige
|
2020-08-28 22:36:50 +02:00
|
|
|
</button>
|
|
|
|
<button id="nextButton" onclick="goNext()">
|
2020-09-10 22:02:18 +02:00
|
|
|
Volgende
|
2020-08-28 22:36:50 +02:00
|
|
|
</button>
|
2020-03-19 22:24:54 +01:00
|
|
|
</div>
|
2020-03-11 00:04:27 +01:00
|
|
|
<script>
|
2020-08-28 23:48:27 +02:00
|
|
|
// This is always the same, like a static macro
|
|
|
|
const _groups = (function resetGroups() {
|
2020-08-28 22:36:50 +02:00
|
|
|
const groups = {};
|
|
|
|
<% all_groups().each do |group| %>
|
|
|
|
groups["d_<%= group[:id] %>"] = document.getElementById("d_<%= group[:id] %>");
|
|
|
|
<% end %>
|
|
|
|
return groups;
|
|
|
|
})();
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
// This keeps the state of the current question/result
|
|
|
|
// So navigating between questions works without reload
|
|
|
|
const state = {
|
|
|
|
'question': {
|
|
|
|
'index': 0,
|
|
|
|
'id': "",
|
|
|
|
'element': undefined
|
|
|
|
},
|
|
|
|
'answers': {},
|
|
|
|
'allQuestions': document.getElementsByClassName("question"),
|
|
|
|
};
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
var depth = 0;
|
|
|
|
const previousButton = document.getElementById("previousButton");
|
|
|
|
const nextButton = document.getElementById("nextButton");
|
|
|
|
const resultWrapper = document.getElementById("resultWrapper");
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
function reset() {
|
|
|
|
const params = getParams();
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
state.question.index = 'vraag' in params ? parseInt(params['vraag']) : 1;
|
|
|
|
state.question.id = "question_"+state.question.index;
|
|
|
|
state.question.element = document.getElementById(state.question.id);
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
state.answers = {};
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
save_state();
|
|
|
|
showCorrectElements();
|
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
function getParams() {
|
|
|
|
const url = window.location.search;
|
|
|
|
if(!url) return {};
|
|
|
|
const paramsBuilder = {};
|
|
|
|
for(let currentVar of url.substring(1).split("&")) {
|
|
|
|
var pair = currentVar.split('=');
|
|
|
|
paramsBuilder[pair[0]] = decodeURIComponent(pair[1]);
|
2020-03-11 15:34:50 +01:00
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
return paramsBuilder;
|
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
function createRelativeUrl(newQuestionIndex) {
|
|
|
|
return window.location.pathname + "?vraag="+newQuestionIndex;
|
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
function showCorrectElements() {
|
|
|
|
// This part resets to the original state;
|
|
|
|
for(let question of state.allQuestions) {
|
|
|
|
question.classList.add("hidden");
|
|
|
|
}
|
|
|
|
resultWrapper.classList.add("hidden");
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
if(state.question.index == 1) {
|
|
|
|
previousButton.disabled = true;
|
|
|
|
} else {
|
|
|
|
previousButton.disabled = false;
|
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
if(!state.question.element) {
|
|
|
|
nextButton.disabled = true;
|
|
|
|
resultWrapper.classList.remove("hidden");
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
const result = {};
|
2020-08-28 23:48:27 +02:00
|
|
|
for(let question of state.allQuestions) {
|
2020-08-28 22:36:50 +02:00
|
|
|
const div = window.sessionStorage[question.id];
|
|
|
|
if(!div) continue;
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
const resultObj = JSON.parse(div);
|
|
|
|
for (let key in resultObj) {
|
|
|
|
if (!(key in result)) result[key] = 0;
|
|
|
|
result[key] += resultObj[key];
|
2020-03-19 22:24:54 +01:00
|
|
|
}
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
show_result(result);
|
|
|
|
} else {
|
2020-08-28 23:48:27 +02:00
|
|
|
nextButton.disabled = false;
|
|
|
|
state.question.element.classList.remove("hidden");
|
2020-03-11 15:34:50 +01:00
|
|
|
}
|
2020-08-28 23:48:27 +02:00
|
|
|
};
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
function save_answers(vers) {
|
2020-08-28 23:48:27 +02:00
|
|
|
for (let ver of vers) {
|
|
|
|
if (state.answers[ver]) {
|
|
|
|
state.answers[ver] += 1;
|
|
|
|
} else {
|
|
|
|
state.answers[ver] = 1;
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
2020-08-28 23:48:27 +02:00
|
|
|
}
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
function del_answers(vers) {
|
|
|
|
for (let ver of vers) {
|
2020-08-28 23:48:27 +02:00
|
|
|
state.answers[ver] -= 1;
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
function show_result(result) {
|
|
|
|
for (let ver in result) {
|
|
|
|
if (result[ver] == 0) {
|
|
|
|
_groups["d_"+ver].style.display = "none";
|
|
|
|
} else {
|
|
|
|
_groups["d_"+ver].style.display = "block";
|
|
|
|
_groups["d_"+ver].style.order = -1 * result[ver];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
function save_state() {
|
2020-08-28 23:48:27 +02:00
|
|
|
window.sessionStorage.setItem(state.question.id, JSON.stringify(state.answers));
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
function getSelectValues(select) {
|
|
|
|
var result = [];
|
|
|
|
var options = select && select.options;
|
|
|
|
var opt;
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
for (var i=0, iLen=options.length; i<iLen; i++) {
|
|
|
|
opt = options[i];
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
if (opt.selected) {
|
|
|
|
result.push(opt.value || opt.text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
function goNext() {
|
2020-08-28 23:48:27 +02:00
|
|
|
window.history.pushState("object or string", "", createRelativeUrl(state.question.index + 1));
|
|
|
|
depth += 1;
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
reset();
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 22:36:50 +02:00
|
|
|
function goPrevious() {
|
2020-08-28 23:48:27 +02:00
|
|
|
if(depth > 1) {
|
|
|
|
depth -= 1;
|
|
|
|
window.history.replaceState("", "", createRelativeUrl(state.question.index - 1));
|
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
reset();
|
2020-08-28 22:36:50 +02:00
|
|
|
}
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
window.addEventListener("popstate", e => {
|
|
|
|
e.preventDefault();
|
|
|
|
goPrevious();
|
|
|
|
});
|
2020-09-10 00:28:10 +02:00
|
|
|
|
2020-08-28 23:48:27 +02:00
|
|
|
reset();
|
2020-03-11 00:04:27 +01:00
|
|
|
</script>
|
2020-02-27 21:45:34 +01:00
|
|
|
<%= yield %>
|