$(document).ready(function() { let index = 1; let activeSection = null; function addOption(type, container) { let optionIndex = container.children().length + 1; let optionHtml; if (type === 'multiple-choice' || type === 'checkboxes') { optionHtml = `
×
`; } else if (type === 'dropdown') { optionHtml = `
×
`; } container.append(optionHtml); } function createFormSection() { let newSection = `
${index === 1 ? '
' : ''}
Required
`; $('#form-container').append(newSection); index++; positionAddSectionButton(); } function positionAddSectionButton() { if (activeSection) { let position = activeSection.position(); let buttonWidth = $('#add-section-btn').outerWidth(); let buttonHeight = $('#add-section-btn').outerHeight(); $('#add-section-btn').css({ position: 'absolute', left: position.left - buttonWidth - 47 + 'px', top: position.top + activeSection.height() / 2 - buttonHeight / 2 + 'px' }); } else { let containerPosition = $('#form-container').position(); let buttonWidth = $('#add-section-btn').outerWidth(); let buttonHeight = $('#add-section-btn').outerHeight(); $('#add-section-btn').css({ position: 'absolute', left: containerPosition.left + 'px', top: containerPosition.top + $('#form-container').height() + 20 + 'px' }); } } $('#add-section-btn').on('click', function() { createFormSection(); $('.form-section').removeClass('active'); activeSection = $('.form-section').last(); activeSection.addClass('active'); positionAddSectionButton(); }); $(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(); if (type === 'short-answer') { container.append(''); } else if (type === 'paragraph') { container.append(''); } else { addOption(type, container); $(this).closest('.form-section').append(''); } }); $(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); }); $(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; } if (prevSection.length > 0) { prevSection.find('.delete-section-icon').appendTo(prevSection.find('.form-section')); activeSection = prevSection; } else if (nextSection.length > 0) { nextSection.find('.delete-section-icon').appendTo(nextSection.find('.form-header')); activeSection = nextSection; } positionAddSectionButton(); }); $(document).on('click', '.delete-option-icon', function() { let option = $(this).closest('.option'); let container = option.closest('.options-container'); option.remove(); }); $(document).on('click', '.form-section', function() { $('.form-section').removeClass('active'); $(this).addClass('active'); activeSection = $(this); positionAddSectionButton(); }); $('#preview-btn').on('click', function() { let previewWindow = window.open('', '_blank'); let previewContent = ` Form Preview

Untitled Form

`; $('.form-section').each(function() { previewContent += '
'; previewContent += '
'; previewContent += ''; previewContent += '
'; let type = $(this).find('.custom-select').val(); let optionsContainer = $(this).find('.options-container'); if (type === 'multiple-choice') { optionsContainer.find('.option').each(function() { previewContent += `
`; }); } else if (type === 'checkboxes') { optionsContainer.find('.option').each(function() { previewContent += `
`; }); } else if (type === 'short-answer') { previewContent += ''; } else if (type === 'paragraph') { previewContent += ''; } else if (type === 'dropdown') { let dropdownHtml = ''; previewContent += dropdownHtml; } previewContent += '
'; }); previewContent += `
`; previewWindow.document.write(previewContent); previewWindow.document.close(); }); $('#form-container').sortable({ placeholder: 'ui-state-highlight', start: function (event, ui) { ui.placeholder.height(ui.item.height()); }, stop: function (event, ui) { positionAddSectionButton(); } }); $('#form-container').disableSelection(); $(document).on('change', '.toggleButton', function() { let status = $(this).prop('checked'); if (status) { $(this).closest('.form-section').addClass('required-section'); } else { $(this).closest('.form-section').removeClass('required-section'); } }); });