// 긴급 공지 — ERP 모든 화면(erp_base.html) 가운데에 띄운다(2026-10-12). // · GET /notice/api/pending — 띄울 시각이 지났고 아직 [확인] 안 한 내 공지(오래된 것부터). 30초마다·창을 다시 볼 때. // · [확인]을 누를 때까지 닫히지 않는다(Esc·바깥 클릭으로 안 닫힘). 여러 건이면 차례대로(1/3). // · [확인] = POST /notice/api/{id}/ack — 실패하면 창을 그대로 두고 알린다. 다른 탭에서 확인하면 이 탭도 다음 확인 때 닫힌다. // · 본문 HTML 은 서버가 정리한 것(이미지는 /notice/images/{id} 만). 수신함 오른쪽 작업 창(iframe) 안에서는 돌지 않는다. // · 024 미적용(503)·로그아웃(401)이면 조용히 멈춘다. (function () { "use strict"; if (window.top !== window) return; // iframe(수신함 작업 창 등) 안에서는 바깥 화면이 띄운다 const POLL_MS = 30000; let queue = [], shown = null, box = null, timer = 0, busy = false, stopped = false; function esc(s) { return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) { return { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]; }); } function when(iso) { const m = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(iso || ""); return m ? m[2] + "/" + m[3] + " " + m[4] + ":" + m[5] : ""; } function who(email) { const id = String(email || "").split("@")[0]; return id ? id.charAt(0).toUpperCase() + id.slice(1) : ""; } function schedule() { clearTimeout(timer); if (!stopped) timer = setTimeout(poll, POLL_MS); } async function poll() { if (busy || stopped) return; if (document.hidden) { schedule(); return; } busy = true; try { const res = await fetch("/notice/api/pending", { headers: { Accept: "application/json" }, credentials: "same-origin" }); if (res.status === 401 || res.status === 403 || res.status === 503 || res.status === 404) { stopped = true; return; } if (!res.ok) return; const data = await res.json(); queue = Array.isArray(data.notices) ? data.notices : []; // 다른 탭·다른 곳에서 확인했거나 취소된 공지는 닫는다 if (shown && !queue.some(function (n) { return n.id === shown.id; })) close(); if (!shown && queue.length) open(queue[0]); else if (shown) paintCount(); } catch (_) { // 네트워크 오류는 다음 주기에 다시 } finally { busy = false; schedule(); } } function paintCount() { if (!box || !shown) return; const i = queue.findIndex(function (n) { return n.id === shown.id; }); const c = box.querySelector(".erp-notice-count"); c.textContent = queue.length > 1 ? (i + 1) + " / " + queue.length : ""; c.hidden = queue.length <= 1; } function open(n) { shown = n; if (!box) { box = document.createElement("div"); box.className = "erp-notice-backdrop"; box.innerHTML = '
' + '
긴급 공지
' + '

' + '
' + '' + '
' + '
'; document.body.appendChild(box); // 화면 배율(.erp-app zoom) 밖 — 늘 화면 정가운데 box.querySelector(".erp-notice-ok").addEventListener("click", ack); // [확인] 전에는 닫지 않는다 — Esc 는 막고, 포커스는 창 안에 box.addEventListener("keydown", function (e) { if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); } if (e.key === "Tab") { e.preventDefault(); box.querySelector(".erp-notice-ok").focus(); } }); } box.querySelector(".erp-notice-title").textContent = n.title || ""; box.querySelector(".erp-notice-body").innerHTML = n.body_html || ""; // 서버가 정리한 HTML box.querySelector(".erp-notice-meta").textContent = [who(n.created_by), when(n.show_at)].filter(Boolean).join(" · "); box.querySelector(".erp-notice-error").hidden = true; box.querySelector(".erp-notice-body").scrollTop = 0; box.hidden = false; paintCount(); const ok = box.querySelector(".erp-notice-ok"); ok.disabled = false; ok.focus(); } function close() { shown = null; if (box) box.hidden = true; } async function ack() { if (!shown) return; const n = shown, ok = box.querySelector(".erp-notice-ok"), err = box.querySelector(".erp-notice-error"); ok.disabled = true; err.hidden = true; try { const res = await fetch("/notice/api/" + n.id + "/ack", { method: "POST", credentials: "same-origin" }); if (!res.ok && res.status !== 404) throw new Error("HTTP " + res.status); // 404 = 취소·삭제됨 — 닫는다 try { localStorage.setItem("erp_notice_ack", String(n.id) + ":" + Date.now()); } catch (_) {} queue = queue.filter(function (x) { return x.id !== n.id; }); if (queue.length) open(queue[0]); else close(); } catch (e) { err.textContent = "확인을 저장하지 못했습니다. 다시 눌러 주세요. (" + e.message + ")"; err.hidden = false; ok.disabled = false; ok.focus(); } } window.addEventListener("storage", function (e) { if (e.key === "erp_notice_ack") poll(); }); document.addEventListener("visibilitychange", function () { if (!document.hidden) poll(); }); if (window.__ERP_NOTICE_TEST__) window.__erpNotice = { poll: poll, state: function () { return { queue: queue, shown: shown, stopped: stopped }; } }; if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", poll); else poll(); })();