2024-07-15 06:24:05 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
|
|
const questionsSection = document.getElementById("questions_section");
|
|
|
|
|
|
|
|
function addOption(button) {
|
|
|
|
const optionContainer = button.previousElementSibling;
|
|
|
|
const optionDiv = document.createElement("div");
|
|
|
|
optionDiv.className = "option";
|
|
|
|
optionDiv.innerHTML = `
|
2024-07-16 19:58:18 +00:00
|
|
|
<input type="text" class="form-control option-input" placeholder="New Option" />
|
|
|
|
<span class="delete-option" onclick="deleteOption(this)">✕</span>
|
2024-07-15 06:24:05 +00:00
|
|
|
`;
|
|
|
|
optionContainer.appendChild(optionDiv);
|
|
|
|
updateAddButtonPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteOption(span) {
|
2024-07-17 06:39:33 +00:00
|
|
|
console.log("Yash");
|
2024-07-15 06:24:05 +00:00
|
|
|
const optionDiv = span.parentElement;
|
|
|
|
optionDiv.remove();
|
|
|
|
updateAddButtonPosition();
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:12:07 +00:00
|
|
|
function changeQuestionType(selectElement) {
|
2024-07-24 05:41:55 +00:00
|
|
|
const questionContainer = selectElement.closest(".question");
|
|
|
|
const optionsContainer =
|
|
|
|
questionContainer.querySelector(".options-container");
|
|
|
|
const addOptionButton =
|
|
|
|
questionContainer.querySelector(".btn-secondary");
|
2024-07-18 21:12:07 +00:00
|
|
|
const questionType = selectElement.value;
|
|
|
|
|
|
|
|
// Clear the options container
|
2024-07-24 05:41:55 +00:00
|
|
|
optionsContainer.innerHTML = "";
|
|
|
|
|
|
|
|
if (
|
|
|
|
questionType === "multiple_choice" ||
|
|
|
|
questionType === "checkbox" ||
|
|
|
|
questionType === "dropdown"
|
|
|
|
) {
|
|
|
|
const optionDiv = document.createElement("div");
|
|
|
|
optionDiv.className = "option d-flex align-items-center mb-2";
|
2024-07-18 21:12:07 +00:00
|
|
|
optionDiv.innerHTML = `
|
|
|
|
<input type="text" name="option" class="form-control option-input" placeholder="Option 1" />
|
|
|
|
<span class="delete-option ml-2 text-danger" onclick="deleteOption(this)" style="cursor: pointer;">✕</span>
|
|
|
|
`;
|
|
|
|
optionsContainer.appendChild(optionDiv);
|
2024-07-24 05:41:55 +00:00
|
|
|
addOptionButton.style.display = "inline-block"; // Show the "Add Option" button
|
|
|
|
} else if (questionType === "text") {
|
|
|
|
addOptionButton.style.display = "none"; // Hide the "Add Option" button
|
2024-07-18 21:12:07 +00:00
|
|
|
}
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let questionCount = document.querySelectorAll(".question").length;
|
|
|
|
|
|
|
|
function addNewQuestion() {
|
|
|
|
const newQuestionDiv = document.createElement("div");
|
2024-07-19 11:08:05 +00:00
|
|
|
// newQuestionDiv.className = "question";
|
2024-07-15 06:24:05 +00:00
|
|
|
newQuestionDiv.innerHTML = `
|
2024-07-24 06:13:50 +00:00
|
|
|
<div class="question mb-4 p-4 border rounded bg-white shadow-sm">
|
2024-07-17 06:39:33 +00:00
|
|
|
<select class="form-control question_type mb-1" onchange="changeQuestionType(this)">
|
2024-07-24 06:13:50 +00:00
|
|
|
<option style="border:1px solid rgb(103,58,183);" value="">Select Question Type</option>
|
2024-07-16 19:58:18 +00:00
|
|
|
<option value="multiple_choice">Multiple Choice</option>
|
|
|
|
<option value="checkbox">Checkbox</option>
|
|
|
|
<option value="dropdown">Dropdown</option>
|
2024-07-18 21:12:07 +00:00
|
|
|
<option value="text">Text</option>
|
2024-07-16 19:58:18 +00:00
|
|
|
</select>
|
2024-07-24 06:13:50 +00:00
|
|
|
<input style="border:none; border-bottom: 2px solid rgb(103,58,183); border-radius:0" type="text" name="question" class="form-control question-input mb-3" placeholder="Type your question here" />
|
2024-07-16 19:58:18 +00:00
|
|
|
<div class="options-container mb-3">
|
2024-07-19 11:08:05 +00:00
|
|
|
|
2024-07-16 19:58:18 +00:00
|
|
|
</div>
|
2024-07-18 21:12:07 +00:00
|
|
|
<button class="btn btn-secondary add-option-btn" onclick="addOption(this)">
|
2024-07-16 19:58:18 +00:00
|
|
|
Add Option
|
|
|
|
</button>
|
2024-07-17 06:39:33 +00:00
|
|
|
<button class="btn btn-md" id="moveUpButton" onclick="deleteQuestion(this);">
|
|
|
|
<img src="/images/bin.png" alt="" width="20px" height="20px" />
|
2024-07-16 19:58:18 +00:00
|
|
|
</button>
|
2024-07-18 21:12:07 +00:00
|
|
|
<label class="ml-3">
|
|
|
|
<input type="checkbox" class="required-checkbox"> Required
|
|
|
|
</label>
|
2024-07-15 06:24:05 +00:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
questionsSection.appendChild(newQuestionDiv);
|
|
|
|
questionCount++;
|
|
|
|
updateAddButtonPosition();
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:58:18 +00:00
|
|
|
function deleteQuestion(element) {
|
|
|
|
let questionContainer = element.closest(".question");
|
|
|
|
if (questionContainer) {
|
|
|
|
questionContainer.remove();
|
2024-07-17 06:39:33 +00:00
|
|
|
questionCount--;
|
2024-07-16 19:58:18 +00:00
|
|
|
updateAddButtonPosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateAddButtonPosition() {
|
|
|
|
const questions = questionsSection.querySelectorAll(".question");
|
2024-07-24 05:41:55 +00:00
|
|
|
const sidebar = document.getElementById("moveableDiv");
|
2024-07-17 06:39:33 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
if (questions.length > 0) {
|
|
|
|
const lastQuestion = questions[questions.length - 1];
|
|
|
|
const offsetTop = lastQuestion.offsetTop;
|
|
|
|
const sidebarHeight = sidebar.offsetHeight;
|
|
|
|
const containerHeight = questionsSection.offsetHeight;
|
2024-07-17 06:39:33 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
// Calculate the position of the last question relative to the top of the container
|
|
|
|
const newPosition = offsetTop + lastQuestion.offsetHeight;
|
2024-07-16 19:58:18 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
// Ensure the sidebar stays within the bounds of the container
|
|
|
|
if (newPosition + sidebarHeight <= containerHeight) {
|
|
|
|
sidebar.style.transform = `translateY(${newPosition}px)`;
|
|
|
|
console.log(`Moving sidebar to: ${newPosition}px`);
|
|
|
|
} else {
|
|
|
|
sidebar.style.transform = `translateY(${
|
|
|
|
containerHeight - sidebarHeight
|
|
|
|
}px)`;
|
|
|
|
console.log(`Moving sidebar to bottom of container`);
|
|
|
|
}
|
2024-07-16 19:58:18 +00:00
|
|
|
} else {
|
2024-07-24 05:41:55 +00:00
|
|
|
sidebar.style.transform = `translateY(0px)`;
|
|
|
|
console.log("No questions, moving sidebar to top");
|
2024-07-16 19:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
function saveForm() {
|
|
|
|
const formTitle = document.getElementById("form-title").value;
|
2024-07-24 05:41:55 +00:00
|
|
|
const formDescription =
|
|
|
|
document.getElementById("form-description").value;
|
2024-07-15 06:24:05 +00:00
|
|
|
const questions = document.querySelectorAll(".question");
|
|
|
|
let formData = [];
|
|
|
|
|
2024-07-19 11:08:05 +00:00
|
|
|
console.log(questions);
|
|
|
|
|
2024-07-18 21:12:07 +00:00
|
|
|
questions.forEach((question) => {
|
2024-07-15 06:24:05 +00:00
|
|
|
const questionType = question.querySelector("select").value;
|
2024-07-24 05:41:55 +00:00
|
|
|
const questionText =
|
|
|
|
question.querySelector(".question-input").value;
|
|
|
|
const isRequired =
|
|
|
|
question.querySelector(".required-checkbox").checked;
|
2024-07-18 21:12:07 +00:00
|
|
|
let options = [];
|
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
if (
|
|
|
|
questionType === "multiple_choice" ||
|
|
|
|
questionType === "checkbox" ||
|
|
|
|
questionType === "dropdown"
|
|
|
|
) {
|
|
|
|
options = Array.from(
|
|
|
|
question.querySelectorAll(".option-input")
|
|
|
|
).map((input) => input.value);
|
2024-07-18 21:12:07 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
formData.push({
|
|
|
|
type: questionType,
|
2024-07-16 19:58:18 +00:00
|
|
|
text: questionText,
|
2024-07-15 06:24:05 +00:00
|
|
|
options: options,
|
2024-07-24 05:41:55 +00:00
|
|
|
required: isRequired,
|
2024-07-15 06:24:05 +00:00
|
|
|
});
|
2024-07-19 11:08:05 +00:00
|
|
|
|
|
|
|
console.log({
|
|
|
|
type: questionType,
|
|
|
|
text: questionText,
|
|
|
|
options: options,
|
2024-07-24 05:41:55 +00:00
|
|
|
required: isRequired,
|
2024-07-19 11:08:05 +00:00
|
|
|
});
|
2024-07-15 06:24:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const csrfTokenMeta = document.querySelector('meta[name="csrf-token"]');
|
|
|
|
let csrfToken = "";
|
|
|
|
if (csrfTokenMeta) {
|
|
|
|
csrfToken = csrfTokenMeta.getAttribute("content");
|
|
|
|
} else {
|
|
|
|
console.error("CSRF token meta tag not found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
title: formTitle,
|
|
|
|
description: formDescription,
|
2024-07-24 05:41:55 +00:00
|
|
|
questions: formData,
|
2024-07-15 06:24:05 +00:00
|
|
|
};
|
|
|
|
|
2024-07-19 11:08:05 +00:00
|
|
|
console.log(data);
|
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
fetch("/forms", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
2024-07-24 05:41:55 +00:00
|
|
|
"X-CSRF-TOKEN": csrfToken,
|
2024-07-15 06:24:05 +00:00
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
})
|
2024-07-24 05:41:55 +00:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((result) => {
|
|
|
|
if (result.success) {
|
|
|
|
Swal.fire({
|
|
|
|
title: "Success!",
|
|
|
|
text: "Form saved successfully.",
|
|
|
|
icon: "success",
|
|
|
|
confirmButtonText: "OK",
|
|
|
|
}).then((result) => {
|
|
|
|
if (result.isConfirmed) {
|
|
|
|
window.location.href = "/forms";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Swal.fire({
|
|
|
|
title: "Error!",
|
|
|
|
text: "Failed to save form.",
|
|
|
|
icon: "error",
|
|
|
|
confirmButtonText: "OK",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error("Error saving form:", error);
|
|
|
|
alert("An error occurred while saving the form.");
|
|
|
|
});
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addNewQuestion = addNewQuestion;
|
|
|
|
window.deleteQuestion = deleteQuestion;
|
|
|
|
window.addOption = addOption;
|
2024-07-17 06:39:33 +00:00
|
|
|
window.deleteOption = deleteOption;
|
2024-07-15 06:24:05 +00:00
|
|
|
window.changeQuestionType = changeQuestionType;
|
|
|
|
window.saveForm = saveForm;
|
|
|
|
|
2024-07-19 11:08:05 +00:00
|
|
|
// document.getElementById("add-question-button").addEventListener("click", addNewQuestion);
|
2024-07-15 06:24:05 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
document
|
|
|
|
.getElementById("questions_section")
|
|
|
|
.addEventListener("DOMNodeInserted", updateAddButtonPosition);
|
|
|
|
document
|
|
|
|
.getElementById("questions_section")
|
|
|
|
.addEventListener("DOMNodeRemoved", updateAddButtonPosition);
|
2024-07-16 19:58:18 +00:00
|
|
|
});
|