Files
korea-research/front/src/main/resources/static/assets/scripts/common.js
2025-07-17 14:22:10 +09:00

376 lines
12 KiB
JavaScript

etUI.utils.ready(function () {
console.log('document ready');
// etUI.initUI();
// [공통] 헤더 스크롤 관련 기능
etUI.pages.headerScroll = function () {
const header = document.querySelector('.header');
const modalFulls = document.querySelectorAll('.component-modal');
const updateHeaderClass = () => {
if (!header) return;
header.classList.toggle('scroll', window.scrollY > 0);
};
const updateModalHeaderClass = () => {
modalFulls.forEach(modalFull => {
const modalContent = modalFull.querySelector('.modal-content');
const modalHeader = modalFull.querySelector('.modal-header');
if (modalContent && modalHeader) {
const isScroll = modalContent.scrollTop > 0;
modalHeader.classList.toggle('scroll', isScroll);
}
});
};
etUI.hooks.useEventListener(window, 'scroll', updateHeaderClass);
modalFulls.forEach(modalFull => {
const modalContent = modalFull.querySelector('.modal-content');
if (modalContent) {
modalContent.addEventListener('scroll', updateModalHeaderClass);
}
});
updateHeaderClass();
};
// [공통] container 클래스 업데이트
etUI.pages.updateContainerClassByLayout = function () {
const container = document.getElementById('container');
if (!container) return;
const hasFixedBtns = document.querySelector('.component-btns.fixed');
const hasFooter = document.querySelector('footer');
// container 클래스 처리
if (hasFixedBtns) {
container.classList.add('btm-fixbtn');
} else {
container.classList.remove('btm-fixbtn');
}
if (hasFooter) {
container.classList.add('btm-footer');
} else {
container.classList.remove('btm-footer');
}
};
// [공통] 탭 버튼 활성화 토글
etUI.pages.tabButtonToggle = function () {
const tabGroups = document.querySelectorAll('.component-btns.type-tab');
tabGroups.forEach(group => {
const buttons = group.querySelectorAll('.btn');
buttons.forEach(button => {
button.addEventListener('click', () => {
buttons.forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
});
});
});
};
// [공통] 스위치 체크 스타일
etUI.pages.initSwitchStyleToggle = function () {
const switches = document.querySelectorAll('.switch-inner input[type="checkbox"]');
switches.forEach(input => {
const wrapper = input.closest('.switch-inner');
// 초기 상태 적용
if (input.checked) {
wrapper.classList.add('checked');
} else {
wrapper.classList.remove('checked');
}
// 상태 변경 이벤트 처리
input.addEventListener('change', () => {
if (input.checked) {
wrapper.classList.add('checked');
} else {
wrapper.classList.remove('checked');
}
});
});
};
// [공통] type-price input에 3자리 콤마 포맷 적용
etUI.pages.initPriceInputComma = function () {
const priceInputs = document.querySelectorAll('.input-field.type-price input[type="text"]');
priceInputs.forEach(input => {
const formatNumber = (val) => {
const onlyNums = val.replace(/[^\d]/g, '');
return onlyNums.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
// 초기값 포맷팅
if (input.value) {
input.value = formatNumber(input.value);
}
// 입력 중 포맷 적용
input.addEventListener('input', function () {
const cursorPos = this.selectionStart;
const rawValue = this.value.replace(/[^\d]/g, '');
this.value = formatNumber(rawValue);
});
// 포커스 시 커서 끝 이동
input.addEventListener('focus', function () {
const val = this.value;
this.value = '';
this.value = val;
});
});
};
// [공통] 셀렉트박스 라디오 동작 기능
etUI.pages.radioSelectbox = function () {
const selectBoxes = document.querySelectorAll('.select-area .component-select .select-box');
if (!selectBoxes.length) return;
// 활성화된 셀렉트박스
let activeSelectBox = null;
// 각 셀렉트박스의 기본 텍스트 저장
const defaultTexts = {};
// Selectbox 컴포넌트 가져오기
const selectBoxComp = etUI.components.SelectBox();
// 기본 텍스트 값 초기화
selectBoxes.forEach((box, index) => {
const selectTxt = box.querySelector('.select-txt');
if (selectTxt) {
const selectId = `select-${index}`;
box.setAttribute('data-select-id', selectId);
defaultTexts[selectId] = selectTxt.textContent;
}
});
// 옵션선택 후 active class 추가
etUI.hooks.useEventListener(document, 'click', (e) => {
if (e.target.classList.contains('option') || e.target.closest('.option')) {
const option = e.target.classList.contains('option') ? e.target : e.target.closest('.option');
const selectBox = option.closest('.component-select').querySelector('.select-box');
if (selectBox) {
// 기존에 active된 셀렉트박스 텍스트 초기화
document.querySelectorAll('.component-select .select-box.active').forEach(activeBox => {
if (activeBox !== selectBox) {
const selectTxt = activeBox.querySelector('.select-txt');
if (selectTxt) {
selectTxt.textContent = '';
}
}
});
// Selectbox.js의 removeAllActiveClass 함수 사용
selectBoxComp.removeAllActiveClass(defaultTexts);
// 셀렉트박스에 active 클래스 추가 (직접 구현)
if (selectBox) {
selectBox.classList.add('active');
}
activeSelectBox = selectBox;
}
}
});
};
// [공통] option 선택 시 매칭되는 input 표시
etUI.pages.initDirectInputToggle = function () {
const selectGroups = document.querySelectorAll('.select-group');
selectGroups.forEach(group => {
const select = group.querySelector('.component-select');
const selectBox = select.querySelector('.select-box');
const options = select.querySelectorAll('.option');
const inputs = group.querySelectorAll('.component-input');
// 모든 input 기본 숨김 (show 클래스 제거)
inputs.forEach(input => {
input.classList.remove('show');
});
const updateVisibility = (selectedText) => {
inputs.forEach(input => {
const matchValue = input.getAttribute('data-option');
if (matchValue === selectedText) {
input.classList.add('show');
} else {
input.classList.remove('show');
}
});
};
// 초기 상태 설정
const currentSelectedText = selectBox.querySelector('.select-txt')?.textContent.trim();
updateVisibility(currentSelectedText);
// 옵션 클릭 시 처리
options.forEach(option => {
option.addEventListener('click', function () {
const selectedText = this.textContent.trim();
selectBox.querySelector('.select-txt').textContent = selectedText;
updateVisibility(selectedText);
});
});
});
};
// [공통] select-group.type-tab 탭 스타일 셀렉트 처리
etUI.pages.initTabStyleSelect = function () {
const tabSelectGroups = document.querySelectorAll('.select-group.type-tab');
tabSelectGroups.forEach(group => {
const select = group.querySelector('.component-select');
const selectBox = select.querySelector('.select-box');
const options = select.querySelectorAll('.option');
const details = group.querySelectorAll('.select-detail');
// 모든 detail 숨기기
const updateDetailVisibility = (selectedText) => {
details.forEach(detail => {
const matchValue = detail.getAttribute('data-option');
if (matchValue === selectedText) {
detail.classList.add('show');
} else {
detail.classList.remove('show');
}
});
};
// 초기 설정: select-box에 현재 선택된 텍스트 확인
const defaultSelectedText = selectBox.querySelector('.select-txt')?.textContent.trim();
if (defaultSelectedText) {
updateDetailVisibility(defaultSelectedText);
}
// 옵션 클릭 이벤트
options.forEach(option => {
option.addEventListener('click', function () {
const selectedText = this.textContent.trim();
// 텍스트 변경
selectBox.querySelector('.select-txt').textContent = selectedText;
// current 클래스 업데이트
options.forEach(opt => opt.classList.remove('current'));
this.classList.add('current');
// detail 표시 갱신
updateDetailVisibility(selectedText);
});
});
});
};
// 2025.06.17 삭제
// [공통] appbar 아이콘 활성화
// etUI.pages.activeAppbarOnPath = function () {
// const appbar = document.querySelector('.footer-menu');
// const currentPath = window.location.pathname;
// if (!appbar) return;
// appbar.querySelectorAll('.active').forEach(el => el.classList.remove('active'));
// // 경로와 클래스 매핑
// const pathMap = {
// '/pages/ACCO': 'menu-acco',
// '/pages/REGI': 'menu-regi',
// '/pages/STAT': 'menu-stat',
// '/pages/INFO': 'menu-info',
// };
// // 경로에 해당하는 앱바 아이콘 활성화
// for (const [path, className] of Object.entries(pathMap)) {
// if (currentPath.includes(path)) {
// const targetElement = appbar.querySelector(`.${className}`);
// if (targetElement) targetElement.classList.add('active');
// break;
// }
// }
// };
// [공통] top 버튼
etUI.pages.scrollTop = function () {
const floatingEl = document.querySelector('.floating-area');
const hasFooter = document.querySelector('.footer');
const isFixedBtn = document.querySelector('.component-btns.fixed');
let isVisible = false;
if (!floatingEl) return;
// 초기 숨김 상태 설정
gsap.set(floatingEl, {
opacity: 0,
// scale: 0,
pointerEvents: 'none'
});
etUI.hooks.useEventListener(window, 'scroll', () => {
const scrollY = window.scrollY;
// bottom 위치 설정
if (hasFooter) {
floatingEl.style.bottom = '72px';
} else if (isFixedBtn) {
floatingEl.style.bottom = '112px';
} else {
floatingEl.style.bottom = '';
}
if (scrollY <= 0 && isVisible) {
// 스크롤 맨 위: 숨기기
isVisible = false;
gsap.to(floatingEl, {
duration: 0.4,
opacity: 0,
// scale: 0,
pointerEvents: 'none',
ease: 'power4.out'
});
} else if (scrollY > 0 && !isVisible) {
// 스크롤 내림: 보이기
isVisible = true;
gsap.to(floatingEl, {
duration: 0.4,
opacity: 1,
scale: 1,
pointerEvents: 'auto',
ease: 'power4.out'
});
}
});
// 클릭 시 top으로 부드럽게 이동
etUI.hooks.useEventListener(floatingEl, 'click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
};
document.fonts.ready.then(() => {
etUI.pages.initSwitchStyleToggle();
etUI.pages.initPriceInputComma();
etUI.pages.radioSelectbox();
etUI.pages.initDirectInputToggle();
etUI.pages.updateContainerClassByLayout();
etUI.pages.headerScroll();
etUI.pages.tabButtonToggle();
etUI.pages.scrollTop();
etUI.pages.initTabStyleSelect();
});
});