Files
dbx-main/app/static/erp-notice.js
T
king 2aa6b6efae feat(notice): urgent notices centered on every ERP screen until acknowledged
- Super admin only: /notice/ page (title, body with images, show time KST, all or picked people,
  preview, edit with optional re-show, cancel/undo, per-person acknowledgement status)
- Shown on every ERP page (erp_base, main, admin) dead center; [확인] only (no Esc/backdrop close),
  queue in order, ack failure keeps the dialog, closes when acked/cancelled elsewhere
- project_db migration 024 (urgent_notices, targets, acks, images as bytea); shares ProjectStore.pool
- Body sanitized like task descriptions, images restricted to /notice/images/{id}, type sniffed
- Entry points: ERP sidebar foot and Project options dialog (super admin)
- Docs (NOTICE_MODULE, DEPLOYMENT 024, overview, CLAUDE.md); tests (DB-less, real PostgreSQL, jsdom)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:07:10 +09:00

119 lines
5.9 KiB
JavaScript

// 긴급 공지 — 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 { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[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 =
'<div class="erp-notice-card" role="alertdialog" aria-modal="true" aria-labelledby="erp-notice-title">' +
'<div class="erp-notice-head"><span class="erp-notice-badge">긴급 공지</span><span class="erp-notice-count" hidden></span></div>' +
'<h2 class="erp-notice-title" id="erp-notice-title"></h2>' +
'<div class="erp-notice-body"></div>' +
'<p class="erp-notice-error" role="alert" hidden></p>' +
'<div class="erp-notice-foot"><span class="erp-notice-meta"></span>' +
'<button type="button" class="erp-notice-ok">확인</button></div></div>';
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();
})();