136 lines
4.8 KiB
TypeScript
136 lines
4.8 KiB
TypeScript
import { LOCATION_DATA } from './SharedData';
|
|
|
|
/**
|
|
* 모달 조작 및 UI 생성을 위한 공통 유틸리티
|
|
*/
|
|
|
|
// 1. Select 박스의 Option HTML 생성
|
|
export function generateOptionsHTML(list: string[], defaultValue: string = '', includeSelectHint: boolean = true): string {
|
|
let html = includeSelectHint ? '<option value="">선택</option>' : '';
|
|
html += list.map(item => `<option value="${item}" ${item === defaultValue ? 'selected' : ''}>${item}</option>`).join('');
|
|
return html;
|
|
}
|
|
|
|
// 2. 안전하게 폼 필드 값 설정 (Null 에러 방지)
|
|
export function setFieldValue(id: string, value: any) {
|
|
const el = document.getElementById(id) as HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
if (el) {
|
|
el.value = value || '';
|
|
}
|
|
}
|
|
|
|
// 3. 안전하게 폼 필드 값 읽기
|
|
export function getFieldValue(id: string): string {
|
|
const el = document.getElementById(id) as HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
return el ? el.value : '';
|
|
}
|
|
|
|
// 4. 위치 정보 파싱 및 UI 세팅
|
|
export function parseAndSetLocation(locationStr: string, bldgId: string, detailId: string, etcGroupId: string, etcInputId: string) {
|
|
const bldgSelect = document.getElementById(bldgId) as HTMLSelectElement;
|
|
const detailSelect = document.getElementById(detailId) as HTMLSelectElement;
|
|
const etcGroup = document.getElementById(etcGroupId);
|
|
const etcInput = document.getElementById(etcInputId) as HTMLInputElement;
|
|
|
|
if (!bldgSelect || !detailSelect) return;
|
|
|
|
// 초기화
|
|
bldgSelect.value = '';
|
|
detailSelect.innerHTML = '<option value="">선택</option>';
|
|
if (etcGroup) etcGroup.style.display = 'none';
|
|
|
|
if (!locationStr) return;
|
|
|
|
const parts = locationStr.split(' ');
|
|
const bldg = parts[0];
|
|
|
|
if (LOCATION_DATA[bldg]) {
|
|
bldgSelect.value = bldg;
|
|
// 상세 목록 갱신
|
|
detailSelect.innerHTML = generateOptionsHTML(LOCATION_DATA[bldg]);
|
|
|
|
const detail = parts[1];
|
|
if (detail) {
|
|
detailSelect.value = detail;
|
|
if (detail === '기타' && etcGroup && etcInput) {
|
|
etcGroup.style.display = 'flex';
|
|
etcInput.value = parts.slice(2).join(' ');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 5. 위치 종속성(Cascade) 이벤트 바인딩
|
|
export function bindLocationEvents(bldgId: string, detailId: string, etcGroupId: string, etcInputId: string) {
|
|
const bldgSelect = document.getElementById(bldgId) as HTMLSelectElement;
|
|
const detailSelect = document.getElementById(detailId) as HTMLSelectElement;
|
|
const etcGroup = document.getElementById(etcGroupId);
|
|
const etcInput = document.getElementById(etcInputId) as HTMLInputElement;
|
|
|
|
if (!bldgSelect || !detailSelect) return;
|
|
|
|
bldgSelect.addEventListener('change', () => {
|
|
const bldg = bldgSelect.value;
|
|
detailSelect.innerHTML = generateOptionsHTML(LOCATION_DATA[bldg] || []);
|
|
if (etcGroup) etcGroup.style.display = 'none';
|
|
if (etcInput) etcInput.value = '';
|
|
});
|
|
|
|
detailSelect.addEventListener('change', () => {
|
|
if (etcGroup) {
|
|
etcGroup.style.display = detailSelect.value === '기타' ? 'flex' : 'none';
|
|
}
|
|
});
|
|
}
|
|
|
|
// 6. 위치 문자열 조합 (저장용)
|
|
export function getCombinedLocation(bldgId: string, detailId: string, etcInputId: string): string {
|
|
const bldg = getFieldValue(bldgId);
|
|
const detail = getFieldValue(detailId);
|
|
const etc = getFieldValue(etcInputId);
|
|
|
|
let combined = bldg;
|
|
if (detail) combined += ` ${detail}`;
|
|
if (detail === '기타' && etc) combined += ` ${etc}`;
|
|
|
|
return combined.trim();
|
|
}
|
|
|
|
// 7. 조회/수정 모드 UI 통합 제어
|
|
export function setEditLock(
|
|
formId: string,
|
|
mode: 'view' | 'add' | 'edit',
|
|
options: {
|
|
saveBtnId: string,
|
|
revertBtnId: string,
|
|
generateBtnId?: string
|
|
}
|
|
) {
|
|
const form = document.getElementById(formId) as HTMLFormElement;
|
|
const saveBtn = document.getElementById(options.saveBtnId);
|
|
const revertBtn = document.getElementById(options.revertBtnId);
|
|
const generateBtn = options.generateBtnId ? document.getElementById(options.generateBtnId) : null;
|
|
|
|
if (!form || !saveBtn || !revertBtn) return;
|
|
|
|
if (mode === 'add' || mode === 'edit') {
|
|
// 편집 모드 활성화
|
|
form.classList.remove('is-view-mode');
|
|
form.classList.add('is-edit-mode');
|
|
saveBtn.textContent = '저장';
|
|
revertBtn.classList.toggle('hidden', mode === 'add'); // 신규 추가 시에는 취소 버튼 숨김 (닫기가 대신함)
|
|
|
|
// 번호 생성 버튼은 '추가' 시에만 노출
|
|
if (generateBtn) generateBtn.classList.toggle('hidden', mode !== 'add');
|
|
} else {
|
|
// 조회 모드 (잠금)
|
|
form.classList.remove('is-edit-mode');
|
|
form.classList.add('is-view-mode');
|
|
saveBtn.textContent = '수정';
|
|
revertBtn.classList.add('hidden');
|
|
|
|
// 조회 모드에서는 번호 생성 버튼 무조건 숨김
|
|
if (generateBtn) generateBtn.classList.add('hidden');
|
|
}
|
|
}
|