(function() { //console.log('form.js가 로드되었습니다.'); function initializeModalComponents(modal) { //console.log('모달 컴포넌트 초기화 시작'); // 약간의 지연을 주어 모달이 완전히 렌더링되도록 함 setTimeout(() => { initializeCalendar(modal); initializeSelectField(modal); initializeTinyMCE(modal); }, 100); } function initializeCalendar(container) { const calendarInputs = container.querySelectorAll('.calender'); if (calendarInputs.length > 0) { //console.log('달력 초기화 시작'); $(calendarInputs).datepicker({ changeYear: true, changeMonth: true, yearRange: "-10:+0", dateFormat: "yy-mm-dd" }); //console.log('달력 초기화 완료'); } } function initializeSelectField(container) { const selectFields = container.querySelectorAll('.select-field'); if (selectFields.length > 0 && typeof $.fn.select2 !== 'undefined') { //console.log('선택 필드 초기화 시작'); $(selectFields).select2({ theme: "classic", width: 'style', dropdownAutoWidth: true, dropdownParent: $('#index_action_form'), containerCssClass: 'text-start', // 왼쪽 정렬을 위한 클래스 추가 dropdownCssClass: 'text-start' // 드롭다운 메뉴도 왼쪽 정렬 }); //console.log('선택 필드 초기화 완료'); } } function initializeTinyMCE(container) { const textareas = container.querySelectorAll('textarea.tinymce'); if (textareas.length > 0 && typeof tinymce !== 'undefined') { //console.log('TinyMCE 초기화 시작'); tinymce.init({ selector: textareas, plugins: ['code', 'image', 'preview', 'table', 'emoticons', 'autoresize'], height: 600, automatic_uploads: false, images_upload_url: '/tinymce_upload.php', images_upload_handler: function (blobInfo, success, failure) { var xhr, formData; xhr = new XMLHttpRequest(); xhr.withCredentials = false; xhr.open('POST', '/tinymce_upload.php'); xhr.onload = function () { var json; if (xhr.status != 200) { failure('HTTP Error: ' + xhr.status); return; } json = JSON.parse(xhr.responseText); if (!json || typeof json.file_path != 'string') { failure('Invalid JSON: ' + xhr.responseText); return; } success(json.file_path); }; formData = new FormData(); formData.append('file', blobInfo.blob(), blobInfo.filename()); xhr.send(formData); }, setup: function(editor) { editor.on('init', function() { //console.log('TinyMCE 에디터 초기화 완료'); }); } }); } } // MutationObserver 설정 const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.type === 'childList') { const addedNodes = mutation.addedNodes; for (let i = 0; i < addedNodes.length; i++) { if (addedNodes[i].nodeType === 1 && addedNodes[i].matches('.modal')) { //console.log('새로운 모달이 추가되었습니다.'); initializeModalComponents(addedNodes[i]); } } } }); }); // 전체 문서에 대해 MutationObserver 시작 observer.observe(document.body, { childList: true, subtree: true }); // 모달 표시 이벤트 리스너 document.body.addEventListener('shown.bs.modal', function(event) { //console.log('모달이 표시되었습니다.'); initializeModalComponents(event.target); }); // 페이지 로드 시 전체 문서에 대해 초기화 실행 //console.log('페이지 로드 시 초기화 시작'); initializeModalComponents(document.body); // 전역 스코프에 함수 노출 window.initializeForm = function() { //console.log('initializeForm 함수가 호출되었습니다.'); initializeModalComponents(document.body); }; })();