Fully implement drop-down selections

Closes #8
This commit is contained in:
Yingtong Li 2018-01-08 14:29:01 +08:00
parent e6d37b73f8
commit 05de3ec5d7
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -107,9 +107,7 @@
function choicesChanged() { function choicesChanged() {
// Recalculate numbers // Recalculate numbers
$(".preferential-choices .preferential-choice .number select").each(function(i, el) { $(".preferential-choices .preferential-choice .number select").val("");
$(el).val("");
});
var selectedChoices = $("#question-choices-selected .dragarea > .preferential-choice > .number select"); var selectedChoices = $("#question-choices-selected .dragarea > .preferential-choice > .number select");
selectedChoices.each(function(i, el) { selectedChoices.each(function(i, el) {
$(el).val(i + 1); $(el).val(i + 1);
@ -147,6 +145,10 @@
choicesChanged(); choicesChanged();
} }
// =============
// DRAG AND DROP
// =============
var dragulaChoices = dragula( var dragulaChoices = dragula(
[document.querySelector("#question-choices-selected .dragarea"), document.querySelector("#question-choices-remaining .dragarea")].concat([].slice.apply(document.querySelectorAll(".ticket-choices"))), [document.querySelector("#question-choices-selected .dragarea"), document.querySelector("#question-choices-remaining .dragarea")].concat([].slice.apply(document.querySelectorAll(".ticket-choices"))),
{ {
@ -164,9 +166,10 @@
); );
function breakTicket(ticket) { function breakTicket(ticket) {
ticket.find(".ticket-choices .preferential-choice").each(function(i, el) { //ticket.find(".ticket-choices .preferential-choice").each(function(i, el) {
$(el).detach().insertAfter(ticket); // $(el).detach().insertAfter(ticket);
}); //});
ticket.find(".ticket-choices .preferential-choice").detach().insertAfter(ticket);
ticket.remove(); ticket.remove();
} }
@ -182,6 +185,47 @@
choicesChanged(); choicesChanged();
}); });
// ===============
// DROP DOWN BOXES
// ===============
$(".preferential-choices .preferential-choice .number select").change(function(evt) {
var index = parseInt($(this).val());
var choiceEl = $(this).parents(".preferential-choice").first();
var ticket = null;
// If the source is within a ticket, break the ticket afterwards
// (It is impossible for the target to be within a ticket)
if (choiceEl.parents(".ticket").length > 0) {
ticket = choiceEl.parents(".ticket").first();
}
// Simulate drag-and-drop
var selectedChoices = $("#question-choices-selected .dragarea > .preferential-choice");
if ($(this).val() === "") {
// Deselect
choiceEl.detach().appendTo($("#question-choices-remaining .dragarea"));
} else if (index > selectedChoices.length) {
// Append to end
choiceEl.detach().appendTo($("#question-choices-selected .dragarea"));
} else {
// Insert in the middle
choiceEl.detach().insertBefore(selectedChoices[index - 1]);
}
// Now break the ticket
if (ticket !== null) {
breakTicket(ticket);
}
choicesChanged();
});
// =======
// GENERAL
// =======
function saveSelections() { function saveSelections() {
selections = []; selections = [];
$("#question-choices-selected .preferential-choice:not(.ticket)").each(function(i, el) { $("#question-choices-selected .preferential-choice:not(.ticket)").each(function(i, el) {