/**
* SweetAlert2 기반 alert 대체
* - 기본 스타일을 크고 읽기 쉽게 설정
* - window.alert() 호출을 SweetAlert2로 라우팅
*/
(function () {
if (typeof Swal === 'undefined') {
console.warn('[sweetalert-init] SweetAlert2가 로드되지 않았습니다.');
return;
}
// 공통 커스텀 스타일 주입
var styleId = 'swal-custom-style';
if (!document.getElementById(styleId)) {
var s = document.createElement('style');
s.id = styleId;
s.textContent = [
'.swal2-popup.swal-hy {',
' width: 560px; max-width: 92vw;',
' padding: 40px 32px 32px;',
' border-radius: 20px;',
' font-family: Pretendard, -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "맑은 고딕", "malgun gothic", sans-serif;',
' box-shadow: 0 20px 60px rgba(0,32,72,0.18);',
'}',
'.swal2-popup.swal-hy .swal2-title {',
' font-size: 22px; font-weight: 700; color: #002446;',
' margin: 4px 0 14px; line-height: 1.4;',
'}',
'.swal2-popup.swal-hy .swal2-html-container {',
' font-size: 16px; color: #2c2c2c;',
' line-height: 1.6; white-space: pre-line;',
' margin: 0 0 8px;',
'}',
'.swal2-popup.swal-hy .swal2-actions { gap: 12px; margin-top: 24px; }',
'.swal2-popup.swal-hy .swal2-styled {',
' font-size: 16px; font-weight: 600;',
' padding: 12px 28px; border-radius: 10px;',
' box-shadow: none !important;',
'}',
'.swal2-popup.swal-hy .swal2-confirm {',
' background: #1187f5 !important;',
'}',
'.swal2-popup.swal-hy .swal2-cancel {',
' background: #f1f3f5 !important; color: #333 !important;',
'}',
'.swal2-popup.swal-hy .swal2-icon {',
' width: 70px; height: 70px;',
' border-width: 3px; margin: 6px auto 18px;',
'}',
'@media (max-width: 480px) {',
' .swal2-popup.swal-hy { padding: 28px 20px 22px; border-radius: 14px; }',
' .swal2-popup.swal-hy .swal2-title { font-size: 18px; }',
' .swal2-popup.swal-hy .swal2-html-container { font-size: 14px; }',
'}'
].join('\n');
document.head.appendChild(s);
}
// 아이콘 자동 판별 (메시지 키워드 기반)
function detectIcon(text) {
var t = String(text || '');
if (/(완료|저장|성공|등록되었|이용해|가입되|삭제되|승인)/.test(t)) return 'success';
if (/(실패|오류|에러|잘못|권한|없습니다|불가|금지)/.test(t)) return 'error';
if (/(확인|주의|경고|필수|입력해|선택해)/.test(t)) return 'warning';
return 'info';
}
window.__hy_detect_icon = detectIcon;
var _native_alert = window.alert;
window.alert = function (message) {
try {
var msg = (message == null) ? '' : String(message);
Swal.fire({
text: msg,
icon: detectIcon(msg),
confirmButtonText: '확인',
customClass: { popup: 'swal-hy' },
buttonsStyling: true,
allowOutsideClick: false,
showCloseButton: true
});
} catch (e) {
_native_alert(message);
}
};
// 다른 곳에서 직접 쓰고 싶을 때 바로 쓸 수 있게 헬퍼 제공
window.hyAlert = function (text, opts) {
opts = opts || {};
return Swal.fire(Object.assign({
text: String(text || ''),
icon: opts.icon || detectIcon(text),
confirmButtonText: '확인',
customClass: { popup: 'swal-hy' },
allowOutsideClick: false,
showCloseButton: true
}, opts));
};
window.hyConfirm = function (text, opts) {
opts = opts || {};
return Swal.fire(Object.assign({
text: String(text || ''),
icon: opts.icon || 'question',
showCancelButton: true,
confirmButtonText: opts.confirmText || '확인',
cancelButtonText: opts.cancelText || '취소',
customClass: { popup: 'swal-hy' },
reverseButtons: true,
focusConfirm: true
}, opts));
};
//? PHP alert+nav 패턴용 글로벌 헬퍼: SweetAlert 후 이동
// 사용(PHP): echo ''; exit;
// 또는 echo ''; (url 생략 시 history.back)
window.hyAlertNav = function (msg, url) {
function go() {
if (url) location.href = url;
else history.back();
}
function run() {
if (!window.Swal) { alert(msg); go(); return; }
Swal.fire({
text: String(msg || ''),
icon: detectIcon(msg),
confirmButtonText: '돌아가기',
customClass: { popup: 'swal-hy' },
allowOutsideClick: false
}).then(go);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', run);
} else {
run();
}
};
//? 팝업용: SweetAlert 후 self.close()
window.hyAlertClose = function (msg) {
function close() { self.close(); }
function run() {
if (!window.Swal) { alert(msg); close(); return; }
Swal.fire({
text: String(msg || ''),
icon: detectIcon(msg),
confirmButtonText: '닫기',
customClass: { popup: 'swal-hy' },
allowOutsideClick: false
}).then(close);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', run);
} else {
run();
}
};
//? 페이지 로드 시 sessionStorage에 남긴 flash 메시지가 있으면 Swal로 표시
function showFlash() {
try {
var msg = sessionStorage.getItem('__hy_flash');
if (msg) {
sessionStorage.removeItem('__hy_flash');
Swal.fire({
text: msg,
icon: detectIcon(msg),
confirmButtonText: '확인',
customClass: { popup: 'swal-hy' },
allowOutsideClick: false,
showCloseButton: true
});
}
} catch (e) { /* ignore */ }
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', showFlash);
} else {
showFlash();
}
// window.confirm 오버라이드 (동기 제약 → 재클릭 기반)
// - 마지막 trusted 버튼 클릭을 전역적으로 추적해 SweetAlert 확인 후 재클릭
// - `if(!confirm(x)){return false;}` 패턴 자동 지원
var _lastClickedButton = null;
document.addEventListener('click', function (ev) {
if (!ev.isTrusted) return; // 자체 재클릭 루프 방지
var el = ev.target;
if (!el || !el.closest) return;
var btn = el.closest('button, input[type="button"], input[type="submit"], a');
if (btn) _lastClickedButton = btn;
}, true); // capture phase
var _confirmedMessages = new Set();
window.confirm = function (message) {
var msg = (message == null) ? '' : String(message);
if (_confirmedMessages.has(msg)) {
_confirmedMessages.delete(msg);
return true;
}
var srcEl = _lastClickedButton;
Swal.fire({
text: msg,
icon: 'question',
showCancelButton: true,
confirmButtonText: '확인',
cancelButtonText: '취소',
customClass: { popup: 'swal-hy' },
reverseButtons: true,
focusConfirm: true,
allowOutsideClick: false
}).then(function (result) {
if (result.isConfirmed) {
_confirmedMessages.add(msg);
if (srcEl && typeof srcEl.click === 'function') {
//? 스택 초기화 후 재클릭 (jQuery 위임 핸들러까지 자연스럽게 재실행)
setTimeout(function () { srcEl.click(); }, 0);
}
}
});
return false;
};
})();