google_forms/assets/js/scripts.js

346 lines
12 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
$(document).ready(function () {
2024-08-21 06:34:30 +00:00
let index = 1;
let activeSection = null;
2024-07-19 10:46:18 +00:00
2024-08-09 12:04:48 +00:00
function addOption(type, container) {
2024-07-19 10:46:18 +00:00
// let optionIndex = container.children().length + 1;
2024-08-21 06:34:30 +00:00
let optionHtml;
if (type === "multiple-choice" || type === "checkboxes") {
2024-08-09 12:04:48 +00:00
optionHtml = `
2024-08-21 06:34:30 +00:00
<div class="option">
<input type="${type === "multiple-choice" ? "radio" : "checkbox"}" disabled>
<input type="text" class="form-control option-label" >
<span class="delete-option-icon">&times;</span>
</div>
`;
} else if (type === "dropdown") {
2024-08-09 12:04:48 +00:00
optionHtml = `
2024-08-21 06:34:30 +00:00
<div class="option">
<input type="text" class="form-control option-label">
<span class="delete-option-icon">&times;</span>
</div>
`;
2024-07-10 12:37:36 +00:00
}
2024-08-21 06:34:30 +00:00
container.append(optionHtml);
2024-08-09 12:04:48 +00:00
}
2024-07-19 10:46:18 +00:00
2024-08-09 12:04:48 +00:00
function createFormSection() {
let newSection = `
2024-07-10 12:37:36 +00:00
<div class="form-section" data-index="${index}">
<div class="header-row">
2024-08-21 06:34:30 +00:00
${index === 1 ? '<div class="violet-border"></div>' : ""}
2024-07-24 13:12:00 +00:00
<input type="text" class="form-control untitled-question" placeholder="Untitled Question" rows="1"> <select class="custom-select">
2024-07-10 12:37:36 +00:00
<option value="short-answer">Short Answer</option>
<option value="paragraph">Paragraph</option>
<option value="multiple-choice">Multiple Choice</option>
<option value="checkboxes">Checkboxes</option>
<option value="dropdown">Dropdown</option>
</select>
<label class="toggle-switch">
2024-07-11 04:02:56 +00:00
<input type="checkbox" class="required-toggle">
2024-07-10 12:37:36 +00:00
<span class="slider"></span>
</label>
2024-07-11 04:02:56 +00:00
<span class="delete-section-icon"><i class="fas fa-trash-alt"></i></span>
2024-07-10 12:37:36 +00:00
</div>
2024-07-11 04:02:56 +00:00
<div class="options-container"></div>
2024-07-10 12:37:36 +00:00
</div>
2024-08-21 06:34:30 +00:00
`;
$("#form-container").append(newSection);
index++;
positionAddSectionButton();
2024-08-09 12:04:48 +00:00
}
2024-07-10 12:37:36 +00:00
2024-08-09 12:04:48 +00:00
function positionAddSectionButton() {
if (activeSection) {
2024-08-21 06:34:30 +00:00
let position = activeSection.position();
let buttonWidth = $("#add-section-btn").outerWidth();
let buttonHeight = $("#add-section-btn").outerHeight();
2024-07-10 12:37:36 +00:00
2024-08-21 06:34:30 +00:00
$("#add-section-btn").css({
position: "absolute",
left: position.left - buttonWidth - 47 + "px",
2024-08-09 12:04:48 +00:00
top:
2024-08-21 06:34:30 +00:00
position.top + activeSection.height() / 2 - buttonHeight / 2 + "px",
});
2024-07-10 12:37:36 +00:00
}
2024-08-09 12:04:48 +00:00
}
2024-07-19 10:46:18 +00:00
2024-08-21 06:34:30 +00:00
$("#add-section-btn").on("click", function () {
createFormSection();
$(".form-section").removeClass("active");
activeSection = $(".form-section").last();
activeSection.addClass("active");
positionAddSectionButton();
});
2024-07-19 10:46:18 +00:00
2024-08-21 06:34:30 +00:00
$(document).on("change", ".custom-select", function () {
let type = $(this).val();
let container = $(this).closest(".form-section").find(".options-container");
container.empty();
$(this).closest(".form-section").find(".add-option-btn").remove();
2024-07-10 12:37:36 +00:00
2024-08-21 06:34:30 +00:00
if (type === "short-answer") {
2024-08-09 12:04:48 +00:00
container.append(
2024-08-21 06:34:30 +00:00
'<input type="text" class="form-control" disabled placeholder="Short answer text">',
);
} else if (type === "paragraph") {
2024-08-09 12:04:48 +00:00
container.append(
2024-08-21 06:34:30 +00:00
'<textarea class="form-control" disabled placeholder="Paragraph text"></textarea>',
);
2024-08-09 12:04:48 +00:00
} else {
2024-08-21 06:34:30 +00:00
addOption(type, container);
2024-08-09 12:04:48 +00:00
$(this)
2024-08-21 06:34:30 +00:00
.closest(".form-section")
2024-08-09 12:04:48 +00:00
.append(
2024-08-21 06:34:30 +00:00
'<button class="btn btn-secondary add-option-btn">Add Option</button>',
);
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
});
2024-07-19 10:46:18 +00:00
2024-08-21 06:34:30 +00:00
$(document).on("click", ".add-option-btn", function () {
let type = $(this).closest(".form-section").find(".custom-select").val();
let container = $(this).closest(".form-section").find(".options-container");
addOption(type, container);
});
2024-07-19 10:46:18 +00:00
2024-08-21 06:34:30 +00:00
$(document).on("click", ".delete-section-icon", function () {
let section = $(this).closest(".form-section");
let prevSection = section.prev(".form-section");
let nextSection = section.next(".form-section");
section.remove();
if (section.hasClass("active")) {
activeSection = null;
2024-08-09 12:04:48 +00:00
}
if (prevSection.length > 0) {
prevSection
2024-08-21 06:34:30 +00:00
.find(".delete-section-icon")
.appendTo(prevSection.find(".form-section"));
activeSection = prevSection;
row;
2024-08-09 12:04:48 +00:00
} else if (nextSection.length > 0) {
nextSection
2024-08-21 06:34:30 +00:00
.find(".delete-section-icon")
.appendTo(nextSection.find(".form-header"));
activeSection = nextSection;
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
positionAddSectionButton();
});
2024-07-10 12:37:36 +00:00
2024-08-21 06:34:30 +00:00
$(document).on("click", ".delete-option-icon", function () {
let option = $(this).closest(".option");
let container = option.closest(".options-container");
option.remove();
});
2024-07-19 10:46:18 +00:00
2024-08-21 06:34:30 +00:00
$(document).on("click", ".required-toggle", function () {
$(this).closest(".form-section").toggleClass("required");
});
2024-07-10 12:37:36 +00:00
2024-08-21 06:34:30 +00:00
$("#preview-btn").on("click", function () {
let previewWindow = window.open("", "_blank");
2024-08-09 12:04:48 +00:00
let previewContent = `
2024-07-10 12:37:36 +00:00
<html>
<head>
<title>Form Preview</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: rgb(240, 235, 248); }
.container { margin-top: 30px; }
.form-section {background-color: white;width: 56%;margin-bottom: 30px;margin-left: 240px;padding: 20px;position: relative;align-items: center;border-radius: 10px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }
.form-header {background-color: white;padding: 20px;margin-bottom: 10px;margin-left: 240px;border-radius: 10px 10px 0 0;display: flex;justify-content: space-between;align-items: center; position: relative;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);border-top: 10px solid rgb(103, 58, 183);width: 56%; }
.form-section h2 { text-align: center; margin-bottom: 30px; }
.form-section .question-section { margin-bottom: 20px; } /* Add margin-bottom to the question section */
</style>
</head>
<body>
<div class="container">
<div class="form-header">
2024-07-24 13:12:00 +00:00
<h3>Form Preview</h3>
2024-07-10 12:37:36 +00:00
</div>
2024-08-21 06:34:30 +00:00
`;
$(".form-section").each(function () {
previewContent += '<div class="form-section">';
previewContent += '<div class="question-section">';
2024-08-09 12:04:48 +00:00
previewContent +=
'<div class="question-label">' +
2024-08-21 06:34:30 +00:00
$(this).find(".untitled-question").val() +
"</div>";
previewContent += "</div>";
let type = $(this).find(".custom-select").val();
let optionsContainer = $(this).find(".options-container");
2024-07-11 04:02:56 +00:00
2024-08-21 06:34:30 +00:00
if (type === "multiple-choice") {
optionsContainer.find(".option").each(function () {
2024-08-09 12:04:48 +00:00
previewContent += `
2024-07-10 12:37:36 +00:00
<div class="option">
<input type="radio" name="option-${index}">
2024-08-21 06:34:30 +00:00
<label>${$(this).find(".option-label").val()}</label>
2024-07-10 12:37:36 +00:00
</div>
2024-08-21 06:34:30 +00:00
`;
});
} else if (type === "checkboxes") {
optionsContainer.find(".option").each(function () {
2024-08-09 12:04:48 +00:00
previewContent += `
2024-07-10 12:37:36 +00:00
<div class="option">
<input type="checkbox">
2024-08-21 06:34:30 +00:00
<label>${$(this).find(".option-label").val()}</label>
2024-07-10 12:37:36 +00:00
</div>
2024-08-21 06:34:30 +00:00
`;
});
} else if (type === "short-answer") {
2024-08-09 12:04:48 +00:00
previewContent +=
2024-08-21 06:34:30 +00:00
'<input type="text" class="form-control" placeholder="Short answer text">';
} else if (type === "paragraph") {
2024-08-09 12:04:48 +00:00
previewContent +=
2024-08-21 06:34:30 +00:00
'<textarea class="form-control" placeholder="Paragraph text"></textarea>';
} else if (type === "dropdown") {
let dropdownHtml = '<select class="form-control">';
optionsContainer.find(".option .option-label").each(function () {
dropdownHtml += `<option>${$(this).val()}</option>`;
});
dropdownHtml += "</select>";
previewContent += dropdownHtml;
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
previewContent += "</div>";
});
2024-08-09 12:04:48 +00:00
previewContent += `
2024-07-10 12:37:36 +00:00
</div>
</body>
</html>
2024-08-21 06:34:30 +00:00
`;
previewWindow.document.write(previewContent);
previewWindow.document.close();
});
2024-07-10 12:37:36 +00:00
2024-08-21 06:34:30 +00:00
$(document).on("click", ".form-section", function () {
$(".form-section").removeClass("active");
$(this).addClass("active");
activeSection = $(this);
positionAddSectionButton();
});
2024-07-11 04:02:56 +00:00
2024-08-21 06:34:30 +00:00
$("#form-container").sortable({
placeholder: "ui-state-highlight",
2024-08-09 12:04:48 +00:00
start: function (event, ui) {
2024-08-21 06:34:30 +00:00
ui.placeholder.height(ui.item.height());
2024-08-09 12:04:48 +00:00
},
stop: function (event, ui) {
2024-08-21 06:34:30 +00:00
positionAddSectionButton();
2024-08-09 12:04:48 +00:00
},
2024-08-21 06:34:30 +00:00
});
2024-07-19 10:46:18 +00:00
2024-08-09 12:04:48 +00:00
function collectFormData() {
var formData = {
questions: [],
2024-08-21 06:34:30 +00:00
};
2024-07-19 10:46:18 +00:00
2024-08-21 06:34:30 +00:00
$(".form-section").each(function () {
var questionType = $(this).find(".custom-select").val();
2024-08-09 12:04:48 +00:00
var questionData = {
2024-08-21 06:34:30 +00:00
text: $(this).find(".untitled-question").val(),
2024-08-09 12:04:48 +00:00
type: questionType,
2024-08-21 06:34:30 +00:00
is_required: $(this).find(".required-toggle").is(":checked"),
2024-08-09 12:04:48 +00:00
options: [],
2024-08-21 06:34:30 +00:00
};
2024-08-09 12:04:48 +00:00
// Only add options if the question type supports them
if (
2024-08-21 06:34:30 +00:00
questionType === "multiple-choice" ||
questionType === "checkboxes" ||
questionType === "dropdown"
2024-08-09 12:04:48 +00:00
) {
$(this)
2024-08-21 06:34:30 +00:00
.find(".option-label")
2024-08-09 12:04:48 +00:00
.each(function () {
2024-08-21 06:34:30 +00:00
questionData.options.push($(this).val());
});
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
formData.questions.push(questionData);
});
2024-08-09 12:04:48 +00:00
2024-08-21 06:34:30 +00:00
console.log(formData);
return formData;
2024-08-09 12:04:48 +00:00
}
function validateFormData(formData) {
for (let question of formData.questions) {
if (!question.text.trim()) {
2024-08-21 06:34:30 +00:00
return { isValid: false, message: "All questions must have text." };
2024-08-09 12:04:48 +00:00
}
if (
2024-08-21 06:34:30 +00:00
(question.type === "multiple-choice" ||
question.type === "checkboxes" ||
question.type === "dropdown") &&
2024-08-09 12:04:48 +00:00
question.options.length === 0
) {
return {
isValid: false,
2024-08-21 06:34:30 +00:00
message: "All options-based questions must have at least one option.",
};
2024-08-09 12:04:48 +00:00
}
for (let option of question.options) {
if (!option.trim()) {
2024-08-21 06:34:30 +00:00
return { isValid: false, message: "All options must have text." };
2024-07-19 10:46:18 +00:00
}
2024-08-09 12:04:48 +00:00
}
2024-07-19 10:46:18 +00:00
}
2024-08-21 06:34:30 +00:00
return { isValid: true };
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
$("#submit-btn").on("click", function () {
let formData = collectFormData();
console.log(formData);
2024-08-09 12:04:48 +00:00
2024-08-21 06:34:30 +00:00
let validation = validateFormData(formData);
2024-08-09 12:04:48 +00:00
if (!validation.isValid) {
2024-08-21 06:34:30 +00:00
alert(validation.message);
return;
2024-08-09 12:04:48 +00:00
}
$.ajax({
2024-08-21 06:34:30 +00:00
url: base_url + "New_form_controller/submit_form",
type: "POST",
2024-08-09 12:04:48 +00:00
data: { formData: formData },
2024-08-21 06:34:30 +00:00
dataType: "JSON",
2024-08-09 12:04:48 +00:00
success: function (response) {
2024-08-21 06:34:30 +00:00
if (response.status === "success") {
2024-08-09 12:04:48 +00:00
Swal.fire({
2024-08-21 06:34:30 +00:00
title: "Success!",
text: "Form submitted successfully!",
icon: "success",
confirmButtonText: "OK",
2024-08-09 12:04:48 +00:00
}).then((result) => {
2024-08-21 06:34:30 +00:00
window.location.href = base_url;
});
2024-08-09 12:04:48 +00:00
} else {
Swal.fire({
2024-08-21 06:34:30 +00:00
title: "Error!",
2024-08-09 12:04:48 +00:00
text: response.message,
2024-08-21 06:34:30 +00:00
icon: "error",
confirmButtonText: "OK",
});
console.log(response);
2024-07-19 10:46:18 +00:00
}
2024-08-09 12:04:48 +00:00
},
error: function (error) {
Swal.fire({
2024-08-21 06:34:30 +00:00
title: "Error!",
text: "Error submitting form!",
icon: "error",
confirmButtonText: "OK",
width: "400px",
height: "300px",
padding: "auto",
2024-08-09 12:04:48 +00:00
}).then((result) => {
if (result.isConfirmed) {
2024-08-21 06:34:30 +00:00
window.location.href = home;
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
});
console.log(error);
2024-08-09 12:04:48 +00:00
},
2024-08-21 06:34:30 +00:00
});
});
2024-08-09 12:04:48 +00:00
2024-08-21 06:34:30 +00:00
$("#form-container").disableSelection();
});