Files
dbx-main/app/static/erp-dialog.js
T
king 6376092bb7 fix(performance): 11번가 연결 테스트 — XML 아닌 응답은 실패, 성공 알림은 초록 체크
- 11번가가 200 으로 웹페이지(IP 차단 안내 등)나 XML 아닌 응답을 주면 "0건 정상"으로
  오판할 수 있어 실패로 처리
- 공용 알림 창 erpAlert(msg, { tone: "ok" }) — 경고 삼각형 대신 초록 체크.
  기존 호출은 그대로. 실적 연결 테스트 성공 알림에 적용

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 00:40:40 +09:00

196 lines
8.0 KiB
JavaScript

/* ════════════════════════════════════════════════════
ERP 공용 확인/경고 창 — window.confirm()/alert() 대체
────────────────────────────────────────────────────
브라우저 기본 창은 제목에 도메인("dbx.no1king.freeddns.org 내용:")이 찍히고
위치·정렬·아이콘을 바꿀 수 없다. 그래서 같은 역할의 창을 직접 그린다.
쓰는 법 (기본 창과 달리 **비동기**다 — Promise 를 돌려준다):
erpConfirm("지울까요?").then(function (ok) { if (ok) ... });
erpAlert("실패했습니다.").then(...)
erpPrompt("주소를 입력하세요", "https://").then(function (v) { if (v) ... }); // 취소면 null
erpAlert("저장했습니다.", { tone: "ok" }) // 성공 알림 — 경고 삼각형 대신 초록 체크
창 제목은 기본 "No.1 King ERP 프로그램". 화면마다 바꾸려면 그 화면 스크립트에서
window.ERP_DIALOG_TITLE = "..." 를 두거나, 한 번만 options.title 로 넘긴다.
HTML 만으로 쓰려면 form 에 data-erp-confirm 을 달면 된다. 제출을 가로채
확인을 받은 뒤 통과시킨다(가로챈 제출은 form.submit() 으로 다시 보내므로
이 스크립트의 submit 리스너를 다시 타지 않는다).
<form ... data-erp-confirm="정말 지울까요?">
════════════════════════════════════════════════════ */
(function () {
"use strict";
var TITLE = "No.1 King ERP 프로그램";
// 경고 삼각형. 외부 아이콘 라이브러리를 쓰지 않는다(자체 호스팅 원칙).
var ICON =
'<svg width="32" height="32" viewBox="0 0 24 24" fill="none" aria-hidden="true">' +
'<path d="M12 3.2 1.8 20.4h20.4L12 3.2Z" stroke="currentColor" stroke-width="1.6"' +
' stroke-linejoin="round"/>' +
'<path d="M12 9.4v4.8" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>' +
'<circle cx="12" cy="17.1" r="1.05" fill="currentColor"/>' +
"</svg>";
// 성공(tone: "ok") — 원 안의 체크.
var ICON_OK =
'<svg width="32" height="32" viewBox="0 0 24 24" fill="none" aria-hidden="true">' +
'<circle cx="12" cy="12" r="9.2" stroke="currentColor" stroke-width="1.6"/>' +
'<path d="m7.6 12.3 3 3 5.8-6.2" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"' +
' stroke-linejoin="round"/>' +
"</svg>";
function titleOf(options) {
return options.title || window.ERP_DIALOG_TITLE || TITLE;
}
function build(message, options) {
var TITLE_TEXT = titleOf(options);
var backdrop = document.createElement("div");
backdrop.className = "erp-dialog-backdrop";
var box = document.createElement("div");
box.className = "erp-dialog" + (options.ask ? " erp-dialog-ask" : "");
box.setAttribute("role", "alertdialog");
box.setAttribute("aria-modal", "true");
box.setAttribute("aria-label", TITLE_TEXT);
var title = document.createElement("h2");
title.className = "erp-dialog-title";
title.textContent = TITLE_TEXT;
var body = document.createElement("div");
body.className = "erp-dialog-body";
var icon = document.createElement("span");
icon.className = "erp-dialog-icon" + (options.tone === "ok" ? " is-ok" : "");
icon.innerHTML = options.tone === "ok" ? ICON_OK : ICON;
var text = document.createElement("p");
text.className = "erp-dialog-message";
// 메시지는 반드시 textContent 로 넣는다 — 서버/사용자 문자열이 들어와도
// HTML 로 해석되지 않게.
text.textContent = String(message == null ? "" : message);
// 입력창(erpPrompt) — 메시지 아래 한 줄.
var input = null;
if (options.prompt) {
input = document.createElement("input");
input.type = "text";
input.className = "erp-dialog-input";
input.value = options.defaultValue == null ? "" : String(options.defaultValue);
}
var actions = document.createElement("div");
actions.className = "erp-dialog-actions";
var cancel = null;
if (options.ask) {
cancel = document.createElement("button");
cancel.type = "button";
cancel.className = "erp-btn erp-btn-outline";
cancel.textContent = options.cancelText || "취소";
actions.appendChild(cancel);
}
var ok = document.createElement("button");
ok.type = "button";
ok.className = "erp-btn erp-btn-primary";
ok.textContent = options.okText || "확인";
actions.appendChild(ok);
body.appendChild(icon);
body.appendChild(text);
box.appendChild(title);
box.appendChild(body);
if (input) box.appendChild(input);
box.appendChild(actions);
backdrop.appendChild(box);
return { backdrop: backdrop, box: box, ok: ok, cancel: cancel, input: input };
}
function open(message, options) {
return new Promise(function (resolve) {
var el = build(message, options);
var previous = document.activeElement;
var closed = false;
function close(result) {
if (closed) return;
closed = true;
document.removeEventListener("keydown", onKey, true);
el.backdrop.remove();
if (previous && typeof previous.focus === "function") previous.focus();
// 입력창이면 확인 = 입력한 글자, 취소 = null
if (el.input) resolve(result ? el.input.value : null);
else resolve(result);
}
function onKey(e) {
if (e.key === "Escape") {
e.preventDefault();
close(false); // 확인창의 Esc = 취소, 알림창의 Esc = 닫기
} else if (e.key === "Enter") {
e.preventDefault();
close(true);
} else if (e.key === "Tab") {
// 포커스가 창 밖으로 나가지 않게 두 버튼 사이에서만 돈다.
var focusable = (el.input ? [el.input] : []).concat(el.cancel ? [el.cancel, el.ok] : [el.ok]);
var index = focusable.indexOf(document.activeElement);
e.preventDefault();
var next = e.shiftKey ? index - 1 : index + 1;
if (next < 0) next = focusable.length - 1;
if (next >= focusable.length) next = 0;
focusable[next].focus();
}
}
el.ok.addEventListener("click", function () { close(true); });
if (el.cancel) el.cancel.addEventListener("click", function () { close(false); });
// 바깥을 누르면 취소(= 안전한 쪽). 창 안쪽 클릭은 그대로 둔다.
el.backdrop.addEventListener("mousedown", function (e) {
if (e.target === el.backdrop) close(false);
});
document.addEventListener("keydown", onKey, true);
document.body.appendChild(el.backdrop);
if (el.input) { el.input.focus(); el.input.select(); }
else el.ok.focus();
});
}
window.erpConfirm = function (message, options) {
var opts = options || {};
opts.ask = true;
return open(message, opts);
};
window.erpAlert = function (message, options) {
var opts = options || {};
opts.ask = false;
return open(message, opts).then(function () { return undefined; });
};
window.erpPrompt = function (message, defaultValue, options) {
var opts = options || {};
opts.ask = true;
opts.prompt = true;
opts.defaultValue = defaultValue;
return open(message, opts);
};
// data-erp-confirm 이 달린 폼은 제출 전에 확인을 받는다.
document.addEventListener("submit", function (e) {
var form = e.target;
if (!form || !form.dataset || !form.dataset.erpConfirm) return;
if (form.dataset.erpConfirmed === "1") return; // 확인을 마치고 다시 보낸 것
e.preventDefault();
window.erpConfirm(form.dataset.erpConfirm).then(function (ok) {
if (!ok) return;
form.dataset.erpConfirmed = "1";
form.submit(); // submit 이벤트를 다시 타지 않는다
});
});
})();