durfdoen-2.0/layouts/partials/question.erb

70 lines
2.6 KiB
Text
Raw Normal View History

2020-02-28 01:18:42 +01:00
<h2> <%= question[:vraag] %> </h2>
2020-03-11 01:44:54 +01:00
<select id="<%= (question[:tekst] || "") + (question[:vraag] || "") %>_option">
2020-02-28 01:18:42 +01:00
<option value="" selected="selected disabled hidden">Maak uw keuze</option>
<% for answer in question[:antwoorden]%>
2020-03-10 22:49:04 +01:00
<option value='<%= answer[:tekst] + (answer[:vraag] || "") %>'>
2020-02-28 01:18:42 +01:00
<%= answer[:tekst] %>
</option>
<% end %>
</select>
<% for answer in question[:antwoorden]%>
<% if answer[:vraag] %>
2020-03-11 01:44:54 +01:00
<div id='<%= answer[:tekst] + answer[:vraag] %>' style="display:none;">
2020-02-28 01:18:42 +01:00
<%= render '/partials/question.*', question: answer %>
2020-02-27 21:48:17 +01:00
</div>
2020-02-27 22:45:50 +01:00
<% end %>
2020-02-27 21:48:17 +01:00
<% end %>
2020-02-27 22:45:50 +01:00
2020-02-27 21:48:17 +01:00
<script>
2020-03-11 01:44:54 +01:00
{
// Reference back to the last selected option
// This way we can 'deselect' the question results
2020-03-11 00:04:27 +01:00
let last_answered = null;
2020-03-10 22:49:04 +01:00
// Create list of all subquestions
const sub_questions = {};
2020-03-11 01:44:54 +01:00
// Create list of all student organisations this answer entails
const question_results = {};
2020-03-11 00:04:27 +01:00
<% for answer in question[:antwoorden] %>
2020-03-10 22:49:04 +01:00
<% if answer[:vraag] %>
sub_questions['<%= answer[:tekst] + answer[:vraag] %>'] = document.getElementById('<%= answer[:tekst] + answer[:vraag] %>');
<% end %>
2020-03-11 00:04:27 +01:00
2020-03-11 01:44:54 +01:00
question_results['<%= answer[:tekst] + (answer[:vraag] || "") %>'] = [];
2020-03-11 00:04:27 +01:00
<% if answer[:verenigingen] %>
<% for vereniging in answer[:verenigingen] %>
2020-03-11 01:44:54 +01:00
question_results['<%= answer[:tekst] + (answer[:vraag] || "") %>'].push(
"<%= vereniging[:naam] %>"
2020-03-11 00:04:27 +01:00
);
<% end %>
<% end %>
2020-03-10 22:49:04 +01:00
<% end %>
2020-03-11 01:44:54 +01:00
document.getElementById("<%= (question[:tekst] || "") + (question[:vraag] || "") %>_option").addEventListener('change', (event) => {
2020-03-11 00:04:27 +01:00
if (last_answered && sub_questions[last_answered]) {
sub_questions[last_answered].style.display = "none";
2020-03-11 01:44:54 +01:00
// sub is the select dom element, that listens for changes
// When a parent is changed, it should notify it's last_answered child that it is changed
const sub = document.getElementById(last_answered+"_option");
if (sub) {
sub.value = null;
sub.dispatchEvent(new event.constructor(event.type, event));
2020-03-11 00:04:27 +01:00
}
2020-03-10 22:49:04 +01:00
}
2020-03-11 00:04:27 +01:00
2020-03-11 01:44:54 +01:00
// Update results
save_answers(question_results[event.target.value] || []);
del_answers(question_results[last_answered] || []);
2020-03-11 00:04:27 +01:00
2020-03-11 01:44:54 +01:00
// Make possible sub question visible
2020-03-10 22:49:04 +01:00
const quest = sub_questions[event.target.value];
2020-03-11 00:04:27 +01:00
last_answered = event.target.value;
2020-03-10 22:49:04 +01:00
if (quest) {
delete quest.style.removeProperty("display");
}
});
2020-03-11 01:44:54 +01:00
}
2020-02-27 21:48:17 +01:00
</script>