2024-08-06 09:20:56 +00:00
|
|
|
$(document).ready(function () {
|
|
|
|
let index = 1
|
|
|
|
let activeSection = null
|
2024-08-06 10:38:01 +00:00
|
|
|
|
2024-08-09 07:09:33 +00:00
|
|
|
function addOption(type, container)
|
2024-08-09 07:03:11 +00:00
|
|
|
let optionHtml
|
|
|
|
|
|
|
|
if (type === 'multiple-choice' || type === 'checkboxes') {
|
|
|
|
optionHtml = `
|
|
|
|
<div class="option">
|
|
|
|
<input type="${type === 'multiple-choice' ? 'radio' : 'checkbox'}" disabled>
|
|
|
|
<input type="text" class="form-control option-label">
|
|
|
|
|
|
|
|
<span class="delete-option-icon">×</span>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
} else if (type === 'dropdown') {
|
|
|
|
optionHtml = `
|
|
|
|
<div class="option">
|
|
|
|
<input type="text" class="form-control option-label">
|
|
|
|
<span class="delete-option-icon">×</span>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
container.append(optionHtml)
|
|
|
|
}
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-08-06 09:20:56 +00:00
|
|
|
function createFormSection() {
|
|
|
|
let newSection = `
|
2024-07-19 10:46:18 +00:00
|
|
|
<div class="form-section" data-index="${index}">
|
2024-08-06 09:20:56 +00:00
|
|
|
<div class="header-row">
|
|
|
|
${index === 1 ? '<div class="violet-border"></div>' : ''}
|
|
|
|
<textarea class="form-control untitled-question" placeholder="Untitled Question" rows="1"></textarea>
|
2024-07-19 10:46:18 +00:00
|
|
|
<select class="custom-select">
|
|
|
|
<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">
|
|
|
|
<input type="checkbox" class="required-toggle">
|
|
|
|
<span class="slider"></span>
|
|
|
|
</label>
|
|
|
|
<span class="delete-section-icon"><i class="fas fa-trash-alt"></i></span>
|
|
|
|
</div>
|
|
|
|
<div class="options-container"></div>
|
|
|
|
</div>
|
2024-08-06 09:20:56 +00:00
|
|
|
`
|
|
|
|
$('#form-container').append(newSection)
|
|
|
|
index++
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-08-06 09:20:56 +00:00
|
|
|
positionAddSectionButton()
|
|
|
|
}
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-08-06 09:20:56 +00:00
|
|
|
function positionAddSectionButton() {
|
|
|
|
if (activeSection) {
|
|
|
|
let position = activeSection.position()
|
|
|
|
let buttonWidth = $('#add-section-btn').outerWidth()
|
|
|
|
let buttonHeight = $('#add-section-btn').outerHeight()
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-08-06 09:20:56 +00:00
|
|
|
$('#add-section-btn').css({
|
|
|
|
position: 'absolute',
|
|
|
|
left: position.left - buttonWidth - 47 + 'px',
|
|
|
|
top:
|
|
|
|
position.top + activeSection.height() / 2 - buttonHeight / 2 + 'px',
|
|
|
|
})
|
2024-07-19 10:46:18 +00:00
|
|
|
}
|
2024-08-06 09:20:56 +00:00
|
|
|
}
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-08-06 09:20:56 +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-06 09:20:56 +00:00
|
|
|
$(document).on('change', '.custom-select', function () {
|
|
|
|
let type = $(this).val()
|
|
|
|
let container = $(this).closest('.form-section').find('.options-container')
|
|
|
|
container.empty()
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-08-06 09:20:56 +00:00
|
|
|
$(this).closest('.form-section').find('.add-option-btn').remove()
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-08-06 09:20:56 +00:00
|
|
|
if (type === 'short-answer') {
|
|
|
|
container.append(
|
|
|
|
'<input type="text" class="form-control" disabled placeholder="Short answer text">'
|
|
|
|
)
|
|
|
|
} else if (type === 'paragraph') {
|
2024-08-09 06:59:24 +00:00
|
|
|
container.append(
|
2024-08-06 09:20:56 +00:00
|
|
|
'<textarea class="form-control" disabled placeholder="Paragraph text"></textarea>'
|
|
|
|
)
|
|
|
|
} else {
|
2024-08-09 06:59:24 +00:00
|
|
|
addOption(type, container)
|
2024-08-06 09:20:56 +00:00
|
|
|
$(this)
|
|
|
|
.closest('.form-section')
|
|
|
|
.append(
|
2024-08-09 07:03:11 +00:00
|
|
|
'<button class="btn btn-secondary add-option-btn">Add Option</button>'
|
2024-08-06 09:20:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|