durfdoen-2.0/layouts/partials/question.erb

120 lines
4.3 KiB
Text
Raw Normal View History

2020-09-06 20:41:54 +02:00
<h3> <%= question[:vraag] %> </h3>
2020-09-05 18:01:24 +02:00
<% if question[:type] == "specialCase1" %>
<p>
Geef je postcode in
</p>
2020-09-10 22:27:07 +02:00
<input id="postcodeField" type=number placeholder=9000>
2020-09-05 18:01:24 +02:00
<%else%>
<form id="<%= number %>_form">
<fieldset>
2020-09-14 21:33:13 +02:00
<% question[:antwoorden].sort_by{ |x| x[:tekst] }.each_with_index do |answer, idx| %>
<p>
2020-09-06 20:41:54 +02:00
<label>
<input type=<% if question[:type] == "meerkeuze" %>"checkbox"<% else %>"radio"<% end %>
value='<%=number+"#"+idx.to_s%>' name=<%= number %> >
2020-09-05 18:01:24 +02:00
<%= answer[:tekst] %>
</label>
</p>
<% end %>
</fieldset>
</form>
2020-09-14 21:51:16 +02:00
<% question[:antwoorden].sort_by{ |x| x[:tekst] }.each_with_index do |answer, idx|%>
<% if answer[:vraag] %>
2020-09-14 22:54:14 +02:00
<div id='<%= number+"#"+idx.to_s %>' class="question_wrapper" style="display:none;">
<%= render '/partials/question.*', question: answer, number: number+"."+idx.to_s %>
</div>
2020-08-28 22:36:50 +02:00
<% end %>
2020-02-27 21:48:17 +01:00
<% end %>
<%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-15 20:19:26 +02:00
console.log(postcodes);
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 = () => {
2020-09-15 20:19:26 +02:00
console.log(postcodeField.value);
2020-09-05 21:40:05 +02:00
const selected = postcodes[postcodeField.value] || [];
2020-09-10 22:27:07 +02:00
2020-09-05 21:40:05 +02:00
// 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-09-13 11:18:49 +02:00
// If false and the this question changes, all children should loose `checked`
// This might not actually work when questions are infinitely deep
const multiple_allowed = <%= question[:type] == "meerkeuze" %>;
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-14 22:27:46 +02:00
<% question[:antwoorden].sort_by{ |x| x[:tekst] }.each_with_index do |answer, idx| %>
<% if answer[:vraag] %>sub_questions['<%= number+"#"+idx.to_s %>'] = document.getElementById('<%= number+"#"+idx.to_s %>');<% end %>
question_results['<%= number+"#"+idx.to_s %>'] = <%= (answer[:verenigingen] || []).map { |vereniging| vereniging[:naam] }.to_a %>;
2020-08-28 22:36:50 +02:00
<% end %>
2020-09-05 18:01:24 +02:00
const formElement = document.getElementById("<%= number %>_form");
2020-09-10 22:27:07 +02:00
formElement.addEventListener('change', () => {
2020-08-28 22:36:50 +02:00
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
// Notice the replace, we move from an answer to a subquestion
const sub = document.getElementById(la.replace("#", ".")+"_form");
2020-09-13 11:18:49 +02:00
if (sub && !multiple_allowed) {
[...sub.elements].filter(e => e.nodeName == "INPUT").forEach(e => e.checked = false);
2020-09-10 22:27:07 +02:00
sub.dispatchEvent(new Event('change'));
2020-08-28 22:36:50 +02:00
}
}
}
2020-09-10 22:27:07 +02:00
const selected = [...formElement.elements].filter(e => e.nodeName == "INPUT").filter(e => e.checked).map(e => e.value || e.text);
2020-09-05 21:40:05 +02:00
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");
}
}, false);
2020-08-28 22:36:50 +02:00
}
2020-09-05 21:40:05 +02:00
<%end%>
2020-02-27 21:48:17 +01:00
</script>