durfdoen-2.0/layouts/partials/question.erb

120 lines
4 KiB
Text
Raw Normal View History

2020-02-28 01:18:42 +01:00
<h2> <%= question[:vraag] %> </h2>
2020-09-05 18:01:24 +02:00
<% if question[:type] == "specialCase1" %>
<p>
Geef je postcode in
</p>
2020-09-05 21:40:05 +02:00
<input id="postcodeField" type=number>
2020-09-05 18:01:24 +02:00
<%else%>
<select <% if question[:type] == "meerkeuze" %> multiple="multiple" <% end %>
id="<%= (question[:tekst] || "") + (question[:vraag] || "") %>_option">
2020-09-05 18:01:24 +02:00
<option value="" selected="selected disabled hidden">Maak uw keuze</option>
<% for answer in question[:antwoorden]%>
<option value='<%= answer[:tekst] + (answer[:vraag] || "") %>'>
<%= answer[:tekst] %>
</option>
<% end %>
</select>
2020-09-05 21:40:05 +02:00
<% for answer in question[:antwoorden]%>
<% if answer[:vraag] %>
<div id='<%= html_escape(answer[:tekst] + answer[:vraag]) %>' style="display:none;">
<%= render '/partials/question.*', question: answer %>
</div>
<% end %>
2020-08-28 22:36:50 +02:00
<% end %>
2020-02-27 21:48:17 +01:00
<% end %>
2020-09-05 21:40:05 +02:00
<script>
<% if question[:type] == "specialCase1" %>
(function setupPostCodeField() {
const postcodes = (function genpostcodes() {
const postcodes = {};
2020-09-05 18:01:24 +02:00
2020-09-05 21:40:05 +02:00
for(let ver of <%= postcodes_per_vereniging.to_json %> ) {
for(let post of ver["postcodes"] || []) {
if(!(post in postcodes)) postcodes[post+""] = [];
2020-09-05 18:01:24 +02:00
2020-09-05 21:40:05 +02:00
postcodes[post+""].push(ver["id"]);
}
}
2020-09-05 18:01:24 +02:00
2020-09-05 21:40:05 +02:00
return postcodes;
})();
2020-09-05 18:01:24 +02:00
2020-09-05 21:40:05 +02:00
let last_answered = [];
2020-09-05 18:01:24 +02:00
2020-09-05 21:40:05 +02:00
const postcodeField = document.getElementById("postcodeField");
2020-09-05 18:01:24 +02:00
2020-09-05 21:40:05 +02:00
const handler = () => {
const selected = postcodes[postcodeField.value] || [];
// Update results
save_answers(selected);
del_answers(last_answered);
save_state();
last_answered = selected;
};
postcodeField.addEventListener("past", handler, false);
postcodeField.addEventListener("input", handler, false);
})();
<%else%>
{
2020-08-28 22:36:50 +02:00
// Reference back to the last selected option
// This way we can 'deselect' the question results
let last_answered = [];
2020-09-05 18:01:24 +02:00
2020-08-28 22:36:50 +02:00
// Create list of all subquestions
const sub_questions = {};
// Create list of all student organisations this answer entails
const question_results = {};
2020-09-05 18:01:24 +02:00
2020-08-28 22:36:50 +02:00
<% for answer in question[:antwoorden] %>
<% if answer[:vraag] %>
sub_questions['<%= html_escape(answer[:tekst] + answer[:vraag]) %>'] = document.getElementById('<%= html_escape(answer[:tekst] + answer[:vraag]) %>');
<% end %>
2020-09-05 18:01:24 +02:00
2020-08-28 22:36:50 +02:00
question_results['<%= answer[:tekst] + (answer[:vraag] || "") %>'] = [];
<% if answer[:verenigingen] %>
<% for vereniging in answer[:verenigingen] %>
question_results['<%= answer[:tekst] + (answer[:vraag] || "") %>'].push(
"<%= vereniging[:naam] %>"
);
<% end %>
<% end %>
<% end %>
2020-09-05 18:01:24 +02:00
2020-08-28 22:36:50 +02:00
document.getElementById("<%= (question[:tekst] || "") + (question[:vraag] || "") %>_option").addEventListener('change', (event) => {
for (let la of last_answered) {
if (sub_questions[la]) {
sub_questions[la].style.display = "none";
2020-09-05 18:01:24 +02:00
2020-08-28 22:36:50 +02: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(la+"_option");
if (sub) {
sub.value = null;
sub.dispatchEvent(new event.constructor(event.type, event));
}
}
}
2020-09-05 18:01:24 +02:00
2020-08-28 22:36:50 +02:00
const selected = getSelectValues(event.target);
2020-09-05 18:01:24 +02:00
2020-09-05 21:40:05 +02:00
console.log(event.target);
2020-08-28 22:36:50 +02:00
// Update results
save_answers(selected.flatMap(e => question_results[e] || []));
del_answers(last_answered.flatMap(e => question_results[e] || []));
save_state();
2020-09-05 18:01:24 +02:00
2020-08-28 22:36:50 +02:00
// Make possible sub question visible
const quests = selected.flatMap(e => sub_questions[e] || []);
last_answered = selected;
for (let quest of quests) {
delete quest.style.removeProperty("display");
}
});
}
2020-09-05 21:40:05 +02:00
<%end%>
2020-02-27 21:48:17 +01:00
</script>