[FO] HTML 레이아웃 적용
This commit is contained in:
196
front/src/main/resources/static/assets/scripts/common.js
Normal file
196
front/src/main/resources/static/assets/scripts/common.js
Normal file
@@ -0,0 +1,196 @@
|
||||
etUI.utils.ready(function () {
|
||||
console.log('document ready');
|
||||
etUI.initUI();
|
||||
|
||||
// [공통] 스위치 체크 스타일
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// all(전체선택)
|
||||
document.querySelectorAll('.component-select .select-box.all').forEach(allBox => {
|
||||
allBox.addEventListener('click', function (e) {
|
||||
const selectBox = e.target.closest('.component-select').querySelector('.select-box');
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
// active 상태면 이벤트x
|
||||
if (this.classList.contains('active')) return;
|
||||
|
||||
// all 버튼 제외한 나머지 셀렉트박스 텍스트 초기화
|
||||
document.querySelectorAll('.component-select .select-box').forEach(activeBox => {
|
||||
if (allBox) {
|
||||
const selectTxt = activeBox.querySelector('.select-txt');
|
||||
if (selectTxt) {
|
||||
selectTxt.textContent = '';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Selectbox.js의 removeAllActiveClass 함수 사용
|
||||
selectBoxComp.removeAllActiveClass(defaultTexts);
|
||||
|
||||
// all 버튼에 active 클래스 추가 (직접 구현)
|
||||
if (this) {
|
||||
this.classList.add('active');
|
||||
}
|
||||
activeSelectBox = this;
|
||||
|
||||
// all일 경우 옵션 숨기기
|
||||
const options = this.closest('.component-select').querySelector('.select-options');
|
||||
if (options) {
|
||||
options.style.display = 'none';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
// [공통] 직접 입력 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 directInput = group.querySelector('.component-input');
|
||||
|
||||
// 직접입력 input이 없다면 skip
|
||||
if (!directInput) return;
|
||||
|
||||
// 직접입력 input 기본 숨김
|
||||
directInput.style.display = 'none';
|
||||
|
||||
const updateVisibility = (selectedText) => {
|
||||
const isDirect = selectedText === '직접 입력';
|
||||
directInput.style.display = isDirect ? '' : 'none';
|
||||
};
|
||||
|
||||
// 초기 상태 설정
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
document.fonts.ready.then(() => {
|
||||
etUI.pages.initSwitchStyleToggle();
|
||||
etUI.pages.initPriceInputComma();
|
||||
etUI.pages.radioSelectbox();
|
||||
etUI.pages.initDirectInputToggle();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user