Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/projects/quiz-extended/js/
1 filer
create_quiz.js
178 lines ASCII Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
// REMEMBER THAT ALL NUMBER ADJUSTMENTS SHOULD TAKE INTO ACCOUNT TWO DIGIT-SIZES WHEN STRING HANDLING SUCH AS q10_Txt...
var btnsNewOption = document.querySelectorAll(".btnNewOption");
var btnNewQuestion = document.getElementById("btnNewQuestion");
var btnsRemoveOption = document.querySelectorAll(".btnRemoveOption");
var btnsRemoveQuestion = document.querySelectorAll(".btnRemoveQuestion");
btnsNewOption.forEach((btn) => {
btn.addEventListener("click", newOption);
});
btnNewQuestion.addEventListener("click", newQuestion);
btnsRemoveOption.forEach((btn) => {
btn.addEventListener("click", removeOption);
});
btnsRemoveQuestion.forEach((btn) => {
btn.addEventListener("click", removeQuestion);
});
function newOption() {
var options = this.parentNode.querySelectorAll(".newOption"); // all siblings with newOption class
var optionId = (options.length + 1);
var questionId = this.parentNode.parentNode.dataset.questionId;
var newOptionElement = createOptionElement(questionId, optionId); // QHICH QUESTIOn
this.parentNode.insertBefore(newOptionElement, this); // insert new element before clicked button in the DOM
}
function newQuestion() {
var questions = document.getElementsByClassName("newQuestion");
var questionId = (questions.length + 1);
var newQuestionElement = createQuestionElement(questionId);
this.parentNode.insertBefore(newQuestionElement, this); // insert new element before clicked button in the DOM
}
// removes option and adjusts numbers of subsequent options (eg. 4 -> 3)
function removeOption() {
var optionElement = this.parentNode;
var option = optionElement;
while (option.nextElementSibling.classList.contains("newOption")) {
option = option.nextElementSibling;
var newOptionId = option.dataset.optionId - 1;
option.dataset.optionId = newOptionId;
option.firstElementChild.firstElementChild.previousSibling.textContent = "Option " + newOptionId;
var textarea = option.firstElementChild.lastElementChild;
var radioButton = option.firstElementChild.nextElementSibling;
var textareaNameNoNumber = textarea.name.slice(0, textarea.name.lastIndexOf("_") + 1); // eg. "q2_optText_"
textarea.name = textareaNameNoNumber + newOptionId;
radioButton.value--;
}
optionElement.parentNode.removeChild(optionElement); // removes the option
}
function removeQuestion() {
var questionElement = this.parentNode;
var question = questionElement;
while (question.nextElementSibling.classList.contains("newQuestion")) {
question = question.nextElementSibling;
var newQuestionId = question.dataset.questionId - 1;
question.dataset.questionId = newQuestionId;
var label = question.firstElementChild;
label.firstElementChild.previousSibling.textContent = "Question " + newQuestionId;
var textarea = label.lastElementChild;
textarea.name = "qstText_" + newQuestionId;
var options = question.lastElementChild.querySelectorAll(".newOption");
setQuestionIdToOptions(newQuestionId, options);
}
questionElement.parentNode.removeChild(questionElement); // removes the question in question haha! :P
}
function setQuestionIdToOptions(questionId, options) {
options.forEach((option) => {
var textarea = option.firstElementChild.lastElementChild;
textarea.name = "q" + questionId + textarea.name.slice(textarea.name.indexOf("_"));
var radioButton = option.children[1];
radioButton.name = "q" + questionId + radioButton.name.slice(radioButton.name.indexOf("_"));
});
}
function createQuestionElement(questionId) {
var newQuestionDiv = document.createElement("div");
newQuestionDiv.classList.add("newQuestion");
newQuestionDiv.setAttribute("data-question-id", questionId);
var label = document.createElement("label");
label.innerHTML = "Question " + questionId + "<br>";
var removeQuestionBtn = document.createElement("div");
removeQuestionBtn.classList.add("btnRemoveQuestion");
removeQuestionBtn.addEventListener("click", removeQuestion);
var textarea = document.createElement("textarea");
textarea.classList.add("questionText");
textarea.name = "qstText_" + questionId;
var newOptionsDiv = document.createElement("div");
newOptionsDiv.classList.add("newOptions");
newOptionsDiv.innerHTML = "Mark the <strong>correct</strong> option using the radio buttons.<br><br>";
var newOptionsBtn = document.createElement("button");
newOptionsBtn.classList.add("btnNewOption");
newOptionsBtn.type = "button"; // prevents page reloading on click
newOptionsBtn.innerHTML = "+ new option";
newOptionsBtn.addEventListener("click", newOption);
newOptionsDiv.appendChild(newOptionsBtn);
newOptionsDiv.insertBefore(createOptionElement(questionId, 1), newOptionsBtn);
newOptionsDiv.insertBefore(createOptionElement(questionId, 2), newOptionsBtn);
label.appendChild(textarea);
newQuestionDiv.appendChild(label);
newQuestionDiv.appendChild(removeQuestionBtn)
newQuestionDiv.appendChild(newOptionsDiv);
return newQuestionDiv;
}
function createOptionElement(questionId, optionId) {
var newOptionDiv = document.createElement("div");
newOptionDiv.classList.add("newOption");
newOptionDiv.setAttribute("data-option-id", optionId);
var label = document.createElement("label");
var textarea = document.createElement("textarea");
textarea.classList.add("optionText");
textarea.name = "q" + questionId + "_optText_" + optionId;
var input = document.createElement("input");
input.type = "radio";
input.name = "q" + questionId + "_correctOption";
input.value = optionId;
input.required = true;
if (optionId === 1)
input.checked = true;
var removeOptionBtn = document.createElement("button");
removeOptionBtn.classList.add("btnRemoveOption");
removeOptionBtn.type = "button";
removeOptionBtn.addEventListener("click", removeOption);
label.innerHTML = "Option " + optionId + "<br>";
label.appendChild(textarea);
newOptionDiv.appendChild(label);
newOptionDiv.appendChild(input);
newOptionDiv.appendChild(removeOptionBtn);
return newOptionDiv;
}