/** * 템플릿 작성/수정 폼 JavaScript */ let questionIndex = 0; // 페이지 로드 시 초기화 document.addEventListener('DOMContentLoaded', function() { initTemplateForm(); }); function initTemplateForm() { // 초기 데이터 설정 if (window.templateFormData) { questionIndex = window.templateFormData.questionIndex; } // 폼 검증 이벤트 initFormValidation(); // 기존 질문들의 타입에 따라 옵션 표시 document.querySelectorAll('.question-type-select').forEach(select => { toggleOptions(select.closest('.question-item').dataset.index); }); } function addQuestion() { const container = document.getElementById('questionsContainer'); const emptyState = document.getElementById('emptyQuestions'); if (emptyState) { emptyState.remove(); } const questionHtml = `
${questionIndex + 1}
질문 ${questionIndex + 1}
`; container.insertAdjacentHTML('beforeend', questionHtml); questionIndex++; updateQuestionNumbers(); } function removeQuestion(index) { if (confirm('이 질문을 삭제하시겠습니까?')) { const questionItem = document.querySelector(`[data-index="${index}"]`); questionItem.remove(); const remainingQuestions = document.querySelectorAll('.question-item'); if (remainingQuestions.length === 0) { const container = document.getElementById('questionsContainer'); container.innerHTML = `

질문을 추가해주세요

아래 버튼을 클릭하여 첫 번째 질문을 만들어보세요.

`; } else { updateQuestionNumbers(); } } } function moveQuestion(index, direction) { const questionItem = document.querySelector(`[data-index="${index}"]`); const container = document.getElementById('questionsContainer'); if (direction === 'up' && questionItem.previousElementSibling) { container.insertBefore(questionItem, questionItem.previousElementSibling); } else if (direction === 'down' && questionItem.nextElementSibling) { container.insertBefore(questionItem.nextElementSibling, questionItem); } updateQuestionNumbers(); } function updateQuestionNumbers() { const questions = document.querySelectorAll('.question-item'); questions.forEach((question, index) => { const numberElement = question.querySelector('.question-number'); const titleElement = question.querySelector('.question-title-text'); numberElement.textContent = index + 1; titleElement.textContent = `질문 ${index + 1}`; // data-index 업데이트 question.dataset.index = index; // input name 속성 업데이트 const inputs = question.querySelectorAll('input, textarea, select'); inputs.forEach(input => { if (input.name && input.name.includes('questions[')) { input.name = input.name.replace(/questions\[\d+\]/, `questions[${index}]`); } }); // 옵션 컨테이너 ID 업데이트 const optionsContainer = question.querySelector('.options-container'); if (optionsContainer) { optionsContainer.id = `optionsContainer${index}`; } }); } function toggleOptions(index) { const questionItem = document.querySelector(`[data-index="${index}"]`); const typeSelect = questionItem.querySelector('.question-type-select'); const selectedType = typeSelect.value; // 기존 옵션 컨테이너 제거 const existingOptions = questionItem.querySelector('.options-container'); if (existingOptions) { existingOptions.remove(); } // 객관식 질문인 경우 옵션 컨테이너 추가 if (['radio', 'checkbox', 'select'].includes(selectedType)) { const optionsHtml = `
`; questionItem.insertAdjacentHTML('beforeend', optionsHtml); } } function addOption(questionIndex) { const container = document.getElementById(`optionsContainer${questionIndex}`); const addButton = container.querySelector('.btn-add-option'); const optionHtml = `
`; addButton.insertAdjacentHTML('beforebegin', optionHtml); } function removeOption(button) { const optionsContainer = button.closest('.options-container'); const optionItems = optionsContainer.querySelectorAll('.option-item'); if (optionItems.length > 1) { button.parentElement.remove(); } else { alert('최소 1개의 옵션이 필요합니다.'); } } // 폼 검증 function initFormValidation() { document.querySelector('form[name="templateForm"]').addEventListener('submit', function(e) { const templateName = document.querySelector('input[name="st_name"]').value.trim(); if (!templateName) { alert('템플릿 이름을 입력해주세요.'); e.preventDefault(); return; } const questions = document.querySelectorAll('.question-item'); if (questions.length === 0) { alert('최소 1개 이상의 질문을 추가해주세요.'); e.preventDefault(); return; } // 각 질문의 제목 검증 let hasEmptyTitle = false; questions.forEach((question, index) => { const titleInput = question.querySelector('input[name*="[stq_title]"]'); if (!titleInput.value.trim()) { alert(`질문 ${index + 1}의 제목을 입력해주세요.`); hasEmptyTitle = true; return; } }); if (hasEmptyTitle) { e.preventDefault(); return; } // 객관식 질문의 옵션 검증 let hasEmptyOptions = false; questions.forEach((question, index) => { const typeSelect = question.querySelector('.question-type-select'); const selectedType = typeSelect.value; if (['radio', 'checkbox', 'select'].includes(selectedType)) { const optionInputs = question.querySelectorAll('.option-input'); const filledOptions = Array.from(optionInputs).filter(input => input.value.trim()); if (filledOptions.length < 2) { alert(`질문 ${index + 1}은 최소 2개의 옵션이 필요합니다.`); hasEmptyOptions = true; return; } } }); if (hasEmptyOptions) { e.preventDefault(); return; } }); }